Mage_Catalog_Model_Product_Link_Api Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Link_Api:

Mage_Catalog_Model_Api_Resource Mage_Api_Model_Resource_Abstract Mage_Catalog_Model_Product_Link_Api_V2

List of all members.

Public Member Functions

 __construct ()
 items ($type, $productId)
 assign ($type, $productId, $linkedProductId, $data=array())
 update ($type, $productId, $linkedProductId, $data=array())
 remove ($type, $productId, $linkedProductId)
 attributes ($type)
 types ()

Protected Member Functions

 _getTypeId ($type)
 _initProduct ($productId)
 _initCollection ($link, $product)
 _collectionToEditableArray ($collection)

Protected Attributes

 $_typeMap


Detailed Description

Definition at line 34 of file Api.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 43 of file Api.php.

00044     {
00045         $this->_storeIdSessionField = 'product_store_id';
00046     }


Member Function Documentation

_collectionToEditableArray ( collection  )  [protected]

Export collection to editable array

Parameters:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection $collection
Returns:
array

Definition at line 310 of file Api.php.

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     }

_getTypeId ( type  )  [protected]

Retrieve link type id by code

Parameters:
string $type
Returns:
int

Definition at line 251 of file Api.php.

00252     {
00253         if (!isset($this->_typeMap[$type])) {
00254             $this->_fault('type_not_exists');
00255         }
00256 
00257         return $this->_typeMap[$type];
00258     }

_initCollection ( link,
product 
) [protected]

Initialize and return linked products collection

Parameters:
Mage_Catalog_Model_Product_Link $link
Mage_Catalog_Model_Product $product
Returns:
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection

Definition at line 294 of file Api.php.

00295     {
00296         $collection = $link
00297             ->getProductCollection()
00298             ->setIsStrongMode()
00299             ->setProduct($product);
00300 
00301         return $collection;
00302     }

_initProduct ( productId  )  [protected]

Initilize and return product model

Parameters:
int $productId
Returns:
Mage_Catalog_Model_Product

Definition at line 266 of file Api.php.

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     }

assign ( type,
productId,
linkedProductId,
data = array() 
)

Add product link association

Parameters:
string $type
int|string $productId
int|string $linkedProductId
array $data
Returns:
boolean

Reimplemented in Mage_Catalog_Model_Product_Link_Api_V2.

Definition at line 95 of file Api.php.

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     }

attributes ( type  ) 

Retrieve attribute list for specified type

Parameters:
string $type
Returns:
array

Definition at line 216 of file Api.php.

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     }

items ( type,
productId 
)

Retrieve product link associations

Parameters:
string $type
int|sku $productId
Returns:
array

Definition at line 55 of file Api.php.

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     }

remove ( type,
productId,
linkedProductId 
)

Remove product link association

Parameters:
string $type
int|string $productId
int|string $linkedProductId
Returns:
boolean

Definition at line 179 of file Api.php.

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     }

types (  ) 

Retrieve link types

Returns:
array

Definition at line 240 of file Api.php.

00241     {
00242         return array_keys($this->_typeMap);
00243     }

update ( type,
productId,
linkedProductId,
data = array() 
)

Update product link association info

Parameters:
string $type
int|string $productId
int|string $linkedProductId
array $data
Returns:
boolean

Reimplemented in Mage_Catalog_Model_Product_Link_Api_V2.

Definition at line 138 of file Api.php.

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     }


Member Data Documentation

$_typeMap [protected]


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:23:41 2009 for Magento by  doxygen 1.5.8