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_Attribute_Media_Api extends Mage_Catalog_Model_Api_Resource
00035 {
00036
00037
00038
00039
00040 const ATTRIBUTE_CODE = 'media_gallery';
00041
00042
00043
00044
00045
00046
00047 protected $_mimeTypes = array(
00048 'image/jpeg' => 'jpg',
00049 'image/gif' => 'gif',
00050 'image/png' => 'png'
00051 );
00052
00053 public function __construct()
00054 {
00055 $this->_storeIdSessionField = 'product_store_id';
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065 public function items($productId, $store = null)
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 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 public function info($productId, $file, $store = null)
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 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 public function create($productId, $data, $store = null)
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
00148 $ioAdapter->checkAndCreateFolder($tmpDirectory);
00149 $ioAdapter->open(array('path'=>$tmpDirectory));
00150
00151 $ioAdapter->write($fileName, $fileContent, 0666);
00152 unset($fileContent);
00153
00154
00155 $file = $gallery->getBackend()->addImage(
00156 $product,
00157 $tmpDirectory . DS . $fileName,
00158 null,
00159 true
00160 );
00161
00162
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 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 public function update($productId, $file, $data, $store = null)
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 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 public function remove($productId, $file)
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 }
00255
00256
00257
00258
00259
00260
00261
00262
00263 public function types($setId)
00264 {
00265 $attributes = Mage::getModel('catalog/product')->getResource()
00266 ->loadAllAttributes()
00267 ->getSortedAttributes($setId);
00268
00269 $result = array();
00270
00271 foreach ($attributes as $attribute) {
00272
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 }
00292
00293
00294
00295
00296
00297
00298
00299 protected function _getGalleryAttribute($product)
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 }
00310
00311
00312
00313
00314
00315
00316
00317 protected function _getMediaConfig()
00318 {
00319 return Mage::getSingleton('catalog/product_media_config');
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329 protected function _imageToArray(&$image, $product)
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 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 protected function _initProduct($productId, $store = null)
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
00367
00368 $product->load($productId);
00369
00370 if (!$product->getId()) {
00371 $this->_fault('product_not_exists');
00372 }
00373
00374 return $product;
00375 }
00376 }