hasFilter()) { return $this; } if ($filterFields && array_key_exists('query', $this->_filter)) { Qs_Db_Filter::where($select, array($tableAlias => $filterFields), $this->_filter['query']); } $filter = $this->_getCleanedFilter($this->_filter); Qs_Db::filter($select, $filter, $tableAlias); return $this; } /** * @return \Zend_Db_Select */ protected function _getGalleryList4Select() { $select = $this->_db->select(); $columns = array( 'type' => new Zend_Db_Expr('"' . static::TYPE_ALBUM . '"'), 'galleryId' => 'id', 'id', 'image' => new Zend_Db_Expr('(' . $this->_getAlbumImageColumn() . ')'), 'title', 'description' ); $select->from($this->_getPair(), $columns); $select->where('`g`.`enabled` = "y"'); $select->where('EXISTS (' . $this->_getAlbumVisibleImagesSelect() . ')'); $this->_unionFilter($select, 'g', $this->_albumFilterFields); return $select; } /** * @return string */ protected function _getAlbumImageColumn() { $column = 'IFNULL(`g`.`image`, (' . $this->_getAlbumImageSelect() . '))'; return $column; } protected function _getAlbumVisibleImagesSelect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'gi'), new Zend_Db_Expr('1')); $select->where('`gi`.`galleryId` = `g`.`id`'); $select->where('`gi`.`enabled` = "y"'); $select->limit(1); return $select; } /** * @return \Zend_Db_Select */ protected function _getAlbumImageSelect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'gi'), 'image'); $select->where('`gi`.`galleryId` = `g`.`id`'); $select->where('`gi`.`enabled` = "y"'); $select->order('gi.sorter'); $select->limit('1'); return $select; } /** * @return \Zend_Db_Select */ protected function _getGalleryImageList4Select() { $select = $this->_db->select(); $columns = array( 'type' => new Zend_Db_Expr('"' . static::TYPE_IMAGE . '"'), 'galleryId', 'id', 'image', 'title', 'description' ); $select->from($this->_getPair('GalleryImage', 'gi'), $columns); $select->join( $this->_getPair('Gallery', 'g'), '`g`.`id` = `gi`.`galleryId`', array() ); $select->where('`gi`.`enabled` = "y"'); $select->where('`g`.`enabled` = "y"'); $this->_unionFilter($select, 'gi', $this->_filterFields); return $select; } /** * @return \Zend_Db_Select */ public function getListSelect() { if (null === $this->_select) { $this->_select = $this->_db->select(); $this->_select->union(array( $this->_getGalleryList4Select(), $this->_getGalleryImageList4Select() )); $this->_applySelectOptions($this->_select, $this->_selectOptions); } return $this->_select; } /** * @param int $imageId * @return array */ protected function _getAlbumByImageId($imageId) { $select = $this->_db->select(); $select->from($this->_getPair('Gallery', 'g'), array('id', 'title')); $select->join( $this->_getPair('GalleryImage', 'gi'), '`gi`.`galleryId` = `g`.`id`', array() ); $select->where('`gi`.`id` = ?', (string) $imageId, Qs_Db::INT_TYPE); return $this->_db->fetchRow($select); } /** * @return bool|mixed|string * @throws \Qs_Exception */ protected function _getSitemapUlr() { if (null === $this->_sitemapUrl) { $this->_sitemapUrl = Qs_SiteMap::findFirst(null, array('type' => 'Gallery\\'), null, 'url'); if (!$this->_sitemapUrl) { throw new Qs_Exception('Can not determine Gallery URL'); } } return $this->_sitemapUrl; } /** * @param array $data * @return array */ protected function _getPreparedImageRow(array $data) { $data['title'] = Qs_Text_Mark::markSearchWords($data['title'], $this->getFilter('query')); $content = Qs_Text_Mark::prepareSearchText($data, array('description')); $data['content'] = Qs_Text_Mark::markSearchWords($content, $this->getFilter('query')); $data['searchUrl'] = $this->_getSitemapUlr() . '/' . $data['galleryId'] . '#image-' . $data['id']; return $data; } /** * @param array $list items list * @param array $providerOptions provider options from site/App/Search/config.php * @return \Qs_Db_Obj */ protected function _prepareSearchList(array &$list, array $providerOptions) { $this->_prepareList($list); if ($list) { /** @var array $albumIndexes format galleryId => listIndex */ $albumIndexes = array(); foreach ($list as $index => &$row) { if (static::TYPE_ALBUM == $row['type']) { $albumIndexes[$row['galleryId']] = $index; $row['title'] = Qs_Text_Mark::markSearchWords($row['title'], $this->getFilter('query')); $content = Qs_Text_Mark::prepareSearchText($row, array('description')); $row['content'] = Qs_Text_Mark::markSearchWords($content, $this->getFilter('query')); $row['searchUrl'] = $this->_getSitemapUlr() . '/' . $row['id']; } } unset($row); foreach ($list as $index => $row) { if (static::TYPE_IMAGE == $row['type']) { if (array_key_exists($row['galleryId'], $albumIndexes)) { $listIndex = $albumIndexes[$row['galleryId']]; $list[$listIndex]['imagesList'][] = $this->_getPreparedImageRow($row); unset($list[$index]); } else { $album = $this->_getAlbumByImageId($row['id']); if ($album) { $albumIndexes[$album['id']] = $index; $list[$index] = $album; $list[$index]['imagesList'][] = $this->_getPreparedImageRow($row); } } } } } return $this; } }