_filterBlockByPage($select); return parent::_filter($select); } protected function _filterBlockByPage(Zend_Db_Select $select) { if (!empty($this->_filter['idPage'])) { $block2Page = $this->_tableAlias . '2Page'; $where = [ "`{$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), [] ); } 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'] = []; } return $this; } protected function _prepareList(&$list) { parent::_prepareList($list); if ($list && is_array($list)) { $sitePages = self::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'] = []; 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 = $this->_getTable('Page')->fetchColumn('id', ['showSideBlock' => 'y']); } else { $auto = false; $pageIds = $this->_data['idPage']; } $deleteWhere = [ '`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, ['auto' => ($auto ? 'y' : 'n')]); return $this; } /** * 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, ['idPage', 'idSideBlock', 'sorter'], $pageIds); $select->where("`{$this->_tableAlias}`.`id` = ?", $this->_primaryKey); if (($list = $this->_db->fetchAssoc($select))) { $links = []; foreach ($pageIds as $pageId) { $links[] = [ '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 = []; 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( ['sorter' => (int) $sorter], ['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'), ['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), ['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, ['sorter' => 0]); $this->_insertLinks($this->_tableAlias . '2Page', $links, ['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; } }