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: * Eav Resource Config model
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Eav_Model_Resource_Config extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Array of entity types
39: *
40: * @var array
41: */
42: protected static $_entityTypes = array();
43:
44: /**
45: * Array of attributes
46: *
47: * @var array
48: */
49: protected static $_attributes = array();
50:
51: /**
52: * Resource initialization
53: */
54: protected function _construct()
55: {
56: $this->_init('eav/entity_type', 'entity_type_id');
57: }
58:
59: /**
60: * Load all entity types
61: *
62: * @return Mage_Eav_Model_Resource_Config
63: */
64: protected function _loadTypes()
65: {
66: $adapter = $this->_getReadAdapter();
67: if (!$adapter) {
68: return $this;
69: }
70: if (empty(self::$_entityTypes)) {
71: $select = $adapter->select()->from($this->getMainTable());
72: $data = $adapter->fetchAll($select);
73: foreach ($data as $row) {
74: self::$_entityTypes['by_id'][$row['entity_type_id']] = $row;
75: self::$_entityTypes['by_code'][$row['entity_type_code']] = $row;
76: }
77: }
78:
79: return $this;
80: }
81:
82: /**
83: * Load attribute types
84: *
85: * @param ind $typeId
86: * @return array
87: */
88: protected function _loadTypeAttributes($typeId)
89: {
90: if (!isset(self::$_attributes[$typeId])) {
91: $adapter = $this->_getReadAdapter();
92: $bind = array('entity_type_id' => $typeId);
93: $select = $adapter->select()
94: ->from($this->getTable('eav/attribute'))
95: ->where('entity_type_id = :entity_type_id');
96:
97: self::$_attributes[$typeId] = $adapter->fetchAll($select, $bind);
98: }
99:
100: return self::$_attributes[$typeId];
101: }
102:
103: /**
104: * Retrieve entity type data
105: *
106: * @param string $entityType
107: * @return array
108: */
109: public function fetchEntityTypeData($entityType)
110: {
111: $this->_loadTypes();
112:
113: if (is_numeric($entityType)) {
114: $info = isset(self::$_entityTypes['by_id'][$entityType])
115: ? self::$_entityTypes['by_id'][$entityType] : null;
116: } else {
117: $info = isset(self::$_entityTypes['by_code'][$entityType])
118: ? self::$_entityTypes['by_code'][$entityType] : null;
119: }
120:
121: $data = array();
122: if ($info) {
123: $data['entity'] = $info;
124: $attributes = $this->_loadTypeAttributes($info['entity_type_id']);
125: $data['attributes'] = array();
126: foreach ($attributes as $attribute) {
127: $data['attributes'][$attribute['attribute_id']] = $attribute;
128: $data['attributes'][$attribute['attribute_code']] = $attribute['attribute_id'];
129: }
130: }
131:
132: return $data;
133: }
134: }
135: