Public Member Functions | |
create ($productId, $data, $store=null) | |
update ($productId, $file, $data, $store=null) | |
Protected Member Functions | |
_prepareImageData ($data) |
Definition at line 34 of file V2.php.
_prepareImageData | ( | $ | data | ) | [protected] |
Prepare data to create or update image
stdClass | $data |
Definition at line 42 of file V2.php.
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 }
create | ( | $ | productId, | |
$ | data, | |||
$ | store = null | |||
) |
Create new image for product and return image filename
int|string | $productId | |
array | $data | |
string|int | $store |
Reimplemented from Mage_Catalog_Model_Product_Attribute_Media_Api.
Definition at line 68 of file V2.php.
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 // Create temporary directory for api 00101 $ioAdapter->checkAndCreateFolder($tmpDirectory); 00102 $ioAdapter->open(array('path'=>$tmpDirectory)); 00103 // Write image file 00104 $ioAdapter->write($fileName, $fileContent, 0666); 00105 unset($fileContent); 00106 00107 // Adding image to gallery 00108 $file = $gallery->getBackend()->addImage( 00109 $product, 00110 $tmpDirectory . DS . $fileName, 00111 null, 00112 true 00113 ); 00114 00115 // Remove temporary directory 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 }
update | ( | $ | productId, | |
$ | file, | |||
$ | data, | |||
$ | store = null | |||
) |
Update image data
int|string | $productId | |
string | $file | |
array | $data | |
string|int | $store |
Reimplemented from Mage_Catalog_Model_Product_Attribute_Media_Api.
Definition at line 145 of file V2.php.
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 }