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_Catalog_Model_Product_Link_Api extends Mage_Catalog_Model_Api_Resource
35: {
36: 37: 38: 39: 40:
41: protected $_typeMap = array(
42: 'related' => Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED,
43: 'up_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL,
44: 'cross_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL,
45: 'grouped' => Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED
46: );
47:
48: public function __construct()
49: {
50: $this->_storeIdSessionField = 'product_store_id';
51: }
52:
53: 54: 55: 56: 57: 58: 59: 60:
61: public function items($type, $productId, $identifierType = null)
62: {
63: $typeId = $this->_getTypeId($type);
64:
65: $product = $this->_initProduct($productId, $identifierType);
66:
67: $link = $product->getLinkInstance()
68: ->setLinkTypeId($typeId);
69:
70: $collection = $this->_initCollection($link, $product);
71:
72: $result = array();
73:
74: foreach ($collection as $linkedProduct) {
75: $row = array(
76: 'product_id' => $linkedProduct->getId(),
77: 'type' => $linkedProduct->getTypeId(),
78: 'set' => $linkedProduct->getAttributeSetId(),
79: 'sku' => $linkedProduct->getSku()
80: );
81:
82: foreach ($link->getAttributes() as $attribute) {
83: $row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
84: }
85:
86: $result[] = $row;
87: }
88:
89: return $result;
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
102: public function assign($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
103: {
104: $typeId = $this->_getTypeId($type);
105:
106: $product = $this->_initProduct($productId, $identifierType);
107:
108: $link = $product->getLinkInstance()
109: ->setLinkTypeId($typeId);
110:
111: $collection = $this->_initCollection($link, $product);
112: $idBySku = $product->getIdBySku($linkedProductId);
113: if ($idBySku) {
114: $linkedProductId = $idBySku;
115: }
116:
117: $links = $this->_collectionToEditableArray($collection);
118:
119: $links[(int)$linkedProductId] = array();
120:
121: foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
122: if (isset($data[$attribute['code']])) {
123: $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
124: }
125: }
126:
127: try {
128: if ($type == 'grouped') {
129: $link->getResource()->saveGroupedLinks($product, $links, $typeId);
130: } else {
131: $link->getResource()->saveProductLinks($product, $links, $typeId);
132: }
133:
134: $_linkInstance = Mage::getSingleton('catalog/product_link');
135: $_linkInstance->saveProductRelations($product);
136:
137: $indexerStock = Mage::getModel('cataloginventory/stock_status');
138: $indexerStock->updateStatus($productId);
139:
140: $indexerPrice = Mage::getResourceModel('catalog/product_indexer_price');
141: $indexerPrice->reindexProductIds($productId);
142: } catch (Exception $e) {
143: $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product does not exist.'));
144: }
145:
146: return true;
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156: 157: 158:
159: public function update($type, $productId, $linkedProductId, $data = array(), $identifierType = null)
160: {
161: $typeId = $this->_getTypeId($type);
162:
163: $product = $this->_initProduct($productId, $identifierType);
164:
165: $link = $product->getLinkInstance()
166: ->setLinkTypeId($typeId);
167:
168: $collection = $this->_initCollection($link, $product);
169:
170: $links = $this->_collectionToEditableArray($collection);
171:
172: $idBySku = $product->getIdBySku($linkedProductId);
173: if ($idBySku) {
174: $linkedProductId = $idBySku;
175: }
176:
177: foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
178: if (isset($data[$attribute['code']])) {
179: $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
180: }
181: }
182:
183: try {
184: if ($type == 'grouped') {
185: $link->getResource()->saveGroupedLinks($product, $links, $typeId);
186: } else {
187: $link->getResource()->saveProductLinks($product, $links, $typeId);
188: }
189:
190: $_linkInstance = Mage::getSingleton('catalog/product_link');
191: $_linkInstance->saveProductRelations($product);
192:
193: $indexerStock = Mage::getModel('cataloginventory/stock_status');
194: $indexerStock->updateStatus($productId);
195:
196: $indexerPrice = Mage::getResourceModel('catalog/product_indexer_price');
197: $indexerPrice->reindexProductIds($productId);
198: } catch (Exception $e) {
199: $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product does not exist.'));
200: }
201:
202: return true;
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213:
214: public function remove($type, $productId, $linkedProductId, $identifierType = null)
215: {
216: $typeId = $this->_getTypeId($type);
217:
218: $product = $this->_initProduct($productId, $identifierType);
219:
220: $link = $product->getLinkInstance()
221: ->setLinkTypeId($typeId);
222:
223: $collection = $this->_initCollection($link, $product);
224:
225: $idBySku = $product->getIdBySku($linkedProductId);
226: if ($idBySku) {
227: $linkedProductId = $idBySku;
228: }
229:
230: $links = $this->_collectionToEditableArray($collection);
231:
232: if (isset($links[$linkedProductId])) {
233: unset($links[$linkedProductId]);
234: }
235:
236: try {
237: $link->getResource()->saveProductLinks($product, $links, $typeId);
238: } catch (Exception $e) {
239: $this->_fault('not_removed');
240: }
241:
242: return true;
243: }
244:
245: 246: 247: 248: 249: 250:
251: public function attributes($type)
252: {
253: $typeId = $this->_getTypeId($type);
254:
255: $attributes = Mage::getModel('catalog/product_link')
256: ->getAttributes($typeId);
257:
258: $result = array();
259:
260: foreach ($attributes as $attribute) {
261: $result[] = array(
262: 'code' => $attribute['code'],
263: 'type' => $attribute['type']
264: );
265: }
266:
267: return $result;
268: }
269:
270: 271: 272: 273: 274:
275: public function types()
276: {
277: return array_keys($this->_typeMap);
278: }
279:
280: 281: 282: 283: 284: 285:
286: protected function _getTypeId($type)
287: {
288: if (!isset($this->_typeMap[$type])) {
289: $this->_fault('type_not_exists');
290: }
291:
292: return $this->_typeMap[$type];
293: }
294:
295: 296: 297: 298: 299: 300: 301:
302: protected function _initProduct($productId, $identifierType = null)
303: {
304: $product = Mage::helper('catalog/product')->getProduct($productId, null, $identifierType);
305: if (!$product->getId()) {
306: $this->_fault('product_not_exists');
307: }
308:
309: return $product;
310: }
311:
312: 313: 314: 315: 316: 317: 318:
319: protected function _initCollection($link, $product)
320: {
321: $collection = $link
322: ->getProductCollection()
323: ->setIsStrongMode()
324: ->setProduct($product);
325:
326: return $collection;
327: }
328:
329: 330: 331: 332: 333: 334:
335: protected function _collectionToEditableArray($collection)
336: {
337: $result = array();
338:
339: foreach ($collection as $linkedProduct) {
340: $result[$linkedProduct->getId()] = array();
341:
342: foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
343: $result[$linkedProduct->getId()][$attribute['code']] = $linkedProduct->getData($attribute['code']);
344: }
345: }
346:
347: return $result;
348: }
349: }
350: