123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- <?php
- namespace app\common\components;
- use Yii;
- /**
- * author: Jack.Chen
- * create time: 15-3-31 上午9:46
- * description: 多条件搜索URL处理
- */
- class MultiSearchUrl{
- public $url;//基础URL
- public $conditions;//新传入的搜索条件
- public $existConditions;//已经存在的搜索条件
- public $keywordName = 'kw';//关键词参数名称keywordName
- public $filterTypes;//待过滤搜索条件类型
- public $conditionParamName='sch';
- public $splitFlag = '-';//条件之间的分割符
- public $orderTypeName = 'st';//排序条件键名
- public $keyword='';
- public $fieldConfig =[];
- public $sortConfig =[];
- public $filterAll = false;
- //初始化实例
- public static function getSelfInstance($params)
- {
- static $instance = null;
- if(is_null($instance))
- {
- $instance = new MultiSearchUrl();
- }
- if(is_array($params)) extract($params);
- if(isset($url))$instance->url = $url;
- if(isset($fieldConfig))$instance->fieldConfig = $fieldConfig;
- if(isset($sortConfig))$instance->sortConfig = $sortConfig;
- if(isset($conditions)&&!empty($conditions)){
- $instance->conditions = $conditions;
- }
- else
- {
- $instance->conditions = array();
- }
- foreach($instance->conditions as $k=>$v)
- {
- if($v==null)unset($instance->conditions[$k]);
- }
- if(isset($keywordName))$instance->keywordName = $keywordName;
- if(isset($filterAll))$instance->filterAll = $filterAll;
- if(!isset($filterAll))$instance->filterAll = false;
- if(isset($filterTypes)&&!empty($filterTypes)){
- $instance->filterTypes = $filterTypes;
- }
- else
- {
- $instance->filterTypes = array();
- }
- if(isset($_GET[$instance->keywordName]))$instance->keyword = safe_replace($_GET[$instance->keywordName]);
- $instance->existConditions = $instance->getSearchConditions();
- return $instance;
- }
- /**根据搜索条件组合后的Url(格式 a1-b2-c10)
- * @param $conditions
- * @return string
- */
- public function createSimpleSearchCondition()
- {
- $conditionStr = '';
- $conditionList = array();
- if(is_array($this->conditions))
- {
- foreach($this->conditions as $key=>$value)
- {
- $conditionList[] = $key.$value;
- }
- }
- asort($conditionList);
- if(!empty($conditionList))
- {
- $conditionStr = join($this->splitFlag,$conditionList);
- $this->url .= $conditionStr.'/';
- }
- return $this->url;
- }
- /**在已有搜索条件上组合传入的搜索条件生成新的Url
- * @return string
- */
- public function createSearchCondition()
- {
- //条件类型
- $types = array();
- $diffTypes = array();
- $inputTypes = array();
- if(is_array($this->existConditions))
- {
- foreach($this->existConditions as $condition)
- {
- $types[] = $this->_getTypeFromCondition($condition);
- }
- }
- //排除掉传入的相同选项类型
- if(!empty($types))$types = array_unique($types);
- if(!empty($this->conditions))$inputTypes = array_keys($this->conditions);
- if(isset($inputTypes)&&isset($diffTypes))$diffTypes = array_unique(array_diff($types,$inputTypes));
- //排除掉已经存在的搜索条件中不满足条件的项目(被过滤的项目和被新传入条件替换的项目)
- if(is_array($this->existConditions))
- {
- foreach($this->existConditions as $key=>$condition)
- {
- $type =$this->_getTypeFromCondition($condition);
- if(empty($diffTypes)||(!in_array($type,$diffTypes)&&!empty($diffTypes))||in_array($type,$this->filterTypes)||$this->filterAll==true)
- {
- unset($this->existConditions[$key]);
- }
- }
- }
- //对新传入条件排序
- $conditionStr = '';
- if(!empty($this->conditions))ksort($this->conditions);
- $conditionList = array();
- if(is_array($this->conditions))
- {
- foreach($this->conditions as $key=>$value)
- {
- if(!in_array($key,$diffTypes)||empty($this->existConditions))
- {
- $conditionList[] = $key.$value;
- }
- }
- }
- //合并已有条件和新条件
- if(is_array($this->existConditions)&&!empty($this->existConditions))
- {
- $conditionList = array_merge($conditionList,$this->existConditions);
- }
- return $this->_createUrl($conditionList);
- }
- /**选择不限帅选条件时候Url重新生成
- * @return string
- */
- public function stickOut()
- {
- $conditionList = array();
- if(!empty($this->existConditions))
- {
- foreach($this->existConditions as $condition)
- {
- $type = $this->_getTypeFromCondition($condition);
- if(!in_array($type,$this->filterTypes))
- {
- $conditionList[] = $condition;
- }
- }
- }
- return $this->_createUrl($conditionList);
- }
- /**获取当前帅选条件
- * @return array|mixed
- */
- public function getSearchConditions()
- {
- $searchCondition = isset($_GET[$this->conditionParamName])?safe_replace($_GET[$this->conditionParamName]):array();
- if(!empty($searchCondition))
- {
- $searchCondition = explode($this->splitFlag,$searchCondition);
- }
- return $searchCondition;
- }
- //当前条件类型
- public function getConditionsTypes()
- {
- //条件类型
- $types = array();
- if(is_array($this->existConditions))
- {
- foreach($this->existConditions as $condition)
- {
- $types[] = $this->_getTypeFromCondition($condition);
- }
- }
- return $types;
- }
- /**反序列化筛选条件
- * @return array
- */
- public function sqllizeConditions()
- {
- $where = '';
- $order = '';
- $sortValue=null;//当前排序条件键值
- $typeField = $this->getTypeFieldConfig(); //搜索条件键名和字段值对应关系
- if(is_array($this->existConditions))
- {
- foreach($this->existConditions as $condition)
- {
- if(strpos($condition,$this->keywordName) === 0){
- $type = $this->keywordName;
- $value = str_replace($this->keywordName,'',$condition);
- }
- else
- {
- $type = $this->_getTypeFromCondition($condition);
- $value = $this->_getValueFromCondition($condition);
- }
- if($type==$this->orderTypeName)//如果为排序条件
- {
- $sortInfo = $this->getSortConditionConfig($value);
- $order = " order by ".$sortInfo['order'];
- $sortValue = $value;
- }
- else
- {
- if(isset($typeField[$type]))
- {
- $field = $typeField[$type]['field'];
- $flag = $typeField[$type]['flag'];
- $sqlType = $typeField[$type]['type'];
- switch($sqlType)
- {
- case 'cat_id':
- $where .= " and $field $flag $value ";
- break;
- case 'cat_ids':
- $where .= " and $field $flag '%,".$value.",%' ";
- break;
- case 'boollist':
- $field = $field[$value-1];
- $where .= " and $field = 1 ";
- break;
- case 'commonlike':
- $where .= " and $field like '%".$value."%'";
- break;
- case 'leftlike':
- $where .= " and $field like '%".$value."'";
- break;
- case 'rightlike':
- $where .= " and $field like '".$value."%'";
- break;
- case 'equals':
- $fieldRs = explode(",",$field);
- $kk = 0;
- $songwhere = '';
- foreach($fieldRs as $frs)
- {
- if($kk==0){
- $songwhere = " $frs $flag $value ";
- }
- else
- {
- $songwhere .= " or $frs $flag $value ";
- }
- $kk++;
- }
- $where .= " and ($songwhere) ";
- break;
- case 'equal':
- $where .= " and $field $flag $value ";
- break;
- case 'gt':
- $where .= " and $field $flag $value ";
- break;
- case 'lt':
- $where .= " and $field $flag $value ";
- break;
- case 'linkmenu':
- $menu = \app\models\Linkmenu::findOne($value);
- $childIds = $menu->arr_child_ids;
- $where .= !empty($childIds)?" and $field in($childIds) ":" and $field $flag $value ";
- break;
- case 'day':
- $starttime = TIMESTAMP-$value*24*3600;
- $where .= " and $field $flag $starttime ";
- break;
- }
- }
- }
- }
- }
- return array('order'=>$order,'where'=>$where,'sortValue'=>$sortValue);
- }
- //根据搜索选项类型获取搜索选项值
- public function getSearchConditionValue($key,$onCondition)
- {
- if($onCondition)return;
- if(!empty($this->existConditions))
- {
- foreach($this->existConditions as $condition)
- {
- $type = $this->_getTypeFromCondition($condition);
- if($type==$key)
- {
- return $this->_getValueFromCondition($condition);
- //return preg_replace('/'.$key.'+/','',$condition);
- }
- }
- }
- }
- //根据最终的搜索条件生成URL
- private function _createUrl($conditionList)
- {
- asort($conditionList);
- if(strpos($this->url,'.html')!==false)
- {
- $this->url = str_replace('.html','',$this->url).'/';
- $suffix = '.html';
- }
- if(!empty($conditionList))
- {
- $conditionStr = join("-",$conditionList);
- $this->url.=$conditionStr.'/';
- /*if(!empty($this->keyword))//如果有关键词,在搜索条件后增加关键词条件
- {
- $this->url .= $this->keywordName.'/'.$this->keyword.'/';
- }*/
- }
- else
- {
- /*if(!empty($this->keyword))//如果有关键词,在搜索条件后增加关键词条件
- {
- $this->url .= $this->keywordName.'/'.$this->keyword.'/';
- }*/
- }
- if($suffix!='')
- {
- return rtrim($this->url,'/').$suffix;
- }
- else
- {
- return $this->url;
- }
- }
- //根据条件获取条件类型(如传入a1返回a)
- public function _getTypeFromCondition($condition)
- {
- return preg_replace('/[0-9_]+/','',$condition);
- }
- //根据条件获取条件值(如传入a1返回1)
- public function _getValueFromCondition($condition)
- {
- return preg_replace('/[a-zA-Z]+/','',$condition);
- }
- //根据搜索条件类型获取条件值
- public function getConditionValueByType($searchConditions,$searchType)
- {
- if(is_array($searchConditions))foreach($searchConditions as $condition)
- {
- $type = preg_replace('/[0-9]+/','',$condition);
- $value = preg_replace('/[a-zA-Z]+/','',$condition);
- if($type==$searchType)
- {
- return $value;
- }
- }
- }
- //获取搜索关键词
- public function getKeyword($searchConditions)
- {
- if(is_array($searchConditions))foreach($searchConditions as $condition)
- {
- if(strpos($condition,$this->keywordName) === 0){
- return str_replace($this->keywordName,'',$condition);
- }
- }
- }
- //搜索条件字段对应值配置
- public function getTypeFieldConfig()
- {
- $array = $this->fieldConfig;
- /*$array = array(
- 'r'=>array('type'=>'linkmenu','flag'=>'=', 'field'=>'region_id'),
- 'c'=>array('type'=>'equal','flag'=>'=','field'=>'cat_id'),
- 't'=>array('type'=>'equal','flag'=>'=','field'=>'doc_type'),
- 't1'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type1'),
- 't2'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type2'),
- 't3'=>array('type'=>'equal','flag'=>'=','field'=>'ext_type3'),
- );*/
- return $array;
- }
- //排序条件配置
- public function getSortConditionConfig($value = NULL)
- {
- $array = $this->sortConfig;
- /* $array = array(
- //文库
- '1'=>array('value'=>1, 'order'=>'create_time desc','title'=>'按发布时间排序','tips'=>'点击按发布时间排序'),
- '2'=>array('value'=>2, 'order'=>'down_num desc','title'=>'按下载数量排序','tips'=>'点击按下载数量排序'),
- '3'=>array('value'=>3, 'order'=>'views desc','title'=>'按浏览数排序','tips'=>'点击按浏览数排序'),
- //商城
- '7'=>array('value'=>7, 'order'=>'sales desc','title'=>'按销量排序','tips'=>'按销量排序'),
- '8'=>array('value'=>8, 'order'=>'sales asc','title'=>'按销量排序','tips'=>'按销量排序'),
- '9'=>array('value'=>9, 'order'=>'market_price desc','title'=>'按价格排序','tips'=>'按价格排序'),
- '10'=>array('value'=>10, 'order'=>'market_price asc','title'=>'按价格排序','tips'=>'按价格排序'),
- '11'=>array('value'=>11, 'order'=>'create_time desc','title'=>'按上架时间排序','tips'=>'按销量排序'),
- '12'=>array('value'=>12, 'order'=>'create_time asc','title'=>'按上架时间排序','tips'=>'按销量排序'),
- '999'=>array('value'=>999, 'order'=>'is_recommend desc','title'=>'推荐','tips'=>'推荐'),
- '1000'=>array('value'=>1000, 'order'=>'is_recommend asc','title'=>'推荐','tips'=>'推荐'),
- //论坛
- '13'=>array('value'=>13, 'order'=>'create_time desc','title'=>'按最新','tips'=>'按最新'),
- '14'=>array('value'=>14, 'order'=>'post_num desc','title'=>'按热议','tips'=>'按热议'),
- '15'=>array('value'=>15, 'order'=>'reward desc','title'=>'按报酬排序','tips'=>'按报酬排序'),
- '16'=>array('value'=>16, 'order'=>'reward asc','title'=>'按报酬排序','tips'=>'按报酬排序'),
- '17'=>array('value'=>17, 'order'=>'stock desc','title'=>'按需求数量排序','tips'=>'按需求数量排序'),
- '18'=>array('value'=>18, 'order'=>'stock asc','title'=>'按需求数量排序','tips'=>'按需求数量排序'),
- );*/
- if(!is_null($value))
- {
- return $array[$value];
- }
- else
- {
- return $array;
- }
- }
- }
- /* HTML 示例:
- <div class="ts_header">
- <div class="ts_head_grade">
- <p>所在年级:</p>
- <li <?php if(!in_array('c',$conditionsTypes)){ ?>class="on" <?php }?>> <a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'filterTypes'=>array('c')))->createSearchCondition();?>">全部</a></li>
- <?php if(!empty($catList)&&is_array($catList))foreach($catList as $cat){?>
- <li <?php if(in_array('c'.$cat['cat_id'],$searchConditions)){?> class="on" <?php }?>><a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'conditions'=>array('c'=>$cat['cat_id'])))->createSearchCondition();?>"><?php echo $cat['cat_name'];?>
- </a></li>
- <?php }?>
- <div class="clear"></div>
- </div>
- <?php if($childCatList){?>
- <div class="ts_head_hig" >
- <div class="ts_head_hig_cot" style="padding-left: 104px;">
- <?php if(!empty($childCatList)&&is_array($childCatList))foreach($childCatList as $cat){?>
- <li <?php if(in_array('c'.$cat['cat_id'],$searchConditions)){?> class="on" <?php }?>><a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'conditions'=>array('c'=>$cat['cat_id'])))->createSearchCondition();?>"><?php echo $cat['cat_name'];?>
- </a></li>
- <?php }?>
- <div class="clear"></div>
- </div>
- </div>
- <?php }?>
- <?php if($getTypeList){?>
- <div class="ts_head_class">
- <div class="ts_head_class_cot">
- <p>课程分类:</p>
- <li <?php if(!in_array('t',$conditionsTypes)){ ?>class="on" <?php }?>> <a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'filterTypes'=>array('t')))->createSearchCondition();?>">全部</a></li>
- <?php if(!empty($getTypeList)&&is_array($getTypeList))foreach($getTypeList as $type){?>
- <li <?php if(in_array('t'.$type['type_id'],$searchConditions)){?> class="on" <?php }?>><a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'conditions'=>array('t'=>$type['type_id'])))->createSearchCondition();?>"><?php echo $type['name'];?></a></li>
- <?php }?>
- <div class="clear"></div>
- </div>
- </div>
- <?php }?>
- <div class="ts_head_class">
- <div class="ts_head_class_cot">
- <p>授课方式:</p>
- <li <?php if(!in_array('tt',$conditionsTypes)){ ?>class="on" <?php }?>> <a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'filterTypes'=>array('tt')))->createSearchCondition();?>">全部</a></li>
- <?php if($teachTypeList)foreach($teachTypeList as $key=>$name){
- if($key==Yii::app()->params['weiCourseTypeId'])continue;
- ?>
- <li <?php if(in_array('tt'.$key,$searchConditions)){?> class="on" <?php }?>><a href="<?php echo MultiSearchUrl::getSelfInstance(array('url'=>Url::allCourse(),'conditions'=>array('tt'=>$key)))->createSearchCondition();?>"><?php echo $name;?></a></li>
- <?php }?>
- </div>
- </div>
- </div>
- */
- /*php示例
- public function actionAllCourse()
- {
- //初始化栏目信息
- $topCat = $this->categorys[Yii::app()->params['teacherCatId']];
- $catList = array();
- $catIds = explode(",",$topCat['arr_child_id']);
- if(is_array($catIds))foreach($catIds as $id)
- {
- $cat = $this->categorys[$id];
- if($cat['cat_id']!=$topCat['cat_id'])
- {
- $catList[] = $cat;
- }
- }
- //解析组合条件
- $searchConditions = MultiSearchUrl::getSelfInstance(array())->getSearchConditions();
- $childCatList = array();
- $currentCatId = $this->_getCatId($searchConditions);
- if($this->categorys[$currentCatId]['arr_child_id'])
- {
- $catIds = explode(",", $this->categorys[$currentCatId]['arr_child_id']);
- if(is_array($catIds))foreach($catIds as $id)
- {
- $cat = $this->categorys[$id];
- if($cat['cat_id']!=$currentCatId)
- {
- $childCatList[] = $cat;
- }
- }
- }
- //当选取三级栏目时候的处理
- if($currentCatId&&!$childCatList)
- {
- $catIds = explode(",", $this->categorys[$this->categorys[$currentCatId]['parent_id']]['arr_child_id']);
- if(is_array($catIds))foreach($catIds as $id)
- {
- $cat = $this->categorys[$id];
- if($cat['cat_id']!=$this->categorys[$currentCatId]['parent_id'])
- {
- $childCatList[] = $cat;
- }
- }
- }
- //类别
- if($currentCatId)
- {
- $typeList = json_decode(Yii::app()->filecache->get(CacheId::typeCacheId($this->siteId)),true);
- if($this->categorys[$currentCatId]['usable_type'])
- {
- $types = explode(",",$this->categorys[$currentCatId]['usable_type']);
- if(is_array($types))foreach($types as $typeId)
- {
- $getTypeList[] = $typeList[$typeId];
- }
- }
- }
- $teachTypeList = Yii::app()->params['options']['teachType'];
- $keyword = isset($_GET[MultiSearchUrl::getSelfInstance()->keywordName])?safe_replace($_GET[MultiSearchUrl::getSelfInstance()->keywordName]):'';
- //获取搜索条件类型
- $conditionsTypes = MultiSearchUrl::getSelfInstance()->getConditionsTypes();
- //分页参数
- $page = isset($_GET['page'])?intval($_GET['page']):1;
- $pageSize = Yii::app()->params['teacherPageSize'];
- $offset = ($page-1)*$pageSize;
- //解析SQL
- $sqlInfo = MultiSearchUrl::getSelfInstance(array())->sqllizeConditions();
- $where = " where status=1 ";
- $where .= !empty($sqlInfo['where'])?$sqlInfo['where']:'';
- $likeStr = !empty($keyword)?" and (title like '%".$keyword."%')":'';
- $orderStr = !empty($sqlInfo['order'])?$sqlInfo['order']:' order by point desc';
- $sqlCount = "select count(distinct(user_name)) as count from {{cource}} $where $likeStr";
- $sqlList = "select * from (select * from {{user}} where user_name in(select distinct(user_name) from {{cource}} $where ) $orderStr limit $offset,$pageSize ) a left join {{user_teacher}} b on a.user_id=b.user_id ";
- $countModel = Yii::app()->db->createCommand($sqlCount);
- $result = $countModel->queryAll();
- $count = $result[0]['count'];
- $listModel = Yii::app()->db->createCommand($sqlList);
- $resultlist = $listModel->queryAll();
- $pages = new CPagination($count);
- $pages->pageSize = intval($pageSize);
- $this->render('allcourse',array('resultlist'=>$resultlist,'pages'=>$pages,'catList'=>$catList,'searchConditions'=>$searchConditions,'conditionsTypes'=>$conditionsTypes,'childCatList'=>$childCatList,'getTypeList'=>$getTypeList,'teachTypeList'=>$teachTypeList));
- }
- */
|