array( 'class' => AccessControlBehavior::className(), 'rules' => array( array( 'allow' => true, 'actions' => array('tag-list') ) ) ) ); } /** * Повертає список міток та знайдених оголошень для сторінки пошуку * * @api {get} search/tag-list * @apiName GetSearchTagList * @apiGroup Search * @apiParam {Object.} tagList * [tagId => bool|['lo' => mixed, 'hi' => mixed, 'tp' => string], ...] * mixed lo - lower limit * mixed hi - upper limit * mixed tp - value type "n" - number, "d" - date * @apiSuccess {Boolean} status * @apiSuccess {Object} data * @apiSuccess {Array.} data.tags * @apiSuccess {Array.} data.tickets */ public function actionTagList() { $search = new Search(); //$request = $this->getInputJson(); $request = $this->getRequestGetParams(); $tagList = isset($request['tagList']) ? $this->_prepareTagList($request['tagList']) : []; if ($this->_checkTagsRenew($tagList)) { $search->setSearchSession(1); $search->setSearchTags($tagList); } else { $search->setSearchSession(0); } $sortAttributes = $search->getSortAttributes(); $criteria = $search->getSearchCriteria(); $sortAttributeId = $this->_prepareSortIndex( empty($request['sortAttributeId']) ? null : (int) $request['sortAttributeId'], $sortAttributes ); $tickets = $search->getSearchResult($sortAttributeId); $result = [ 'status' => true, 'data' => [ 'tags' => $criteria, 'tickets' => $tickets['list'], 'sortAttributeId' => $sortAttributeId, 'sortAttributes' => $sortAttributes, ] ]; $this->displayJson($result); } protected function _prepareSortIndex($sortIndex, $sortAttributes) { if (empty($sortAttributes)) { return null; } if (null !== $sortIndex) { $sortIndex = (int) $sortIndex; foreach ($sortAttributes as $row) { if ($sortIndex === ($index = (int) $row['id'])) { return $sortIndex; } } } return reset($sortAttributes)['id']; } protected function _checkTagsRenew($tags) { $currentTags = (array) yii::$app->session->get('searchTags'); ksort($currentTags); ksort($tags); if (serialize($tags) !== serialize($currentTags)) { yii::$app->session->set('searchTags', $tags); return true; } return false; } /** * @param array $tagList [tagId => bool|['lo' => mixed, 'hi' => mixed, 'tp' => string]] * * @throws \Exception * @return array */ protected function _prepareTagList($tagList) { $result = array(); foreach($tagList as $key => $value) { if (is_array($value)) { if (empty($value['tp']) || !in_array($value['tp'], ['n', 'd'])) { if ('dev' === YII_ENV) { throw new Exception('Wrong attribute definition : ' . var_export($value, true)); } continue; } if (isset($value['lo']) || isset($value['hi'])) { $result[$key] = [ 'tp' => $value['tp'], 'lo' => isset($value['lo']) ? $value['lo'] : null, 'hi' => isset($value['hi']) ? $value['hi'] : null, ]; } } else { if ($value === true) { $result[$key] = (bool) $value; } } } return $result; } }