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 Form Type Resource Model
30: *
31: * @category Mage
32: * @package Mage_Eav
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Eav_Model_Resource_Form_Type extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Initialize connection and define main table
39: *
40: */
41: protected function _construct()
42: {
43: $this->_init('eav/form_type', 'type_id');
44: $this->addUniqueField(array(
45: 'field' => array('code', 'theme', 'store_id'),
46: 'title' => Mage::helper('eav')->__('Form Type with the same code')
47: ));
48: }
49:
50: /**
51: * Load an object
52: *
53: * @param Mage_Eav_Model_Form_Type $object
54: * @param mixed $value
55: * @param string $field field to load by (defaults to model id)
56: * @return Mage_Eav_Model_Resource_Form_Type
57: */
58: public function load(Mage_Core_Model_Abstract $object, $value, $field = null)
59: {
60: if (is_null($field) && !is_numeric($value)) {
61: $field = 'code';
62: }
63: return parent::load($object, $value, $field);
64: }
65:
66: /**
67: * Retrieve form type entity types
68: *
69: * @param Mage_Eav_Model_Form_Type $object
70: * @return array
71: */
72: public function getEntityTypes($object)
73: {
74: $objectId = $object->getId();
75: if (!$objectId) {
76: return array();
77: }
78: $adapter = $this->_getReadAdapter();
79: $bind = array(':type_id' => $objectId);
80: $select = $adapter->select()
81: ->from($this->getTable('eav/form_type_entity'), 'entity_type_id')
82: ->where('type_id = :type_id');
83:
84: return $adapter->fetchCol($select, $bind);
85: }
86:
87: /**
88: * Save entity types after save form type
89: *
90: * @see Mage_Core_Model_Resource_Db_Abstract#_afterSave($object)
91: *
92: * @param Mage_Eav_Model_Form_Type $object
93: * @return Mage_Eav_Model_Resource_Form_Type
94: */
95: protected function _afterSave(Mage_Core_Model_Abstract $object)
96: {
97: if ($object->hasEntityTypes()) {
98: $new = $object->getEntityTypes();
99: $old = $this->getEntityTypes($object);
100:
101: $insert = array_diff($new, $old);
102: $delete = array_diff($old, $new);
103:
104: $adapter = $this->_getWriteAdapter();
105:
106: if (!empty($insert)) {
107: $data = array();
108: foreach ($insert as $entityId) {
109: if (empty($entityId)) {
110: continue;
111: }
112: $data[] = array(
113: 'entity_type_id' => (int)$entityId,
114: 'type_id' => $object->getId()
115: );
116: }
117: if ($data) {
118: $adapter->insertMultiple($this->getTable('eav/form_type_entity'), $data);
119: }
120: }
121:
122: if (!empty($delete)) {
123: $where = array(
124: 'entity_type_id IN (?)' => $delete,
125: 'type_id = ?' => $object->getId()
126: );
127: $adapter->delete($this->getTable('eav/form_type_entity'), $where);
128: }
129: }
130:
131: return parent::_afterSave($object);
132: }
133:
134: /**
135: * Retrieve form type filtered by given attribute
136: *
137: * @param Mage_Eav_Model_Entity_Attribute_Abstract|int $attribute
138: * @return array
139: */
140: public function getFormTypesByAttribute($attribute)
141: {
142: if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract) {
143: $attribute = $attribute->getId();
144: }
145: if (!$attribute) {
146: return array();
147: }
148: $bind = array(':attribute_id' => $attribute);
149: $select = $this->_getReadAdapter()->select()
150: ->from($this->getTable('eav/form_element'))
151: ->where('attribute_id = :attribute_id');
152:
153: return $this->_getReadAdapter()->fetchAll($select, $bind);
154: }
155: }
156: