PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Eav/Model/Entity/Type.php

https://bitbucket.org/dnejedly/eaparts
PHP | 344 lines | 142 code | 35 blank | 167 comment | 14 complexity | 409c2e970ea69ff6c402080aaaa7f197 MD5 | raw file
  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) 2012 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 type model
  28. *
  29. * @method Mage_Eav_Model_Resource_Entity_Type _getResource()
  30. * @method Mage_Eav_Model_Resource_Entity_Type getResource()
  31. * @method Mage_Eav_Model_Entity_Type setEntityTypeCode(string $value)
  32. * @method string getEntityModel()
  33. * @method Mage_Eav_Model_Entity_Type setEntityModel(string $value)
  34. * @method Mage_Eav_Model_Entity_Type setAttributeModel(string $value)
  35. * @method Mage_Eav_Model_Entity_Type setEntityTable(string $value)
  36. * @method Mage_Eav_Model_Entity_Type setValueTablePrefix(string $value)
  37. * @method Mage_Eav_Model_Entity_Type setEntityIdField(string $value)
  38. * @method int getIsDataSharing()
  39. * @method Mage_Eav_Model_Entity_Type setIsDataSharing(int $value)
  40. * @method string getDataSharingKey()
  41. * @method Mage_Eav_Model_Entity_Type setDataSharingKey(string $value)
  42. * @method Mage_Eav_Model_Entity_Type setDefaultAttributeSetId(int $value)
  43. * @method string getIncrementModel()
  44. * @method Mage_Eav_Model_Entity_Type setIncrementModel(string $value)
  45. * @method int getIncrementPerStore()
  46. * @method Mage_Eav_Model_Entity_Type setIncrementPerStore(int $value)
  47. * @method int getIncrementPadLength()
  48. * @method Mage_Eav_Model_Entity_Type setIncrementPadLength(int $value)
  49. * @method string getIncrementPadChar()
  50. * @method Mage_Eav_Model_Entity_Type setIncrementPadChar(string $value)
  51. * @method string getAdditionalAttributeTable()
  52. * @method Mage_Eav_Model_Entity_Type setAdditionalAttributeTable(string $value)
  53. * @method Mage_Eav_Model_Entity_Type setEntityAttributeCollection(string $value)
  54. *
  55. * @category Mage
  56. * @package Mage_Eav
  57. * @author Magento Core Team <core@magentocommerce.com>
  58. */
  59. class Mage_Eav_Model_Entity_Type extends Mage_Core_Model_Abstract
  60. {
  61. /**
  62. * Collection of attributes
  63. *
  64. * @var Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
  65. */
  66. protected $_attributes;
  67. /**
  68. * Array of attributes
  69. *
  70. * @var array
  71. */
  72. protected $_attributesBySet = array();
  73. /**
  74. * Collection of sets
  75. *
  76. * @var Mage_Eav_Model_Mysql4_Entity_Attribute_Set_Collection
  77. */
  78. protected $_sets;
  79. /**
  80. * Resource initialization
  81. */
  82. protected function _construct()
  83. {
  84. $this->_init('eav/entity_type');
  85. }
  86. /**
  87. * Load type by code
  88. *
  89. * @param string $code
  90. * @return Mage_Eav_Model_Entity_Type
  91. */
  92. public function loadByCode($code)
  93. {
  94. $this->_getResource()->loadByCode($this, $code);
  95. $this->_afterLoad();
  96. return $this;
  97. }
  98. /**
  99. * Retrieve entity type attributes collection
  100. *
  101. * @param int $setId
  102. * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
  103. */
  104. public function getAttributeCollection($setId = null)
  105. {
  106. if ($setId === null) {
  107. if ($this->_attributes === null) {
  108. $this->_attributes = $this->_getAttributeCollection()
  109. ->setEntityTypeFilter($this);
  110. }
  111. $collection = $this->_attributes;
  112. } else {
  113. if (!isset($this->_attributesBySet[$setId])) {
  114. $this->_attributesBySet[$setId] = $this->_getAttributeCollection()
  115. ->setEntityTypeFilter($this)
  116. ->setAttributeSetFilter($setId);
  117. }
  118. $collection = $this->_attributesBySet[$setId];
  119. }
  120. return $collection;
  121. }
  122. /**
  123. * Init and retreive attribute collection
  124. *
  125. * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
  126. */
  127. protected function _getAttributeCollection()
  128. {
  129. $collection = Mage::getModel('eav/entity_attribute')->getCollection();
  130. $objectsModel = $this->getAttributeModel();
  131. if ($objectsModel) {
  132. $collection->setModel($objectsModel);
  133. }
  134. return $collection;
  135. }
  136. /**
  137. * Retrieve entity tpe sets collection
  138. *
  139. * @return Mage_Eav_Model_Mysql4_Entity_Attribute_Set_Collection
  140. */
  141. public function getAttributeSetCollection()
  142. {
  143. if (empty($this->_sets)) {
  144. $this->_sets = Mage::getModel('eav/entity_attribute_set')->getResourceCollection()
  145. ->setEntityTypeFilter($this->getId());
  146. }
  147. return $this->_sets;
  148. }
  149. /**
  150. * Retreive new incrementId
  151. *
  152. * @param int $storeId
  153. * @return string
  154. */
  155. public function fetchNewIncrementId($storeId = null)
  156. {
  157. if (!$this->getIncrementModel()) {
  158. return false;
  159. }
  160. if (!$this->getIncrementPerStore() || ($storeId === null)) {
  161. /**
  162. * store_id null we can have for entity from removed store
  163. */
  164. $storeId = 0;
  165. }
  166. // Start transaction to run SELECT ... FOR UPDATE
  167. $this->_getResource()->beginTransaction();
  168. $entityStoreConfig = Mage::getModel('eav/entity_store')
  169. ->loadByEntityStore($this->getId(), $storeId);
  170. if (!$entityStoreConfig->getId()) {
  171. $entityStoreConfig
  172. ->setEntityTypeId($this->getId())
  173. ->setStoreId($storeId)
  174. ->setIncrementPrefix($storeId)
  175. ->save();
  176. }
  177. $incrementInstance = Mage::getModel($this->getIncrementModel())
  178. ->setPrefix($entityStoreConfig->getIncrementPrefix())
  179. ->setPadLength($this->getIncrementPadLength())
  180. ->setPadChar($this->getIncrementPadChar())
  181. ->setLastId($entityStoreConfig->getIncrementLastId())
  182. ->setEntityTypeId($entityStoreConfig->getEntityTypeId())
  183. ->setStoreId($entityStoreConfig->getStoreId());
  184. /**
  185. * do read lock on eav/entity_store to solve potential timing issues
  186. * (most probably already done by beginTransaction of entity save)
  187. */
  188. $incrementId = $incrementInstance->getNextId();
  189. $entityStoreConfig->setIncrementLastId($incrementId);
  190. $entityStoreConfig->save();
  191. // Commit increment_last_id changes
  192. $this->_getResource()->commit();
  193. return $incrementId;
  194. }
  195. /**
  196. * Retreive entity id field
  197. *
  198. * @return string|null
  199. */
  200. public function getEntityIdField()
  201. {
  202. return isset($this->_data['entity_id_field']) ? $this->_data['entity_id_field'] : null;
  203. }
  204. /**
  205. * Retreive entity table name
  206. *
  207. * @return string|null
  208. */
  209. public function getEntityTable()
  210. {
  211. return isset($this->_data['entity_table']) ? $this->_data['entity_table'] : null;
  212. }
  213. /**
  214. * Retrieve entity table prefix name
  215. *
  216. * @return string
  217. */
  218. public function getValueTablePrefix()
  219. {
  220. $prefix = $this->getEntityTablePrefix();
  221. if ($prefix) {
  222. return $this->getResource()->getTable($prefix);
  223. }
  224. return null;
  225. }
  226. /**
  227. * Retrieve entity table prefix
  228. *
  229. * @return string
  230. */
  231. public function getEntityTablePrefix()
  232. {
  233. $tablePrefix = trim($this->_data['value_table_prefix']);
  234. if (empty($tablePrefix)) {
  235. $tablePrefix = $this->getEntityTable();
  236. }
  237. return $tablePrefix;
  238. }
  239. /**
  240. * Get default attribute set identifier for etity type
  241. *
  242. * @return string|null
  243. */
  244. public function getDefaultAttributeSetId()
  245. {
  246. return isset($this->_data['default_attribute_set_id']) ? $this->_data['default_attribute_set_id'] : null;
  247. }
  248. /**
  249. * Retreive entity type id
  250. *
  251. * @return string|null
  252. */
  253. public function getEntityTypeId()
  254. {
  255. return isset($this->_data['entity_type_id']) ? $this->_data['entity_type_id'] : null;
  256. }
  257. /**
  258. * Retreive entity type code
  259. *
  260. * @return string|null
  261. */
  262. public function getEntityTypeCode()
  263. {
  264. return isset($this->_data['entity_type_code']) ? $this->_data['entity_type_code'] : null;
  265. }
  266. /**
  267. * Retreive attribute codes
  268. *
  269. * @return array|null
  270. */
  271. public function getAttributeCodes()
  272. {
  273. return isset($this->_data['attribute_codes']) ? $this->_data['attribute_codes'] : null;
  274. }
  275. /**
  276. * Get attribute model code for entity type
  277. *
  278. * @return string
  279. */
  280. public function getAttributeModel()
  281. {
  282. if (empty($this->_data['attribute_model'])) {
  283. return Mage_Eav_Model_Entity::DEFAULT_ATTRIBUTE_MODEL;
  284. }
  285. return $this->_data['attribute_model'];
  286. }
  287. /**
  288. * Retreive resource entity object
  289. *
  290. * @return Mage_Core_Model_Resource_Abstract
  291. */
  292. public function getEntity()
  293. {
  294. return Mage::getResourceSingleton($this->_data['entity_model']);
  295. }
  296. /**
  297. * Return attribute collection. If not specify return default
  298. *
  299. * @return string
  300. */
  301. public function getEntityAttributeCollection()
  302. {
  303. $collection = $this->_getData('entity_attribute_collection');
  304. if ($collection) {
  305. return $collection;
  306. }
  307. return 'eav/entity_attribute_collection';
  308. }
  309. }