_select) { $this->_select = $this->_db->select(); $this->_select->union([ $this->_getCategoryList4Select(), $this->_getQuestionList4Select(), ]); $this->_applySelectOptions($this->_select, $this->_selectOptions); } return $this->_select; } /** * @param Zend_Db_Select $select * @param string $tableAlias * @param array $filterFields * @return App_Faq_Search_Obj */ protected function _unionFilter(Zend_Db_Select $select, $tableAlias, array $filterFields) { if (!$this->hasFilter()) { return $this; } if (!empty($filterFields) && array_key_exists('query', $this->_filter)) { Qs_Db_Filter::where($select, [$tableAlias => $filterFields], $this->_filter['query']); } $filter = $this->_getCleanedFilter($this->_filter); Qs_Db::filter($select, $filter, $this->_tableShortAlias ?: $this->_tableAlias); return $this; } /** * @return Zend_Db_Select */ protected function _getCategoryList4Select() { $select = $this->_db->select(); $columns = [ 'type' => new Zend_Db_Expr('"' . static::TYPE_CATEGORY . '"'), 'categoryId' => 'id', 'id', 'title', 'alias', 'content' => new Zend_Db_Expr('""'), ]; $select->from($this->_getPair($this->_tableAliasCategory), $columns); $select->where('`' . $this->_tableAliasCategory . '`.`enabled` = "y"'); $select->where('EXISTS (' . $this->_getHasQuestionsSelect() . ')'); $this->_unionFilter($select, $this->_tableAliasCategory, $this->_categoryFilterFields); return $select; } /** * @return Zend_Db_Select; */ protected function _getHasQuestionsSelect() { $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias), new Zend_Db_Expr('1')); $select->where('`' . $this->_tableAlias . '`.`categoryId` = `' . $this->_tableAliasCategory . '`.`id`'); $select->where('`' . $this->_tableAlias . '`.`enabled` = "y"'); $select->limit(1); return $select; } /** * @return Zend_Db_Select */ protected function _getQuestionList4Select() { $select = $this->_db->select(); $columns = [ 'type' => new Zend_Db_Expr('"' . static::TYPE_QUESTION . '"'), 'categoryId', 'id', 'title' => 'question', 'alias' => "CONCAT({$this->_tableAliasCategory}.`alias`, '#n-', {$this->_tableAlias}.id)", 'content' => 'answer', ]; $select->from($this->_getPair($this->_tableAlias), $columns); $select->join( $this->_getPair($this->_tableAliasCategory), '`' . $this->_tableAlias . '`.`categoryId` = `' . $this->_tableAliasCategory . '`.`id`', [] ); $select->where('`' . $this->_tableAlias . '`.`enabled` = "y"'); $select->where('`' . $this->_tableAliasCategory . '`.`enabled` = "y"'); $this->_unionFilter($select, $this->_tableAlias, $this->_filterFields); return $select; } /** * @return bool|string * @throws Qs_Exception */ protected function _getSitemapUlr() { if (null === $this->_sitemapUrl) { $this->_sitemapUrl = Qs_SiteMap::findFirst(null, ['type' => 'Faq_'], null, 'url'); if (!$this->_sitemapUrl) { throw new Qs_Exception('Can not determine FAQ URL'); } } return $this->_sitemapUrl; } /** * @param array $list items list * @param array $providerOptions provider options from site/App/Search/config.php * @return App_Faq_Search_Obj */ protected function _prepareSearchList(array &$list, array $providerOptions) { $this->_prepareList($list); if (!empty($list)) { foreach ($list as &$row) { $row['title'] = Qs_Text_Mark::markSearchWords($row['title'], $this->getFilter('query')); $row['searchUrl'] = $this->_getSitemapUlr() . '/' . $row['alias']; if (static::TYPE_QUESTION == $row['type']) { $row['content'] = Qs_Text_Mark::prepareSearchText($row, ['content']); $row['content'] = Qs_Text_Mark::markSearchWords($row['content'], $this->getFilter('query')); } } } return $this; } }