Mage_Catalog_Model_Product_Attribute_Media_Api Class Reference

Inheritance diagram for Mage_Catalog_Model_Product_Attribute_Media_Api:

Mage_Catalog_Model_Api_Resource Mage_Api_Model_Resource_Abstract Mage_Catalog_Model_Product_Attribute_Media_Api_V2

List of all members.

Public Member Functions

 __construct ()
 items ($productId, $store=null)
 info ($productId, $file, $store=null)
 create ($productId, $data, $store=null)
 update ($productId, $file, $data, $store=null)
 remove ($productId, $file)
 types ($setId)

Public Attributes

const ATTRIBUTE_CODE = 'media_gallery'

Protected Member Functions

 _getGalleryAttribute ($product)
 _getMediaConfig ()
 _imageToArray (&$image, $product)
 _initProduct ($productId, $store=null)

Protected Attributes

 $_mimeTypes


Detailed Description

Definition at line 34 of file Api.php.


Constructor & Destructor Documentation

__construct (  ) 

Definition at line 53 of file Api.php.

00054     {
00055         $this->_storeIdSessionField = 'product_store_id';
00056     }


Member Function Documentation

_getGalleryAttribute ( product  )  [protected]

Retrieve gallery attribute from product

Parameters:
Mage_Catalog_Model_Product $product
Mage_Catalog_Model_Resource_Eav_Mysql4_Attribute|boolean 

Definition at line 299 of file Api.php.

00300     {
00301         $attributes = $product->getTypeInstance(true)
00302             ->getSetAttributes($product);
00303 
00304         if (!isset($attributes[self::ATTRIBUTE_CODE])) {
00305             $this->_fault('not_media');
00306         }
00307 
00308         return $attributes[self::ATTRIBUTE_CODE];
00309     }

_getMediaConfig (  )  [protected]

Retrie ve media config

Returns:
Mage_Catalog_Model_Product_Media_Config

Definition at line 317 of file Api.php.

00318     {
00319         return Mage::getSingleton('catalog/product_media_config');
00320     }

_imageToArray ( &$  image,
product 
) [protected]

Converts image to api array data

Parameters:
array $image
Mage_Catalog_Model_Product $product
Returns:
array

Definition at line 329 of file Api.php.

00330     {
00331         $result = array(
00332             'file'      => $image['file'],
00333             'label'     => $image['label'],
00334             'position'  => $image['position'],
00335             'exclude'   => $image['disabled'],
00336             'url'       => $this->_getMediaConfig()->getMediaUrl($image['file']),
00337             'types'     => array()
00338         );
00339 
00340 
00341         foreach ($product->getMediaAttributes() as $attribute) {
00342             if ($product->getData($attribute->getAttributeCode()) == $image['file']) {
00343                 $result['types'][] = $attribute->getAttributeCode();
00344             }
00345         }
00346 
00347         return $result;
00348     }

_initProduct ( productId,
store = null 
) [protected]

Retrieve product

Parameters:
int|string $productId
string|int $store
Returns:
Mage_Catalog_Model_Product

Definition at line 357 of file Api.php.

00358     {
00359         $product = Mage::getModel('catalog/product')
00360                        ->setStoreId($this->_getStoreId($store));
00361 
00362         $idBySku = $product->getIdBySku($productId);
00363         if ($idBySku) {
00364             $productId = $idBySku;
00365         }
00366         /* @var $product Mage_Catalog_Model_Product */
00367 
00368         $product->load($productId);
00369 
00370         if (!$product->getId()) {
00371             $this->_fault('product_not_exists');
00372         }
00373 
00374         return $product;
00375     }

create ( productId,
data,
store = null 
)

Create new image for product and return image filename

Parameters:
int|string $productId
array $data
string|int $store
Returns:
string

Reimplemented in Mage_Catalog_Model_Product_Attribute_Media_Api_V2.

Definition at line 115 of file Api.php.

00116     {
00117         $product = $this->_initProduct($productId, $store);
00118 
00119         $gallery = $this->_getGalleryAttribute($product);
00120 
00121         if (!isset($data['file']) || !isset($data['file']['mime']) || !isset($data['file']['content'])) {
00122             $this->_fault('data_invalid', Mage::helper('catalog')->__('Image not specified.'));
00123         }
00124 
00125         if (!isset($this->_mimeTypes[$data['file']['mime']])) {
00126             $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
00127         }
00128 
00129         $fileContent = @base64_decode($data['file']['content'], true);
00130         if (!$fileContent) {
00131             $this->_fault('data_invalid', Mage::helper('catalog')->__('Image content is not valid base64 data.'));
00132         }
00133 
00134         unset($data['file']['content']);
00135 
00136         $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
00137 
00138         if (isset($data['file']['name']) && $data['file']['name']) {
00139             $fileName  = $data['file']['name'];
00140         } else {
00141             $fileName  = 'image';
00142         }
00143         $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
00144 
00145         $ioAdapter = new Varien_Io_File();
00146         try {
00147             // Create temporary directory for api
00148             $ioAdapter->checkAndCreateFolder($tmpDirectory);
00149             $ioAdapter->open(array('path'=>$tmpDirectory));
00150             // Write image file
00151             $ioAdapter->write($fileName, $fileContent, 0666);
00152             unset($fileContent);
00153 
00154             // Adding image to gallery
00155             $file = $gallery->getBackend()->addImage(
00156                 $product,
00157                 $tmpDirectory . DS . $fileName,
00158                 null,
00159                 true
00160             );
00161 
00162             // Remove temporary directory
00163             $ioAdapter->rmdir($tmpDirectory, true);
00164 
00165             $gallery->getBackend()->updateImage($product, $file, $data);
00166 
00167             if (isset($data['types'])) {
00168                 $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
00169             }
00170 
00171             $product->save();
00172         } catch (Mage_Core_Exception $e) {
00173             $this->_fault('not_created', $e->getMessage());
00174         } catch (Exception $e) {
00175             $this->_fault('not_created', Mage::helper('catalog')->__('Can\'t create image.'));
00176         }
00177 
00178         return $gallery->getBackend()->getRenamedImage($file);
00179     }

