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_Tag
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: * Tag relation model
30: *
31: * @method Mage_Tag_Model_Resource_Tag_Relation _getResource()
32: * @method Mage_Tag_Model_Resource_Tag_Relation getResource()
33: * @method int getTagId()
34: * @method Mage_Tag_Model_Tag_Relation setTagId(int $value)
35: * @method int getCustomerId()
36: * @method Mage_Tag_Model_Tag_Relation setCustomerId(int $value)
37: * @method int getProductId()
38: * @method Mage_Tag_Model_Tag_Relation setProductId(int $value)
39: * @method int getStoreId()
40: * @method Mage_Tag_Model_Tag_Relation setStoreId(int $value)
41: * @method int getActive()
42: * @method Mage_Tag_Model_Tag_Relation setActive(int $value)
43: * @method string getCreatedAt()
44: * @method Mage_Tag_Model_Tag_Relation setCreatedAt(string $value)
45: *
46: * @category Mage
47: * @package Mage_Tag
48: * @author Magento Core Team <core@magentocommerce.com>
49: */
50: class Mage_Tag_Model_Tag_Relation extends Mage_Core_Model_Abstract
51: {
52: /**
53: * Relation statuses
54: */
55: const STATUS_ACTIVE = 1;
56: const STATUS_NOT_ACTIVE = 0;
57:
58: /**
59: * Entity code.
60: * Can be used as part of method name for entity processing
61: */
62: const ENTITY = 'tag_relation';
63:
64: /**
65: * Initialize resource model
66: *
67: */
68: protected function _construct()
69: {
70: $this->_init('tag/tag_relation');
71: }
72:
73: /**
74: * Retrieve Resource Instance wrapper
75: *
76: * @return Mage_Tag_Model_Mysql4_Tag_Relation
77: */
78: protected function _getResource()
79: {
80: return parent::_getResource();
81: }
82:
83: /**
84: * Init indexing process after tag data commit
85: *
86: * @return Mage_Tag_Model_Tag_Relation
87: */
88: public function afterCommitCallback()
89: {
90: parent::afterCommitCallback();
91: Mage::getSingleton('index/indexer')->processEntityAction(
92: $this, self::ENTITY, Mage_Index_Model_Event::TYPE_SAVE
93: );
94: return $this;
95: }
96:
97: /**
98: * Load relation by Product (optional), tag, customer and store
99: *
100: * @param int $productId
101: * @param int $tagId
102: * @param int $customerId
103: * @param int $storeId
104: * @return Mage_Tag_Model_Tag_Relation
105: */
106: public function loadByTagCustomer($productId=null, $tagId, $customerId, $storeId=null)
107: {
108: $this->setProductId($productId);
109: $this->setTagId($tagId);
110: $this->setCustomerId($customerId);
111: if(!is_null($storeId)) {
112: $this->setStoreId($storeId);
113: }
114: $this->_getResource()->loadByTagCustomer($this);
115: return $this;
116: }
117:
118: /**
119: * Retrieve Relation Product Ids
120: *
121: * @return array
122: */
123: public function getProductIds()
124: {
125: $ids = $this->getData('product_ids');
126: if (is_null($ids)) {
127: $ids = $this->_getResource()->getProductIds($this);
128: $this->setProductIds($ids);
129: }
130: return $ids;
131: }
132:
133: /**
134: * Retrieve list of related tag ids for products specified in current object
135: *
136: * @return array
137: */
138: public function getRelatedTagIds()
139: {
140: if (is_null($this->getData('related_tag_ids'))) {
141: $this->setRelatedTagIds($this->_getResource()->getRelatedTagIds($this));
142: }
143: return $this->getData('related_tag_ids');
144: }
145:
146: /**
147: * Deactivate tag relations (using current settings)
148: *
149: * @return Mage_Tag_Model_Tag_Relation
150: */
151: public function deactivate()
152: {
153: $this->_getResource()->deactivate($this->getTagId(), $this->getCustomerId());
154: return $this;
155: }
156:
157: /**
158: * Add TAG to PRODUCT relations
159: *
160: * @param Mage_Tag_Model_Tag $model
161: * @param array $productIds
162: * @return Mage_Tag_Model_Tag_Relation
163: */
164: public function addRelations(Mage_Tag_Model_Tag $model, $productIds = array())
165: {
166: $this->setAddedProductIds($productIds);
167: $this->setTagId($model->getTagId());
168: $this->setCustomerId(null);
169: $this->setStoreId($model->getStore());
170: $this->_getResource()->addRelations($this);
171: return $this;
172: }
173: }
174: