/** * Process redirect (R) and permanent redirect (RP) * * @return Mage_Core_Model_Url_Rewrite_Request */ protected function _processRedirectOptions() { $isPermanentRedirectOption = $this->_rewrite->hasOption('RP'); $external = substr($this->_rewrite->getTargetPath(), 0, 6); if ($external === 'http:/' || $external === 'https:') { $destinationStoreCode = $this->_app->getStore($this->_rewrite->getStoreId())->getCode(); $this->_setStoreCodeCookie($destinationStoreCode); $this->_sendRedirectHeaders($this->_rewrite->getTargetPath(), $isPermanentRedirectOption); } $targetUrl = $this->_request->getBaseUrl() . '/' . $this->_rewrite->getTargetPath(); $storeCode = $this->_app->getStore()->getCode(); if (Mage::getStoreConfig('web/url/use_store') && !empty($storeCode)) { $targetUrl = $this->_request->getBaseUrl() . '/' . $storeCode . '/' . $this->_rewrite->getTargetPath(); } if ($this->_rewrite->hasOption('R') || $isPermanentRedirectOption) { $this->_sendRedirectHeaders($targetUrl, $isPermanentRedirectOption); } $queryString = $this->_getQueryString(); if ($queryString) { $targetUrl .= '?' . $queryString; } $this->_request->setRequestUri($targetUrl); $this->_request->setPathInfo($this->_rewrite->getTargetPath()); return $this; }
public function loadByRequestPath(Mage_Core_Model_Url_Rewrite $object, $path) { if (!is_array($path)) { $path = array($path); } $pathBind = array(); foreach ($path as $key => $url) { $pathBind['path' . $key] = urldecode($url); } // Form select $adapter = $this->_getReadAdapter(); $select = $adapter->select('*')->from($this->getMainTable())->where('request_path IN (:' . implode(', :', array_flip($pathBind)) . ')')->where('store_id IN(?)', array(Mage_Core_Model_App::ADMIN_STORE_ID, (int) $object->getStoreId())); $items = $adapter->fetchAll($select, $pathBind); // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store $mapPenalty = array_flip(array_values($path)); // we got mapping array(path => index), lower index - better $currentPenalty = null; $foundItem = null; foreach ($items as $item) { $penalty = isset($mapPenalty[$item['request_path']]) ? $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1) : 0; if (!$foundItem || $currentPenalty > $penalty) { $foundItem = $item; $currentPenalty = $penalty; if (!$currentPenalty) { break; // Found best matching item with zero penalty, no reason to continue } } } // Set data and finish loading if ($foundItem) { $object->setData($foundItem); } // Finish $this->unserializeFields($object); $this->_afterLoad($object); return $this; }
public function testGetStoreId() { $this->_model->setStoreId(10); $this->assertEquals(10, $this->_model->getStoreId()); }
/** * Load rewrite information for request * If $path is array - we must load all possible records and choose one matching earlier record in array * * @param Mage_Core_Model_Url_Rewrite $object * @param array|string $path * @return Mage_Core_Model_Mysql4_Url_Rewrite */ public function loadByRequestPath(Mage_Core_Model_Url_Rewrite $object, $path) { if (!is_array($path)) { $path = array($path); } // Form select $read = $this->_getReadAdapter(); $select = $read->select()->from($this->getMainTable())->where($this->getMainTable() . '.request_path IN (?)', $path)->where('store_id IN(?)', array(0, $object->getStoreId())); $items = $read->fetchAll($select); // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store $mapPenalty = array_flip(array_values($path)); // we got mapping array(path => index), lower index - better $currentPenalty = null; $foundItem = null; foreach ($items as $item) { $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1); if (!$foundItem || $currentPenalty > $penalty) { $foundItem = $item; $currentPenalty = $penalty; if (!$currentPenalty) { break; // Found best matching item with zero penalty, no reason to continue } } } // Set data and finish loading if ($foundItem) { $object->setData($foundItem); } // Finish $this->unserializeFields($object); $this->_afterLoad($object); return $this; }
/** * Prevent loading disallowed urlrewrites * * @param Mage_Core_Model_Url_Rewrite $model */ public function coreUrlRewriteLoadAfter($model) { if (!$model->getId()) { return; } if (!$this->_role->hasStoreAccess($model->getStoreId())) { $this->_throwLoad(); } }
public function saveRewrite(Mage_Core_Model_Url_Rewrite $rewrite) { if (!$rewrite->getId()) { $old = Mage::getModel('core/url_rewrite')->setStoreId($rewrite->getStoreId())->loadByIdPath($rewrite->getIdPath()); if (!$old) { $old->loadByRequestPath($rewrite->getRequestPath()); } if ($old) { $rewrite->setId($old->getId()); } } $rewrite->save(); $this->_rewrites[$rewrite->getStoreId()][$rewrite->getIdPath()] = $rewrite; $this->_paths[$rewrite->getStoreId()][$rewrite->getRequestPath()] = $rewrite->getIdPath(); return $this; }