IcePHP框架中的快速后台中的通用CRUD功能框架(九) SCrudOperationIndex 及 SCrudOperationSearch 类

缑赤岩
2023-12-01
<?php
/**
 * 首页操作类
 *
 * @author bluehire
 *
 */
class SCrudOperationIndex extends SCrudOperation {
    public function __construct($father){
        parent::__construct($father, SCrudOperation::METHOD_INDEX);
    }

    /**
     * 首页处理
     *
     * @see SCrudOperationTable::process()
     */
    public function process(SRequest $req) {
        // @todo:细节检查权限
        $this->output ();
    }

    /**
     * 显示首页
     */
    private function output() {
        //输出首页头
        $this->crud->display('index_header',array('title'=>$this->crud->title));

        //输出用户信息,退出登录等
        $this->crud->display('index_userinfo');

        //输出操作菜单
        $this->crud->display('index_menu');

        //输出脚本
        $this->crud->display('javascript');

        //输出功能区头
        $this->crud->display('work_header');

        //输出搜索区
        $this->crud->display('search_header');
        foreach ( $this->crud->fields as $field ) {
            $field->showSearch();
        }
        $this->crud->display('search_footer');

        //输出表级操作
        $this->crud->operation->outputTable();

        // 输出分页及排序
        $this->crud->display ( 'page_sort', array (
                'pagesize' => $this->crud->pageSize,  //默认每页行数
                'all' => $this->crud->listSortable(), //全部字段配置信息
                'curField' => $this->crud->pageSort, //默认字段
                'dir' => strtolower ( $this->crud->pageDir )  //排序方向
        ) );

        //输出空白表格容器
        $this->crud->display ( 'grid_container');

        //输出多选操作
        $this->crud->operation->outputMulti();

        //输出分页
        $this->crud->display('page_page');

        //输出功能区尾
        $this->crud->display('work_footer');

        //输出首页尾
        $this->crud->display('index_footer');
    }

    public function show($row=null){
        //@todo:显示操作
        echo $this->title;
    }

}



<?php
/**
 * 默认搜索操作
 * @author bluehire
 *
 */
class SCrudOperationSearch extends SCrudOperation{
    public function __construct(SCrud $father) {
        parent::__construct($father, self::METHOD_SEARCH);
    }

    /**
     * 点击查询按钮后的操作
     * @see SCrudOperation::process()
     *
     */
    public function process(SRequest $req){
        //如果请求中指定了页面尺寸,设置页面尺寸
        if ($req->exist ( '_pagesize' )) {
            $size = intval ( $req->_pagesize );
            $size = max ( min ( $size, 1000 ), 1 );
            $this->crud->pageSize = $size;
        } else {
            $size = $this->crud->pageSize;
        }

        //如果请求中指定了排序依据
        if ($req->exist ( '_orderby' )) {
            $orderby = trim ( $req->_orderby );
            $orderField = $this->crud->field ( $orderby );
                
            if ($orderField and $orderField->isSortable ()) {
                if (! $req->exist ( '_dir' )) {
                    $dir = 'desc';
                } else {
                    $dir = trim ( $req->_dir );
                    if ($dir !== 'asc' and $dir !== 'desc') {
                        $dir = 'desc';
                    }
                }
                $this->crud->pageSort = $orderby;
                $this->crud->pageDir = $dir;
            }
        }
        $orderby = $this->crud->pageSort;
        $dir = $this->crud->pageDir;

        //查看所有搜索条件
        $filter = $this->crud->gridFilter;
        if (! $filter) {
            $where = array ();
        } elseif (is_array ( $filter )) {
            $where = $filter;
        } else {
            $where = array (
                    $filter
            );
        }

        //处理所有 可搜索字段,构造查询条件
        foreach ( $this->crud->listSearchable () as $field ) {
            $w = $field->where ( $req );
            if (! $w) {
                continue;
            }
            if (is_array ( $w )) {
                $where = array_merge ( $where, $w );
            } else {
                $where [] = $w;
            }
        }

        if (! $req->exist ( '_page' )) {
            $page = 1;
        } else {
            $page = max(1,intval ( $req->_page ));
        }
        $this->output ( $where, $page, $size, $orderby, $dir );
    }

    //查询数据并输出
    private function output($where,$page,$size,$orderby,$dir){
        //所有满足条件的数据数量
        $count=$this->crud->model->count($where);

        //页数
        $pageAll=ceil($count/$size);

        //当前页
        $page=max(min($page,$pageAll),1);

        //主键字段必须查询
        $fields=array_keys($this->crud->listGridable());

        //查询数据
        $data=$this->crud->model->fields($fields)->where($where)->orderby(array($orderby,$dir))->limit(array(($page-1)*$size,$size))->select();

        //生成表格内容的显示
        $grid=$this->outputGrid($data);

        //Ajax形式返回前端
        echo json_encode(array(
                'count'=>$count,
                'pageAll'=>$pageAll,
                'page'=>$page,
                'size'=>$size,
                'grid'=>$grid
        ));
    }

    /**
     * 根据数据输出表格内容
     * @param SResult $data
     * @return string
     */
    private function outputGrid(SResult $data) {
        $rowNo = $this->crud->rowNo;
        $multi = $this->crud->operation->hasMulti ();
        $operations = $this->crud->operation->rowOperations;
        $fields = $this->crud->fields;

        // 显示表头
        $this->crud->display ( 'grid_header', array (
                'rowNo' => $rowNo,
                'multi' => $multi,
                'operations' => $operations,
                'fields' => $fields
        ) );

        // 显示表格数据行
        $this->crud->display ( 'grid_body', array (
                'rowNo' => $rowNo,
                'multi' => $multi,
                'operations' => $operations,
                'fields' => $fields,
                'data' => $data
        ) );

        // 显示表格尾
        $this->crud->display('grid_footer');

        return ob_get_clean ();
    }

    public function show($row=null){
        //此处不被调用
    }
}


 类似资料: