-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tag.php
424 lines (381 loc) · 11.8 KB
/
Tag.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_Tag
<<<<<<< HEAD
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
=======
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
>>>>>>> origin/master
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Tag model
*
* @method Mage_Tag_Model_Resource_Tag _getResource()
* @method Mage_Tag_Model_Resource_Tag getResource()
* @method Mage_Tag_Model_Tag setName(string $value)
* @method int getStatus()
* @method Mage_Tag_Model_Tag setStatus(int $value)
* @method int getFirstCustomerId()
* @method Mage_Tag_Model_Tag setFirstCustomerId(int $value)
* @method int getFirstStoreId()
* @method Mage_Tag_Model_Tag setFirstStoreId(int $value)
*
* @category Mage
* @package Mage_Tag
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Tag_Model_Tag extends Mage_Core_Model_Abstract
{
const STATUS_DISABLED = -1;
const STATUS_PENDING = 0;
const STATUS_APPROVED = 1;
// statuses for tag relation add
const ADD_STATUS_SUCCESS = 'success';
const ADD_STATUS_NEW = 'new';
const ADD_STATUS_EXIST = 'exist';
const ADD_STATUS_REJECTED = 'rejected';
/**
* Entity code.
* Can be used as part of method name for entity processing
*/
const ENTITY = 'tag';
/**
* Event prefix for observer
*
* @var string
*/
protected $_eventPrefix = 'tag';
/**
* This flag means should we or not add base popularity on tag load
*
* @var bool
*/
protected $_addBasePopularity = false;
protected function _construct()
{
$this->_init('tag/tag');
}
/**
* Init indexing process after tag data commit
*
* @return Mage_Tag_Model_Tag
*/
public function afterCommitCallback()
{
parent::afterCommitCallback();
Mage::getSingleton('index/indexer')->processEntityAction(
$this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
);
return $this;
}
/**
* Setter for addBasePopularity flag
*
* @param bool $flag
* @return Mage_Tag_Model_Tag
*/
public function setAddBasePopularity($flag = true)
{
$this->_addBasePopularity = $flag;
return $this;
}
/**
* Getter for addBasePopularity flag
*
* @return bool
*/
public function getAddBasePopularity()
{
return $this->_addBasePopularity;
}
/**
* Product event tags collection getter
*
* @param Varien_Event_Observer $observer
* @return Mage_Tag_Model_Mysql4_Tag_Collection
*/
protected function _getProductEventTagsCollection(Varien_Event_Observer $observer)
{
return $this->getResourceCollection()
->joinRel()
->addProductFilter($observer->getEvent()->getProduct()->getId())
->addTagGroup()
->load();
}
public function getPopularity()
{
return $this->_getData('popularity');
}
public function getName()
{
return $this->_getData('name');
}
public function getTagId()
{
return $this->_getData('tag_id');
}
public function getRatio()
{
return $this->_getData('ratio');
}
public function setRatio($ratio)
{
$this->setData('ratio', $ratio);
return $this;
}
public function loadByName($name)
{
$this->_getResource()->loadByName($this, $name);
return $this;
}
public function aggregate()
{
$this->_getResource()->aggregate($this);
return $this;
}
public function productEventAggregate($observer)
{
$this->_getProductEventTagsCollection($observer)->walk('aggregate');
return $this;
}
/**
* Product delete event action
*
* @param Varien_Event_Observer $observer
* @return Mage_Tag_Model_Tag
*/
public function productDeleteEventAction($observer)
{
$this->_getResource()->decrementProducts($this->_getProductEventTagsCollection($observer)->getAllIds());
return $this;
}
/**
* Add summary data to current object
*
* @deprecated after 1.4.0.0
* @param int $storeId
* @return Mage_Tag_Model_Tag
*/
public function addSummary($storeId)
{
$this->setStoreId($storeId);
$this->_getResource()->addSummary($this);
return $this;
}
/**
* getter for self::STATUS_APPROVED
*/
public function getApprovedStatus()
{
return self::STATUS_APPROVED;
}
/**
* getter for self::STATUS_PENDING
*/
public function getPendingStatus()
{
return self::STATUS_PENDING;
}
/**
* getter for self::STATUS_DISABLED
*/
public function getDisabledStatus()
{
return self::STATUS_DISABLED;
}
public function getEntityCollection()
{
return Mage::getResourceModel('tag/product_collection');
}
public function getCustomerCollection()
{
return Mage::getResourceModel('tag/customer_collection');
}
public function getTaggedProductsUrl()
{
return Mage::getUrl('tag/product/list', array('tagId' => $this->getTagId()));
}
public function getViewTagUrl()
{
return Mage::getUrl('tag/customer/view', array('tagId' => $this->getTagId()));
}
public function getEditTagUrl()
{
return Mage::getUrl('tag/customer/edit', array('tagId' => $this->getTagId()));
}
public function getRemoveTagUrl()
{
return Mage::getUrl('tag/customer/remove', array('tagId' => $this->getTagId()));
}
public function getPopularCollection()
{
return Mage::getResourceModel('tag/popular_collection');
}
/**
* Retrieves array of related product IDs
*
* @return array
*/
public function getRelatedProductIds()
{
return Mage::getModel('tag/tag_relation')
->setTagId($this->getTagId())
->setStoreId($this->getStoreId())
->setStatusFilter($this->getStatusFilter())
->setCustomerId(null)
->getProductIds();
}
/**
* Checks is available current tag in specified store
*
* @param int $storeId
* @return bool
*/
public function isAvailableInStore($storeId = null)
{
$storeId = (is_null($storeId)) ? Mage::app()->getStore()->getId() : $storeId;
return in_array($storeId, $this->getVisibleInStoreIds());
}
protected function _beforeDelete()
{
$this->_protectFromNonAdmin();
return parent::_beforeDelete();
}
/**
* Save tag relation with product, customer and store
*
* @param int $productId
* @param int $customerId
* @param int $storeId
* @return string - relation add status
*/
public function saveRelation($productId, $customerId, $storeId)
{
/** @var $relationModel Mage_Tag_Model_Tag_Relation */
$relationModel = Mage::getModel('tag/tag_relation');
$relationModel->setTagId($this->getId())
->setStoreId($storeId)
->setProductId($productId)
->setCustomerId($customerId)
->setActive(Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE)
->setCreatedAt($relationModel->getResource()->formatDate(time()));
$relationModelSaveNeed = false;
switch($this->getStatus()) {
case $this->getApprovedStatus():
if($this->_checkLinkBetweenTagProduct($relationModel)) {
$relation = $this->_getLinkBetweenTagCustomerProduct($relationModel);
if ($relation->getId()) {
if (!$relation->getActive()) {
// activate relation if it was inactive
$relationModel->setId($relation->getId());
$relationModelSaveNeed = true;
}
} else {
$relationModelSaveNeed = true;
}
$result = self::ADD_STATUS_EXIST;
} else {
$relationModelSaveNeed = true;
$result = self::ADD_STATUS_SUCCESS;
}
break;
case $this->getPendingStatus():
$relation = $this->_getLinkBetweenTagCustomerProduct($relationModel);
if ($relation->getId()) {
if (!$relation->getActive()) {
$relationModel->setId($relation->getId());
$relationModelSaveNeed = true;
}
} else {
$relationModelSaveNeed = true;
}
$result = self::ADD_STATUS_NEW;
break;
case $this->getDisabledStatus():
if($this->_checkLinkBetweenTagCustomerProduct($relationModel)) {
$result = self::ADD_STATUS_REJECTED;
} else {
$this->setStatus($this->getPendingStatus())->save();
$relationModelSaveNeed = true;
$result = self::ADD_STATUS_NEW;
}
break;
}
if ($relationModelSaveNeed) {
$relationModel->save();
}
return $result;
}
/**
* Check whether product is already marked in store with tag
*
* @param Mage_Tag_Model_Tag_Relation $relationModel
* @return boolean
*/
protected function _checkLinkBetweenTagProduct($relationModel)
{
$customerId = $relationModel->getCustomerId();
$relationModel->setCustomerId(null);
$result = in_array($relationModel->getProductId(), $relationModel->getProductIds());
$relationModel->setCustomerId($customerId);
return $result;
}
/**
* Check whether product is already marked in store with tag by customer
*
* @param Mage_Tag_Model_Tag_Relation $relationModel
* @return bool
*/
protected function _checkLinkBetweenTagCustomerProduct($relationModel)
{
return (count($this->_getLinkBetweenTagCustomerProduct($relationModel)->getProductIds()) > 0);
}
/**
* Get relation model for product marked in store with tag by customer
*
* @param Mage_Tag_Model_Tag_Relation $relationModel
* @return Mage_Tag_Model_Tag_Relation
*/
protected function _getLinkBetweenTagCustomerProduct($relationModel)
{
return Mage::getModel('tag/tag_relation')->loadByTagCustomer(
$relationModel->getProductId(),
$this->getId(),
$relationModel->getCustomerId(),
$relationModel->getStoreId()
);
}
/**
* Processing object after save data
*
* @return Mage_Core_Model_Abstract
*/
protected function _afterSave()
{
if ($this->hasData('tag_assigned_products')) {
$tagRelationModel = Mage::getModel('tag/tag_relation');
$tagRelationModel->addRelations($this, $this->getData('tag_assigned_products'));
}
return parent::_afterSave();
}
}