Public Member Functions | |
afterLoad ($object) | |
beforeSave ($object) | |
getRenamedImage ($file) | |
afterSave ($object) | |
addImage (Mage_Catalog_Model_Product $product, $file, $mediaAttribute=null, $move=false, $exclude=true) | |
updateImage (Mage_Catalog_Model_Product $product, $file, $data) | |
removeImage (Mage_Catalog_Model_Product $product, $file) | |
getImage (Mage_Catalog_Model_Product $product, $file) | |
clearMediaAttribute (Mage_Catalog_Model_Product $product, $mediaAttribute) | |
setMediaAttribute (Mage_Catalog_Model_Product $product, $mediaAttribute, $value) | |
duplicate ($object) | |
Protected Member Functions | |
_getDefaultValue ($key, &$image) | |
_getResource () | |
_getConfig () | |
_moveImageFromTmp ($file) | |
_copyImage ($file) | |
Protected Attributes | |
$_renamedImages = array() |
Definition at line 34 of file Media.php.
_copyImage | ( | $ | file | ) | [protected] |
Copy image and return new filename.
string | $file |
Definition at line 488 of file Media.php.
00489 { 00490 try { 00491 $ioObject = new Varien_Io_File(); 00492 $destDirectory = dirname($this->_getConfig()->getMediaPath($file)); 00493 $ioObject->open(array('path'=>$destDirectory)); 00494 $destFile = dirname($file) . $ioObject->dirsep() 00495 . Varien_File_Uploader::getNewFileName($this->_getConfig()->getMediaPath($file)); 00496 00497 if (!$ioObject->fileExists($this->_getConfig()->getMediaPath($file),true)) { 00498 throw new Exception(); 00499 } 00500 $ioObject->cp( 00501 $this->_getConfig()->getMediaPath($file), 00502 $this->_getConfig()->getMediaPath($destFile) 00503 ); 00504 } catch (Exception $e) { 00505 Mage::throwException( 00506 Mage::helper('catalog')->__('Failed to copy file %s. Please, delete media with non-existing images and try again.', 00507 $this->_getConfig()->getMediaPath($file)) 00508 ); 00509 } 00510 00511 return str_replace($ioObject->dirsep(), '/', $destFile); 00512 }
_getConfig | ( | ) | [protected] |
Retrive media config
Definition at line 445 of file Media.php.
00446 { 00447 return Mage::getSingleton('catalog/product_media_config'); 00448 }
_getDefaultValue | ( | $ | key, | |
&$ | image | |||
) | [protected] |
_getResource | ( | ) | [protected] |
Retrieve resource model
Definition at line 435 of file Media.php.
00436 { 00437 return Mage::getResourceSingleton('catalog/product_attribute_backend_media'); 00438 }
_moveImageFromTmp | ( | $ | file | ) | [protected] |
Move image from temporary directory to normal
string | $file |
Definition at line 456 of file Media.php.
00457 { 00458 $ioObject = new Varien_Io_File(); 00459 $destDirectory = dirname($this->_getConfig()->getMediaPath($file)); 00460 try { 00461 $ioObject->open(array('path'=>$destDirectory)); 00462 } catch (Exception $e) { 00463 $ioObject->mkdir($destDirectory, 0777, true); 00464 $ioObject->open(array('path'=>$destDirectory)); 00465 } 00466 00467 if (strrpos($file, '.tmp') == strlen($file)-4) { 00468 $file = substr($file, 0, strlen($file)-4); 00469 } 00470 00471 $destFile = dirname($file) . $ioObject->dirsep() 00472 . Varien_File_Uploader::getNewFileName($this->_getConfig()->getMediaPath($file)); 00473 00474 $ioObject->mv( 00475 $this->_getConfig()->getTmpMediaPath($file), 00476 $this->_getConfig()->getMediaPath($destFile) 00477 ); 00478 00479 return str_replace($ioObject->dirsep(), '/', $destFile); 00480 }
addImage | ( | Mage_Catalog_Model_Product $ | product, | |
$ | file, | |||
$ | mediaAttribute = null , |
|||
$ | move = false , |
|||
$ | exclude = true | |||
) |
Add image to media gallery and return new filename
Mage_Catalog_Model_Product | $product | |
string | $file file path of image in file system | |
string|array | $mediaAttribute code of attribute with type 'media_image', leave blank if image should be only in gallery | |
boolean | $move if true, it will move source file | |
boolean | $exclude mark image as disabled in product page view |
Definition at line 216 of file Media.php.
00217 { 00218 $file = realpath($file); 00219 00220 if (!$file || !file_exists($file)) { 00221 Mage::throwException(Mage::helper('catalog')->__('Image not exists')); 00222 } 00223 $pathinfo = pathinfo($file); 00224 if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), array('jpg','jpeg','gif','png'))) { 00225 Mage::throwException(Mage::helper('catalog')->__('Invalid image file type')); 00226 } 00227 00228 $fileName = Varien_File_Uploader::getCorrectFileName($pathinfo['basename']); 00229 $dispretionPath = Varien_File_Uploader::getDispretionPath($fileName); 00230 $fileName = $dispretionPath . DS . $fileName; 00231 00232 $fileName = $dispretionPath . DS 00233 . Varien_File_Uploader::getNewFileName($this->_getConfig()->getTmpMediaPath($fileName)); 00234 00235 $ioAdapter = new Varien_Io_File(); 00236 $ioAdapter->setAllowCreateFolders(true); 00237 $distanationDirectory = dirname($this->_getConfig()->getTmpMediaPath($fileName)); 00238 00239 try { 00240 $ioAdapter->open(array( 00241 'path'=>$distanationDirectory 00242 )); 00243 00244 if ($move) { 00245 $ioAdapter->mv($file, $this->_getConfig()->getTmpMediaPath($fileName)); 00246 } else { 00247 $ioAdapter->cp($file, $this->_getConfig()->getTmpMediaPath($fileName)); 00248 $ioAdapter->chmod($this->_getConfig()->getTmpMediaPath($fileName), 0777); 00249 } 00250 } 00251 catch (Exception $e) { 00252 Mage::throwException(Mage::helper('catalog')->__('Failed to move file: %s', $e->getMessage())); 00253 } 00254 00255 $fileName = str_replace(DS, '/', $fileName); 00256 00257 $attrCode = $this->getAttribute()->getAttributeCode(); 00258 $mediaGalleryData = $product->getData($attrCode); 00259 $position = 0; 00260 if (!is_array($mediaGalleryData)) { 00261 $mediaGalleryData = array( 00262 'images' => array() 00263 ); 00264 } 00265 00266 foreach ($mediaGalleryData['images'] as &$image) { 00267 if (isset($image['position']) && $image['position'] > $position) { 00268 $position = $image['position']; 00269 } 00270 } 00271 00272 $position++; 00273 $mediaGalleryData['images'][] = array( 00274 'file' => $fileName, 00275 'position' => $position, 00276 'label' => '', 00277 'disabled' => (int) $exclude 00278 ); 00279 00280 $product->setData($attrCode, $mediaGalleryData); 00281 00282 if (!is_null($mediaAttribute)) { 00283 $this->setMediaAttribute($product, $mediaAttribute, $fileName); 00284 } 00285 00286 return $fileName; 00287 }
afterLoad | ( | $ | object | ) |
Load attribute data after product loaded
Mage_Catalog_Model_Product | $object |
Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.
Definition at line 43 of file Media.php.
00044 { 00045 $attrCode = $this->getAttribute()->getAttributeCode(); 00046 $value = array(); 00047 $value['images'] = array(); 00048 $value['values'] = array(); 00049 $localAttributes = array('label', 'position', 'disabled'); 00050 00051 foreach ($this->_getResource()->loadGallery($object, $this) as $image) { 00052 foreach ($localAttributes as $localAttribute) { 00053 if (is_null($image[$localAttribute])) { 00054 $image[$localAttribute] = $this->_getDefaultValue($localAttribute, $image); 00055 } 00056 } 00057 $value['images'][] = $image; 00058 } 00059 00060 $object->setData($attrCode, $value); 00061 }
afterSave | ( | $ | object | ) |
Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.
Definition at line 159 of file Media.php.
00160 { 00161 if ($object->getIsDuplicate() == true) { 00162 $this->duplicate($object); 00163 return; 00164 } 00165 00166 $attrCode = $this->getAttribute()->getAttributeCode(); 00167 $value = $object->getData($attrCode); 00168 if (!is_array($value) || !isset($value['images']) || $object->isLockedAttribute($attrCode)) { 00169 return; 00170 } 00171 $toDelete = array(); 00172 $filesToValueIds = array(); 00173 foreach ($value['images'] as &$image) { 00174 if(!empty($image['removed'])) { 00175 if(isset($image['value_id'])) { 00176 $toDelete[] = $image['value_id']; 00177 } 00178 continue; 00179 } 00180 00181 if(!isset($image['value_id'])) { 00182 $data = array(); 00183 $data['entity_id'] = $object->getId(); 00184 $data['attribute_id'] = $this->getAttribute()->getId(); 00185 $data['value'] = $image['file']; 00186 $image['value_id'] = $this->_getResource()->insertGallery($data); 00187 } 00188 00189 $this->_getResource()->deleteGalleryValueInStore($image['value_id'], $object->getStoreId()); 00190 00191 // Add per store labels, position, disabled 00192 $data = array(); 00193 $data['value_id'] = $image['value_id']; 00194 $data['label'] = $image['label']; 00195 $data['position'] = (int) $image['position']; 00196 $data['disabled'] = (int) $image['disabled']; 00197 $data['store_id'] = (int) $object->getStoreId(); 00198 00199 $this->_getResource()->insertGalleryValueInStore($data); 00200 } 00201 00202 $this->_getResource()->deleteGallery($toDelete); 00203 }
beforeSave | ( | $ | object | ) |
Reimplemented from Mage_Eav_Model_Entity_Attribute_Backend_Abstract.
Definition at line 72 of file Media.php.
00073 { 00074 $attrCode = $this->getAttribute()->getAttributeCode(); 00075 $value = $object->getData($attrCode); 00076 if (!is_array($value) || !isset($value['images'])) { 00077 return; 00078 } 00079 00080 if(!is_array($value['images']) && strlen($value['images']) > 0) { 00081 $value['images'] = Zend_Json::decode($value['images']); 00082 } 00083 00084 if (!is_array($value['images'])) { 00085 $value['images'] = array(); 00086 } 00087 00088 00089 00090 $clearImages = array(); 00091 $newImages = array(); 00092 $existImages = array(); 00093 if ($object->getIsDuplicate()!=true) { 00094 foreach ($value['images'] as &$image) { 00095 if(!empty($image['removed'])) { 00096 $clearImages[] = $image['file']; 00097 } else if (!isset($image['value_id'])) { 00098 $newFile = $this->_moveImageFromTmp($image['file']); 00099 $image['new_file'] = $newFile; 00100 $newImages[$image['file']] = $image; 00101 $this->_renamedImages[$image['file']] = $newFile; 00102 $image['file'] = $newFile; 00103 } else { 00104 $existImages[$image['file']] = $image; 00105 } 00106 } 00107 } else { 00108 // For duplicating we need copy original images. 00109 $duplicate = array(); 00110 foreach ($value['images'] as &$image) { 00111 if (!isset($image['value_id'])) { 00112 continue; 00113 } 00114 $duplicate[$image['value_id']] = $this->_copyImage($image['file']); 00115 $newImages[$image['file']] = $duplicate[$image['value_id']]; 00116 } 00117 00118 $value['duplicate'] = $duplicate; 00119 } 00120 00121 foreach ($object->getMediaAttributes() as $mediaAttribute) { 00122 $mediaAttrCode = $mediaAttribute->getAttributeCode(); 00123 $attrData = $object->getData($mediaAttrCode); 00124 00125 if (in_array($attrData, $clearImages)) { 00126 $object->setData($mediaAttrCode, null); 00127 } 00128 00129 if (in_array($attrData, array_keys($newImages))) { 00130 $object->setData($mediaAttrCode, $newImages[$attrData]['new_file']); 00131 $object->setData($mediaAttrCode.'_label', $newImages[$attrData]['label']); 00132 } 00133 00134 if (in_array($attrData, array_keys($existImages))) { 00135 $object->setData($mediaAttrCode.'_label', $existImages[$attrData]['label']); 00136 } 00137 } 00138 00139 $object->setData($attrCode, $value); 00140 00141 return $this; 00142 }
clearMediaAttribute | ( | Mage_Catalog_Model_Product $ | product, | |
$ | mediaAttribute | |||
) |
Clear media attribute value
Mage_Catalog_Model_Product | $product | |
string|array | $mediaAttribute |
Definition at line 387 of file Media.php.
00388 { 00389 $mediaAttributeCodes = array_keys($product->getMediaAttributes()); 00390 00391 if (is_array($mediaAttribute)) { 00392 foreach ($mediaAttribute as $atttribute) { 00393 if (in_array($atttribute, $mediaAttributeCodes)) { 00394 $product->setData($atttribute, null); 00395 } 00396 } 00397 } elseif (in_array($mediaAttribute, $mediaAttributeCodes)) { 00398 $product->setData($mediaAttribute, null); 00399 00400 } 00401 00402 return $this; 00403 }
duplicate | ( | $ | object | ) |
Definition at line 514 of file Media.php.
00515 { 00516 $attrCode = $this->getAttribute()->getAttributeCode(); 00517 $mediaGalleryData = $object->getData($attrCode); 00518 00519 if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { 00520 return $this; 00521 } 00522 00523 $this->_getResource()->duplicate( 00524 $this, 00525 (isset($mediaGalleryData['duplicate']) ? $mediaGalleryData['duplicate'] : array()), 00526 $object->getOriginalId(), 00527 $object->getId() 00528 ); 00529 00530 return $this; 00531 }
getImage | ( | Mage_Catalog_Model_Product $ | product, | |
$ | file | |||
) |
Retrive image from gallery
Mage_Catalog_Model_Product | $product | |
string | $file |
Definition at line 363 of file Media.php.
00364 { 00365 $attrCode = $this->getAttribute()->getAttributeCode(); 00366 $mediaGalleryData = $product->getData($attrCode); 00367 if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { 00368 return false; 00369 } 00370 00371 foreach ($mediaGalleryData['images'] as $image) { 00372 if ($image['file'] == $file) { 00373 return $image; 00374 } 00375 } 00376 00377 return false; 00378 }
getRenamedImage | ( | $ | file | ) |
removeImage | ( | Mage_Catalog_Model_Product $ | product, | |
$ | file | |||
) |
Remove image from gallery
Mage_Catalog_Model_Product | $product | |
string | $file |
Definition at line 335 of file Media.php.
00336 { 00337 $attrCode = $this->getAttribute()->getAttributeCode(); 00338 00339 $mediaGalleryData = $product->getData($attrCode); 00340 00341 if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { 00342 return $this; 00343 } 00344 00345 foreach ($mediaGalleryData['images'] as &$image) { 00346 if ($image['file'] == $file) { 00347 $image['removed'] = 1; 00348 } 00349 } 00350 00351 $product->setData($attrCode, $mediaGalleryData); 00352 00353 return $this; 00354 }
setMediaAttribute | ( | Mage_Catalog_Model_Product $ | product, | |
$ | mediaAttribute, | |||
$ | value | |||
) |
Set media attribute value
Mage_Catalog_Model_Product | $product | |
string|array | $mediaAttribute | |
string | $value |
Definition at line 413 of file Media.php.
00414 { 00415 $mediaAttributeCodes = array_keys($product->getMediaAttributes()); 00416 00417 if (is_array($mediaAttribute)) { 00418 foreach ($mediaAttribute as $atttribute) { 00419 if (in_array($atttribute, $mediaAttributeCodes)) { 00420 $product->setData($atttribute, $value); 00421 } 00422 } 00423 } elseif (in_array($mediaAttribute, $mediaAttributeCodes)) { 00424 $product->setData($mediaAttribute, $value); 00425 } 00426 00427 return $this; 00428 }
updateImage | ( | Mage_Catalog_Model_Product $ | product, | |
$ | file, | |||
$ | data | |||
) |
Update image in gallery
Mage_Catalog_Model_Product | $product | |
sting | $file | |
array | $data |
Definition at line 297 of file Media.php.
00298 { 00299 $fieldsMap = array( 00300 'label' => 'label', 00301 'position' => 'position', 00302 'disabled' => 'disabled', 00303 'exclude' => 'disabled' 00304 ); 00305 00306 $attrCode = $this->getAttribute()->getAttributeCode(); 00307 00308 $mediaGalleryData = $product->getData($attrCode); 00309 00310 if (!isset($mediaGalleryData['images']) || !is_array($mediaGalleryData['images'])) { 00311 return $this; 00312 } 00313 00314 foreach ($mediaGalleryData['images'] as &$image) { 00315 if ($image['file'] == $file) { 00316 foreach ($fieldsMap as $mappedField=>$realField) { 00317 if (isset($data[$mappedField])) { 00318 $image[$realField] = $data[$mappedField]; 00319 } 00320 } 00321 } 00322 } 00323 00324 $product->setData($attrCode, $mediaGalleryData); 00325 return $this; 00326 }