一、 Discuz 之 block 簡介

discuz 的 block 指的是 diy 功能中可自定義表單樣式,表單標題,並實現自己來設定資料呼叫。可以從門戶->模組管理->資料呼叫中進去,並新增新資料。

QQ截圖20180312232508.png

透過內部呼叫或者外部呼叫可以將資料呼叫到任何一個位置。

QQ截圖20180312232608.png

配置檔案存放在 sourceclassblock 目錄下。
QQ截圖20180312232721.png

二、檔案解析

使用者想自定義表單欄位來進行填寫內容的話需要重新建立檔案


     */
    //模組名稱 模組分類
    function name() {
    return "自己的資料";
    }
    /**
     * 必須!
     * 返回一個陣列: 第一個值為本資料類所在的模組分類;第二個值為模組分類顯示的名稱 (顯示在 DIY 模組面板)
     * @return 
     */
    function blockclass(){
    return array('othermk', "[開發] 模組");
    }
    /**
     * 必須!
     * 返回資料類中可供 「模組樣式」 使用的欄位。
     * 格式見示例:
     * name 為該欄位的顯示名稱
     * formtype 決定編輯單條資料時該欄位的顯示方式: 型別有: text, textarea, date, title, summary, pic; 詳見 portalcp_block.htm 模板 (搜 $field[formtype])
     * datatype 決定該欄位的資料展示,型別有: string, int, date, title, summary, pic; 詳見 function_block.php 中 block_template 函式
     * @return 
     */
    //這個模組要返回的欄位
    function fields() {
        return array(
        'title' => array('name' => '名稱', 'formtype' => 'title', 'datatype' => 'title'),
        'url' => array('name' => '地址', 'formtype' => 'text', 'datatype' => 'string'),
        'img' => array('name' => '圖片', 'formtype' => 'pic', 'datatype' => 'pic'),
        );
    }

    /**
     * 必須!
     * 返回使用本資料類呼叫資料時的設定項
     * 格式見示例:
     * title 為顯示的名稱
     * type 為表單型別, 有: text, password, number, textarea, radio, select, mselect, mradio, mcheckbox, calendar; 詳見 function_block.php 中 block_makeform() 函式
     * @return 
     */
    //設定需要帥選的引數 然後引數會自動傳給  getdata($style, $parameter)
    function getsetting() {
        global $_G;
        $settings = array();
        $settings = array(
            'titlelength' => array(
            'title' => '標題',
            'type' => 'text',
            'default' => ""
            )
        );
        return $settings;
    }
    /**
     * 必須!
     * 處理設定引數,返回資料
     * 返回資料有兩種:
     * 一種是返回 html,放到模組 summary 欄位,直接顯示; 返回格式為: array('html'=>'返回內容', 'data'=>null)
     * 一種是返回 data,透過模組樣式渲染後展示,返回的資料應該包含 fields() 函式中指定的所有欄位; 返回格式為: array('html'=>'', 'data'=>array(array('title'=>'value1'), array('title'=>'value2')))
     * 特別的:
     * parameter 引數包含 getsetting() 提交後的內容;並附加了欄位:
     * items ,為使用者指定顯示的模組資料條數;
     * bannedids ,為使用者選擇遮蔽某資料時記錄在模組中的該資料 id 。 應該在獲取資料時遮蔽該資料;
     *
     * 如果返回的資料給 data, 那麼應該包含 fields() 函式指定的所有欄位。並附加以下欄位:
     * id 標誌該資料的 id,如果使用者遮蔽某資料時,會將該資料的 id 新增到 parameter[bannedids] 裡
     * idtype 標誌該資料的 idtype
     *
     * @param  $style 模組樣式 (見 common_block_style 表) 。 可以根據模組樣式中用到的欄位來選擇性的獲取/不獲取某些資料
     * @param  $parameter 使用者對 getsetting() 給出的表單提交後的內容。
     * @return 
     */
    //返回資料的函式,自己發揮吧,一定要保證你返回的欄位 在上個 fields 函式裡面要有

     function getdata($style, $parameter) {

         // 返回summary
         return array('html' => '

這是一個演示模組資料類

', 'data' => null); // 返回資料 // 需要注意: 除 id,idtype, title, url, pic, picflag, summary 幾個欄位外,其它欄位需要放到 fields 陣列裡。 可以參考系統內建模組類 source/class/block/block_thread.php return array('html'=>'', 'data' => array( array( 'id' => '1', 'idtype' => 'sampleid', 'title' => 'title1', 'url' => '#', 'pic' => 'nophoto.gif', 'picflag' => '1', 'summary' => '', 'fields' => array( 'field1' => 'value1' ) ) )); } } ?>

1 、 name 函式

    /**
     * 必須!
     * 返回本資料呼叫類的顯示名稱 (顯示在建立模組時選擇 「模組資料」 的下拉式清單裡)
     * @return 
     */
    //模組名稱 模組分類
    function name() {
    return "自己的資料";
    }

QQ截圖20180312233739.png

2 、 blockclass 函式

    /**
     * 必須!
     * 返回一個陣列: 第一個值為本資料類所在的模組分類;第二個值為模組分類顯示的名稱 (顯示在 DIY 模組面板)
     * @return 
     */
    function blockclass(){
    return array('othermk', "[開發] 模組");
    }

QQ截圖20180312234054.png

相當於給該 diy 命名

3 、 fields 函式

    /**
     * 必須!
     * 返回資料類中可供 「模組樣式」 使用的欄位。
     * 格式見示例:
     * name 為該欄位的顯示名稱
     * formtype 決定編輯單條資料時該欄位的顯示方式: 型別有: text, textarea, date, title, summary, pic; 詳見 portalcp_block.htm 模板 (搜 $field[formtype])
     * datatype 決定該欄位的資料展示,型別有: string, int, date, title, summary, pic; 詳見 function_block.php 中 block_template 函式
     * @return 
     */
    //這個模組要返回的欄位
    function fields() {
        return array(
        'title' => array('name' => '名稱', 'formtype' => 'title', 'datatype' => 'title'),
        'url' => array('name' => '地址', 'formtype' => 'text', 'datatype' => 'string'),
        'img' => array('name' => '圖片', 'formtype' => 'pic', 'datatype' => 'pic'),
        );
    }

這個函式就是表單的引數函式,可以透過這個來設定表單分類名稱。比如活動中:活動標題、地點、時間、參與方式啥的,都可以進行自定義,大概效果如圖所示。

QQ截圖20180312234721.png

4 、 getsetting 函式

    /**
     * 必須!
     * 返回使用本資料類呼叫資料時的設定項
     * 格式見示例:
     * title 為顯示的名稱
     * type 為表單型別, 有: text, password, number, textarea, radio, select, mselect, mradio, mcheckbox, calendar; 詳見 function_block.php 中 block_makeform() 函式
     * @return 
     */
    //設定需要帥選的引數 然後引數會自動傳給  getdata($style, $parameter)
    function getsetting() {
        global $_G;
        $settings = array();
        $settings = array(
            'titlelength' => array(
            'title' => '標題',
            'type' => 'text',
            'default' => ""
            )
        );
        return $settings;
    }

QQ截圖20180312234943.png

三、資料呼叫

1 、簡單呼叫

這個暫時不做說明,用 [loop] 呼叫即可。

2 、用函式呼叫

block_get 函式

function block_get_batch($parameter) {
    global $_G;
    $bids = $parameter && is_array($parameter) ? $parameter : ($parameter ? explode(',', $parameter) : array());
        .
        .
        .
    if($styleids) {
        block_getstyle($styleids);
    }
}

可以用這個函式進行呼叫表中資料。
QQ截圖20180312235301.png

因為在 function_core.php 中簡化了函式 block_get_batch 可以用 block_get 代替。

function block_get($parameter) {
    include_once libfile('function/block');
    block_get_batch($parameter);
}

使用方法

//$parameter 可以是多個值,如:$parameter = '31,32,33';
$parameter = '31';
$bid= 31;
$items = block_get($parameter);
var_dump($_G['block'][$bid]['itemlist']);

QQ截圖20180313001229.png

block_display 函式

function block_display($bid) {
    include_once libfile('function/block');
    block_display_batch($bid);
}

首先要在頁面使用 block_get 函式,然後使用 block_display 函式,函式的意思很明顯,獲取和展示。並且展示的時候是帶有 diy 模板樣式的。