1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Catalog_Model_Resource_Product_Attribute_Backend_Media extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: const GALLERY_TABLE = 'catalog/product_attribute_media_gallery';
38: const GALLERY_VALUE_TABLE = 'catalog/product_attribute_media_gallery_value';
39: const GALLERY_IMAGE_TABLE = 'catalog/product_attribute_media_gallery_image';
40:
41: 42: 43:
44: protected function _construct()
45: {
46: $this->_init(self::GALLERY_TABLE, 'value_id');
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: public function loadGallery($product, $object)
57: {
58: $adapter = $this->_getReadAdapter();
59:
60: $positionCheckSql = $adapter->getCheckSql('value.position IS NULL', 'default_value.position', 'value.position');
61:
62:
63: $select = $adapter->select()
64: ->from(
65: array('main'=>$this->getMainTable()),
66: array('value_id', 'value AS file')
67: )
68: ->joinLeft(
69: array('value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
70: $adapter->quoteInto('main.value_id = value.value_id AND value.store_id = ?', (int)$product->getStoreId()),
71: array('label','position','disabled')
72: )
73: ->joinLeft(
74: array('default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)),
75: 'main.value_id = default_value.value_id AND default_value.store_id = 0',
76: array(
77: 'label_default' => 'label',
78: 'position_default' => 'position',
79: 'disabled_default' => 'disabled'
80: )
81: )
82: ->where('main.attribute_id = ?', $object->getAttribute()->getId())
83: ->where('main.entity_id = ?', $product->getId())
84: ->order($positionCheckSql . ' ' . Varien_Db_Select::SQL_ASC);
85:
86: $result = $adapter->fetchAll($select);
87: $this->_removeDuplicates($result);
88: return $result;
89: }
90:
91: 92: 93: 94: 95: 96:
97: protected function _removeDuplicates(&$result)
98: {
99: $fileToId = array();
100:
101: foreach (array_keys($result) as $index) {
102: if (!isset($fileToId[$result[$index]['file']])) {
103: $fileToId[$result[$index]['file']] = $result[$index]['value_id'];
104: } elseif ($fileToId[$result[$index]['file']] != $result[$index]['value_id']) {
105: $this->deleteGallery($result[$index]['value_id']);
106: unset($result[$index]);
107: }
108: }
109:
110: $result = array_values($result);
111: return $this;
112: }
113:
114: 115: 116: 117: 118: 119:
120: public function insertGallery($data)
121: {
122: $adapter = $this->_getWriteAdapter();
123: $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getMainTable());
124: $adapter->insert($this->getMainTable(), $data);
125:
126: return $adapter->lastInsertId($this->getMainTable());
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function deleteGallery($valueId)
136: {
137: if (is_array($valueId) && count($valueId)>0) {
138: $condition = $this->_getWriteAdapter()->quoteInto('value_id IN(?) ', $valueId);
139: } elseif (!is_array($valueId)) {
140: $condition = $this->_getWriteAdapter()->quoteInto('value_id = ? ', $valueId);
141: } else {
142: return $this;
143: }
144:
145: $this->_getWriteAdapter()->delete($this->getMainTable(), $condition);
146: return $this;
147: }
148:
149: 150: 151: 152: 153: 154:
155: public function insertGalleryValueInStore($data)
156: {
157: $data = $this->_prepareDataForTable(new Varien_Object($data), $this->getTable(self::GALLERY_VALUE_TABLE));
158: $this->_getWriteAdapter()->insert($this->getTable(self::GALLERY_VALUE_TABLE), $data);
159:
160: return $this;
161: }
162:
163: 164: 165: 166: 167: 168: 169:
170: public function deleteGalleryValueInStore($valueId, $storeId)
171: {
172: $adapter = $this->_getWriteAdapter();
173:
174: $conditions = implode(' AND ', array(
175: $adapter->quoteInto('value_id = ?', (int) $valueId),
176: $adapter->quoteInto('store_id = ?', (int) $storeId),
177: ));
178:
179: $adapter->delete($this->getTable(self::GALLERY_VALUE_TABLE), $conditions);
180:
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189: 190: 191: 192:
193: public function duplicate($object, $newFiles, $originalProductId, $newProductId)
194: {
195: $select = $this->_getReadAdapter()->select()
196: ->from($this->getMainTable(), array('value_id', 'value'))
197: ->where('attribute_id = ?', $object->getAttribute()->getId())
198: ->where('entity_id = ?', $originalProductId);
199:
200: $valueIdMap = array();
201:
202: foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
203: $data = array(
204: 'attribute_id' => $object->getAttribute()->getId(),
205: 'entity_id' => $newProductId,
206: 'value' => (isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'])
207: );
208:
209: $valueIdMap[$row['value_id']] = $this->insertGallery($data);
210: }
211:
212: if (count($valueIdMap) == 0) {
213: return $this;
214: }
215:
216:
217: $select = $this->_getReadAdapter()->select()
218: ->from($this->getTable(self::GALLERY_VALUE_TABLE))
219: ->where('value_id IN(?)', array_keys($valueIdMap));
220:
221: foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
222: $row['value_id'] = $valueIdMap[$row['value_id']];
223: $this->insertGalleryValueInStore($row);
224: }
225:
226: return $this;
227: }
228: }
229: