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_Eav_Model_Mysql4_Entity_Attribute extends Mage_Core_Model_Mysql4_Abstract
00035 {
00036 protected static $_entityAttributes = null;
00037
00038 protected function _construct()
00039 {
00040 $this->_init('eav/attribute', 'attribute_id');
00041 }
00042
00043
00044
00045
00046
00047
00048 protected function _initUniqueFields()
00049 {
00050 $this->_uniqueFields = array(array(
00051 'field' => array('attribute_code','entity_type_id'),
00052 'title' => Mage::helper('eav')->__('Attribute with the same code')
00053 ));
00054 return $this;
00055 }
00056
00057 protected function _loadTypeAttributes($entityTypeId)
00058 {
00059 if (!isset(self::$_entityAttributes[$entityTypeId])) {
00060 $select = $this->_getReadAdapter()->select()->from($this->getMainTable())
00061 ->where('entity_type_id=?', $entityTypeId);
00062 $data = $this->_getReadAdapter()->fetchAll($select);
00063 foreach ($data as $row) {
00064 self::$_entityAttributes[$entityTypeId][$row['attribute_code']] = $row;
00065 }
00066 }
00067 return $this;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 public function loadByCode(Mage_Core_Model_Abstract $object, $entityTypeId, $code)
00079 {
00080 $select = $this->_getLoadSelect('attribute_code', $code, $object)
00081 ->where('entity_type_id=?', $entityTypeId);
00082 $data = $this->_getReadAdapter()->fetchRow($select);
00083
00084 if ($data) {
00085 $object->setData($data);
00086 $this->_afterLoad($object);
00087 return true;
00088 }
00089 return false;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098 private function _getMaxSortOrder(Mage_Core_Model_Abstract $object)
00099 {
00100 if( intval($object->getAttributeGroupId()) > 0 ) {
00101 $read = $this->_getReadAdapter();
00102 $select = $read->select()
00103 ->from($this->getTable('entity_attribute'), new Zend_Db_Expr("MAX(`sort_order`)"))
00104 ->where("{$this->getTable('entity_attribute')}.attribute_set_id = ?", $object->getAttributeSetId())
00105 ->where("{$this->getTable('entity_attribute')}.attribute_group_id = ?", $object->getAttributeGroupId());
00106 $maxOrder = $read->fetchOne($select);
00107 return $maxOrder;
00108 }
00109
00110 return 0;
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 public function deleteEntity(Mage_Core_Model_Abstract $object)
00120 {
00121 $write = $this->_getWriteAdapter();
00122 $condition = $write->quoteInto($this->getTable('entity_attribute').'.entity_attribute_id = ?', $object->getEntityAttributeId());
00123
00124
00125
00126 $select = $write->select()
00127 ->from($this->getTable('entity_attribute'))
00128 ->where($condition);
00129 $data = $write->fetchRow($select);
00130 if (!empty($data)) {
00131
00132
00133
00134 $attribute = Mage::getModel('eav/entity_attribute')
00135 ->load($data['attribute_id'])
00136 ->setEntity(Mage::getSingleton('catalog/product')->getResource());
00137
00138 if ($this->isUsedBySuperProducts($attribute, $data['attribute_set_id'])) {
00139 Mage::throwException(Mage::helper('eav')->__("Attribute '%s' used in configurable products.", $attribute->getAttributeCode()));
00140 }
00141
00142 if ($backendTable = $attribute->getBackend()->getTable()) {
00143 $clearCondition = array(
00144 $write->quoteInto('entity_type_id=?',$attribute->getEntityTypeId()),
00145 $write->quoteInto('attribute_id=?',$attribute->getId()),
00146 $write->quoteInto('entity_id IN (
00147 SELECT entity_id FROM '.$attribute->getEntity()->getEntityTable().' WHERE attribute_set_id=?)',
00148 $data['attribute_set_id'])
00149 );
00150 $write->delete($backendTable, $clearCondition);
00151 }
00152 }
00153
00154 $write->delete($this->getTable('entity_attribute'), $condition);
00155 return $this;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 protected function _beforeSave(Mage_Core_Model_Abstract $object)
00165 {
00166 $frontendLabel = $object->getFrontendLabel();
00167 if (is_array($frontendLabel)) {
00168 if (!isset($frontendLabel[0]) || is_null($frontendLabel[0]) || $frontendLabel[0]=='') {
00169 Mage::throwException(Mage::helper('eav')->__('Frontend label is not defined'));
00170 }
00171 $object->setFrontendLabel($frontendLabel[0]);
00172
00173 if ($object->getData('modulePrefix')) {
00174 $str = $object->getData('modulePrefix') . Mage_Core_Model_Translate::SCOPE_SEPARATOR . $frontendLabel[0];
00175 }
00176 else {
00177 $str = $frontendLabel[0];
00178 }
00179 Mage::getModel('core/translate_string')
00180 ->setString($str)
00181 ->setTranslate($frontendLabel[0])
00182 ->setStoreTranslations($frontendLabel)
00183 ->save();
00184 }
00185 $applyTo = $object->getApplyTo();
00186
00187 if (is_array($applyTo)) {
00188 $object->setApplyTo(implode(',', $applyTo));
00189 }
00190
00191
00192
00193
00194 if (!$object->getId()) {
00195 if ($object->getFrontendInput()=='select') {
00196 $object->setSourceModel('eav/entity_attribute_source_table');
00197 }
00198 }
00199
00200 return parent::_beforeSave($object);
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 protected function _afterSave(Mage_Core_Model_Abstract $object)
00210 {
00211 $this->saveInSetIncluding($object)
00212 ->_saveOption($object);
00213 return parent::_afterSave($object);
00214 }
00215
00216
00217
00218
00219
00220
00221
00222 public function saveInSetIncluding(Mage_Core_Model_Abstract $object)
00223 {
00224 $attrId = $object->getId();
00225 $setId = (int) $object->getAttributeSetId();
00226 $groupId= (int) $object->getAttributeGroupId();
00227
00228 if ($setId && $groupId && $object->getEntityTypeId()) {
00229 $write = $this->_getWriteAdapter();
00230 $table = $this->getTable('entity_attribute');
00231
00232
00233 $data = array(
00234 'entity_type_id' => $object->getEntityTypeId(),
00235 'attribute_set_id' => $setId,
00236 'attribute_group_id' => $groupId,
00237 'attribute_id' => $attrId,
00238 'sort_order' => (($object->getSortOrder()) ? $object->getSortOrder() : $this->_getMaxSortOrder($object) + 1),
00239 );
00240
00241 $condition = "$table.attribute_id = '$attrId'
00242 AND $table.attribute_set_id = '$setId'";
00243 $write->delete($table, $condition);
00244 $write->insert($table, $data);
00245
00246 }
00247 return $this;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 protected function _saveOption(Mage_Core_Model_Abstract $object)
00257 {
00258 $option = $object->getOption();
00259 if (is_array($option)) {
00260 $write = $this->_getWriteAdapter();
00261 $optionTable = $this->getTable('attribute_option');
00262 $optionValueTable = $this->getTable('attribute_option_value');
00263 $stores = Mage::getModel('core/store')
00264 ->getResourceCollection()
00265 ->setLoadDefault(true)
00266 ->load();
00267
00268 if (isset($option['value'])) {
00269 $attributeDefaultValue = array();
00270 if (!is_array($object->getDefault())) {
00271 $object->setDefault(array());
00272 }
00273
00274 foreach ($option['value'] as $optionId => $values) {
00275 $intOptionId = (int) $optionId;
00276 if (!empty($option['delete'][$optionId])) {
00277 if ($intOptionId) {
00278 $condition = $write->quoteInto('option_id=?', $intOptionId);
00279 $write->delete($optionTable, $condition);
00280 }
00281
00282 continue;
00283 }
00284
00285 if (!$intOptionId) {
00286 $data = array(
00287 'attribute_id' => $object->getId(),
00288 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00289 );
00290 $write->insert($optionTable, $data);
00291 $intOptionId = $write->lastInsertId();
00292 }
00293 else {
00294 $data = array(
00295 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00296 );
00297 $write->update($optionTable, $data, $write->quoteInto('option_id=?', $intOptionId));
00298 }
00299
00300 if (in_array($optionId, $object->getDefault())) {
00301 if ($object->getFrontendInput() == 'multiselect') {
00302 $attributeDefaultValue[] = $intOptionId;
00303 } else if ($object->getFrontendInput() == 'select') {
00304 $attributeDefaultValue = array($intOptionId);
00305 }
00306 }
00307
00308
00309
00310 if (!isset($values[0])) {
00311 Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
00312 }
00313
00314 $write->delete($optionValueTable, $write->quoteInto('option_id=?', $intOptionId));
00315 foreach ($stores as $store) {
00316 if (isset($values[$store->getId()]) && (!empty($values[$store->getId()]) || $values[$store->getId()] == "0")) {
00317 $data = array(
00318 'option_id' => $intOptionId,
00319 'store_id' => $store->getId(),
00320 'value' => $values[$store->getId()],
00321 );
00322 $write->insert($optionValueTable, $data);
00323 }
00324 }
00325 }
00326
00327 $write->update($this->getMainTable(), array(
00328 'default_value' => implode(',', $attributeDefaultValue)
00329 ), $write->quoteInto($this->getIdFieldName() . '=?', $object->getId()));
00330 }
00331 }
00332 return $this;
00333 }
00334
00335 public function isUsedBySuperProducts(Mage_Core_Model_Abstract $object, $attributeSet=null)
00336 {
00337 $read = $this->_getReadAdapter();
00338 $attrTable = $this->getTable('catalog/product_super_attribute');
00339 $productTable = $this->getTable('catalog/product');
00340 $select = $read->select()
00341 ->from(array('_main_table' => $attrTable), 'COUNT(*)')
00342 ->join(array('_entity'=> $productTable), '_main_table.product_id = _entity.entity_id')
00343 ->where("_main_table.attribute_id = ?", $object->getAttributeId())
00344 ->group('_main_table.attribute_id')
00345 ->limit(1);
00346
00347 if (!is_null($attributeSet)) {
00348 $select->where('_entity.attribute_set_id = ?', $attributeSet);
00349 }
00350 $valueCount = $read->fetchOne($select);
00351 return $valueCount;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361 public function getIdByCode($entityType, $code)
00362 {
00363 $select = $this->_getReadAdapter()->select()
00364 ->from(array('a'=>$this->getTable('eav/attribute')), array('a.attribute_id'))
00365 ->join(array('t'=>$this->getTable('eav/entity_type')), 'a.entity_type_id = t.entity_type_id', array())
00366 ->where('t.entity_type_code = ?', $entityType)
00367 ->where('a.attribute_code = ?', $code);
00368
00369 return $this->_getReadAdapter()->fetchOne($select);
00370 }
00371
00372 public function getAttributeCodesByFrontendType($type)
00373 {
00374 $select = $this->_getReadAdapter()->select();
00375 $select
00376 ->from($this->getTable('eav/attribute'), 'attribute_code')
00377 ->where('frontend_input = ?', $type);
00378
00379 $result = $this->_getReadAdapter()->fetchCol($select);
00380
00381 if ($result) {
00382 return $result;
00383 } else {
00384 return array();
00385 }
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395 public function getFlatUpdateSelect(Mage_Eav_Model_Entity_Attribute_Abstract $attribute, $store) {
00396 $joinCondition = "`e`.`entity_id`=`t1`.`entity_id`";
00397 if ($attribute->getFlatAddChildData()) {
00398 $joinCondition .= " AND `e`.`child_id`=`t1`.`entity_id`";
00399 }
00400 $select = $this->_getReadAdapter()->select()
00401 ->joinLeft(
00402 array('t1' => $attribute->getBackend()->getTable()),
00403 $joinCondition,
00404 array()
00405 )
00406 ->joinLeft(
00407 array('t2' => $attribute->getBackend()->getTable()),
00408 "t2.entity_id = t1.entity_id"
00409 . " AND t1.entity_type_id = t2.entity_type_id"
00410 . " AND t1.attribute_id = t2.attribute_id"
00411 . " AND t2.store_id = {$store}",
00412 array($attribute->getAttributeCode() => "IFNULL(t2.value, t1.value)"))
00413 ->where("t1.entity_type_id=?", $attribute->getEntityTypeId())
00414 ->where("t1.attribute_id=?", $attribute->getId())
00415 ->where("t1.store_id=?", 0);
00416 if ($attribute->getFlatAddChildData()) {
00417 $select->where("e.is_child=?", 0);
00418 }
00419 return $select;
00420 }
00421
00422
00423
00424
00425
00426
00427
00428 public function describeTable($table) {
00429 return $this->_getReadAdapter()->describeTable($table);
00430 }
00431
00432 }