PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 296 lines | 154 code | 37 blank | 105 comment | 10 complexity | 7f7d8d066c4cf9b6b29b708e7b4b8c19 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@magento.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.magento.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Catalog
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.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. protected $_eventPrefix = 'catalog_product_attribute_backend_media';
  39. private $_attributeId = null;
  40. /**
  41. * Resource initialization
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init(self::GALLERY_TABLE, 'value_id');
  46. }
  47. /**
  48. * Load gallery images for product using reusable select method
  49. *
  50. * @param Mage_Catalog_Model_Product $product
  51. * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  52. * @return array
  53. */
  54. public function loadGallery($product, $object)
  55. {
  56. $eventObjectWrapper = new Varien_Object(
  57. array(
  58. 'product' => $product,
  59. 'backend_attribute' => $object
  60. )
  61. );
  62. Mage::dispatchEvent(
  63. $this->_eventPrefix . '_load_gallery_before',
  64. array('event_object_wrapper' => $eventObjectWrapper)
  65. );
  66. if ($eventObjectWrapper->hasProductIdsOverride()) {
  67. $productIds = $eventObjectWrapper->getProductIdsOverride();
  68. } else {
  69. $productIds = array($product->getId());
  70. }
  71. $select = $this->_getLoadGallerySelect($productIds, $product->getStoreId(), $object->getAttribute()->getId());
  72. $adapter = $this->_getReadAdapter();
  73. $result = $adapter->fetchAll($select);
  74. $this->_removeDuplicates($result);
  75. return $result;
  76. }
  77. /**
  78. * Remove duplicates
  79. *
  80. * @param array $result
  81. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  82. */
  83. protected function _removeDuplicates(&$result)
  84. {
  85. $fileToId = array();
  86. foreach (array_keys($result) as $index) {
  87. if (!isset($fileToId[$result[$index]['file']])) {
  88. $fileToId[$result[$index]['file']] = $result[$index]['value_id'];
  89. } elseif ($fileToId[$result[$index]['file']] != $result[$index]['value_id']) {
  90. $this->deleteGallery($result[$index]['value_id']);
  91. unset($result[$index]);
  92. }
  93. }
  94. $result = array_values($result);
  95. return $this;
  96. }
  97. /**
  98. * Insert gallery value to db and retrive last id
  99. *
  100. * @param array $data
  101. * @return interger
  102. */
  103. public function insertGallery($data)
  104. {
  105. $adapter = $this->_getWriteAdapter();
  106. $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getMainTable());
  107. $adapter->insert($this->getMainTable(), $data);
  108. return $adapter->lastInsertId($this->getMainTable());
  109. }
  110. /**
  111. * Delete gallery value in db
  112. *
  113. * @param array|integer $valueId
  114. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  115. */
  116. public function deleteGallery($valueId)
  117. {
  118. if (is_array($valueId) && count($valueId)>0) {
  119. $condition = $this->_getWriteAdapter()->quoteInto('value_id IN(?) ', $valueId);
  120. } elseif (!is_array($valueId)) {
  121. $condition = $this->_getWriteAdapter()->quoteInto('value_id = ? ', $valueId);
  122. } else {
  123. return $this;
  124. }
  125. $this->_getWriteAdapter()->delete($this->getMainTable(), $condition);
  126. return $this;
  127. }
  128. /**
  129. * Insert gallery value for store to db
  130. *
  131. * @param array $data
  132. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  133. */
  134. public function insertGalleryValueInStore($data)
  135. {
  136. $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getTable(self::GALLERY_VALUE_TABLE));
  137. $this->_getWriteAdapter()->insert($this->getTable(self::GALLERY_VALUE_TABLE), $data);
  138. return $this;
  139. }
  140. /**
  141. * Delete gallery value for store in db
  142. *
  143. * @param integer $valueId
  144. * @param integer $storeId
  145. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  146. */
  147. public function deleteGalleryValueInStore($valueId, $storeId)
  148. {
  149. $adapter = $this->_getWriteAdapter();
  150. $conditions = implode(' AND ', array(
  151. $adapter->quoteInto('value_id = ?', (int) $valueId),
  152. $adapter->quoteInto('store_id = ?', (int) $storeId),
  153. ));
  154. $adapter->delete($this->getTable(self::GALLERY_VALUE_TABLE), $conditions);
  155. return $this;
  156. }
  157. /**
  158. * Duplicates gallery db values
  159. *
  160. * @param Mage_Catalog_Model_Product_Attribute_Backend_Media $object
  161. * @param array $newFiles
  162. * @param int $originalProductId
  163. * @param int $newProductId
  164. * @return Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media
  165. */
  166. public function duplicate($object, $newFiles, $originalProductId, $newProductId)
  167. {
  168. $select = $this->_getReadAdapter()->select()
  169. ->from($this->getMainTable(), array('value_id', 'value'))
  170. ->where('attribute_id = ?', $object->getAttribute()->getId())
  171. ->where('entity_id = ?', $originalProductId);
  172. $valueIdMap = array();
  173. // Duplicate main entries of gallery
  174. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  175. $data = array(
  176. 'attribute_id' => $object->getAttribute()->getId(),
  177. 'entity_id' => $newProductId,
  178. 'value' => (isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'])
  179. );
  180. $valueIdMap[$row['value_id']] = $this->insertGallery($data);
  181. }
  182. if (count($valueIdMap) == 0) {
  183. return $this;
  184. }
  185. // Duplicate per store gallery values
  186. $select = $this->_getReadAdapter()->select()
  187. ->from($this->getTable(self::GALLERY_VALUE_TABLE))
  188. ->where('value_id IN(?)', array_keys($valueIdMap));
  189. foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
  190. $row['value_id'] = $valueIdMap[$row['value_id']];
  191. $this->insertGalleryValueInStore($row);
  192. }
  193. return $this;
  194. }
  195. /**
  196. * Get select to retrieve media gallery images
  197. * for given product IDs.
  198. *
  199. * @param array $productIds
  200. * @param $storeId
  201. * @param int $attributeId
  202. * @return Varien_Db_Select
  203. */
  204. protected function _getLoadGallerySelect(array $productIds, $storeId, $attributeId) {
  205. $adapter = $this->_getReadAdapter();
  206. $positionCheckSql = $adapter->getCheckSql('value.position IS NULL', 'default_value.position', 'value.position');
  207. // Select gallery images for product
  208. $select = $adapter->select()
  209. ->from(
  210. array('main'=>$this->getMainTable()),
  211. array('value_id', 'value AS file', 'product_id' => 'entity_id')
  212. )
  213. ->joinLeft(
  214. array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
  215. $adapter->quoteInto('main.value_id = value.value_id AND value.store_id = ?', (int)$storeId),
  216. array('label','position','disabled')
  217. )
  218. ->joinLeft( // Joining default values
  219. array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
  220. 'main.value_id = default_value.value_id AND default_value.store_id = 0',
  221. array(
  222. 'label_default' => 'label',
  223. 'position_default' => 'position',
  224. 'disabled_default' => 'disabled'
  225. )
  226. )
  227. ->where('main.attribute_id = ?', $attributeId)
  228. ->where('main.entity_id in (?)', $productIds)
  229. ->order($positionCheckSql . ' ' . Varien_Db_Select::SQL_ASC);
  230. return $select;
  231. }
  232. /**
  233. * Get attribute ID
  234. *
  235. * @return int
  236. */
  237. protected function _getAttributeId() {
  238. if(is_null($this->_attributeId)) {
  239. $attribute = Mage::getModel('eav/entity_attribute')
  240. ->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'media_gallery');
  241. $this->_attributeId = $attribute->getId();
  242. }
  243. return $this->_attributeId;
  244. }
  245. /**
  246. * Get media gallery set for given product IDs
  247. *
  248. * @param array $productIds
  249. * @param $storeId
  250. * @return array
  251. */
  252. public function loadGallerySet(array $productIds, $storeId) {
  253. $select = $this->_getLoadGallerySelect($productIds, $storeId, $this->_getAttributeId());
  254. $adapter = $this->_getReadAdapter();
  255. $result = $adapter->fetchAll($select);
  256. $this->_removeDuplicates($result);
  257. return $result;
  258. }
  259. }