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