_filterBlockByPage($select); return parent::_filter($select); } protected function _filterBlockByPage(Zend_Db_Select $select) { if (!empty($this->_filter['idPage'])) { $block2Page = $this->_tableAlias . '2Page'; $where = array( "`{$block2Page}`.`idSideBlock` = `{$this->_tableAlias}`.`id`", $this->_db->quoteInto("`{$block2Page}`.`idPage` = ?", $this->_filter['idPage'], Qs_Db::INT_TYPE), $this->_db->quoteInto( "`{$block2Page}`.`auto` = IF(`{$this->_tableAlias}`.`pageType` = ?, 'y', 'n')", App_SideBlock_Obj::PAGE_ALL ), ); $select->join( $this->_getPair($block2Page), Qs_Db::getWhereSql($where), array() ); } return $this; } protected function _addDependenciesFromDb(array &$data) { $select = $this->_db->select(); $select->from($this->_getTableName($this->_tableAlias . '2Page'), 'idPage') ->where('`idSideBlock` = ?', $data['id'], Zend_Db::INT_TYPE) ->where('`auto` = "n"'); $data['idPage'] = $this->_db->fetchCol($select); if (!is_array($data['idPage'])) { $data['idPage'] = array(); } return $this; } protected function _prepareList(&$list) { parent::_prepareList($list); if ($list && is_array($list)) { $sitePages = App_Cms_Obj::getInstance()->getPages4Select(); $blocks2Pages = $this->_getBlocks2Pages(); foreach ($list as &$block) { if (App_SideBlock_Obj::TYPE_HTML !== $block['blockType']) { $block['content'] = Qs_Array::get(App_SideBlock_View::$blockTypeTitles, $block['blockType'], ''); } $block['pageList'] = array(); foreach ($blocks2Pages as $blockPage) { if ($blockPage['idSideBlock'] == $block['id'] && isset($sitePages[$blockPage['idPage']])) { $block['pageList'][] = str_replace(' ', '', $sitePages[$blockPage['idPage']]); } } sort($block['pageList']); } } return $this; } protected function _insertDependency() { $this->_updateDependency(); return $this; } protected function _updateDependency() { if (App_SideBlock_Obj::PAGE_ALL === $this->_data['pageType']) { $auto = true; $pageIds = array_keys(App_Cms_Obj::getInstance()->getPages4Select()); } else { $auto = false; $pageIds = $this->_data['idPage']; } $deleteWhere = array( '`idSideBlock` = ?' => $this->_primaryKey, '`auto` = "' . ($auto ? 'y' : 'n') . '"' ); $links = $this->_prepareBlock2PageLinks($pageIds); // preserve links sorter $this->_getTable($this->_tableAlias . '2Page')->delete($deleteWhere); $this->_insertLinks($this->_tableAlias . '2Page', $links, array('auto' => ($auto ? 'y' : 'n'))); return $this; } protected function _deleteDependency() { $this->_deleteIds($this->_tableAlias . '2Page', 'idSideBlock'); return parent::_deleteDependency(); } /** * IMPORTANT: Function must be called before deleting links from SideBlock2Page table * @param array $pageIds * @return array|null */ protected function _prepareBlock2PageLinks($pageIds) { $select = $this->_getBasicListSelect(null, array('idPage', 'idSideBlock', 'sorter'), $pageIds); $select->where("`{$this->_tableAlias}`.`id` = ?", $this->_primaryKey); if (($list = $this->_db->fetchAssoc($select))) { $links = array(); foreach ($pageIds as $pageId) { $links[] = array( 'idSideBlock' => $this->_primaryKey, 'idPage' => $pageId, 'sorter' => (isset($list[$pageId]) ? $list[$pageId]['sorter'] : 0), ); } } else { $links = $this->_ids2Links('idPage', $pageIds, 'idSideBlock', (int) $this->_primaryKey); } return $links; } protected function _getBlocks2Pages() { $select = $this->_db->select(); $select->from($this->_getTableName($this->_tableAlias . '2Page'), '*'); $select->where('`auto` = "n"'); return $this->_db->fetchAll($select); } public function getReorderOptions($keyColumn = null, $titleColumn = null) { $keyColumn = (null === $keyColumn) ? $this->_reorderKeyColumn : $keyColumn; $titleColumn = (null === $titleColumn) ? $this->_reorderTitleColumn : $titleColumn; $select = $this->getListSelect(); $this->_where4Sorter($select); $select->order($this->_tableAlias . '2Page.sorter'); $list = $this->_db->fetchAll($select); $options = array(); foreach ($list as $row) { $options[$row[$keyColumn]] = $row[$titleColumn]; } return $options; } public function updateOrder(array $order) { if (($idPage = $this->getFilter('idPage'))) { $table = $this->_getTable($this->_tableAlias . '2Page'); foreach ($order as $sorter => $blockId) { $table->update( array('sorter' => (int) $sorter), array('idSideBlock = ?' => (int) $blockId, 'idPage = ?' => $idPage) ); } } return $this; } public static function onPagePostPublish(App_Cms_Event $event) { $obj = new self(); $obj->_onPagePostPublish($event); return; } protected function _onPagePostPublish(App_Cms_Event $event) { if ('y' === Qs_Array::get($event->pageData, 'system')) { return $this; } $_pageId = (int) $event->pageId; // get all-pages blocks without current page $subSelect = $this->_db->select(); $subSelect->from($this->_getPair($this->_tableAlias . '2Page'), array('idSideBlock')); $subSelect->where("`{$this->_tableAlias}2Page`.`idPage` = ?", $_pageId, Qs_Db::INT_TYPE); $subSelect->where("`{$this->_tableAlias}2Page`.`idSideBlock` = `{$this->_tableAlias}`.`id`"); $subSelect->limit(1); $select = $this->_db->select(); $select->from($this->_getPair($this->_tableAlias), array('id')); $select->where("`{$this->_tableAlias}`.`pageType` = ?", App_SideBlock_Obj::PAGE_ALL); $select->where('NOT EXISTS (' . $subSelect . ')'); $blocksWithoutPage = $this->_db->fetchCol($select); // add current to side blocks $links = $this->_ids2Links('idSideBlock', $blocksWithoutPage, 'idPage', $_pageId, array('sorter' => 0)); $this->_insertLinks($this->_tableAlias . '2Page', $links, array('auto' => 'y')); return $this; } public static function onPagePostDelete(App_Cms_Event $event) { $db = Qs_Db::getInstance(); $_tableName = $db->quoteIdentifier(Qs_Db::getTableName('SideBlock2Page')); $_pageId = (int) $event->pageId; $sql = 'DELETE FROM ' . $_tableName . ' WHERE `idPage` = ' . $_pageId; $db->query($sql); return; } }