Example #1
0
 /**
  * Load gallery images for product
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  * @return array
  */
 public function loadGallery($product, $object)
 {
     $adapter = $this->_getReadAdapter();
     $positionCheckSql = $adapter->getCheckSql('value.position IS NULL', 'default_value.position', 'value.position');
     // Select gallery images for product
     $select = $adapter->select()->from(array('main' => $this->getMainTable()), array('value_id', 'value AS file'))->joinLeft(array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)), $adapter->quoteInto('main.value_id = value.value_id AND value.store_id = ?', (int) $product->getStoreId()), array('label', 'position', 'disabled'))->joinLeft(array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)), 'main.value_id = default_value.value_id AND default_value.store_id = 0', array('label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled', 'associated_attributes_default' => 'associated_attributes'))->where('main.attribute_id = ?', $object->getAttribute()->getId())->where('main.entity_id = ?', $product->getId())->order($positionCheckSql . ' ' . Varien_Db_Select::SQL_ASC);
     $result = $adapter->fetchAll($select);
     $this->_removeDuplicates($result);
     return $result;
 }
Example #2
0
 public function testGetAffectedFields()
 {
     $valueId = 2345;
     $attributeId = 345345;
     $attribute = $this->getMock('Mage_Eav_Model_Entity_Attribute_Abstract', array('getBackendTable', 'isStatic', 'getAttributeId', 'getName'), array(), '', false);
     $attribute->expects($this->any())->method('getName')->will($this->returnValue('image'));
     $attribute->expects($this->any())->method('getAttributeId')->will($this->returnValue($attributeId));
     $attribute->expects($this->any())->method('isStatic')->will($this->returnValue(false));
     $attribute->expects($this->any())->method('getBackendTable')->will($this->returnValue('table'));
     $this->_model->setAttribute($attribute);
     $object = new Varien_Object();
     $object->setImage(array('images' => array(array('value_id' => $valueId))));
     $object->setId(555);
     $this->assertEquals(array('table' => array(array('value_id' => $valueId, 'attribute_id' => $attributeId, 'entity_id' => $object->getId()))), $this->_model->getAffectedFields($object));
 }
Example #3
0
 protected function _getUniqueFileName($path, $file)
 {
     if (!Mage::helper('magefm_cdn')->isEnabled()) {
         return parent::_moveImageFromTmp($path, $file);
     }
     return MageFM_CDN_Model_Uploader::getNewFileName($path, $file);
 }
 protected function _moveImageFromTmp($file)
 {
     if ($this->_allowUseRenamedImage && isset($this->_renamedImages[$file])) {
         return $this->_renamedImages[$file];
     } else {
         return parent::_moveImageFromTmp($file);
     }
 }
Example #5
0
 public function testSetMediaAttribute()
 {
     $product = new Mage_Catalog_Model_Product();
     $product->setMediaAttributes(array('test_media1', 'test_media2', 'test_media3'));
     $this->_model->setMediaAttribute($product, 'test_media1', 'test1');
     $this->assertEquals('test1', $product->getData('test_media1'));
     $this->_model->setMediaAttribute($product, array('test_media2', 'test_media3'), 'test');
     $this->assertEquals('test', $product->getData('test_media2'));
     $this->assertEquals('test', $product->getData('test_media3'));
 }
Example #6
0
 /**
  * Duplicates gallery db values
  *
  * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  * @param array $newFiles
  * @param int $originalProductId
  * @param int $newProductId
  * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  */
 public function duplicate($object, $newFiles, $originalProductId, $newProductId)
 {
     $select = $this->_getReadAdapter()->select()->from($this->getMainTable(), array('value_id', 'value'))->where('attribute_id = ?', $object->getAttribute()->getId())->where('entity_id = ?', $originalProductId);
     $valueIdMap = array();
     // Duplicate main entries of gallery
     foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
         $data = array('attribute_id' => $object->getAttribute()->getId(), 'entity_id' => $newProductId, 'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value']);
         $valueIdMap[$row['value_id']] = $this->insertGallery($data);
     }
     if (count($valueIdMap) == 0) {
         return $this;
     }
     // Duplicate per store gallery values
     $select = $this->_getReadAdapter()->select()->from($this->getTable(self::GALLERY_VALUE_TABLE))->where('value_id IN(?)', array_keys($valueIdMap));
     foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
         $row['value_id'] = $valueIdMap[$row['value_id']];
         $this->insertGalleryValueInStore($row);
     }
     return $this;
 }
Example #7
0
 /**
  * Save product (import)
  *
  * @param  array $importData
  * @throws Mage_Core_Exception
  * @return bool
  */
 public function saveRow(array $importData)
 {
     $product = $this->getProductModel()->reset();
     if (empty($importData['store'])) {
         if (!is_null($this->getBatchParams('store'))) {
             $store = $this->getStoreById($this->getBatchParams('store'));
         } else {
             $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'store');
             Mage::throwException($message);
         }
     } else {
         $store = $this->getStoreByCode($importData['store']);
     }
     if ($store === false) {
         $message = Mage::helper('catalog')->__('Skipping import row, store "%s" field does not exist.', $importData['store']);
         Mage::throwException($message);
     }
     if (empty($importData['sku'])) {
         $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'sku');
         Mage::throwException($message);
     }
     $product->setStoreId($store->getId());
     $productId = $product->getIdBySku($importData['sku']);
     if ($productId) {
         $product->load($productId);
     } else {
         $productTypes = $this->getProductTypes();
         $productAttributeSets = $this->getProductAttributeSets();
         /**
          * Check product define type
          */
         if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) {
             $value = isset($importData['type']) ? $importData['type'] : '';
             $message = Mage::helper('catalog')->__('Skip import row, is not valid value "%s" for field "%s"', $value, 'type');
             Mage::throwException($message);
         }
         $product->setTypeId($productTypes[strtolower($importData['type'])]);
         /**
          * Check product define attribute set
          */
         if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) {
             $value = isset($importData['attribute_set']) ? $importData['attribute_set'] : '';
             $message = Mage::helper('catalog')->__('Skip import row, the value "%s" is invalid for field "%s"', $value, 'attribute_set');
             Mage::throwException($message);
         }
         $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]);
         foreach ($this->_requiredFields as $field) {
             $attribute = $this->getAttribute($field);
             if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) {
                 $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" for new products is not defined.', $field);
                 Mage::throwException($message);
             }
         }
     }
     // process row with media data only
     if (isset($importData['_media_image']) && strlen($importData['_media_image'])) {
         $this->saveImageDataRow($product, $importData);
         return true;
     }
     $this->setProductTypeInstance($product);
     if (isset($importData['category_ids'])) {
         $product->setCategoryIds($importData['category_ids']);
     }
     foreach ($this->_ignoreFields as $field) {
         if (isset($importData[$field])) {
             unset($importData[$field]);
         }
     }
     if ($store->getId() != 0) {
         $websiteIds = $product->getWebsiteIds();
         if (!is_array($websiteIds)) {
             $websiteIds = array();
         }
         if (!in_array($store->getWebsiteId(), $websiteIds)) {
             $websiteIds[] = $store->getWebsiteId();
         }
         $product->setWebsiteIds($websiteIds);
     }
     if (isset($importData['websites'])) {
         $websiteIds = $product->getWebsiteIds();
         if (!is_array($websiteIds) || !$store->getId()) {
             $websiteIds = array();
         }
         $websiteCodes = explode(',', $importData['websites']);
         foreach ($websiteCodes as $websiteCode) {
             try {
                 $website = Mage::app()->getWebsite(trim($websiteCode));
                 if (!in_array($website->getId(), $websiteIds)) {
                     $websiteIds[] = $website->getId();
                 }
             } catch (Exception $e) {
             }
         }
         $product->setWebsiteIds($websiteIds);
         unset($websiteIds);
     }
     foreach ($importData as $field => $value) {
         if (in_array($field, $this->_inventoryFields)) {
             continue;
         }
         if (is_null($value)) {
             continue;
         }
         $attribute = $this->getAttribute($field);
         if (!$attribute) {
             continue;
         }
         $isArray = false;
         $setValue = $value;
         if ($attribute->getFrontendInput() == 'multiselect') {
             $value = explode(self::MULTI_DELIMITER, $value);
             $isArray = true;
             $setValue = array();
         }
         if ($value && $attribute->getBackendType() == 'decimal') {
             $setValue = $this->getNumber($value);
         }
         if ($attribute->usesSource()) {
             $options = $attribute->getSource()->getAllOptions(false);
             if ($isArray) {
                 foreach ($options as $item) {
                     if (in_array($item['label'], $value)) {
                         $setValue[] = $item['value'];
                     }
                 }
             } else {
                 $setValue = false;
                 foreach ($options as $item) {
                     if (is_array($item['value'])) {
                         foreach ($item['value'] as $subValue) {
                             if (isset($subValue['value']) && $subValue['value'] == $value) {
                                 $setValue = $value;
                             }
                         }
                     } else {
                         if ($item['label'] == $value) {
                             $setValue = $item['value'];
                         }
                     }
                 }
             }
         }
         $product->setData($field, $setValue);
     }
     if (!$product->getVisibility()) {
         $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
     }
     $stockData = array();
     $inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()]) ? $this->_inventoryFieldsProductTypes[$product->getTypeId()] : array();
     foreach ($inventoryFields as $field) {
         if (isset($importData[$field])) {
             if (in_array($field, $this->_toNumber)) {
                 $stockData[$field] = $this->getNumber($importData[$field]);
             } else {
                 $stockData[$field] = $importData[$field];
             }
         }
     }
     $product->setStockData($stockData);
     $arrayToMassAdd = array();
     foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
         if (isset($importData[$mediaAttributeCode])) {
             $file = trim($importData[$mediaAttributeCode]);
             if (!empty($file) && !$this->_galleryBackendModel->getImage($product, $file)) {
                 $arrayToMassAdd[] = array('file' => trim($file), 'mediaAttribute' => $mediaAttributeCode);
             }
         }
     }
     $addedFilesCorrespondence = $this->_galleryBackendModel->addImagesWithDifferentMediaAttributes($product, $arrayToMassAdd, Mage::getBaseDir('media') . DS . 'import', false, false);
     foreach ($product->getMediaAttributes() as $mediaAttributeCode => $mediaAttribute) {
         $addedFile = '';
         if (isset($importData[$mediaAttributeCode . '_label'])) {
             $fileLabel = trim($importData[$mediaAttributeCode . '_label']);
             if (isset($importData[$mediaAttributeCode])) {
                 $keyInAddedFile = array_search($importData[$mediaAttributeCode], $addedFilesCorrespondence['alreadyAddedFiles']);
                 if ($keyInAddedFile !== false) {
                     $addedFile = $addedFilesCorrespondence['alreadyAddedFilesNames'][$keyInAddedFile];
                 }
             }
             if (!$addedFile) {
                 $addedFile = $product->getData($mediaAttributeCode);
             }
             if ($fileLabel && $addedFile) {
                 $this->_galleryBackendModel->updateImage($product, $addedFile, array('label' => $fileLabel));
             }
         }
     }
     $product->setIsMassupdate(true);
     $product->setExcludeUrlRewrite(true);
     $product->save();
     // Store affected products ids
     $this->_addAffectedEntityIds($product->getId());
     return true;
 }
