226, 'height' => 150, 'method' => ThumbGenerator::METHOD_INNER]; /** * @inheritdoc */ public static function tableName() { return 'tagForAdminV'; } public function getSearchCriteria() { self::startSearch(); $query = $this->_getSearchCriteriaQuery(); $list = $this->_prepareCriteria($query->createCommand()->queryAll()); return $list; } public function getSearchResult($sortAttributeId = null) { $query = static::createQuery(); if ($sortAttributeId) { $query->from(['searchResultObjectSortedV ro']); $query->where(['sortAttributeId' => $sortAttributeId]); $query->orderBy('ro.sorterAsc DESC, ro.sorterDesc DESC'); } else { $query->from(['searchResultObjectV ro']); } $query->select([ 'ro.id', 'ro.alias', 'ro.rating', 'ro.address', 'ro.description', 'ro.statusId', 'ro.statusName', 'ro.latitude', 'ro.longitude', 'imageFileFs' => '(' . Ticket::getTicketImageSql('ro.id') . ')', 'imageCount' => '(' . Ticket::getTicketImageCountSql('ro.id') . ')', ]); $query->limit(self::RESULT_LIMIT); return [ 'list' => $this->_prepareTickets($query->createCommand()->queryAll()), ]; } protected function _prepareTickets($list) { $ticketIds = ArrayHelper::getColumn($list, 'id'); $tags = $this->_getGroupedTicketTags($ticketIds); foreach ($list as &$ticket) { $ticket['tags'] = []; if (isset($tags[$ticket['id']])) { $ticket['tags'] = array_values(DataHelper::group($tags[$ticket['id']], ['typeCode', 'typeName', 'internalTypeId'])); } $ticket['thumbnailUrl'] = ThumbGenerator::getThumbnailUrl( empty($ticket['imageFileFs']) ? ThumbGenerator::NO_IMAGE_TICKET : $ticket['imageFileFs'], self::$imgSearch['width'], self::$imgSearch['height'], self::$imgSearch['method'] ); unset($ticket['imageFileFs']); } return $list; } /** * @param $ticketIds * * @return array|mixed|yii\db\DataReader [objectId => [[(tag field)], ...], ...] */ protected function _getGroupedTicketTags($ticketIds) { return Ticket::getTicketTags($ticketIds, 'list'); } // /** // * @param $ticketIds // * // * @return array|mixed|yii\db\DataReader [objectId => [[(image fields)], ...], ...] // */ // protected function _getGroupedTicketImages($ticketIds) // { // if (empty($ticketIds)) { // return []; // } // // $q = static::createQuery(); // $q->select(['nodeId', 'imageId', 'name', 'filenameFs']); // $q->from(['imageV']); // $q->where(['nodeId' => $ticketIds, 'display' => 'y']); // $q->orderBy('sorter'); // // $result = []; // $list = $q->createCommand()->queryAll(); // if ($list) { // foreach ($list as &$row) { // $row['fileUrl'] = ThumbGenerator::getFileUrl($row['filenameFs']); // $row['thumbnailUrl'] = ThumbGenerator::getThumbnailUrl( // $row['filenameFs'], // self::$imgSearch['width'], // self::$imgSearch['height'], // self::$imgSearch['method'] // ); // // $result[$row['nodeId']][] = $row; // } // } // return $result; // } protected function _prepareCriteria($list) { $result = []; foreach ($list as $row) { $typeCode = $row['typeCode']; if (!array_key_exists($typeCode, $result)) { $result[$typeCode] = [ 'typeCode' => $row['typeCode'], 'typeName' => $row['typeName'], ]; } $row['selected'] = ($row['selected'] == 'y'); unset($row['typeCode']); unset($row['typeName']); $result[$typeCode]['list'][] = $row; } return array_values($result); } protected function _getSearchCriteriaQuery() { $query = static::createQuery(); $query->from(['searchTagV AS st']); $query->select([ 'st.id', 'st.name', 'st.typeCode', 'st.typeName', 'st.collapsed', '(IF(st.criterionId IS NULL, "n", "y")) AS selected', 'IF(st.criterionId IS NULL AND st.resultId IS NOT NULL AND st.extraObjects = 0, "y", "n") AS disabled', 'st.attributeDataType dataType', 'st.required', 'st.uofmPre', 'st.uofmPost', 'a.decimalMin', 'a.decimalMax', 'a.decimalRound', 'a.textIsLong', 'adf.name AS datetimeName', 'adf.outputFormatJs AS datetimeOutputFormatJs', 'st.valueLowNumeric', 'st.valueHighNumeric', 'st.valueLowDatetime', 'st.valueHighDatetime' ]); $query->leftJoin('attributeV AS a', 'a.id = st.id AND st.internalTypeId = "attribute"'); $query->leftJoin('attributeDatetimeFormatV AS adf', 'a.datetimeStorageId IS NOT NULL AND adf.id = a.datetimeStorageId'); $query->where('st.canSearch = "y"'); $query->andWhere('st.attributeDataType IS NULL OR st.attributeDataType != "text"'); $query->andWhere('(st.resultId IS NOT NULL) OR (st.criterionId IS NOT NULL)'); $query->orderBy('st.typeSorter, st.sorter'); return $query; } public static function setSearchSession($reset = 0) { $query = static::createQuery(); $query->select(array('search_StartSession("' . yii::$app->session->id . '", ' . $reset . ')')); $query->from(array()); return $query->createCommand()->execute(); } public function setSearchTags($tagList) { $parts = []; foreach ($tagList as $tagId => $info) { $_id = (int) $tagId; if (is_array($info)) { if ('n' === $info['tp']) { $_lo = isset($info['lo']) ? (float) $info['lo'] : 'NULL'; $_hi = isset($info['hi']) ? (float) $info['hi'] : 'NULL'; $parts[] = "search_SetAttributeNumeric({$_id}, {$_lo}, {$_hi})"; } elseif ('d' === $info['tp']) { $_lo = isset($info['lo']) ? static::getDb()->quoteValue($info['lo']) : 'NULL'; $_hi = isset($info['hi']) ? static::getDb()->quoteValue($info['hi']) : 'NULL'; $parts[] = "search_SetAttributeDatetime({$_id}, {$_lo}, {$_hi})"; } else { throw new Exception('Wrong attribute type code: "' . $info['tp'] . '"'); } } else { $parts[] = "search_SetTag({$_id})"; } } if (!empty($tagList)) { $query = static::createQuery(); $query->select(array(implode(' AND ', $parts))); $query->from(array()); $query->createCommand()->execute(); } return $this; } public static function startSearch() { $query = static::createQuery(); $command = $query->createCommand(); $command->setSql('call search_Search()'); return $command->execute(); } public function getSortAttributes() { $query = static::createQuery(); $query->from(['searchAttributeSortV']); $query->select(['id', 'name']); $query->orderBy('sorter'); return $query->createCommand()->queryAll(); } }