public function articleAction() { $this->_checkRegistered(); $this->_checkFrontendEnabled(); if (AW_Kbase_Helper_Data::isModuleOutputDisabled()) { $this->redirectReferer(); return; } try { if (!($articleId = $this->getRequest()->getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_ARTICLE_ID)) && !($articleUrlKey = ($key = AW_Kbase_Helper_Url::getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_ARTICLE)) ? $key : $this->getRequest()->getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_ARTICLE))) { Mage::throwException($this->__('No article specified')); } if (!$articleId) { $article = Mage::getModel('kbase/article')->loadByUrlKey($articleUrlKey); if (!$article->getData()) { Mage::throwException($this->__('Article "%s" does not exist', $articleUrlKey)); } } else { $article = Mage::getModel('kbase/article')->load($articleId); } $article->addCategoryInfo(); $article->addAuthorInfo(); if (($categoryId = $this->getRequest()->getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_CATEGORY_ID)) || ($categoryUrlKey = ($key = AW_Kbase_Helper_Url::getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_CATEGORY)) ? $key : $this->getRequest()->getParam(AW_Kbase_Helper_Url::URL_PARAM_NAME_CATEGORY))) { if (!$categoryId) { $category = Mage::getModel('kbase/category')->loadByUrlKey($categoryUrlKey); if (!($categoryId = $category->getId())) { if ($article->getData()) { return $this->_redirectUrl(Mage::helper('kbase/url')->getHomeUrl() . $article->getData('article_url_key') . Mage::helper('kbase/url')->getUrlKeySuffix()); } else { Mage::throwException($this->__('Category "%s" does not exist', $categoryUrlKey)); } } } else { $category = Mage::getModel('kbase/category')->load($categoryId); } } else { $categoryId = null; $category = null; } if (!$article->getArticleStatus()) { Mage::getSingleton('core/session')->addError($this->__('The article is temporarily disabled')); $this->redirectHome(); return; } $this->loadLayout()->getLayout()->getBlock('kbase_article')->setArticle($article)->setCategoryId($articleId)->setCategory($category)->setCategoryId($categoryId)->preparePage(); $this->renderLayout(); } catch (Exception $e) { // Mage::logException($e); Mage::getSingleton('core/session')->addError($e->getMessage()); $this->redirectHome(); return; } }
public static function matchUrl(Zend_Controller_Request_Http $request) { if (AW_Kbase_Helper_Data::isModuleOutputDisabled() || !AW_Kbase_Helper_Data::getFrontendEnabled()) { return false; } $urlKey = self::getModuleUrlKey(); $pathInfo = $request->getPathInfo(); $requestUri = $request->getRequestUri(); if ($request->getParam('___from_store')) { $_fromStore = Mage::app()->getSafeStore($request->getParam('___from_store')); if ($_fromStore->getData()) { $_oldUrlKey = self::getModuleUrlKeyFromConfig($_fromStore); if (strpos($pathInfo, '/' . $_oldUrlKey) === 0 && $_oldUrlKey != $urlKey) { $_newUrl = self::secureUrl(Mage::getBaseUrl() . $urlKey . '/'); $response = Mage::app()->getResponse(); $response->setRedirect($_newUrl); $response->sendHeaders(); $request->setDispatched(true); return true; } } } if (0 !== strpos($pathInfo, '/' . $urlKey . '/')) { // if path does not begin with /kbase/ return false; } $pathInfo = substr($pathInfo, strlen($urlKey) + 2); $request->setModuleName('kbase')->setControllerName('article'); if (!strlen($pathInfo)) { $request->setActionName('index'); return AW_Kbase_Helper_Data::getFrontendEnabled() && self::cacheRewrite($request->getPathInfo(), 'kbase/article/index'); } $urlKeySuffix = self::getUrlKeySuffix(); $isArticle = false; if (!$urlKeySuffix) { $_path = explode('/', $pathInfo); $_aUrl = false; if (count($_path) == 1) { $_aUrl = $_path[0]; } if (count($_path) == 2) { $_aUrl = $_path[1]; } if ($_aUrl) { $_aUrl = urldecode($_aUrl); $_aId = Mage::getModel('kbase/article')->getResource()->getIdByUrlKey($_aUrl); if (!$_aId) { $_aId = Mage::getModel('kbase/article')->getResource()->getIdByUrlKey($_aUrl, true); } if ($_aId) { $isArticle = true; } } } /** * Checking is customer comes from other store view * having various article suffix id in comparison * with current */ if ($request->getParam('___from_store') && !$isArticle) { if (!isset($_fromStore)) { $_fromStore = Mage::app()->getSafeStore($request->getParam('___from_store')); } if ($_fromStore->getData()) { $_oldUrlSuffix = self::getUrlKeySuffixFromConfig($_fromStore); if ($urlKeySuffix != $_oldUrlSuffix) { $_path = explode('/', $pathInfo); $_aUrl = false; if (count($_path) == 1) { $_aUrl = $_path[0]; } if (count($_path) == 2) { $_aUrl = $_path[1]; } if ($_aUrl) { $_articleUrlKey = substr($_aUrl, 0, strpos($_aUrl, $_oldUrlSuffix)); $_aId = Mage::getModel('kbase/article')->getResource()->getIdByUrlKey($_articleUrlKey); if (!$_aId) { $_aId = Mage::getModel('kbase/article')->getResource()->getIdByUrlKey($_articleUrlKey, true); } if ($_aId) { $_storeIds = Mage::getModel('kbase/article')->getResource()->getArticleStoreIds($_aId); if (in_array(Mage::app()->getStore()->getId(), $_storeIds)) { $_newUrl = self::secureUrl(Mage::getBaseUrl() . $urlKey . '/' . $_articleUrlKey . $urlKeySuffix); $response = Mage::app()->getResponse(); $response->setRedirect($_newUrl); $response->sendHeaders(); $request->setDispatched(true); return true; } } } } } } if ($urlKeySuffix && '/' != $urlKeySuffix && $urlKeySuffix == substr($pathInfo, -strlen($urlKeySuffix)) || $isArticle) { $pathInfo = substr($pathInfo, 0, strlen($pathInfo) - strlen($urlKeySuffix)); $path = explode('/', $pathInfo); if (count($path) == 1) { $request->setActionName('article'); self::$_queryParams[self::URL_PARAM_NAME_ARTICLE] = $path[0]; return self::cacheRewrite($request->getPathInfo(), 'kbase/article/article/' . self::URL_PARAM_NAME_ARTICLE . '/' . $path[0]); } elseif (count($path) == 2) { $request->setActionName('article'); self::$_queryParams[self::URL_PARAM_NAME_CATEGORY] = $path[0]; self::$_queryParams[self::URL_PARAM_NAME_ARTICLE] = $path[1]; return self::cacheRewrite($request->getPathInfo(), 'kbase/article/article/' . self::URL_PARAM_NAME_CATEGORY . '/' . $path[0] . '/' . self::URL_PARAM_NAME_ARTICLE . '/' . $path[1]); } return false; } // since further there will be listing pages only, we should check the condition here if (!Mage::getStoreConfig('kbase/general/frontent_enabled')) { return false; } // init parameters with optional ones self::$_queryParams = array(self::URL_PARAM_NAME_LIMIT => false, self::URL_PARAM_NAME_PAGE => false, self::URL_PARAM_NAME_SORT => false, self::URL_PARAM_NAME_SORT_DIR => false, self::URL_PARAM_NAME_QUERY => false); $pathInfo = substr($pathInfo, 0, strlen($pathInfo) - 1); $path = explode('/', $pathInfo); for ($i = count($path) - 1; $i > 0; $i -= 2) { $paramName = $path[$i - 1]; if (array_key_exists($paramName, self::$_queryParams)) { // for(; $path[$i] != $decoded = urldecode($path[$i]); $path[$i] = $decoded); // self::$_queryParams[$paramName] = $path[$i]; self::$_queryParams[$paramName] = urldecode($path[$i]); unset($path[$i]); unset($path[$i - 1]); } } foreach (self::$_queryParams as $paramName => $value) { if ($value = self::getQueryParam($requestUri, $paramName)) { self::$_queryParams[$paramName] = urldecode($value); $requestUri = self::unsetQueryParam($requestUri, $paramName); } } if (1 == count($path)) { if ('search' == $path[0]) { // if there were known parameters passed as query params if ($requestUri != $request->getRequestUri()) { foreach (self::$_queryParams as $name => $value) { if ($value) { self::$_queryParams[$name] = self::encodeSpecialChars($value); } } Mage::app()->getFrontController()->getResponse()->setRedirect(self::getUrl(self::URL_TYPE_SEARCH, self::$_queryParams))->sendResponse(); exit; } $request->setActionName('search'); return true; } $request->setActionName('category'); self::$_queryParams[self::URL_PARAM_NAME_CATEGORY] = $path[0]; return true; } elseif (2 == count($path) && 'tag' == $path[0] && $path[1]) { $request->setActionName('tag'); self::$_queryParams[self::URL_PARAM_NAME_TAG] = $path[1]; return true; } return false; }