zdcms二次开发增加linkage值可以作为搜索值搜索内容页自定义字段的值
做项目的时候,遇到了这个需求,客户懒得在发布文章的时候勾选checkbox的参数,只愿意复制粘贴到文本字段里,所以我就想到用linkage把搜索字段放进去,然后用这个里面的值作为关键词去搜索文章内容的自定义字段里的内容。
接下来说下具体的实现方法;
首先,给栏目添加自定义字段,勾选主表,类型文本。用于粘贴被搜索内容的。然后记得在搜索权限设置里面勾选下这个字段,要不然搜不到。
然后填加一个自定义linkage去放你要联动的搜索关键词。
接着来前端把linkage里的值循环出来,写法可以参考以下代码;
{linkage code=wzrycs pid=0 return=c1} //循环一级联动菜单 {$c1.name} // 一级联动菜单名称 {linkage code=wzrycs pid=$c1.id return=c2} //循环二级联动菜单 <input type="checkbox" name="field" value="{$c2.name}" > <label class="btn">{$c2.name}</label> // 二级联动菜单名称 {/linkage} {/linkage}
用上面的代码就可以循环出linkage的数据了,接下来要获取linkage前台勾选的值,拼接成搜索url去搜索。
前端js可以这样写;
<script type="text/javascript"> function chk(){ var obj=document.getElementsByName('field'); //选择所有name="'field'"的对象,返回数组 //取到对象数组后,我们来循环检测它是不是被选中 var s=''; for(var i=0; i<obj.length; i++){ if(obj[i].checked) s+=obj[i].value+','; //如果选中,将value添加到变量s中 } //那么现在来检测s的值就知道选中的复选框的值了 //alert(s==''?'你还没有选择任何内容!':s); window.href ='index.php?s=news&c=search&catid={$catid}&zhms=' + s ; } </script>
用上面的代码,就可以在前端生成这样的url了;
网址/index.php?s=news&c=search&catid=当前栏目id&被搜索的自定义字段名=关键词1,关键词2,
到这里了不要高兴,因为你尝试搜索会发现报错,还需要添加下类文件;
我以News模块为例,新建App/News/Models/Search.php
<?php namespace Phpcmf\Model\News; // 模块内容搜索类 class Search extends \Phpcmf\Model\Search { // 获取搜索参数 public function get_param($module) { list($catid, $get) = parent::get_param($module); // 这里可以重组$get变量 return [$catid, $get]; } // 自定义组合查询条件 protected function mysearch($module, $where, $get) { // 重新对where条件的组装 $zhms = $get['zhms']; //zhms 为自定义字段 foreach ($where as $i => $v) { if ($zhms && strpos($v, 'zhms')) { $arr = explode(',', $zhms); $wh = []; foreach ($arr as $t) { $wh[] = "`zhms` like '%".$t."%'"; } $where[$i] = "(".implode(' OR ', $wh).")"; //如果是多个关键词要同时包含查询出结果,把OR替换成AND即可 } } // 比如我加一个条件作为搜索条件 // $where[] = "zt=1"; // 表加一个zt字段=1的数据 return $where; } }
然后打开/dayrui/Fcms/Model/Search.php,修改文件内容为下面代码;
<?php namespace Phpcmf\Model; /** * 本文件是框架系统文件,二次开发时不可以修改本文件,可以通过继承类方法来重写此文件 **/ // 模块搜索类 class Search extends \Phpcmf\Model { public $mytable; // 模块表名称 // 初始化搜索主表 public function init($table) { $this->mytable = SITE_ID.'_'.$table; return $this; } /** * 查询数据并设置缓存 */ public function get($module, $get, $catid) { // 模块表名称 $table = $this->dbprefix($this->mytable); // 排序查询参数 ksort($get); $param = $get; $get['order'] = $get['page'] = null; unset($get['order'], $get['page']); // 查询缓存 $id = md5($table.dr_array2string($get)); if (!IS_DEV && SYS_CACHE_SEARCH) { $data = $this->db->table($this->mytable.'_search')->where('id', $id)->get()->getRowArray(); $time = intval(SYS_CACHE_SEARCH) * 3600; if ($data && $data['inputtime'] + $time < SYS_TIME) { $this->db->table($this->mytable.'_search')->where('id', $id)->delete(); $data = []; } } else { $data = []; } // 缓存不存在重新入库更新缓存 if (!$data) { $get['keyword'] = $get['catid'] = null; unset($get['keyword'], $get['catid']); // 主表的字段 $field = \Phpcmf\Service::L('cache')->get('table-'.SITE_ID, $this->dbprefix($this->mytable)); if (!$field) { return dr_return_data(0, dr_lang('主表【%s】字段不存在', $this->mytable)); } $mod_field = $module['field']; foreach ($field as $i) { !isset($mod_field[$i]) && $mod_field[$i] = ['ismain' => 1]; } // 默认搜索条件 $where = [ '`'.$table.'`.`status` = 9' ]; /* if (dr_is_app('fstatus') && isset($this->module['field']['fstatus']) && $this->module['field']['fstatus']['ismain']) { $where[] = [ '`'.$table.'`.`fstatus` = 1' ]; }*/ // 关键字匹配条件 if ($param['keyword'] != '') { $temp = []; $sfield = explode(',', $module['setting']['search']['field'] ? $module['setting']['search']['field'] : 'title,keywords'); $search_keyword = trim(str_replace([' ', '_'], '%', dr_safe_replace($param['keyword'])), '%'); if ($sfield) { foreach ($sfield as $t) { if ($t && in_array($t, $field)) { $temp[] = '`'.$table.'`.`'.$t.'` LIKE "%'.$search_keyword.'%"'; } } } $where[] = $temp ? '('.implode(' OR ', $temp).')' : '`'.$table.'`.`title` LIKE "%'.$search_keyword.'%"'; } // 模块字段过滤 foreach ($mod_field as $name => $field) { if (isset($field['ismain']) && !$field['ismain']) { continue; } if (isset($get[$name]) && strlen($get[$name])) { $where[] = $this->_where($table, $name, $get[$name], $field); } } // 会员字段过滤 $member_where = []; if (\Phpcmf\Service::C()->member_cache['field']) { foreach (\Phpcmf\Service::C()->member_cache['field'] as $name => $field) { if (isset($field['ismain']) && !$field['ismain']) { continue; } if (!isset($mod_field[$name]) && isset($get[$name]) && strlen($get[$name])) { $member_where[] = $this->_where($this->dbprefix('member_data'), $name, $get[$name], $field); } } } // 按会员组搜索时 if ($param['groupid'] != '') { $member_where[] = '`'.$this->dbprefix('member_data').'`.`id` IN (SELECT `uid` FROM `'.$this->dbprefix('member').'_group_index` WHERE gid='.intval($param['groupid']).')'; } // 组合会员字段 if ($member_where) { $where[] = '`'.$table.'`.`uid` IN (select `id` from `'.$this->dbprefix('member_data').'` where '.implode(' AND ', $member_where).')'; } // 栏目的字段 if ($catid) { $more = 0; $cat_field = $module['category'][$catid]['field']; // 副栏目判断 if (isset($module['field']['catids']) && $module['field']['catids']['fieldtype'] = 'Catids') { $fwhere = []; if ($module['category'][$catid]['child'] && $module['category'][$catid]['childids']) { $fwhere[] = '`'.$table.'`.`catid` IN ('.$module['category'][$catid]['childids'].')'; $catids = @explode(',', $module['category'][$catid]['childids']); } else { $fwhere[] = '`'.$table.'`.`catid` = '.$catid; $catids = [ $catid ]; } foreach ($catids as $c) { if (version_compare(\Phpcmf\Service::M()->db->getVersion(), '5.7.0') < 0) { // 兼容写法 $fwhere[] = '`'.$table.'`.`catids` LIKE "%\"'.intval($c).'\"%"'; } else { // 高版本写法 $fwhere[] = "(`{$table}`.`catids` <>'' AND JSON_CONTAINS (`{$table}`.`catids`->'$[*]', '\"".intval($c)."\"', '$'))"; } } $fwhere && $where[0] = '('.implode(' OR ', $fwhere).')'; } else { // 无副栏目时 $where[0] = '`'.$table.'`.`catid`'.($module['category'][$catid]['child'] ? 'IN ('.$module['category'][$catid]['childids'].')' : '='.(int)$catid); } if ($cat_field) { // 栏目模型表 $more_where = []; $table_more = $this->dbprefix($this->mytable.'_category_data'); foreach ($cat_field as $name) { if (isset($get[$name]) && strlen($get[$name])) { $more = 1; $more_where[] = $this->_where($table_more, $name, $get[$name], $module['category_data_field'][$name]); } /* if (isset($_order_by[$name])) { $more = 1; $order_by[] = '`'.$table.'`.`'.$name.'` '.$_order_by[$name]; }*/ } $more && $where[] = '`'.$table.'`.`id` IN (SELECT `id` FROM `'.$table_more.'` WHERE '.implode(' AND ', $more_where).')'; } } // 筛选空值 foreach ($where as $i => $t) { if (strlen($t) == 0) { unset($where[$i]); } } // 自定义组合查询 $where = $this->mysearch($module, $where, $get); $where = $where ? 'WHERE '.implode(' AND ', $where) : ''; // 组合sql查询结果 $sql = "SELECT `{$table}`.`id` FROM `".$table."` {$where} ORDER BY NULL "; // 统计搜索数量 $ct = $this->db->query("SELECT count(*) as t FROM `".$table."` {$where} ORDER BY NULL ")->getRowArray(); $data = [ 'id' => $id, 'catid' => intval($catid), 'params' => dr_array2string(['param' => $param, 'sql' => $sql]), 'keyword' => $param['keyword'] ? $param['keyword'] : '', 'contentid' => intval($ct['t']), 'inputtime' => SYS_TIME ]; if ($ct['t']) { // 存储数据 $this->db->table($this->mytable.'_search')->replace($data); } } // 格式化值 $p = dr_string2array($data['params']); $data['sql'] = $p['sql']; $data['params'] = $p['param']; if (isset($param['catdir']) && $param['catdir'] && $catid) { # 目录栏目模式 unset($data['params']['catid']); } elseif ($catid) { $data['params']['catid'] = $catid; } $data['params']['order'] = $param['order']; // order 参数不变化 return $data; } // 获取搜索参数 public function get_param($module) { $get = $_GET; $get = isset($get['rewrite']) ? dr_search_rewrite_decode($get['rewrite'], $module['setting']['search']) : $get; $get && $get = \Phpcmf\Service::L('input')->xss_clean($get); $get['s'] = $get['c'] = $get['m'] = $get['id'] = null; unset($get['s'], $get['c'], $get['m'], $get['id']); if (!$get && IS_API_HTTP) { $get = \Phpcmf\Service::L('input')->xss_clean($_POST); } $_GET['page'] = $get['page']; $get['keyword'] = dr_get_keyword($get['keyword']); if (isset($get['catdir']) && $get['catdir']) { $catid = (int)$module['category_dir'][$get['catdir']]; unset($get['catid']); } else { $catid = (int)$get['catid']; isset($get['catid']) && $get['catid'] = $catid; } return [$catid, $get]; } // 自定义组合查询条件 protected function mysearch($module, $where, $get) { return $where; } }
完成以上步骤,你就会发现成功了。