_db->select() ->from($this->_getPair()) ->where('source = ?', $source) ->limit(1); return $this->_db->fetchRow($select); } /** * @param array|string $where * @return App_Redirection_Obj */ protected function _removeRedirect($where) { $this->_getTable()->delete($where); return $this; } /** * @param int $idPage deleting page id * @return App_Redirection_Obj */ public function removePageRedirection($idPage) { $paths = $this->_getPageChildAliases($idPage); foreach ($paths as $path) { $this->_removeRedirect(array('destination = ?' => $path)); } return $this; } /** * @param int $idPage Redirection page id * @return App_Redirection_Obj */ public function updatePageRedirection($idPage) { $sources = $this->_getPageChildAliases($idPage); $aliases = $this->_getPageAliases($idPage, true); $destinationAlias = (empty($aliases['parentAlias'])) ? $aliases['alias'] : ($aliases['parentAlias'] . '/' . $aliases['alias']); if (empty($sources)) { $this->_removeRedirect(array('source = ?' => $destinationAlias)); } else { $sourceAliasLength = strlen($sources[0]); foreach ($sources as $source) { $destination = substr_replace($source, $destinationAlias, 0, $sourceAliasLength); if ($source != $destination) { $this->_removeRedirect(array('source = ?' => $source)); $this->_removeRedirect(array('source = ?' => $destination)); $this->addRedirect($source, $destination); } } } return $this; } protected function _getPageAliases($id, $draft = false) { $draftPrefix = ($draft) ? 'Draft' : ''; $select = $this->_db->select(); $select->from($this->_getPair($draftPrefix . 'Page', 'pg'), array('alias', 'idParent')); $select->where('pg.id = ?', $id, Zend_Db::INT_TYPE); $parentAlias = ''; $row = $this->_db->fetchRow($select); if ($row['idParent']) { $parentAlias = (string) Qs_SiteMap::findFirst(array('id' => $row['idParent']), null, null, 'fullAlias'); } return array('alias' => $row['alias'], 'parentAlias' => $parentAlias); } /** * @param int $idPage * @return array */ protected function _getPageChildAliases($idPage) { $aliases = array(); $siteMap = App_Cms_Obj::getInstance()->getSiteMap(); $page = $this->_findPage($siteMap, $idPage); if (!empty($page)) { $parentAlias = substr($page['fullAlias'], 0, strrpos($page['fullAlias'], '/')); $this->_processPageAliases($page, $aliases, $parentAlias); } return $aliases; } protected function _findPage($sitemap, $idPage) { foreach ($sitemap as &$page) { if (is_array($page) && array_key_exists('id', $page)) { if ($idPage == $page['id']) { return $page; } elseif (isset($page['sub']) && is_array($page = $this->_findPage($page['sub'], $idPage))) { return $page; } } } return false; } protected function _processPageAliases($page, &$data, $alias = '') { $alias .= ('' == $alias) ? $page['alias'] : ('/' . $page['alias']); $data[] = $alias; if (!empty($page['sub'])) { foreach ($page['sub'] as $sub) { $this->_processPageAliases($sub, $data, $alias); } } return $this; } }