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: class Mage_Tag_Model_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39: 40: 41: 42:
43: public function items($productId, $store = null)
44: {
45: $result = array();
46:
47: $fieldsForResult = array('tag_id', 'name');
48:
49:
50: $product = Mage::getModel('catalog/product')->load($productId);
51: if (!$product->getId()) {
52: $this->_fault('product_not_exists');
53: }
54:
55:
56: $tags = Mage::getModel('tag/tag')->getCollection()->joinRel()->addProductFilter($productId);
57: if ($store) {
58: $tags->addStoreFilter($this->_getStoreId($store));
59: }
60:
61:
62: foreach ($tags as $tag) {
63: $result[$tag->getId()] = $tag->toArray($fieldsForResult);
64: }
65:
66: return $result;
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: public function info($tagId, $store)
78: {
79: $result = array();
80: $storeId = $this->_getStoreId($store);
81:
82: $tag = Mage::getModel('tag/tag')->setStoreId($storeId)->setAddBasePopularity()->load($tagId);
83: if (!$tag->getId()) {
84: $this->_fault('tag_not_exists');
85: }
86: $result['status'] = $tag->getStatus();
87: $result['name'] = $tag->getName();
88: $result['base_popularity'] = (is_numeric($tag->getBasePopularity())) ? $tag->getBasePopularity() : 0;
89:
90: $result['products'] = array();
91: $relatedProductsCollection = $tag->getEntityCollection()->addTagFilter($tagId)
92: ->addStoreFilter($storeId)->addPopularity($tagId);
93: foreach ($relatedProductsCollection as $product) {
94: $result['products'][$product->getId()] = $product->getPopularity();
95: }
96:
97: return $result;
98: }
99:
100: 101: 102: 103: 104: 105: 106:
107: public function add($data)
108: {
109: $data = $this->_prepareDataForAdd($data);
110:
111: $product = Mage::getModel('catalog/product')->load($data['product_id']);
112: if (!$product->getId()) {
113: $this->_fault('product_not_exists');
114: }
115:
116: $customer = Mage::getModel('customer/customer')->load($data['customer_id']);
117: if (!$customer->getId()) {
118: $this->_fault('customer_not_exists');
119: }
120: $storeId = $this->_getStoreId($data['store']);
121:
122: try {
123:
124: $tag = Mage::getModel('tag/tag');
125: $tagNamesArr = Mage::helper('tag')->cleanTags(Mage::helper('tag')->extractTags($data['tag']));
126: foreach ($tagNamesArr as $tagName) {
127:
128: $tag->unsetData();
129: $tag->loadByName($tagName);
130: if (!$tag->getId()) {
131: $tag->setName($tagName)
132: ->setFirstCustomerId($customer->getId())
133: ->setFirstStoreId($storeId)
134: ->setStatus($tag->getPendingStatus())
135: ->save();
136: }
137: $tag->saveRelation($product->getId(), $customer->getId(), $storeId);
138: $result[$tagName] = $tag->getId();
139: }
140: } catch (Mage_Core_Exception $e) {
141: $this->_fault('save_error', $e->getMessage());
142: }
143:
144: return $result;
145: }
146:
147: 148: 149: 150: 151: 152: 153: 154:
155: public function update($tagId, $data, $store)
156: {
157: $data = $this->_prepareDataForUpdate($data);
158: $storeId = $this->_getStoreId($store);
159:
160: $tag = Mage::getModel('tag/tag')->setStoreId($storeId)->setAddBasePopularity()->load($tagId);
161: if (!$tag->getId()) {
162: $this->_fault('tag_not_exists');
163: }
164:
165:
166: $tag->setStore($storeId);
167: if (isset($data['base_popularity'])) {
168: $tag->setBasePopularity($data['base_popularity']);
169: }
170: if (isset($data['name'])) {
171: $tag->setName(trim($data['name']));
172: }
173: if (isset($data['status'])) {
174:
175: if (!in_array($data['status'], array(
176: $tag->getApprovedStatus(), $tag->getPendingStatus(), $tag->getDisabledStatus()))) {
177: $this->_fault('invalid_data');
178: }
179: $tag->setStatus($data['status']);
180: }
181:
182: try {
183: $tag->save();
184: } catch (Mage_Core_Exception $e) {
185: $this->_fault('save_error', $e->getMessage());
186: }
187:
188: return true;
189: }
190:
191: 192: 193: 194: 195: 196:
197: public function remove($tagId)
198: {
199:
200: $tag = Mage::getModel('tag/tag')->load($tagId);
201: if (!$tag->getId()) {
202: $this->_fault('tag_not_exists');
203: }
204: try {
205: $tag->delete();
206: } catch (Mage_Core_Exception $e) {
207: $this->_fault('remove_error', $e->getMessage());
208: }
209:
210: return true;
211: }
212:
213: 214: 215: 216: 217: 218:
219: protected function _prepareDataForAdd($data)
220: {
221: if (!isset($data['product_id']) or !isset($data['tag'])
222: or !isset($data['customer_id']) or !isset($data['store'])) {
223: $this->_fault('invalid_data');
224: }
225:
226: return $data;
227: }
228:
229: 230: 231: 232: 233: 234:
235: protected function _prepareDataForUpdate($data)
236: {
237:
238: if ( !(isset($data['name']) or isset($data['status']) or isset($data['base_popularity']))) {
239: $this->_fault('invalid_data');
240: }
241:
242: return $data;
243: }
244: }
245: