00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Eav 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 00028 class Mage_Eav_Model_Mysql4_Entity_Attribute_Group extends Mage_Core_Model_Mysql4_Abstract 00029 { 00030 protected function _construct() 00031 { 00032 $this->_init('eav/attribute_group', 'attribute_group_id'); 00033 } 00034 00035 public function itemExists($object) 00036 { 00037 $read = $this->_getReadAdapter(); 00038 $select = $read->select()->from($this->getMainTable()) 00039 ->where("attribute_group_name='{$object->getAttributeGroupName()}'"); 00040 $data = $read->fetchRow($select); 00041 if (!$data) { 00042 return false; 00043 } 00044 return true; 00045 } 00046 00047 /** 00048 * Perform actions before object save 00049 * 00050 * @param Mage_Core_Model_Abstract $object 00051 */ 00052 protected function _beforeSave(Mage_Core_Model_Abstract $object) 00053 { 00054 if (!$object->getSortOrder()) { 00055 $object->setSortOrder($this->_getMaxSortOrder($object) + 1); 00056 } 00057 return parent::_beforeSave($object); 00058 } 00059 00060 /** 00061 * Perform actions after object save 00062 * 00063 * @param Mage_Core_Model_Abstract $object 00064 */ 00065 protected function _afterSave(Mage_Core_Model_Abstract $object) 00066 { 00067 if ($object->getAttributes()) { 00068 foreach ($object->getAttributes() as $attribute) { 00069 $attribute->setAttributeGroupId($object->getId()); 00070 $attribute->save(); 00071 } 00072 } 00073 return parent::_afterSave($object); 00074 } 00075 00076 private function _getMaxSortOrder($object) 00077 { 00078 $read = $this->_getReadAdapter(); 00079 $select = $read->select() 00080 ->from($this->getMainTable(), new Zend_Db_Expr("MAX(`sort_order`)")) 00081 ->where("{$this->getMainTable()}.attribute_set_id = ?", $object->getAttributeSetId()); 00082 $maxOrder = $read->fetchOne($select); 00083 return $maxOrder; 00084 } 00085 }