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_Resource_Eav_Mysql4_Product_Attribute_Backend_Media extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036
00037
00038
00039 const GALLERY_TABLE = 'catalog/product_attribute_media_gallery';
00040 const GALLERY_VALUE_TABLE = 'catalog/product_attribute_media_gallery_value';
00041 const GALLERY_IMAGE_TABLE = 'catalog/product_attribute_media_gallery_image';
00042
00043 protected function _construct()
00044 {
00045 $this->_init(self::GALLERY_TABLE, 'value_id');
00046 }
00047
00048
00049
00050
00051
00052
00053
00054
00055 public function loadGallery($product, $object)
00056 {
00057
00058 $select = $this->_getReadAdapter()->select()
00059 ->from(
00060 array('main'=>$this->getMainTable()),
00061 array('value_id', 'value AS file')
00062 )
00063 ->joinLeft(
00064 array('value'=>$this->getTable(self::GALLERY_VALUE_TABLE)),
00065 'main.value_id=value.value_id AND value.store_id='.(int)$product->getStoreId(),
00066 array('label','position','disabled')
00067 )
00068 ->joinLeft(
00069 array('default_value'=>$this->getTable(self::GALLERY_VALUE_TABLE)),
00070 'main.value_id=default_value.value_id AND default_value.store_id=0',
00071 array(
00072 'label_default' => 'label',
00073 'position_default' => 'position',
00074 'disabled_default' => 'disabled'
00075 )
00076 )
00077 ->where('main.attribute_id = ?', $object->getAttribute()->getId())
00078 ->where('main.entity_id = ?', $product->getId())
00079 ->order('IF(value.position IS NULL, default_value.position, value.position) ASC');
00080
00081 $result = $this->_getReadAdapter()->fetchAll($select);
00082 $this->_removeDuplicates($result);
00083 return $result;
00084 }
00085
00086 protected function _removeDuplicates(&$result)
00087 {
00088 $fileToId = array();
00089
00090 foreach (array_keys($result) as $index) {
00091 if (!isset($fileToId[$result[$index]['file']])) {
00092 $fileToId[$result[$index]['file']] = $result[$index]['value_id'];
00093 } elseif ($fileToId[$result[$index]['file']] != $result[$index]['value_id']) {
00094 $this->deleteGallery($result[$index]['value_id']);
00095 unset($result[$index]);
00096 }
00097 }
00098
00099 $result = array_values($result);
00100 return $this;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 public function insertGallery($data)
00110 {
00111 $this->_getWriteAdapter()->insert($this->getMainTable(), $data);
00112 return $this->_getWriteAdapter()->lastInsertId();
00113 }
00114
00115
00116
00117
00118
00119
00120
00121 public function deleteGallery($valueId)
00122 {
00123 if (is_array($valueId) && count($valueId)>0) {
00124 $condition = $this->_getWriteAdapter()->quoteInto('value_id IN(?) ', $valueId);
00125 } elseif (!is_array($valueId)) {
00126 $condition = $this->_getWriteAdapter()->quoteInto('value_id = ? ', $valueId);
00127 } else {
00128 return $this;
00129 }
00130
00131 $this->_getWriteAdapter()->delete($this->getMainTable(), $condition);
00132 return $this;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public function insertGalleryValueInStore($data)
00142 {
00143 $this->_getWriteAdapter()->insert($this->getTable(self::GALLERY_VALUE_TABLE), $data);
00144 return $this;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154 public function deleteGalleryValueInStore($valueId, $storeId)
00155 {
00156 $this->_getWriteAdapter()->delete(
00157 $this->getTable(self::GALLERY_VALUE_TABLE),
00158 'value_id = ' . (int)$valueId . ' AND store_id = ' . (int)$storeId
00159 );
00160
00161 return $this;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 public function duplicate($object, $newFiles, $originalProductId, $newProductId)
00174 {
00175 $select = $this->_getReadAdapter()->select()
00176 ->from($this->getMainTable(), array('value_id', 'value'))
00177 ->where('attribute_id = ?', $object->getAttribute()->getId())
00178 ->where('entity_id = ?', $originalProductId);
00179
00180 $valueIdMap = array();
00181
00182 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00183 $data = array(
00184 'attribute_id' => $object->getAttribute()->getId(),
00185 'entity_id' => $newProductId,
00186 'value' => (isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'])
00187 );
00188
00189 $valueIdMap[$row['value_id']] = $this->insertGallery($data);
00190 }
00191
00192 if (count($valueIdMap) == 0) {
00193 return $this;
00194 }
00195
00196
00197 $select = $this->_getReadAdapter()->select()
00198 ->from($this->getTable(self::GALLERY_VALUE_TABLE))
00199 ->where('value_id IN(?)', array_keys($valueIdMap));
00200
00201 foreach ($this->_getReadAdapter()->fetchAll($select) as $row) {
00202 $row['value_id'] = $valueIdMap[$row['value_id']];
00203 $this->insertGalleryValueInStore($row);
00204 }
00205
00206 return $this;
00207 }
00208 }