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
00035 abstract class Mage_Eav_Model_Entity_Attribute_Abstract extends Mage_Core_Model_Abstract
00036 implements Mage_Eav_Model_Entity_Attribute_Interface
00037 {
00038 const TYPE_STATIC = 'static';
00039
00040
00041
00042
00043
00044
00045 protected $_name;
00046
00047
00048
00049
00050
00051
00052 protected $_entity;
00053
00054
00055
00056
00057
00058
00059 protected $_backend;
00060
00061
00062
00063
00064
00065
00066 protected $_frontend;
00067
00068
00069
00070
00071
00072
00073 protected $_source;
00074
00075
00076
00077
00078
00079
00080 protected $_attributeIdCache = array();
00081
00082
00083
00084
00085
00086
00087 protected $_dataTable = null;
00088
00089
00090
00091
00092 protected function _construct()
00093 {
00094 $this->_init('eav/entity_attribute');
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 public function loadByCode($entityType, $code)
00105 {
00106 Varien_Profiler::start('_LOAD_ATTRIBUTE_BY_CODE__');
00107 if (is_numeric($entityType)) {
00108 $entityTypeId = $entityType;
00109 } elseif (is_string($entityType)) {
00110 $entityType = Mage::getModel('eav/entity_type')->loadByCode($entityType);
00111 }
00112 if ($entityType instanceof Mage_Eav_Model_Entity_Type) {
00113 $entityTypeId = $entityType->getId();
00114 }
00115 if (empty($entityTypeId)) {
00116 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid entity supplied'));
00117 }
00118 $this->_getResource()->loadByCode($this, $entityTypeId, $code);
00119 $this->_afterLoad();
00120 Varien_Profiler::stop('_LOAD_ATTRIBUTE_BY_CODE__');
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129 public function getConfig()
00130 {
00131 return $this;
00132 }
00133
00134
00135
00136
00137
00138
00139 public function getName()
00140 {
00141 return $this->_getData('attribute_code');
00142 }
00143
00144
00145
00146
00147
00148
00149
00150 public function setAttributeId($data)
00151 {
00152 $this->_data['attribute_id'] = $data;
00153 return $this;
00154 }
00155
00156
00157
00158
00159
00160
00161 public function getAttributeId()
00162 {
00163 return $this->_getData('attribute_id');
00164 }
00165
00166 public function setAttributeCode($data)
00167 {
00168 return $this->setData('attribute_code', $data);
00169 }
00170
00171 public function getAttributeCode()
00172 {
00173 return $this->_getData('attribute_code');
00174 }
00175
00176 public function setAttributeModel($data)
00177 {
00178 return $this->setData('attribute_model', $data);
00179 }
00180
00181 public function getAttributeModel()
00182 {
00183 return $this->_getData('attribute_model');
00184 }
00185
00186 public function setBackendType($data)
00187 {
00188 return $this->setData('backend_type', $data);
00189 }
00190
00191 public function getBackendType()
00192 {
00193 return $this->_getData('backend_type');
00194 }
00195
00196 public function setBackendModel($data)
00197 {
00198 return $this->setData('backend_model', $data);
00199 }
00200
00201 public function getBackendModel()
00202 {
00203 return $this->_getData('backend_model');
00204 }
00205
00206 public function setBackendTable($data)
00207 {
00208 return $this->setData('backend_table', $data);
00209 }
00210
00211 public function getIsVisibleOnFront()
00212 {
00213 return $this->_getData('is_visible_on_front');
00214 }
00215
00216 public function getDefaultValue()
00217 {
00218 return $this->_getData('default_value');
00219 }
00220
00221 public function getAttributeSetId()
00222 {
00223 return $this->_getData('attribute_set_id');
00224 }
00225
00226 public function setAttributeSetId($id)
00227 {
00228 $this->_data['attribute_set_id'] = $id;
00229 return $this;
00230 }
00231
00232 public function getEntityTypeId()
00233 {
00234 return $this->_getData('entity_type_id');
00235 }
00236
00237 public function setEntityTypeId($id)
00238 {
00239 $this->_data['entity_type_id'] = $id;
00240 return $this;
00241 }
00242
00243 public function setEntityType($type)
00244 {
00245 $this->setData('entity_type', $type);
00246 return $this;
00247 }
00248
00249 public function getIsGlobal()
00250 {
00251 return $this->_getData('is_global');
00252 }
00253
00254
00255
00256
00257
00258
00259
00260 public function getAlias($entity=null)
00261 {
00262 $alias = '';
00263 if (is_null($entity) || ($entity->getType() !== $this->getEntity()->getType())) {
00264 $alias .= $this->getEntity()->getType() . '/';
00265 }
00266 $alias .= $this->getAttributeCode();
00267 return $alias;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276 public function setName($name)
00277 {
00278 return $this->setData('attribute_code', $name);
00279 }
00280
00281 public function getEntityType()
00282 {
00283
00284
00285
00286 return Mage::getSingleton('eav/config')->getEntityType($this->getEntityTypeId());
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 public function setEntity($entity)
00296 {
00297 $this->_entity = $entity;
00298 return $this;
00299 }
00300
00301
00302
00303
00304
00305
00306 public function getEntity()
00307 {
00308 if (!$this->_entity) {
00309 $this->_entity = $this->getEntityType();
00310 }
00311 return $this->_entity;
00312 }
00313
00314 public function getEntityIdField()
00315 {
00316 return $this->getEntity()->getValueEntityIdField();
00317 }
00318
00319
00320
00321
00322
00323
00324 public function getBackend()
00325 {
00326 if (empty($this->_backend)) {
00327 if (!$this->getBackendModel()) {
00328 $this->setBackendModel($this->_getDefaultBackendModel());
00329 }
00330 $backend = Mage::getModel($this->getBackendModel());
00331 if (!$backend) {
00332 throw Mage::exception('Mage_Eav', 'Invalid backend model specified: '.$this->getBackendModel());
00333 }
00334 $this->_backend = $backend->setAttribute($this);
00335 }
00336 return $this->_backend;
00337 }
00338
00339
00340
00341
00342
00343
00344 public function getFrontend()
00345 {
00346 if (empty($this->_frontend)) {
00347 if (!$this->getFrontendModel()) {
00348 $this->setFrontendModel($this->_getDefaultFrontendModel());
00349 }
00350 $this->_frontend = Mage::getModel($this->getFrontendModel())
00351 ->setAttribute($this);
00352 }
00353 return $this->_frontend;
00354 }
00355
00356
00357
00358
00359
00360
00361 public function getSource()
00362 {
00363 if (empty($this->_source)) {
00364 if (!$this->getSourceModel()) {
00365 $this->setSourceModel($this->_getDefaultSourceModel());
00366 }
00367 $this->_source = Mage::getModel($this->getSourceModel())
00368 ->setAttribute($this);
00369 }
00370 return $this->_source;
00371 }
00372
00373 public function usesSource()
00374 {
00375 return $this->getFrontendInput()==='select' || $this->getFrontendInput()==='multiselect';
00376 }
00377
00378 protected function _getDefaultBackendModel()
00379 {
00380 return Mage_Eav_Model_Entity::DEFAULT_BACKEND_MODEL;
00381 }
00382
00383 protected function _getDefaultFrontendModel()
00384 {
00385 return Mage_Eav_Model_Entity::DEFAULT_FRONTEND_MODEL;
00386 }
00387
00388 protected function _getDefaultSourceModel()
00389 {
00390 return $this->getEntity()->getDefaultAttributeSourceModel();
00391 }
00392
00393 public function isValueEmpty($value)
00394 {
00395 $attrType = $this->getBackend()->getType();
00396 $isEmpty = is_array($value)
00397 || is_null($value)
00398 || $value===false && $attrType!='int'
00399 || $value==='' && ($attrType=='int' || $attrType=='decimal' || $attrType=='datetime');
00400 return $isEmpty;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 public function isInSet($setId)
00410 {
00411 if (!$this->hasAttributeSetInfo()) {
00412 return true;
00413 }
00414
00415 if (is_array($setId)
00416 && count(array_intersect($setId, array_keys($this->getAttributeSetInfo())))) {
00417 return true;
00418 }
00419
00420 if (!is_array($setId)
00421 && array_key_exists($setId, $this->getAttributeSetInfo())) {
00422 return true;
00423 }
00424
00425 return false;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435 public function isInGroup($setId, $groupId)
00436 {
00437 if ($this->isInSet($setId) && $this->getData('attribute_set_info/' . $setId . '/group_id') == $groupId) {
00438 return true;
00439 }
00440
00441 return false;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450
00451 public function getIdByCode($entityType, $code)
00452 {
00453 $k = "{$entityType}|{$code}";
00454 if (!isset($this->_attributeIdCache[$k])) {
00455 $this->_attributeIdCache[$k] = $this->getResource()->getIdByCode($entityType, $code);
00456 }
00457 return $this->_attributeIdCache[$k];
00458 }
00459
00460
00461
00462
00463
00464
00465 public function isStatic()
00466 {
00467 return $this->getBackendType() == self::TYPE_STATIC || $this->getBackendType() == '';
00468 }
00469
00470
00471
00472
00473
00474
00475 public function getBackendTable()
00476 {
00477 if ($this->_dataTable === null) {
00478 if ($this->isStatic()) {
00479 $this->_dataTable = $this->getEntityType()->getValueTablePrefix();
00480 } elseif ($this->_getData('backend_table')) {
00481 $this->_dataTable = $this->_getData('backend_table');
00482 } else {
00483 $this->_dataTable = $this->getEntity()->getValueTablePrefix().'_'.$this->getBackendType();
00484 }
00485 }
00486 return $this->_dataTable;
00487 }
00488
00489
00490
00491
00492
00493
00494 public function getFlatColumns() {
00495 if ($this->usesSource() && $this->getBackendType() != 'static') {
00496 return $this->getSource()->getFlatColums();
00497 }
00498
00499 $columns = array();
00500 switch ($this->getBackendType()) {
00501 case 'static':
00502 $describe = $this->_getResource()
00503 ->describeTable($this->getBackend()->getTable());
00504 if (!isset($describe[$this->getAttributeCode()])) {
00505 break;
00506 }
00507 $prop = $describe[$this->getAttributeCode()];
00508 $columns[$this->getAttributeCode()] = array(
00509 'type' => $prop['DATA_TYPE'] . ($prop['LENGTH'] ? "({$prop['LENGTH']})" : ""),
00510 'unsigned' => $prop['UNSIGNED'] ? true: false,
00511 'is_null' => $prop['NULLABLE'],
00512 'default' => $prop['DEFAULT'],
00513 'extra' => null
00514 );
00515 break;
00516 case 'datetime':
00517 $columns[$this->getAttributeCode()] = array(
00518 'type' => 'datetime',
00519 'unsigned' => false,
00520 'is_null' => true,
00521 'default' => null,
00522 'extra' => null
00523 );
00524 break;
00525 case 'decimal':
00526 $columns[$this->getAttributeCode()] = array(
00527 'type' => 'decimal(12,4)',
00528 'unsigned' => false,
00529 'is_null' => true,
00530 'default' => null,
00531 'extra' => null
00532 );
00533 break;
00534 case 'int':
00535 $columns[$this->getAttributeCode()] = array(
00536 'type' => 'int',
00537 'unsigned' => false,
00538 'is_null' => true,
00539 'default' => null,
00540 'extra' => null
00541 );
00542 break;
00543 case 'text':
00544 $columns[$this->getAttributeCode()] = array(
00545 'type' => 'text',
00546 'unsigned' => false,
00547 'is_null' => true,
00548 'default' => null,
00549 'extra' => null
00550 );
00551 break;
00552 case 'varchar':
00553 $columns[$this->getAttributeCode()] = array(
00554 'type' => 'varchar(255)',
00555 'unsigned' => false,
00556 'is_null' => true,
00557 'default' => null,
00558 'extra' => null
00559 );
00560 break;
00561 }
00562 return $columns;
00563 }
00564
00565
00566
00567
00568
00569
00570 public function getFlatIndexes()
00571 {
00572 $condition = $this->getUsedForSortBy();
00573 if ($this->getFlatAddFilterableAttributes()) {
00574 $condition = $condition || $this->getIsFilterable();
00575 }
00576
00577 if ($condition) {
00578 if ($this->usesSource() && $this->getBackendType() != 'static') {
00579 return $this->getSource()->getFlatIndexes();
00580 }
00581 $indexes = array();
00582
00583 switch ($this->getBackendType()) {
00584 case 'static':
00585 $describe = $this->_getResource()
00586 ->describeTable($this->getBackend()->getTable());
00587 if (!isset($describe[$this->getAttributeCode()])) {
00588 break;
00589 }
00590 $indexDataTypes = array(
00591 'varchar',
00592 'varbinary',
00593 'char',
00594 'date',
00595 'datetime',
00596 'timestamp',
00597 'time',
00598 'year',
00599 'enum',
00600 'set',
00601 'bit',
00602 'bool',
00603 'tinyint',
00604 'smallint',
00605 'mediumint',
00606 'int',
00607 'bigint',
00608 'float',
00609 'double',
00610 'decimal',
00611 );
00612 $prop = $describe[$this->getAttributeCode()];
00613 if (in_array($prop['DATA_TYPE'], $indexDataTypes)) {
00614 $indexName = 'IDX_' . strtoupper($this->getAttributeCode());
00615 $indexes[$indexName] = array(
00616 'type' => 'index',
00617 'fields' => array($this->getAttributeCode())
00618 );
00619 }
00620
00621 break;
00622 case 'datetime':
00623 case 'decimal':
00624 case 'int':
00625 case 'varchar':
00626 $indexName = 'IDX_' . strtoupper($this->getAttributeCode());
00627 $indexes[$indexName] = array(
00628 'type' => 'index',
00629 'fields' => array($this->getAttributeCode())
00630 );
00631 break;
00632 }
00633
00634 return $indexes;
00635 }
00636 return array();
00637 }
00638
00639
00640
00641
00642
00643
00644
00645 public function getFlatUpdateSelect($store = null) {
00646 if (is_null($store)) {
00647 foreach (Mage::app()->getStores() as $store) {
00648 $this->getFlatUpdateSelect($store->getId());
00649 }
00650 return $this;
00651 }
00652
00653 if ($this->getBackendType() == 'static') {
00654 return null;
00655 }
00656
00657 if ($this->usesSource()) {
00658 return $this->getSource()->getFlatUpdateSelect($store);
00659 }
00660 return $this->_getResource()->getFlatUpdateSelect($this, $store);
00661 }
00662 }