PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 674 lines | 419 code | 65 blank | 190 comment | 57 complexity | b0ce3695b0dd43a8764bff64906b7434 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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@magentocommerce.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.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Eav
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Entity/Attribute/Model - attribute abstract
  28. *
  29. * @category Mage
  30. * @package Mage_Eav
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. abstract class Mage_Eav_Model_Entity_Attribute_Abstract extends Mage_Core_Model_Abstract
  34. implements Mage_Eav_Model_Entity_Attribute_Interface
  35. {
  36. const TYPE_STATIC = 'static';
  37. /**
  38. * Attribute name
  39. *
  40. * @var string
  41. */
  42. protected $_name;
  43. /**
  44. * Entity instance
  45. *
  46. * @var Mage_Eav_Model_Entity_Abstract
  47. */
  48. protected $_entity;
  49. /**
  50. * Backend instance
  51. *
  52. * @var Mage_Eav_Model_Entity_Attribute_Backend_Abstract
  53. */
  54. protected $_backend;
  55. /**
  56. * Frontend instance
  57. *
  58. * @var Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
  59. */
  60. protected $_frontend;
  61. /**
  62. * Source instance
  63. *
  64. * @var Mage_Eav_Model_Entity_Attribute_Source_Abstract
  65. */
  66. protected $_source;
  67. /**
  68. * Attribute id cache
  69. *
  70. * @var array
  71. */
  72. protected $_attributeIdCache = array();
  73. /**
  74. * Attribute data table name
  75. *
  76. * @var string
  77. */
  78. protected $_dataTable = null;
  79. /**
  80. * Initialize resource model
  81. */
  82. protected function _construct()
  83. {
  84. $this->_init('eav/entity_attribute');
  85. }
  86. /**
  87. * Load attribute data by code
  88. *
  89. * @param mixed $entityType
  90. * @param string $code
  91. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  92. */
  93. public function loadByCode($entityType, $code)
  94. {
  95. Varien_Profiler::start('_LOAD_ATTRIBUTE_BY_CODE__');
  96. if (is_numeric($entityType)) {
  97. $entityTypeId = $entityType;
  98. } elseif (is_string($entityType)) {
  99. $entityType = Mage::getModel('eav/entity_type')->loadByCode($entityType);
  100. }
  101. if ($entityType instanceof Mage_Eav_Model_Entity_Type) {
  102. $entityTypeId = $entityType->getId();
  103. }
  104. if (empty($entityTypeId)) {
  105. throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid entity supplied.'));
  106. }
  107. $this->_getResource()->loadByCode($this, $entityTypeId, $code);
  108. $this->_afterLoad();
  109. Varien_Profiler::stop('_LOAD_ATTRIBUTE_BY_CODE__');
  110. return $this;
  111. }
  112. /**
  113. * Retrieve attribute configuration (deprecated)
  114. *
  115. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  116. */
  117. public function getConfig()
  118. {
  119. return $this;
  120. }
  121. /**
  122. * Get attribute name
  123. *
  124. * @return string
  125. */
  126. public function getName()
  127. {
  128. return $this->_getData('attribute_code');
  129. }
  130. /**
  131. * Specify attribute identifier
  132. *
  133. * @param int $data
  134. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  135. */
  136. public function setAttributeId($data)
  137. {
  138. $this->_data['attribute_id'] = $data;
  139. return $this;
  140. }
  141. /**
  142. * Get attribute identifuer
  143. *
  144. * @return int | null
  145. */
  146. public function getAttributeId()
  147. {
  148. return $this->_getData('attribute_id');
  149. }
  150. public function setAttributeCode($data)
  151. {
  152. return $this->setData('attribute_code', $data);
  153. }
  154. public function getAttributeCode()
  155. {
  156. return $this->_getData('attribute_code');
  157. }
  158. public function setAttributeModel($data)
  159. {
  160. return $this->setData('attribute_model', $data);
  161. }
  162. public function getAttributeModel()
  163. {
  164. return $this->_getData('attribute_model');
  165. }
  166. public function setBackendType($data)
  167. {
  168. return $this->setData('backend_type', $data);
  169. }
  170. public function getBackendType()
  171. {
  172. return $this->_getData('backend_type');
  173. }
  174. public function setBackendModel($data)
  175. {
  176. return $this->setData('backend_model', $data);
  177. }
  178. public function getBackendModel()
  179. {
  180. return $this->_getData('backend_model');
  181. }
  182. public function setBackendTable($data)
  183. {
  184. return $this->setData('backend_table', $data);
  185. }
  186. public function getIsVisibleOnFront()
  187. {
  188. return $this->_getData('is_visible_on_front');
  189. }
  190. public function getDefaultValue()
  191. {
  192. return $this->_getData('default_value');
  193. }
  194. public function getAttributeSetId()
  195. {
  196. return $this->_getData('attribute_set_id');
  197. }
  198. public function setAttributeSetId($id)
  199. {
  200. $this->_data['attribute_set_id'] = $id;
  201. return $this;
  202. }
  203. public function getEntityTypeId()
  204. {
  205. return $this->_getData('entity_type_id');
  206. }
  207. public function setEntityTypeId($id)
  208. {
  209. $this->_data['entity_type_id'] = $id;
  210. return $this;
  211. }
  212. public function setEntityType($type)
  213. {
  214. $this->setData('entity_type', $type);
  215. return $this;
  216. }
  217. /**
  218. * Return is attribute global
  219. *
  220. * @deprecated moved to catalog attribute model
  221. * @return integer
  222. */
  223. public function getIsGlobal()
  224. {
  225. return $this->_getData('is_global');
  226. }
  227. /**
  228. * Get attribute alias as "entity_type/attribute_code"
  229. *
  230. * @param Mage_Eav_Model_Entity_Abstract $entity exclude this entity
  231. * @return string
  232. */
  233. public function getAlias($entity=null)
  234. {
  235. $alias = '';
  236. if (is_null($entity) || ($entity->getType() !== $this->getEntity()->getType())) {
  237. $alias .= $this->getEntity()->getType() . '/';
  238. }
  239. $alias .= $this->getAttributeCode();
  240. return $alias;
  241. }
  242. /**
  243. * Set attribute name
  244. *
  245. * @param string $name
  246. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  247. */
  248. public function setName($name)
  249. {
  250. return $this->setData('attribute_code', $name);
  251. }
  252. public function getEntityType()
  253. {
  254. /*if ($this->hasData('entity_type')) {
  255. return $this->_getData('entity_type');
  256. }*/
  257. return Mage::getSingleton('eav/config')->getEntityType($this->getEntityTypeId());
  258. }
  259. /**
  260. * Set attribute entity instance
  261. *
  262. * @param Mage_Eav_Model_Entity_Abstract $entity
  263. * @return Mage_Eav_Model_Entity_Attribute_Abstract
  264. */
  265. public function setEntity($entity)
  266. {
  267. $this->_entity = $entity;
  268. return $this;
  269. }
  270. /**
  271. * Retrieve entity instance
  272. *
  273. * @return Mage_Eav_Model_Entity_Abstract
  274. */
  275. public function getEntity()
  276. {
  277. if (!$this->_entity) {
  278. $this->_entity = $this->getEntityType();
  279. }
  280. return $this->_entity;
  281. }
  282. public function getEntityIdField()
  283. {
  284. return $this->getEntity()->getValueEntityIdField();
  285. }
  286. /**
  287. * Retrieve backend instance
  288. *
  289. * @return Mage_Eav_Model_Entity_Attribute_Backend_Abstract
  290. */
  291. public function getBackend()
  292. {
  293. if (empty($this->_backend)) {
  294. if (!$this->getBackendModel()) {
  295. $this->setBackendModel($this->_getDefaultBackendModel());
  296. }
  297. $backend = Mage::getModel($this->getBackendModel());
  298. if (!$backend) {
  299. throw Mage::exception('Mage_Eav', 'Invalid backend model specified: '.$this->getBackendModel());
  300. }
  301. $this->_backend = $backend->setAttribute($this);
  302. }
  303. return $this->_backend;
  304. }
  305. /**
  306. * Retrieve frontend instance
  307. *
  308. * @return Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
  309. */
  310. public function getFrontend()
  311. {
  312. if (empty($this->_frontend)) {
  313. if (!$this->getFrontendModel()) {
  314. $this->setFrontendModel($this->_getDefaultFrontendModel());
  315. }
  316. $this->_frontend = Mage::getModel($this->getFrontendModel())
  317. ->setAttribute($this);
  318. }
  319. return $this->_frontend;
  320. }
  321. /**
  322. * Retrieve source instance
  323. *
  324. * @return Mage_Eav_Model_Entity_Attribute_Source_Abstract
  325. */
  326. public function getSource()
  327. {
  328. if (empty($this->_source)) {
  329. if (!$this->getSourceModel()) {
  330. $this->setSourceModel($this->_getDefaultSourceModel());
  331. }
  332. $source = Mage::getModel($this->getSourceModel());
  333. if (!$source) {
  334. throw new Exception(sprintf('Source model "%s" not found for attribute "%s".',
  335. $this->getSourceModel(), $this->getAttributeCode()
  336. ));
  337. }
  338. $this->_source = $source->setAttribute($this);
  339. }
  340. return $this->_source;
  341. }
  342. public function usesSource()
  343. {
  344. return $this->getFrontendInput() === 'select' || $this->getFrontendInput() === 'multiselect'
  345. || $this->getData('source_model') != '';
  346. }
  347. protected function _getDefaultBackendModel()
  348. {
  349. return Mage_Eav_Model_Entity::DEFAULT_BACKEND_MODEL;
  350. }
  351. protected function _getDefaultFrontendModel()
  352. {
  353. return Mage_Eav_Model_Entity::DEFAULT_FRONTEND_MODEL;
  354. }
  355. protected function _getDefaultSourceModel()
  356. {
  357. return $this->getEntity()->getDefaultAttributeSourceModel();
  358. }
  359. public function isValueEmpty($value)
  360. {
  361. $attrType = $this->getBackend()->getType();
  362. $isEmpty = is_array($value)
  363. || is_null($value)
  364. || $value===false && $attrType!='int'
  365. || $value==='' && ($attrType=='int' || $attrType=='decimal' || $attrType=='datetime');
  366. return $isEmpty;
  367. }
  368. /**
  369. * Check if attribute in specified set
  370. *
  371. * @param int|array $setId
  372. * @return boolean
  373. */
  374. public function isInSet($setId)
  375. {
  376. if (!$this->hasAttributeSetInfo()) {
  377. return true;
  378. }
  379. if (is_array($setId)
  380. && count(array_intersect($setId, array_keys($this->getAttributeSetInfo())))) {
  381. return true;
  382. }
  383. if (!is_array($setId)
  384. && array_key_exists($setId, $this->getAttributeSetInfo())) {
  385. return true;
  386. }
  387. return false;
  388. }
  389. /**
  390. * Check if attribute in specified group
  391. *
  392. * @param int $setId
  393. * @param int $groupId
  394. * @return boolean
  395. */
  396. public function isInGroup($setId, $groupId)
  397. {
  398. if ($this->isInSet($setId) && $this->getData('attribute_set_info/' . $setId . '/group_id') == $groupId) {
  399. return true;
  400. }
  401. return false;
  402. }
  403. /**
  404. * Return attribute id
  405. *
  406. * @param string $entityType
  407. * @param string $code
  408. * @return int
  409. */
  410. public function getIdByCode($entityType, $code)
  411. {
  412. $k = "{$entityType}|{$code}";
  413. if (!isset($this->_attributeIdCache[$k])) {
  414. $this->_attributeIdCache[$k] = $this->getResource()->getIdByCode($entityType, $code);
  415. }
  416. return $this->_attributeIdCache[$k];
  417. }
  418. /**
  419. * Check if attribute is static
  420. *
  421. * @return bool
  422. */
  423. public function isStatic()
  424. {
  425. return $this->getBackendType() == self::TYPE_STATIC || $this->getBackendType() == '';
  426. }
  427. /**
  428. * Get attribute backend table name
  429. *
  430. * @return string
  431. */
  432. public function getBackendTable()
  433. {
  434. if ($this->_dataTable === null) {
  435. if ($this->isStatic()) {
  436. $this->_dataTable = $this->getEntityType()->getValueTablePrefix();
  437. } elseif ($backendTable = trim($this->_getData('backend_table'))) {
  438. $this->_dataTable = $backendTable;
  439. } else {
  440. $this->_dataTable = $this->getEntity()->getValueTablePrefix().'_'.$this->getBackendType();
  441. }
  442. }
  443. return $this->_dataTable;
  444. }
  445. /**
  446. * Retrieve Flat Column(s)
  447. *
  448. * @return array
  449. */
  450. public function getFlatColumns() {
  451. if ($this->usesSource() && $this->getBackendType() != 'static') {
  452. return $this->getSource()->getFlatColums();
  453. }
  454. $columns = array();
  455. switch ($this->getBackendType()) {
  456. case 'static':
  457. $describe = $this->_getResource()
  458. ->describeTable($this->getBackend()->getTable());
  459. if (!isset($describe[$this->getAttributeCode()])) {
  460. break;
  461. }
  462. $prop = $describe[$this->getAttributeCode()];
  463. $columns[$this->getAttributeCode()] = array(
  464. 'type' => $prop['DATA_TYPE'] . ($prop['LENGTH'] ? "({$prop['LENGTH']})" : ""),
  465. 'unsigned' => $prop['UNSIGNED'] ? true: false,
  466. 'is_null' => $prop['NULLABLE'],
  467. 'default' => $prop['DEFAULT'],
  468. 'extra' => null
  469. );
  470. break;
  471. case 'datetime':
  472. $columns[$this->getAttributeCode()] = array(
  473. 'type' => 'datetime',
  474. 'unsigned' => false,
  475. 'is_null' => true,
  476. 'default' => null,
  477. 'extra' => null
  478. );
  479. break;
  480. case 'decimal':
  481. $columns[$this->getAttributeCode()] = array(
  482. 'type' => 'decimal(12,4)',
  483. 'unsigned' => false,
  484. 'is_null' => true,
  485. 'default' => null,
  486. 'extra' => null
  487. );
  488. break;
  489. case 'int':
  490. $columns[$this->getAttributeCode()] = array(
  491. 'type' => 'int',
  492. 'unsigned' => false,
  493. 'is_null' => true,
  494. 'default' => null,
  495. 'extra' => null
  496. );
  497. break;
  498. case 'text':
  499. $columns[$this->getAttributeCode()] = array(
  500. 'type' => 'text',
  501. 'unsigned' => false,
  502. 'is_null' => true,
  503. 'default' => null,
  504. 'extra' => null
  505. );
  506. break;
  507. case 'varchar':
  508. $columns[$this->getAttributeCode()] = array(
  509. 'type' => 'varchar(255)',
  510. 'unsigned' => false,
  511. 'is_null' => true,
  512. 'default' => null,
  513. 'extra' => null
  514. );
  515. break;
  516. }
  517. return $columns;
  518. }
  519. /**
  520. * Retrieve index data for Flat table
  521. *
  522. * @return array
  523. */
  524. public function getFlatIndexes()
  525. {
  526. $condition = $this->getUsedForSortBy();
  527. if ($this->getFlatAddFilterableAttributes()) {
  528. $condition = $condition || $this->getIsFilterable();
  529. }
  530. if ($condition) {
  531. if ($this->usesSource() && $this->getBackendType() != 'static') {
  532. return $this->getSource()->getFlatIndexes();
  533. }
  534. $indexes = array();
  535. switch ($this->getBackendType()) {
  536. case 'static':
  537. $describe = $this->_getResource()
  538. ->describeTable($this->getBackend()->getTable());
  539. if (!isset($describe[$this->getAttributeCode()])) {
  540. break;
  541. }
  542. $indexDataTypes = array(
  543. 'varchar',
  544. 'varbinary',
  545. 'char',
  546. 'date',
  547. 'datetime',
  548. 'timestamp',
  549. 'time',
  550. 'year',
  551. 'enum',
  552. 'set',
  553. 'bit',
  554. 'bool',
  555. 'tinyint',
  556. 'smallint',
  557. 'mediumint',
  558. 'int',
  559. 'bigint',
  560. 'float',
  561. 'double',
  562. 'decimal',
  563. );
  564. $prop = $describe[$this->getAttributeCode()];
  565. if (in_array($prop['DATA_TYPE'], $indexDataTypes)) {
  566. $indexName = 'IDX_' . strtoupper($this->getAttributeCode());
  567. $indexes[$indexName] = array(
  568. 'type' => 'index',
  569. 'fields' => array($this->getAttributeCode())
  570. );
  571. }
  572. break;
  573. case 'datetime':
  574. case 'decimal':
  575. case 'int':
  576. case 'varchar':
  577. $indexName = 'IDX_' . strtoupper($this->getAttributeCode());
  578. $indexes[$indexName] = array(
  579. 'type' => 'index',
  580. 'fields' => array($this->getAttributeCode())
  581. );
  582. break;
  583. }
  584. return $indexes;
  585. }
  586. return array();
  587. }
  588. /**
  589. * Retrieve Select For Flat Attribute update
  590. *
  591. * @param int $store
  592. * @return Varien_Db_Select
  593. */
  594. public function getFlatUpdateSelect($store = null) {
  595. if (is_null($store)) {
  596. foreach (Mage::app()->getStores() as $store) {
  597. $this->getFlatUpdateSelect($store->getId());
  598. }
  599. return $this;
  600. }
  601. if ($this->getBackendType() == 'static') {
  602. return null;
  603. }
  604. if ($this->usesSource()) {
  605. return $this->getSource()->getFlatUpdateSelect($store);
  606. }
  607. return $this->_getResource()->getFlatUpdateSelect($this, $store);
  608. }
  609. }