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: * Eav Resource Entity Attribute Group
29: *
30: * @category Mage
31: * @package Mage_Eav
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Eav_Model_Resource_Entity_Attribute_Group extends Mage_Core_Model_Resource_Db_Abstract
35: {
36: /**
37: * Resource initialization
38: */
39: protected function _construct()
40: {
41: $this->_init('eav/attribute_group', 'attribute_group_id');
42: }
43:
44: /**
45: * Checks if attribute group exists
46: *
47: * @param Mage_Eav_Model_Entity_Attribute_Group $object
48: * @return boolean
49: */
50: public function itemExists($object)
51: {
52: $adapter = $this->_getReadAdapter();
53: $bind = array(
54: 'attribute_set_id' => $object->getAttributeSetId(),
55: 'attribute_group_name' => $object->getAttributeGroupName()
56: );
57: $select = $adapter->select()
58: ->from($this->getMainTable())
59: ->where('attribute_set_id = :attribute_set_id')
60: ->where('attribute_group_name = :attribute_group_name');
61:
62: return $adapter->fetchRow($select, $bind) > 0;
63: }
64:
65: /**
66: * Perform actions before object save
67: *
68: * @param Mage_Core_Model_Abstract $object
69: * @return Mage_Core_Model_Resource_Db_Abstract
70: */
71: protected function _beforeSave(Mage_Core_Model_Abstract $object)
72: {
73: if (!$object->getSortOrder()) {
74: $object->setSortOrder($this->_getMaxSortOrder($object) + 1);
75: }
76: return parent::_beforeSave($object);
77: }
78:
79: /**
80: * Perform actions after object save
81: *
82: * @param Mage_Core_Model_Abstract $object
83: * @return Mage_Core_Model_Resource_Db_Abstract
84: */
85: protected function _afterSave(Mage_Core_Model_Abstract $object)
86: {
87: if ($object->getAttributes()) {
88: foreach ($object->getAttributes() as $attribute) {
89: $attribute->setAttributeGroupId($object->getId());
90: $attribute->save();
91: }
92: }
93:
94: return parent::_afterSave($object);
95: }
96:
97: /**
98: * Retreive max sort order
99: *
100: * @param Mage_Core_Model_Abstract $object
101: * @return int
102: */
103: protected function _getMaxSortOrder($object)
104: {
105: $adapter = $this->_getReadAdapter();
106: $bind = array(':attribute_set_id' => $object->getAttributeSetId());
107: $select = $adapter->select()
108: ->from($this->getMainTable(), new Zend_Db_Expr("MAX(sort_order)"))
109: ->where('attribute_set_id = :attribute_set_id');
110:
111: return $adapter->fetchOne($select, $bind);
112: }
113:
114: /**
115: * Set any group default if old one was removed
116: *
117: * @param integer $attributeSetId
118: * @return Mage_Eav_Model_Resource_Entity_Attribute_Group
119: */
120: public function updateDefaultGroup($attributeSetId)
121: {
122: $adapter = $this->_getWriteAdapter();
123: $bind = array(':attribute_set_id' => $attributeSetId);
124: $select = $adapter->select()
125: ->from($this->getMainTable(), $this->getIdFieldName())
126: ->where('attribute_set_id = :attribute_set_id')
127: ->order('default_id ' . Varien_Data_Collection::SORT_ORDER_DESC)
128: ->limit(1);
129:
130: $groupId = $adapter->fetchOne($select, $bind);
131:
132: if ($groupId) {
133: $data = array('default_id' => 1);
134: $where = array('attribute_group_id =?' => $groupId);
135: $adapter->update($this->getMainTable(), $data, $where);
136: }
137:
138: return $this;
139: }
140: }
141: