forked from MaxFurnitureMagento/MagentoSource
/
Label.php
255 lines (222 loc) · 6.61 KB
/
Label.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* DO NOT REMOVE OR MODIFY THIS NOTICE
*
* EasyBanner module for Magento - flexible banner management
*
* @author Templates-Master Team <www.templates-master.com>
*/
class TM_ProLabels_Model_Label extends Mage_CatalogRule_Model_Rule
{
/**
* Matched product ids array
*
* @var array
*/
protected $_productIds;
/**
* Limitation for products collection
*
* @var int|array|null
*/
protected $_productsFilter = null;
protected function _construct()
{
parent::_construct();
$this->_init('prolabels/label');
$this->setIdFieldName('rules_id');
}
public function setProductsFilter($productIds)
{
$this->_productsFilter = $productIds;
}
/**
* Returns products filter
*
* @return array|int|null
*/
public function getProductsFilter()
{
return $this->_productsFilter;
}
public function getConditionsInstance()
{
return Mage::getModel('prolabels/rule_condition_combine');
}
protected function _beforeSave()
{
if ((int)$this->getId() > 3) {
$this->_getResource()->loadLabelProductImage($this);
$this->_getResource()->loadLabelCategoryImage($this);
}
parent::_beforeSave();
}
protected function _afterSave()
{
if ((int)$this->getData('label_status') != 0 and (int)$this->getId() > 3) {
$this->_getResource()->updateProlabelsRuleProductData($this);
}
$this->_getResource()->addLabelStoreIds($this);
//parent::_afterSave();
}
public function loadStoreIds(TM_ProLabels_Model_Label $object)
{
$rulesId = $object->getId();
$storeIds = array();
if ($rulesId) {
$storeIds = $this->lookupStoreIds($rulesId);
}
$object->setStoreIds($storeIds);
}
/**
* Get store ids, to which specified item is assigned
*
* @param int $id
* @return array
*/
public function lookupStoreIds($id)
{
return $this->_getResource()->lookupStoreIds($id);
}
public function getLabelsData($productId, $mode)
{
return $this->_getResource()->getProductLabelsData($productId, $mode);
}
/**
* Get array of product ids which are matched by rule
*
* @return array
*/
public function getMatchingProductIds()
{
if (is_null($this->_productIds)) {
$this->_productIds = array();
$this->setCollectedAttributes(array());
$productCollection = Mage::getResourceModel('catalog/product_collection');
$this->getConditions()->collectValidatedAttributes($productCollection);
Mage::getSingleton('core/resource_iterator')->walk(
$productCollection->getSelect(),
array(array($this, 'callbackValidateProduct')),
array(
'attributes' => $this->getCollectedAttributes(),
'product' => Mage::getModel('catalog/product'),
)
);
}
return $this->_productIds;
}
/**
* Callback function for product matching
*
* @param $args
* @return void
*/
public function callbackValidateProduct($args)
{
$product = clone $args['product'];
$product->setData($args['row']);
if ($this->getConditions()->validate($product)) {
$this->_productIds[] = $product->getId();
}
}
/**
* Apply rule to product
*
* @param int|Mage_Catalog_Model_Product $product
* @param array $websiteIds
* @return void
*/
public function applyToProduct($product, $websiteIds=null)
{
if (is_numeric($product)) {
$product = Mage::getModel('catalog/product')->load($product);
}
if (is_null($websiteIds)) {
$websiteIds = explode(',', $this->getWebsiteIds());
}
$this->getResource()->applyToProduct($this, $product, $websiteIds);
}
/**
* Apply all price rules, invalidate related cache and refresh price index
*
* @return Mage_CatalogRule_Model_Rule
*/
public function applyAll()
{
$collection = $this->getResourceCollection();
$collection->getSelect()
->where('rules_id>3')
->where('label_status=1');
$collection->walk(array($this->_getResource(), 'updateProlabelsRuleProductData'));
// $this->_getResource()->applyAllRulesForDateRange();
}
/**
* Apply all price rules to product
*
* @param int|Mage_Catalog_Model_Product $product
* @return Mage_CatalogRule_Model_Rule
*/
public function applyAllRulesToProduct($product)
{
$this->_getResource()->applyAllRulesForDateRange(NULL, NULL, $product);
$this->_invalidateCache();
if ($product instanceof Mage_Catalog_Model_Product) {
$productId = $product->getId();
} else {
$productId = $product;
}
if ($productId) {
Mage::getSingleton('index/indexer')->processEntityAction(
new Varien_Object(array('id' => $productId)),
Mage_Catalog_Model_Product::ENTITY,
Mage_Catalog_Model_Product_Indexer_Price::EVENT_TYPE_REINDEX_PRICE
);
}
}
/**
* Return true if banner status = 1
* and banner linked to active placeholder
*
* @return boolean
*/
public function isActive()
{
if ($this->getData('label_status')/* && count($this->getPlaceholderIds(true))*/) {
return true;
}
return false;
}
public function getStoreIds()
{
return $this->_getResource()->loadStoreIds($this);;
}
/**
* Checks is banner is active for requested store
* Used to check is it possible to click on banner
*
* @param int $store
* @return mixed int|boolean
*/
public function check($store)
{
return $this->isActive() && (in_array($store, $this->getStoreIds()) || in_array(0, $this->getStoreIds()));
}
/**
* Checks all conditions of the banner
*
* @return bool
*/
public function isVisible()
{
if (!$this->getStatus()
|| (!in_array(Mage::app()->getStore()->getId(), $this->getStoreIds())
&& !in_array(0, $this->getStoreIds()))) { // all stores
return false;
}
$conditions = array(unserialize($this->getConditionsSerialized()));
if (!$this->_validateConditions($conditions)) {
return false;
}
return true;
}
}