给fastadmin增加一个默认搜索的功能

有的时候index控制器会默认搜索,比如默认搜索顶级类别。这样的话,就需要给后台处理增加一个默认的搜索字段数组。

修改,application\common\controller\Backend.php 的buildparams函数。

/**
     * 生成查询所需要的条件,排序方式
     * @param mixed $searchfields 快速查询的字段
     * @param boolean $relationSearch 是否关联查询
     * @param mixed $searchdefault 默认的查询字段
     * @return array
     */
    protected function buildparams($searchfields = null, $relationSearch = null,$searchdefault = null){
        //搜索用到的列
        $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
        $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
        //请求的列值
        $searchform = $this->request->get("search/a");
        $search = array();
        if($searchform){
            if($searchdefault){
                $search = array_merge($searchdefault,$searchform);//合并默认搜索条件和提交的搜索条件。
            }else{
                $search = $searchform;
            }
        }else{
            $search = $searchdefault;
        }
        
        $filter = $this->request->get("filter", '');
        $op = $this->request->get("op", '', 'trim');
        //排序字段
        $sort = $this->request->get("sort", "itemid");
        //DESC倒序--ASC正序
        $order = $this->request->get("order", "DESC");
        //起始值
        $offset = $this->request->get("offset",0);
        //每页显示多少条
        $limit = $this->request->get("limit",10);
        $filter = json_decode($filter, TRUE);
        $op = json_decode($op, TRUE);
        $filter = $filter ? $filter : [];
        $where = [];
        $tableName = '';
        if ($relationSearch){
            if (!empty($this->model)){
                $class = get_class($this->model);
                $name = basename(str_replace('\\', '/', $class));
                $tableName = $this->model->getQuery()->getTable($name) . ".";
            }
            $sort = stripos($sort, ".") === false ? $tableName . $sort : $sort;
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)){
            $where[] = [$this->dataLimitField, 'in', $adminIds];
        }
    		$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
    		foreach ($searcharr as $k => &$v){
            if(isset($search[$k])){
                if(($v=='varcharmid')||($v=='varcharshort')||($v=='varcharlong')){
                    $where[] = [$k, "LIKE", "%".$search[$k]."%"];
                }else{
                    $where[] = [$k, $search[$k]];
                }
            }
            $this->view->assign($k,isset($search[$k])?$search[$k]:'');
    		}
    		unset($v);
        //if ($search){}
        foreach ($filter as $k => $v){
            $sym = isset($op[$k]) ? $op[$k] : '=';
            if (stripos($k, ".") === false){
                $k = $tableName . $k;
            }
            $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
            switch ($sym){
                case '=':
                case '!=':
                    $where[] = [$k, $sym, (string) $v];
                    break;
                case 'LIKE':
                case 'NOT LIKE':
                case 'LIKE %...%':
                case 'NOT LIKE %...%':
                    $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
                    break;
                case '>':
                case '>=':
                case '<':
                case '<=':
                    $where[] = [$k, $sym, intval($v)];
                    break;
                case 'IN':
                case 'IN(...)':
                case 'NOT IN':
                case 'NOT IN(...)':
                    $where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
                    break;
                case 'BETWEEN':
                case 'NOT BETWEEN':
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr))
                        continue;
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '')
                    {
                        $sym = $sym == 'BETWEEN' ? '<=' : '>';
                        $arr = $arr[1];
                    }
                    else if ($arr[1] === '')
                    {
                        $sym = $sym == 'BETWEEN' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $where[] = [$k, $sym, $arr];
                    break;
                case 'RANGE':
                case 'NOT RANGE':
                    $v = str_replace(' - ', ',', $v);
                    $arr = array_slice(explode(',', $v), 0, 2);
                    if (stripos($v, ',') === false || !array_filter($arr))
                        continue;
                    //当出现一边为空时改变操作符
                    if ($arr[0] === '')
                    {
                        $sym = $sym == 'RANGE' ? '<=' : '>';
                        $arr = $arr[1];
                    }
                    else if ($arr[1] === '')
                    {
                        $sym = $sym == 'RANGE' ? '>=' : '<';
                        $arr = $arr[0];
                    }
                    $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
                    break;
                case 'LIKE':
                case 'LIKE %...%':
                    $where[] = [$k, 'LIKE', "%{$v}%"];
                    break;
                case 'NULL':
                case 'IS NULL':
                case 'NOT NULL':
                case 'IS NOT NULL':
                    $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
                    break;
                default:
                    break;
            }
        }
        //print_r($where);exit;
        $where = function($query) use ($where) {
            foreach ($where as $k => $v){
                if (is_array($v)){
                    call_user_func_array([$query, 'where'], $v);
                }
                else{
                    $query->where($v);
                }
            }
        };
        return [$where, $sort, $order, $offset, $limit];
    }




猜你喜欢

转载自blog.csdn.net/u010261924/article/details/79783705