discuz 之block解析

2021-03-20 874 0条评论

一、Discuz之block简介

discuz的block指的是diy功能中可自定义表单样式,表单标题,并实现自己来设置数据调用。可以从门户->模块管理->数据调用中进去,并添加新数据。

QQ截图20180312232508.png

通过内部调用或者外部调用可以将数据调用到任何一个位置。

QQ截图20180312232608.png

配置文件存放在source\class\block\目录下。
QQ截图20180312232721.png

二、文件解析

用户想自定义表单字段来进行填写内容的话需要重新创建文件

<?phpif(!defined('IN_DISCUZ')) {exit('Access Denied');
}class block_othermk extends discuz_block {    function block_othermk() {}    /**
     * 必须!
     * 返回本数据调用类的显示名称(显示在创建模块时选择“模块数据”的下拉列表里)
     * @return <type>
     */
    //模块名称 模块分类
    function name() {    return "自己的数据";
    }    /**
     * 必须!
     * 返回一个数组: 第一个值为本数据类所在的模块分类;第二个值为模块分类显示的名称(显示在 DIY 模块面板)
     * @return <type>
     */
    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 <type>
     */
    //这个模块要返回的字段
    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 <type>
     */
    //设置需要帅选的参数 然后参数会自动传给  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 <type> $style 模块样式(见 common_block_style 表)。 可以根据模块样式中用到的字段来选择性的获取/不获取某些数据
     * @param <type> $parameter 用户对 getsetting() 给出的表单提交后的内容。
     * @return <type>
     */
    //返回数据的函数,自己发挥吧,一定要保证你返回的字段 在上个 fields 函数里面要有

     function getdata($style, $parameter) {         // 返回summary
         return array('html' => '<p>这是一个演示模块数据类</p>', '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 <type>
     */
    //模块名称 模块分类
    function name() {    return "自己的数据";
    }

QQ截图20180312233739.png

2、blockclass函数

    /**
     * 必须!
     * 返回一个数组: 第一个值为本数据类所在的模块分类;第二个值为模块分类显示的名称(显示在 DIY 模块面板)
     * @return <type>
     */
    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 <type>
     */
    //这个模块要返回的字段
    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 <type>
     */
    //设置需要帅选的参数 然后参数会自动传给  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模板样式的。


文章版权及转载声明

本文作者:符文浩 网址:http://blog.fuwenhao.com/post/415.html 发布于 2021-03-20
文章转载或复制请以超链接形式并注明出处。

发表评论

快捷回复:

评论列表 (暂无评论,874人围观)参与讨论

还没有评论,来说两句吧...

取消
微信二维码
微信二维码
支付宝二维码