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_Backend_Media extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
00035 {
00036 protected $_renamedImages = array();
00037
00038
00039
00040
00041
00042
00043 public function afterLoad($object)
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 }
00062
00063 protected function _getDefaultValue($key, &$image)
00064 {
00065 if (isset($image[$key . '_default'])) {
00066 return $image[$key . '_default'];
00067 }
00068
00069 return '';
00070 }
00071
00072 public function beforeSave($object)
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
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 }
00143
00144
00145
00146
00147
00148
00149
00150 public function getRenamedImage($file)
00151 {
00152 if (isset($this->_renamedImages[$file])) {
00153 return $this->_renamedImages[$file];
00154 }
00155
00156 return $file;
00157 }
00158
00159 public function afterSave($object)
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
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 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 public function addImage(Mage_Catalog_Model_Product $product, $file, $mediaAttribute=null, $move=false, $exclude=true)
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 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 public function updateImage(Mage_Catalog_Model_Product $product, $file, $data)
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 }
00327
00328
00329
00330
00331
00332
00333
00334
00335 public function removeImage(Mage_Catalog_Model_Product $product, $file)
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 }
00355
00356
00357
00358
00359
00360
00361
00362
00363 public function getImage(Mage_Catalog_Model_Product $product, $file)
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 }
00379
00380
00381
00382
00383
00384
00385
00386
00387 public function clearMediaAttribute(Mage_Catalog_Model_Product $product, $mediaAttribute)
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 }
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 public function setMediaAttribute(Mage_Catalog_Model_Product $product, $mediaAttribute, $value)
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 }
00429
00430
00431
00432
00433
00434
00435 protected function _getResource()
00436 {
00437 return Mage::getResourceSingleton('catalog/product_attribute_backend_media');
00438 }
00439
00440
00441
00442
00443
00444
00445 protected function _getConfig()
00446 {
00447 return Mage::getSingleton('catalog/product_media_config');
00448 }
00449
00450
00451
00452
00453
00454
00455
00456 protected function _moveImageFromTmp($file)
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 }
00481
00482
00483
00484
00485
00486
00487
00488 protected function _copyImage($file)
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 }
00513
00514 public function duplicate($object)
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 }
00532 }