一、 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 模板样式的。