_db->select() ->from($this->_getPair()) ->where('source = ?', $source) ->limit(1); return $this->_db->fetchRow($select); } /** * @param string $source source page alias * @param string $destination destination page alias * @param int $status redirect status code * @return App_Redirection_Obj */ public function addRedirect($source, $destination, $status = 301) { $data = array('source' => $source, 'destination' => $destination, 'status' => $status); $table = $this->_getTable(); $table->insert($data); unset($data['source']); $table->update($data, array('destination = ?' => $source)); return $this; } /** * @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( array('pg' => $this->_getTableName($draftPrefix . 'Page')), array('alias') ); $select->joinLeft( array('pr' => $this->_getTableName('Page')), "`pg`.`idParent` = `pr`.`id`", array('parentAlias' => 'alias') ); $select->where('pg.id = ?', $id, Zend_Db::INT_TYPE); return $this->_db->fetchRow($select); } /** * @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; } }