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: * Index data aggregation model
29: *
30: * Allow cache some aggregated data with tag dependency
31: *
32: * @method Mage_CatalogIndex_Model_Resource_Aggregation _getResource()
33: * @method Mage_CatalogIndex_Model_Resource_Aggregation getResource()
34: * @method int getStoreId()
35: * @method Mage_CatalogIndex_Model_Aggregation setStoreId(int $value)
36: * @method string getCreatedAt()
37: * @method Mage_CatalogIndex_Model_Aggregation setCreatedAt(string $value)
38: * @method string getKey()
39: * @method Mage_CatalogIndex_Model_Aggregation setKey(string $value)
40: *
41: * @category Mage
42: * @package Mage_CatalogIndex
43: * @author Magento Core Team <core@magentocommerce.com>
44: */
45: class Mage_CatalogIndex_Model_Aggregation extends Mage_Core_Model_Abstract
46: {
47: const CACHE_FLAG_NAME = 'layered_navigation';
48:
49: /**
50: * Initialize resource model
51: */
52: protected function _construct()
53: {
54: $this->_init('catalogindex/aggregation');
55: }
56:
57: protected function _isEnabled()
58: {
59: return Mage::app()->useCache(self::CACHE_FLAG_NAME);
60: }
61:
62: /**
63: * Get aggregated data by data key and store
64: *
65: * @param string $key
66: * @param null|int|string|Mage_Core_Model_Store $store
67: * @return array|null
68: */
69: public function getCacheData($key, $store=null)
70: {
71: if (!$this->_isEnabled()) {
72: return null;
73: }
74:
75: $key = $this->_processKey($key);
76: $store = Mage::app()->getStore($store);
77: $data = $this->_getResource()->getCacheData($key, $store->getId());
78: if (empty($data)) {
79: return null;
80: }
81: return $data;
82: }
83:
84: /**
85: * Save aggregation data to cache
86: *
87: * @param string $key
88: * @param array $tags
89: * @param null|int|string|Mage_Core_Model_Store $store
90: * @return Mage_CatalogIndex_Model_Aggregation
91: */
92: public function saveCacheData($data, $key, $tags, $store=null)
93: {
94: if (!$this->_isEnabled()) {
95: return $this;
96: }
97:
98: $key = $this->_processKey($key);
99: $tags = $this->_processTags($tags);
100: $store = Mage::app()->getStore($store);
101:
102: $this->_getResource()->saveCacheData($data, $key, $tags, $store->getId());
103: return $this;
104: }
105:
106: /**
107: * Delete cached aggregation data
108: *
109: * @param array $tags
110: * @param int|null|string $store
111: * @return Mage_CatalogIndex_Model_Aggregation
112: */
113: public function clearCacheData($tags = array(), $store = null)
114: {
115: $tags = $this->_processTags($tags);
116: if ($store !== null) {
117: $store = Mage::app()->getStore($store)->getId();
118: }
119: $this->_getResource()->clearCacheData($tags, $store);
120: return $this;
121: }
122:
123: /**
124: * Clear all cache data related with products
125: *
126: * @param int|array $productIds
127: * @return Mage_CatalogIndex_Model_Aggregation
128: */
129: public function clearProductData($productIds)
130: {
131: $categoryPaths = $this->_getResource()->getProductCategoryPaths($productIds);
132: if (!empty($categoryPaths)) {
133: $tags = array();
134: foreach ($categoryPaths as $path) {
135: $tags[] = Mage_Catalog_Model_Category::CACHE_TAG.':'.$path;
136: }
137: $this->clearCacheData($tags);
138: }
139: return $this;
140: }
141:
142: /**
143: * Prepare data key
144: *
145: * @param string $key
146: * @return string
147: */
148: protected function _processKey($key)
149: {
150: return $key;
151: return md5($key);
152: }
153:
154: /**
155: * Process tags array
156: *
157: * this method split tags like "category:1,2,3" to four
158: * different tags: category, category1, category2, category3
159: *
160: * @param unknown_type $tags
161: * @return unknown
162: */
163: protected function _processTags($tags)
164: {
165: $newTags = array();
166: foreach ($tags as $tag) {
167: $tagInfo = explode(':', $tag);
168: if (count($tagInfo)==1) {
169: $newTags[] = $tagInfo[0];
170: } else {
171: $tagVariants = explode('/', $tagInfo[1]);
172: foreach ($tagVariants as $tagVariant) {
173: $newTags[] = $tagInfo[0] . $tagVariant;
174: }
175: }
176: }
177: return $newTags;
178: }
179: }
180: