/app/code/core/Mage/Catalog/Model/Resource/Product/Attribute/Backend/Media.php

https://bitbucket.org/acidel/buykoala · PHP · 228 lines · 117 code · 27 blank · 84 comment · 7 complexity · 9cc4becadda4defd0c26b8260a41601c MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Catalog product media gallery attribute backend resource
  28. *
  29. * @category Mage
  30. * @package Mage_Catalog
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media extends Mage_Core_Model_Resource_Db_Abstract
  34. {
  35. const GALLERY_TABLE = 'catalog/product_attribute_media_gallery';
  36. const GALLERY_VALUE_TABLE = 'catalog/product_attribute_media_gallery_value';
  37. const GALLERY_IMAGE_TABLE = 'catalog/product_attribute_media_gallery_image';
  38. /**
  39. * Resource initialization
  40. */
  41. protected function _construct()
  42. {
  43. $this->_init(self::GALLERY_TABLE, 'value_id');
  44. }
  45. /**
  46. * Load gallery images for product
  47. *
  48. * @param Mage_Catalog_Model_Product $product
  49. * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  50. * @return array
  51. */
  52. public function loadGallery($product, $object)
  53. {
  54. $adapter = $this->_getReadAdapter();
  55. $positionCheckSql = $adapter->getCheckSql('value.position IS NULL', 'default_value.position', 'value.position');
  56. // Select gallery images for product
  57. $select = $adapter->select()
  58. ->from(
  59. array('main'=>$this->getMainTable()),
  60. array('value_id', 'value AS file')
  61. )
  62. ->joinLeft(
  63. array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
  64. $adapter->quoteInto('main.value_id = value.value_id AND value.store_id = ?', (int)$product->getStoreId()),
  65. array('label','position','disabled')
  66. )
  67. ->joinLeft( // Joining default values
  68. array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
  69. 'main.value_id = default_value.value_id AND default_value.store_id = 0',
  70. array(
  71. 'label_default' => 'label',
  72. 'position_default' => 'position',
  73. 'disabled_default' => 'disabled'
  74. )
  75. )
  76. ->where('main.attribute_id = ?', $object->getAttribute()->getId())
  77. ->where('main.entity_id = ?', $product->getId())
  78. ->order($positionCheckSql . ' ' . Varien_Db_Select::SQL_ASC);
  79. $result = $adapter->fetchAll($select);
  80. $this->_removeDuplicates($result);
  81. return $result;
  82. }
  83. /**
  84. * Remove duplicates
  85. *
  86. * @param array $result
  87. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  88. */
  89. protected function _removeDuplicates(&$result)
  90. {
  91. $fileToId = array();
  92. foreach (array_keys($result) as $index) {
  93. if (!isset($fileToId[$result[$index]['file']])) {
  94. $fileToId[$result[$index]['file']] = $result[$index]['value_id'];
  95. } elseif ($fileToId[$result[$index]['file']] != $result[$index]['value_id']) {
  96. $this->deleteGallery($result[$index]['value_id']);
  97. unset($result[$index]);
  98. }
  99. }
  100. $result = array_values($result);
  101. return $this;
  102. }
  103. /**
  104. * Insert gallery value to db and retrive last id
  105. *
  106. * @param array $data
  107. * @return interger
  108. */
  109. public function insertGallery($data)
  110. {
  111. $adapter = $this->_getWriteAdapter();
  112. $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getMainTable());
  113. $adapter->insert($this->getMainTable(), $data);
  114. return $adapter->lastInsertId($this->getMainTable());
  115. }
  116. /**
  117. * Delete gallery value in db
  118. *
  119. * @param array|integer $valueId
  120. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  121. */
  122. public function deleteGallery($valueId)
  123. {
  124. if (is_array($valueId) && count($valueId)>0) {
  125. $condition = $this->_getWriteAdapter()->quoteInto('value_id IN(?) ', $valueId);
  126. } elseif (!is_array($valueId)) {
  127. $condition = $this->_getWriteAdapter()->quoteInto('value_id = ? ', $valueId);
  128. } else {
  129. return $this;
  130. }
  131. $this->_getWriteAdapter()->delete($this->getMainTable(), $condition);
  132. return $this;
  133. }
  134. /**
  135. * Insert gallery value for store to db
  136. *
  137. * @param array $data
  138. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  139. */
  140. public function insertGalleryValueInStore($data)
  141. {
  142. $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getTable(self::GALLERY_VALUE_TABLE));
  143. $this->_getWriteAdapter()->insert($this->getTable(self::GALLERY_VALUE_TABLE), $data);
  144. return $this;
  145. }
  146. /**
  147. * Delete gallery value for store in db
  148. *
  149. * @param integer $valueId
  150. * @param integer $storeId
  151. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  152. */
  153. public function deleteGalleryValueInStore($valueId, $storeId)
  154. {
  155. $adapter = $this->_getWriteAdapter();
  156. $conditions = implode(' AND ', array(
  157. $adapter->quoteInto('value_id = ?', (int) $valueId),
  158. $adapter->quoteInto('store_id = ?', (int) $storeId),
  159. ));
  160. $adapter->delete($this->getTable(self::GALLERY_VALUE_TABLE), $conditions);
  161. return $this;
  162. }
  163. /**
  164. * Duplicates gallery db values
  165. *
  166. * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  167. * @param array $newFiles
  168. * @param int $originalProductId
  169. * @param int $newProductId
  170. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  171. */
  172. public function duplicate($object, $newFiles, $originalProductId, $newProductId)
  173. {
  174. $select = $this->_getReadAdapter()->select()
  175. ->from($this->getMainTable(), array('value_id', 'value'))
  176. ->where('attribute_id = ?', $object->getAttribute()->getId())
  177. ->where('entity_id = ?', $originalProductId);
  178. $valueIdMap = array();
  179. // Duplicate main entries of gallery
  180. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  181. $data = array(
  182. 'attribute_id' => $object->getAttribute()->getId(),
  183. 'entity_id' => $newProductId,
  184. 'value' => (isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'])
  185. );
  186. $valueIdMap[$row['value_id']] = $this->insertGallery($data);
  187. }
  188. if (count($valueIdMap) == 0) {
  189. return $this;
  190. }
  191. // Duplicate per store gallery values
  192. $select = $this->_getReadAdapter()->select()
  193. ->from($this->getTable(self::GALLERY_VALUE_TABLE))
  194. ->where('value_id IN(?)', array_keys($valueIdMap));
  195. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  196. $row['value_id'] = $valueIdMap[$row['value_id']];
  197. $this->insertGalleryValueInStore($row);
  198. }
  199. return $this;
  200. }
  201. }