1: <?php
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: class Mage_Tag_Model_Tag extends Mage_Core_Model_Abstract
46: {
47: const STATUS_DISABLED = -1;
48: const STATUS_PENDING = 0;
49: const STATUS_APPROVED = 1;
50:
51:
52: const ADD_STATUS_SUCCESS = 'success';
53: const ADD_STATUS_NEW = 'new';
54: const ADD_STATUS_EXIST = 'exist';
55: const ADD_STATUS_REJECTED = 'rejected';
56:
57: 58: 59: 60:
61: const ENTITY = 'tag';
62:
63: 64: 65: 66: 67:
68: protected $_eventPrefix = 'tag';
69:
70: 71: 72: 73: 74:
75: protected $_addBasePopularity = false;
76:
77: protected function _construct()
78: {
79: $this->_init('tag/tag');
80: }
81:
82: 83: 84: 85: 86:
87: public function afterCommitCallback()
88: {
89: parent::afterCommitCallback();
90: Mage::getSingleton('index/indexer')->processEntityAction(
91: $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
92: );
93: return $this;
94: }
95:
96: 97: 98: 99: 100: 101:
102: public function setAddBasePopularity($flag = true)
103: {
104: $this->_addBasePopularity = $flag;
105: return $this;
106: }
107:
108: 109: 110: 111: 112:
113: public function getAddBasePopularity()
114: {
115: return $this->_addBasePopularity;
116: }
117:
118: 119: 120: 121: 122: 123:
124: protected function _getProductEventTagsCollection(Varien_Event_Observer $observer)
125: {
126: return $this->getResourceCollection()
127: ->joinRel()
128: ->addProductFilter($observer->getEvent()->getProduct()->getId())
129: ->addTagGroup()
130: ->load();
131: }
132:
133: public function getPopularity()
134: {
135: return $this->_getData('popularity');
136: }
137:
138: public function getName()
139: {
140: return $this->_getData('name');
141: }
142:
143: public function getTagId()
144: {
145: return $this->_getData('tag_id');
146: }
147:
148: public function getRatio()
149: {
150: return $this->_getData('ratio');
151: }
152:
153: public function setRatio($ratio)
154: {
155: $this->setData('ratio', $ratio);
156: return $this;
157: }
158:
159: public function loadByName($name)
160: {
161: $this->_getResource()->loadByName($this, $name);
162: return $this;
163: }
164:
165: public function aggregate()
166: {
167: $this->_getResource()->aggregate($this);
168: return $this;
169: }
170:
171: public function productEventAggregate($observer)
172: {
173: $this->_getProductEventTagsCollection($observer)->walk('aggregate');
174: return $this;
175: }
176:
177: 178: 179: 180: 181: 182:
183: public function productDeleteEventAction($observer)
184: {
185: $this->_getResource()->decrementProducts($this->_getProductEventTagsCollection($observer)->getAllIds());
186: return $this;
187: }
188:
189: 190: 191: 192: 193: 194: 195:
196: public function addSummary($storeId)
197: {
198: $this->setStoreId($storeId);
199: $this->_getResource()->addSummary($this);
200: return $this;
201: }
202:
203: 204: 205:
206: public function getApprovedStatus()
207: {
208: return self::STATUS_APPROVED;
209: }
210:
211: 212: 213:
214: public function getPendingStatus()
215: {
216: return self::STATUS_PENDING;
217: }
218:
219: 220: 221:
222: public function getDisabledStatus()
223: {
224: return self::STATUS_DISABLED;
225: }
226:
227: public function getEntityCollection()
228: {
229: return Mage::getResourceModel('tag/product_collection');
230: }
231:
232: public function getCustomerCollection()
233: {
234: return Mage::getResourceModel('tag/customer_collection');
235: }
236:
237: public function getTaggedProductsUrl()
238: {
239: return Mage::getUrl('tag/product/list', array('tagId' => $this->getTagId()));
240: }
241:
242: public function getViewTagUrl()
243: {
244: return Mage::getUrl('tag/customer/view', array('tagId' => $this->getTagId()));
245: }
246:
247: public function getEditTagUrl()
248: {
249: return Mage::getUrl('tag/customer/edit', array('tagId' => $this->getTagId()));
250: }
251:
252: public function getRemoveTagUrl()
253: {
254: return Mage::getUrl('tag/customer/remove', array('tagId' => $this->getTagId()));
255: }
256:
257: public function getPopularCollection()
258: {
259: return Mage::getResourceModel('tag/popular_collection');
260: }
261:
262: 263: 264: 265: 266:
267: public function getRelatedProductIds()
268: {
269: return Mage::getModel('tag/tag_relation')
270: ->setTagId($this->getTagId())
271: ->setStoreId($this->getStoreId())
272: ->setStatusFilter($this->getStatusFilter())
273: ->setCustomerId(null)
274: ->getProductIds();
275: }
276:
277: 278: 279: 280: 281: 282:
283: public function isAvailableInStore($storeId = null)
284: {
285: $storeId = (is_null($storeId)) ? Mage::app()->getStore()->getId() : $storeId;
286: return in_array($storeId, $this->getVisibleInStoreIds());
287: }
288:
289: protected function _beforeDelete()
290: {
291: $this->_protectFromNonAdmin();
292: return parent::_beforeDelete();
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302:
303: public function saveRelation($productId, $customerId, $storeId)
304: {
305:
306: $relationModel = Mage::getModel('tag/tag_relation');
307: $relationModel->setTagId($this->getId())
308: ->setStoreId($storeId)
309: ->setProductId($productId)
310: ->setCustomerId($customerId)
311: ->setActive(Mage_Tag_Model_Tag_Relation::STATUS_ACTIVE)
312: ->setCreatedAt($relationModel->getResource()->formatDate(time()));
313:
314: $relationModelSaveNeed = false;
315: switch($this->getStatus()) {
316: case $this->getApprovedStatus():
317: if($this->_checkLinkBetweenTagProduct($relationModel)) {
318: $relation = $this->_getLinkBetweenTagCustomerProduct($relationModel);
319: if ($relation->getId()) {
320: if (!$relation->getActive()) {
321:
322: $relationModel->setId($relation->getId());
323: $relationModelSaveNeed = true;
324: }
325: } else {
326: $relationModelSaveNeed = true;
327: }
328: $result = self::ADD_STATUS_EXIST;
329: } else {
330: $relationModelSaveNeed = true;
331: $result = self::ADD_STATUS_SUCCESS;
332: }
333: break;
334: case $this->getPendingStatus():
335: $relation = $this->_getLinkBetweenTagCustomerProduct($relationModel);
336: if ($relation->getId()) {
337: if (!$relation->getActive()) {
338: $relationModel->setId($relation->getId());
339: $relationModelSaveNeed = true;
340: }
341: } else {
342: $relationModelSaveNeed = true;
343: }
344: $result = self::ADD_STATUS_NEW;
345: break;
346: case $this->getDisabledStatus():
347: if($this->_checkLinkBetweenTagCustomerProduct($relationModel)) {
348: $result = self::ADD_STATUS_REJECTED;
349: } else {
350: $this->setStatus($this->getPendingStatus())->save();
351: $relationModelSaveNeed = true;
352: $result = self::ADD_STATUS_NEW;
353: }
354: break;
355: }
356: if ($relationModelSaveNeed) {
357: $relationModel->save();
358: }
359:
360: return $result;
361: }
362:
363: 364: 365: 366: 367: 368:
369: protected function _checkLinkBetweenTagProduct($relationModel)
370: {
371: $customerId = $relationModel->getCustomerId();
372: $relationModel->setCustomerId(null);
373: $result = in_array($relationModel->getProductId(), $relationModel->getProductIds());
374: $relationModel->setCustomerId($customerId);
375: return $result;
376: }
377:
378: 379: 380: 381: 382: 383:
384: protected function _checkLinkBetweenTagCustomerProduct($relationModel)
385: {
386: return (count($this->_getLinkBetweenTagCustomerProduct($relationModel)->getProductIds()) > 0);
387: }
388:
389: 390: 391: 392: 393: 394:
395: protected function _getLinkBetweenTagCustomerProduct($relationModel)
396: {
397: return Mage::getModel('tag/tag_relation')->loadByTagCustomer(
398: $relationModel->getProductId(),
399: $this->getId(),
400: $relationModel->getCustomerId(),
401: $relationModel->getStoreId()
402: );
403: }
404:
405: }
406: