1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Core
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Url rewrite resource model class
30: *
31: * @category Mage
32: * @package Mage_Core
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Core_Model_Resource_Url_Rewrite extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Tag table
39: *
40: * @var string
41: */
42: protected $_tagTable;
43:
44: /**
45: * Define main table
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('core/url_rewrite', 'url_rewrite_id');
51: $this->_tagTable = $this->getTable('url_rewrite_tag');
52: }
53:
54: /**
55: * Initialize array fields
56: *
57: * @return Mage_Core_Model_Resource_Url_Rewrite
58: */
59: protected function _initUniqueFields()
60: {
61: $this->_uniqueFields = array(
62: array(
63: 'field' => array('id_path','store_id','is_system'),
64: 'title' => Mage::helper('core')->__('ID Path for Specified Store')
65: ),
66: array(
67: 'field' => array('request_path','store_id'),
68: 'title' => Mage::helper('core')->__('Request Path for Specified Store'),
69: )
70: );
71: return $this;
72: }
73:
74: /**
75: * Retrieve select object for load object data
76: *
77: * @param string $field
78: * @param mixed $value
79: * @param Mage_Core_Model_Url_Rewrite $object
80: * @return Zend_Db_Select
81: */
82: protected function _getLoadSelect($field, $value, $object)
83: {
84: /** @var $select Varien_Db_Select */
85: $select = parent::_getLoadSelect($field, $value, $object);
86:
87: if (!is_null($object->getStoreId())) {
88: $select->where('store_id IN(?)', array(Mage_Core_Model_App::ADMIN_STORE_ID, $object->getStoreId()));
89: $select->order('store_id ' . Varien_Db_Select::SQL_DESC);
90: $select->limit(1);
91: }
92:
93: return $select;
94: }
95:
96: /**
97: * Retrieve request_path using id_path and current store's id.
98: *
99: * @param string $idPath
100: * @param int|Mage_Core_Model_Store $store
101: * @return string|false
102: */
103: public function getRequestPathByIdPath($idPath, $store)
104: {
105: if ($store instanceof Mage_Core_Model_Store) {
106: $storeId = (int)$store->getId();
107: } else {
108: $storeId = (int)$store;
109: }
110:
111: $select = $this->_getReadAdapter()->select();
112: /** @var $select Varien_Db_Select */
113: $select->from(array('main_table' => $this->getMainTable()), 'request_path')
114: ->where('main_table.store_id = :store_id')
115: ->where('main_table.id_path = :id_path')
116: ->limit(1);
117:
118: $bind = array(
119: 'store_id' => $storeId,
120: 'id_path' => $idPath
121: );
122:
123: return $this->_getReadAdapter()->fetchOne($select, $bind);
124: }
125:
126: /**
127: * Load rewrite information for request
128: * If $path is array - we must load all possible records and choose one matching earlier record in array
129: *
130: * @param Mage_Core_Model_Url_Rewrite $object
131: * @param array|string $path
132: * @return Mage_Core_Model_Resource_Url_Rewrite
133: */
134: public function loadByRequestPath(Mage_Core_Model_Url_Rewrite $object, $path)
135: {
136: if (!is_array($path)) {
137: $path = array($path);
138: }
139:
140: $pathBind = array();
141: foreach ($path as $key => $url) {
142: $pathBind['path' . $key] = $url;
143: }
144: // Form select
145: $adapter = $this->_getReadAdapter();
146: $select = $adapter->select()
147: ->from($this->getMainTable())
148: ->where('request_path IN (:' . implode(', :', array_flip($pathBind)) . ')')
149: ->where('store_id IN(?)', array(Mage_Core_Model_App::ADMIN_STORE_ID, (int)$object->getStoreId()));
150:
151: $items = $adapter->fetchAll($select, $pathBind);
152:
153: // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store
154: $mapPenalty = array_flip(array_values($path)); // we got mapping array(path => index), lower index - better
155: $currentPenalty = null;
156: $foundItem = null;
157: foreach ($items as $item) {
158: if (!array_key_exists($item['request_path'], $mapPenalty)) {
159: continue;
160: }
161: $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1);
162: if (!$foundItem || $currentPenalty > $penalty) {
163: $foundItem = $item;
164: $currentPenalty = $penalty;
165: if (!$currentPenalty) {
166: break; // Found best matching item with zero penalty, no reason to continue
167: }
168: }
169: }
170:
171: // Set data and finish loading
172: if ($foundItem) {
173: $object->setData($foundItem);
174: }
175:
176: // Finish
177: $this->unserializeFields($object);
178: $this->_afterLoad($object);
179:
180: return $this;
181: }
182: }
183: