00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Catalog_Model_Product_Link_Api extends Mage_Catalog_Model_Api_Resource
00035 {
00036 protected $_typeMap = array(
00037 'related' => Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED,
00038 'up_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL,
00039 'cross_sell' => Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL,
00040 'grouped' => Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED
00041 );
00042
00043 public function __construct()
00044 {
00045 $this->_storeIdSessionField = 'product_store_id';
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055 public function items($type, $productId)
00056 {
00057 $typeId = $this->_getTypeId($type);
00058
00059 $product = $this->_initProduct($productId);
00060
00061 $link = $product->getLinkInstance()
00062 ->setLinkTypeId($typeId);
00063
00064 $collection = $this->_initCollection($link, $product);
00065
00066 $result = array();
00067
00068 foreach ($collection as $linkedProduct) {
00069 $row = array(
00070 'product_id' => $linkedProduct->getId(),
00071 'type' => $linkedProduct->getTypeId(),
00072 'set' => $linkedProduct->getAttributeSetId(),
00073 'sku' => $linkedProduct->getSku()
00074 );
00075
00076 foreach ($link->getAttributes() as $attribute) {
00077 $row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
00078 }
00079
00080 $result[] = $row;
00081 }
00082
00083 return $result;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 public function assign($type, $productId, $linkedProductId, $data = array())
00096 {
00097 $typeId = $this->_getTypeId($type);
00098
00099 $product = $this->_initProduct($productId);
00100
00101 $link = $product->getLinkInstance()
00102 ->setLinkTypeId($typeId);
00103
00104 $collection = $this->_initCollection($link, $product);
00105 $idBySku = $product->getIdBySku($linkedProductId);
00106 if ($idBySku) {
00107 $linkedProductId = $idBySku;
00108 }
00109
00110 $links = $this->_collectionToEditableArray($collection);
00111
00112 $links[(int)$linkedProductId] = array();
00113
00114 foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
00115 if (isset($data[$attribute['code']])) {
00116 $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
00117 }
00118 }
00119
00120 try {
00121 $link->getResource()->saveProductLinks($product, $links, $typeId);
00122 } catch (Exception $e) {
00123 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.'));
00124 }
00125
00126 return true;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 public function update($type, $productId, $linkedProductId, $data = array())
00139 {
00140 $typeId = $this->_getTypeId($type);
00141
00142 $product = $this->_initProduct($productId);
00143
00144 $link = $product->getLinkInstance()
00145 ->setLinkTypeId($typeId);
00146
00147 $collection = $this->_initCollection($link, $product);
00148
00149 $links = $this->_collectionToEditableArray($collection);
00150
00151 $idBySku = $product->getIdBySku($linkedProductId);
00152 if ($idBySku) {
00153 $linkedProductId = $idBySku;
00154 }
00155
00156 foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
00157 if (isset($data[$attribute['code']])) {
00158 $links[(int)$linkedProductId][$attribute['code']] = $data[$attribute['code']];
00159 }
00160 }
00161
00162 try {
00163 $link->getResource()->saveProductLinks($product, $links, $typeId);
00164 } catch (Exception $e) {
00165 $this->_fault('data_invalid', Mage::helper('catalog')->__('Link product not exists.'));
00166 }
00167
00168 return true;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 public function remove($type, $productId, $linkedProductId)
00180 {
00181 $typeId = $this->_getTypeId($type);
00182
00183 $product = $this->_initProduct($productId);
00184
00185 $link = $product->getLinkInstance()
00186 ->setLinkTypeId($typeId);
00187
00188 $collection = $this->_initCollection($link, $product);
00189
00190 $idBySku = $product->getIdBySku($linkedProductId);
00191 if ($idBySku) {
00192 $linkedProductId = $idBySku;
00193 }
00194
00195 $links = $this->_collectionToEditableArray($collection);
00196
00197 if (isset($links[$linkedProductId])) {
00198 unset($links[$linkedProductId]);
00199 }
00200
00201 try {
00202 $link->getResource()->saveProductLinks($product, $links, $typeId);
00203 } catch (Exception $e) {
00204 $this->_fault('not_removed');
00205 }
00206
00207 return true;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216 public function attributes($type)
00217 {
00218 $typeId = $this->_getTypeId($type);
00219
00220 $attributes = Mage::getModel('catalog/product_link')
00221 ->getAttributes($typeId);
00222
00223 $result = array();
00224
00225 foreach ($attributes as $attribute) {
00226 $result[] = array(
00227 'code' => $attribute['code'],
00228 'type' => $attribute['type']
00229 );
00230 }
00231
00232 return $result;
00233 }
00234
00235
00236
00237
00238
00239
00240 public function types()
00241 {
00242 return array_keys($this->_typeMap);
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 protected function _getTypeId($type)
00252 {
00253 if (!isset($this->_typeMap[$type])) {
00254 $this->_fault('type_not_exists');
00255 }
00256
00257 return $this->_typeMap[$type];
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 protected function _initProduct($productId)
00267 {
00268
00269
00270 $product = Mage::getModel('catalog/product')
00271 ->setStoreId($this->_getStoreId());
00272
00273 $idBySku = $product->getIdBySku($productId);
00274 if ($idBySku) {
00275 $productId = $idBySku;
00276 }
00277
00278 $product->load($productId);
00279
00280 if (!$product->getId()) {
00281 $this->_fault('product_not_exists');
00282 }
00283
00284 return $product;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 protected function _initCollection($link, $product)
00295 {
00296 $collection = $link
00297 ->getProductCollection()
00298 ->setIsStrongMode()
00299 ->setProduct($product);
00300
00301 return $collection;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 protected function _collectionToEditableArray($collection)
00311 {
00312 $result = array();
00313
00314 foreach ($collection as $linkedProduct) {
00315 $result[$linkedProduct->getId()] = array();
00316
00317 foreach ($collection->getLinkModel()->getAttributes() as $attribute) {
00318 $result[$linkedProduct->getId()][$attribute['code']] = $linkedProduct->getData($attribute['code']);
00319 }
00320 }
00321
00322 return $result;
00323 }
00324 }