123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- <?php
- namespace app\common\components;
- use Yii;
- class MultiSearchUrl{
- public $url;
- public $conditions;
- public $existConditions;
- public $keywordName = 'kw';
- 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;
- }
-
- 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;
- }
-
- 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);
- }
-
- 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);
- }
-
- 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;
- }
-
- 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);
-
- }
- }
- }
- }
-
- 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.'/';
-
- }
- else
- {
-
- }
- if($suffix!='')
- {
- return rtrim($this->url,'/').$suffix;
- }
- else
- {
- return $this->url;
- }
- }
-
- public function _getTypeFromCondition($condition)
- {
- return preg_replace('/[0-9_]+/','',$condition);
- }
-
- 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;
-
- return $array;
- }
-
- public function getSortConditionConfig($value = NULL)
- {
- $array = $this->sortConfig;
-
- if(!is_null($value))
- {
- return $array[$value];
- }
- else
- {
- return $array;
- }
- }
- }
|