Example #8
0
 /**
  * Copy image and return new filename.
  *
  * @param string $file
  * @return string
  */
 protected function _copyImage($file)
 {
     if (!Mage::helper('uaudio_storage')->isEnabled()) {
         return parent::_copyImage($file);
     }
     try {
         $storageModel = Mage::getSingleton('core/file_storage')->getStorageModel();
         $filePath = $this->_getConfig()->getMediaPath($file);
         if (!$storageModel->fileExists($filePath)) {
             throw new Exception();
         }
         $destFile = $this->getLocalDestination($storageModel->copyFile($filePath, $filePath));
     } catch (Exception $e) {
         $file = $this->_getConfig()->getMediaPath($file);
         Mage::throwException(Mage::helper('catalog')->__('Failed to copy file %s. Please, delete media with non-existing images and try again.', $file));
     }
     return $destFile;
 }
Example #9
0
 /**
  * Load gallery images for product
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  * @return array
  */
 public function loadGallery($product, $object)
 {
     // Select gallery images for product
     $select = $this->_getReadAdapter()->select()->from(array('main' => $this->getMainTable()), array('value_id', 'value AS file'))->joinLeft(array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)), 'main.value_id=value.value_id AND value.store_id=' . (int) $product->getStoreId(), array('label', 'position', 'disabled'))->joinLeft(array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)), 'main.value_id=default_value.value_id AND default_value.store_id=0', array('label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled'))->where('main.attribute_id = ?', $object->getAttribute()->getId())->where('main.entity_id = ?', $product->getId())->order('IF(value.position IS NULL, default_value.position, value.position) ASC');
     return $this->_getReadAdapter()->fetchAll($select);
 }
Example #10
0
 /**
  * Before saving the object we store the gallery data into a temporary field to avoid
  * the gallery is saved as a standard attribute
  *
  * @param Mage_Catalog_Model_Product $object The saved product
  *
  * @return void othing is returned by this method
  */
 public function beforeSave($object)
 {
     parent::beforeSave($object);
     $attrCode = $this->getAttribute()->getAttributeCode();
     $object->setData($attrCode . '_tmp', $object->getData($attrCode));
     $object->unsetData($attrCode);
 }