info ( productId,
file,
store = null 
)

Retrieve image data

Parameters:
int|string $productId
string $file
string|int $store
Returns:
array

Definition at line 94 of file Api.php.

00095     {
00096         $product = $this->_initProduct($productId, $store);
00097 
00098         $gallery = $this->_getGalleryAttribute($product);
00099 
00100         if (!$image = $gallery->getBackend()->getImage($product, $file)) {
00101             $this->_fault('not_exists');
00102         }
00103 
00104         return $this->_imageToArray($image, $product);
00105     }

items ( productId,
store = null 
)

Retrieve images for product

Parameters:
int|string $productId
string|int $store
Returns:
array

Definition at line 65 of file Api.php.

00066     {
00067         $product = $this->_initProduct($productId, $store);
00068 
00069         $gallery = $this->_getGalleryAttribute($product);
00070 
00071         $galleryData = $product->getData(self::ATTRIBUTE_CODE);
00072 
00073         if (!isset($galleryData['images']) || !is_array($galleryData['images'])) {
00074             return array();
00075         }
00076 
00077         $result = array();
00078 
00079         foreach ($galleryData['images'] as &$image) {
00080             $result[] = $this->_imageToArray($image, $product);
00081         }
00082 
00083         return $result;
00084     }

remove ( productId,
file 
)

Remove image from product

Parameters:
int|string $productId
string $file
Returns:
boolean

Definition at line 235 of file Api.php.

00236     {
00237         $product = $this->_initProduct($productId);
00238 
00239         $gallery = $this->_getGalleryAttribute($product);
00240 
00241         if (!$gallery->getBackend()->getImage($product, $file)) {
00242             $this->_fault('not_exists');
00243         }
00244 
00245         $gallery->getBackend()->removeImage($product, $file);
00246 
00247         try {
00248             $product->save();
00249         } catch (Mage_Core_Exception $e) {
00250             $this->_fault('not_removed', $e->getMessage());
00251         }
00252 
00253         return true;
00254     }

types ( setId  ) 

Retrieve image types (image, small_image, thumbnail, etc...)

Parameters:
int $setId
Returns:
array

Definition at line 263 of file Api.php.

00264     {
00265         $attributes = Mage::getModel('catalog/product')->getResource()
00266                 ->loadAllAttributes()
00267                 ->getSortedAttributes($setId);
00268 
00269         $result = array();
00270 
00271         foreach ($attributes as $attribute) {
00272             /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
00273             if ($attribute->isInSet($setId)
00274                 && $attribute->getFrontendInput() == 'media_image') {
00275                 if ($attribute->isScopeGlobal()) {
00276                     $scope = 'global';
00277                 } elseif ($attribute->isScopeWebsite()) {
00278                     $scope = 'website';
00279                 } else {
00280                     $scope = 'store';
00281                 }
00282 
00283                 $result[] = array(
00284                     'code'         => $attribute->getAttributeCode(),
00285                     'scope'        => $scope
00286                 );
00287             }
00288         }
00289 
00290         return $result;
00291     }

update ( productId,
file,
data,
store = null 
)

Update image data

Parameters:
int|string $productId
string $file
array $data
string|int $store
Returns:
boolean

Reimplemented in Mage_Catalog_Model_Product_Attribute_Media_Api_V2.

Definition at line 190 of file Api.php.

00191     {
00192         $product = $this->_initProduct($productId, $store);
00193 
00194         $gallery = $this->_getGalleryAttribute($product);
00195 
00196         if (!$gallery->getBackend()->getImage($product, $file)) {
00197             $this->_fault('not_exists');
00198         }
00199 
00200         $gallery->getBackend()->updateImage($product, $file, $data);
00201 
00202         if (isset($data['types']) && is_array($data['types'])) {
00203             $oldTypes = array();
00204             foreach ($product->getMediaAttributes() as $attribute) {
00205                 if ($product->getData($attribute->getAttributeCode()) == $file) {
00206                      $oldTypes[] = $attribute->getAttributeCode();
00207                 }
00208             }
00209 
00210             $clear = array_diff($oldTypes, $data['types']);
00211 
00212             if (count($clear) > 0) {
00213                 $gallery->getBackend()->clearMediaAttribute($product, $clear);
00214             }
00215 
00216             $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
00217         }
00218 
00219         try {
00220             $product->save();
00221         } catch (Mage_Core_Exception $e) {
00222             $this->_fault('not_updated', $e->getMessage());
00223         }
00224 
00225         return true;
00226     }


Member Data Documentation

$_mimeTypes [protected]

Initial value:

 array(
        'image/jpeg' => 'jpg',
        'image/gif'  => 'gif',
        'image/png'  => 'png'
    )

Definition at line 47 of file Api.php.

const ATTRIBUTE_CODE = 'media_gallery'

Attribute code for media gallery

Definition at line 40 of file Api.php.


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

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