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_V2 extends Mage_Catalog_Model_Product_Attribute_Media_Api
00035 {
00036
00037
00038
00039
00040
00041
00042 protected function _prepareImageData($data)
00043 {
00044 $_imageData = array();
00045 if (isset($data->label)) {
00046 $_imageData['label'] = $data->label;
00047 }
00048 if (isset($data->position)) {
00049 $_imageData['position'] = $data->position;
00050 }
00051 if (isset($data->disabled)) {
00052 $_imageData['disabled'] = $data->disabled;
00053 }
00054 if (isset($data->exclude)) {
00055 $_imageData['exclude'] = $data->exclude;
00056 }
00057 return $_imageData;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 public function create($productId, $data, $store = null)
00069 {
00070 $product = $this->_initProduct($productId, $store);
00071
00072 $gallery = $this->_getGalleryAttribute($product);
00073
00074 if (!isset($data->file) || !isset($data->file->mime) || !isset($data->file->content)) {
00075 $this->_fault('data_invalid', Mage::helper('catalog')->__('Image not specified.'));
00076 }
00077
00078 if (!isset($this->_mimeTypes[$data->file->mime])) {
00079 $this->_fault('data_invalid', Mage::helper('catalog')->__('Invalid image type.'));
00080 }
00081
00082 $fileContent = @base64_decode($data->file->content, true);
00083 if (!$fileContent) {
00084 $this->_fault('data_invalid', Mage::helper('catalog')->__('Image content is not valid base64 data.'));
00085 }
00086
00087 unset($data->file->content);
00088
00089 $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $this->_getSession()->getSessionId();
00090
00091 if (isset($data['file']['name']) && $data['file']['name']) {
00092 $fileName = $data['file']['name'];
00093 } else {
00094 $fileName = 'image';
00095 }
00096 $fileName .= '.' . $this->_mimeTypes[$data['file']['mime']];
00097
00098 $ioAdapter = new Varien_Io_File();
00099 try {
00100
00101 $ioAdapter->checkAndCreateFolder($tmpDirectory);
00102 $ioAdapter->open(array('path'=>$tmpDirectory));
00103
00104 $ioAdapter->write($fileName, $fileContent, 0666);
00105 unset($fileContent);
00106
00107
00108 $file = $gallery->getBackend()->addImage(
00109 $product,
00110 $tmpDirectory . DS . $fileName,
00111 null,
00112 true
00113 );
00114
00115
00116 $ioAdapter->rmdir($tmpDirectory, true);
00117
00118 $_imageData = $this->_prepareImageData($data);
00119
00120 $gallery->getBackend()->updateImage($product, $file, $_imageData);
00121
00122 if (isset($data->types)) {
00123 $gallery->getBackend()->setMediaAttribute($product, $data->types, $file);
00124 }
00125
00126 $product->save();
00127 } catch (Mage_Core_Exception $e) {
00128 $this->_fault('not_created', $e->getMessage());
00129 } catch (Exception $e) {
00130 $this->_fault('not_created', Mage::helper('catalog')->__('Can\'t create image.'));
00131 }
00132
00133 return $gallery->getBackend()->getRenamedImage($file);
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 public function update($productId, $file, $data, $store = null)
00146 {
00147 $product = $this->_initProduct($productId, $store);
00148
00149 $gallery = $this->_getGalleryAttribute($product);
00150
00151 if (!$gallery->getBackend()->getImage($product, $file)) {
00152 $this->_fault('not_exists');
00153 }
00154
00155 $_imageData = $this->_prepareImageData($data);
00156
00157 $gallery->getBackend()->updateImage($product, $file, $_imageData);
00158
00159 if (isset($data->types) && is_array($data->types)) {
00160 $oldTypes = array();
00161 foreach ($product->getMediaAttributes() as $attribute) {
00162 if ($product->getData($attribute->getAttributeCode()) == $file) {
00163 $oldTypes[] = $attribute->getAttributeCode();
00164 }
00165 }
00166
00167 $clear = array_diff($oldTypes, $data->types);
00168
00169 if (count($clear) > 0) {
00170 $gallery->getBackend()->clearMediaAttribute($product, $clear);
00171 }
00172
00173 $gallery->getBackend()->setMediaAttribute($product, $data->types, $file);
00174 }
00175
00176 try {
00177 $product->save();
00178 } catch (Mage_Core_Exception $e) {
00179 $this->_fault('not_updated', $e->getMessage());
00180 }
00181
00182 return true;
00183 }
00184 }