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_CatalogIndex
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: * Resource Model CatalogIndex Aggregation
30: *
31: * @category Mage
32: * @package Mage_CatalogIndex
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_CatalogIndex_Model_Resource_Aggregation extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Table name of catalogindex/aggregation_tag table
39: *
40: * @var string
41: */
42: protected $_tagTable;
43:
44: /**
45: * Table name of catalogindex/aggregation_to_tag table
46: *
47: * @var string
48: */
49: protected $_toTagTable;
50:
51: /**
52: * Initialize resource tables
53: *
54: */
55: protected function _construct()
56: {
57: $this->_init('catalogindex/aggregation', 'aggregation_id');
58: $this->_tagTable = $this->getTable('catalogindex/aggregation_tag');
59: $this->_toTagTable = $this->getTable('catalogindex/aggregation_to_tag');
60: }
61:
62: /**
63: * Get aggregated cache data by data key and store
64: *
65: * @param string $key
66: * @param int $storeId
67: * @return array
68: */
69: public function getCacheData($key, $storeId)
70: {
71: $select = $this->_getReadAdapter()->select()
72: ->from(array('a'=>$this->getMainTable()), 'data')
73: ->where('a.store_id=?', $storeId)
74: ->where('a.key=?', $key);
75: $data = $this->_getReadAdapter()->fetchOne($select);
76: if ($data) {
77: $data = unserialize($data);
78: } else {
79: $data = array();
80: }
81: return $data;
82: }
83:
84: /**
85: * Save data to aggreagation table with tags relations
86: *
87: * @param array $data
88: * @param string $key
89: * @param array|string $tags
90: * @param int $storeId
91: * @return Mage_CatalogIndex_Model_Resource_Aggregation
92: */
93: public function saveCacheData($data, $key, $tags, $storeId)
94: {
95: $data = serialize($data);
96: $tags = $this->_getTagIds($tags);
97:
98: /*
99: $select = $this->_getWriteAdapter()->select()
100: ->from(array('a'=>$this->getMainTable()), $this->getIdFieldName())
101: ->where('a.store_id=?', $storeId)
102: ->where('a.key=?', $key);
103:
104: $id = $this->_getWriteAdapter()->fetchOne($select);
105: if ($id) {
106: $this->_getWriteAdapter()->update(
107: $this->getMainTable(),
108: array('data'=>$data),
109: $this->_getWriteAdapter()->quoteInto($this->getIdFieldName().'=?', $id)
110: );
111: } else {
112: $this->_getWriteAdapter()->insert($this->getMainTable(), array(
113: 'store_id' => $storeId,
114: 'created_at'=> $this->formatDate(time()),
115: 'key' => $key,
116: 'data' => $data
117: ));
118: $id = $this->_getWriteAdapter()->lastInsertId();
119: }
120: */
121:
122: $this->_getWriteAdapter()->insertOnDuplicate($this->getMainTable(), array(
123: 'store_id' => $storeId,
124: 'created_at'=> $this->formatDate(time()),
125: 'key' => $key,
126: 'data' => $data
127: ), array('created_at', 'data'));
128:
129: $id = $this->_getWriteAdapter()->lastInsertId($this->getMainTable());
130:
131: $this->_saveTagRelations($id, $tags);
132: return $this;
133: }
134:
135: /**
136: * Clear data in cache
137: *
138: * @param array $tags
139: * @param int|null|string $storeId
140: * @return Mage_CatalogIndex_Model_Resource_Aggregation
141: */
142: public function clearCacheData($tags, $storeId)
143: {
144: $conditions = array();
145: if (!$write = $this->_getWriteAdapter()) {
146: return $this;
147: }
148: if (!empty($tags)) {
149: $tagIds = $this->_getTagIds($tags);
150: $select = $write->select()
151: ->from($this->_toTagTable, 'aggregation_id')
152: ->where('tag_id IN (?)', $tagIds);
153: $conditions[] = $write->quoteInto('aggregation_id IN ?', $select);
154: }
155:
156: if ($storeId !== null) {
157: $conditions[] = $write->quoteInto('store_id=?', $storeId);
158: }
159:
160: $write->delete($this->getMainTable(), implode(' AND ', $conditions));
161: return $this;
162: }
163:
164: /**
165: * Save related tags for aggreagation data
166: *
167: * @param int $aggregationId
168: * @param array $tags
169: * @return Mage_CatalogIndex_Model_Resource_Aggregation
170: */
171: protected function _saveTagRelations($aggregationId, $tags)
172: {
173: $query = "REPLACE INTO `{$this->_toTagTable}` (aggregation_id, tag_id) VALUES ";
174: $data = array();
175: foreach ($tags as $tagId) {
176: $data[] = $aggregationId.','.$tagId;
177: }
178: $query.= '(' . implode('),(', $data) . ')';
179: $this->_getWriteAdapter()->query($query);
180: return $this;
181: }
182:
183: /**
184: * Get identifiers of tags
185: * if some tags not exist they will be added
186: *
187: * @param array $tags
188: * @return array
189: */
190: protected function _getTagIds($tags)
191: {
192: if (!is_array($tags)) {
193: $tags = array($tags);
194: }
195:
196: $select = $this->_getReadAdapter()->select()
197: ->from(array('tags'=>$this->_tagTable), array('tag_code', 'tag_id'))
198: ->where('tags.tag_code IN (?)', $tags);
199:
200: $tagIds = $this->_getReadAdapter()->fetchPairs($select);
201:
202: /**
203: * Detect new tags
204: */
205: $newTags = array_diff($tags, array_keys($tagIds));
206: if (!empty($newTags)) {
207: $this->_addTags($newTags);
208: $select->reset(Zend_Db_Select::WHERE)
209: ->where('tags.tag_code IN (?)', $newTags);
210: $newTags = $this->_getReadAdapter()->fetchPairs($select);
211: $tagIds = array_merge($tagIds, $newTags);
212: }
213: return $tagIds;
214: }
215:
216: /**
217: * Insert tags to tag table
218: *
219: * @param string | array $tags
220: * @return Mage_CatalogIndex_Model_Resource_Aggregation
221: */
222: protected function _addTags($tags)
223: {
224: if (is_array($tags)) {
225: $tags = array_unique($tags);
226: foreach ($tags as $index => $tag) {
227: $tags[$index] = $this->_getWriteAdapter()->quote($tag);
228: }
229: $query = "INSERT INTO `{$this->_tagTable}` (tag_code) VALUES (".implode('),(', $tags).")";
230: $this->_getWriteAdapter()->query($query);
231: }
232: else {
233: $this->_getWriteAdapter()->insert($this->_tagTable, array(
234: 'tag_code' => $tags
235: ));
236: }
237: return $this;
238: }
239:
240: /**
241: * ProductCategoryPaths getter
242: *
243: * @param array $productIds
244: * @return array
245: */
246: public function getProductCategoryPaths($productIds)
247: {
248: $select = $this->_getReadAdapter()->select()
249: ->from(array('cat'=>$this->getTable('catalog/category')), 'path')
250: ->joinInner(
251: array('cat_prod'=>$this->getTable('catalog/category_product')),
252: $this->_getReadAdapter()->quoteInto(
253: 'cat.entity_id=cat_prod.category_id AND cat_prod.product_id IN (?)',
254: $productIds
255: ),
256: array()
257: );
258: return $this->_getReadAdapter()->fetchCol($select);
259: }
260: }
261: