Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Catalog_Model_Entity_Product_Attribute_Design_Options_Container
  • Mage_Catalog_Model_Product_Attribute_Source_Inputtype
  • Mage_Eav_Block_Adminhtml_Attribute_Edit_Js
  • Mage_Eav_Block_Adminhtml_Attribute_Edit_Main_Abstract
  • Mage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract
  • Mage_Eav_Helper_Data
  • Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype
  • Mage_Eav_Model_Adminhtml_System_Config_Source_Inputtype_Validator
  • Mage_Eav_Model_Attribute
  • Mage_Eav_Model_Attribute_Data
  • Mage_Eav_Model_Attribute_Data_Abstract
  • Mage_Eav_Model_Attribute_Data_Boolean
  • Mage_Eav_Model_Attribute_Data_Date
  • Mage_Eav_Model_Attribute_Data_File
  • Mage_Eav_Model_Attribute_Data_Hidden
  • Mage_Eav_Model_Attribute_Data_Image
  • Mage_Eav_Model_Attribute_Data_Multiline
  • Mage_Eav_Model_Attribute_Data_Multiselect
  • Mage_Eav_Model_Attribute_Data_Select
  • Mage_Eav_Model_Attribute_Data_Text
  • Mage_Eav_Model_Attribute_Data_Textarea
  • Mage_Eav_Model_Config
  • Mage_Eav_Model_Convert_Adapter_Entity
  • Mage_Eav_Model_Convert_Adapter_Grid
  • Mage_Eav_Model_Convert_Parser_Abstract
  • Mage_Eav_Model_Entity
  • Mage_Eav_Model_Entity_Abstract
  • Mage_Eav_Model_Entity_Attribute
  • Mage_Eav_Model_Entity_Attribute_Abstract
  • Mage_Eav_Model_Entity_Attribute_Backend_Abstract
  • Mage_Eav_Model_Entity_Attribute_Backend_Array
  • Mage_Eav_Model_Entity_Attribute_Backend_Datetime
  • Mage_Eav_Model_Entity_Attribute_Backend_Default
  • Mage_Eav_Model_Entity_Attribute_Backend_Increment
  • Mage_Eav_Model_Entity_Attribute_Backend_Serialized
  • Mage_Eav_Model_Entity_Attribute_Backend_Store
  • Mage_Eav_Model_Entity_Attribute_Backend_Time_Created
  • Mage_Eav_Model_Entity_Attribute_Backend_Time_Updated
  • Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
  • Mage_Eav_Model_Entity_Attribute_Frontend_Datetime
  • Mage_Eav_Model_Entity_Attribute_Frontend_Default
  • Mage_Eav_Model_Entity_Attribute_Group
  • Mage_Eav_Model_Entity_Attribute_Option
  • Mage_Eav_Model_Entity_Attribute_Set
  • Mage_Eav_Model_Entity_Attribute_Source_Abstract
  • Mage_Eav_Model_Entity_Attribute_Source_Boolean
  • Mage_Eav_Model_Entity_Attribute_Source_Config
  • Mage_Eav_Model_Entity_Attribute_Source_Store
  • Mage_Eav_Model_Entity_Attribute_Source_Table
  • Mage_Eav_Model_Entity_Collection
  • Mage_Eav_Model_Entity_Collection_Abstract
  • Mage_Eav_Model_Entity_Increment_Abstract
  • Mage_Eav_Model_Entity_Increment_Alphanum
  • Mage_Eav_Model_Entity_Increment_Numeric
  • Mage_Eav_Model_Entity_Setup
  • Mage_Eav_Model_Entity_Store
  • Mage_Eav_Model_Entity_Type
  • Mage_Eav_Model_Form
  • Mage_Eav_Model_Form_Element
  • Mage_Eav_Model_Form_Fieldset
  • Mage_Eav_Model_Form_Type
  • Mage_Eav_Model_Mysql4_Config
  • Mage_Eav_Model_Mysql4_Entity_Attribute
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Collection
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Group
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Group_Collection
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Option
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Option_Collection
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Set
  • Mage_Eav_Model_Mysql4_Entity_Attribute_Set_Collection
  • Mage_Eav_Model_Mysql4_Entity_Store
  • Mage_Eav_Model_Mysql4_Entity_Type
  • Mage_Eav_Model_Mysql4_Entity_Type_Collection
  • Mage_Eav_Model_Mysql4_Form_Element
  • Mage_Eav_Model_Mysql4_Form_Element_Collection
  • Mage_Eav_Model_Mysql4_Form_Fieldset
  • Mage_Eav_Model_Mysql4_Form_Fieldset_Collection
  • Mage_Eav_Model_Mysql4_Form_Type
  • Mage_Eav_Model_Mysql4_Form_Type_Collection
  • Mage_Eav_Model_Resource_Attribute
  • Mage_Eav_Model_Resource_Attribute_Collection
  • Mage_Eav_Model_Resource_Config
  • Mage_Eav_Model_Resource_Entity_Attribute
  • Mage_Eav_Model_Resource_Entity_Attribute_Collection
  • Mage_Eav_Model_Resource_Entity_Attribute_Group
  • Mage_Eav_Model_Resource_Entity_Attribute_Group_Collection
  • Mage_Eav_Model_Resource_Entity_Attribute_Option
  • Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
  • Mage_Eav_Model_Resource_Entity_Attribute_Set
  • Mage_Eav_Model_Resource_Entity_Attribute_Set_Collection
  • Mage_Eav_Model_Resource_Entity_Store
  • Mage_Eav_Model_Resource_Entity_Type
  • Mage_Eav_Model_Resource_Entity_Type_Collection
  • Mage_Eav_Model_Resource_Form_Attribute
  • Mage_Eav_Model_Resource_Form_Attribute_Collection
  • Mage_Eav_Model_Resource_Form_Element
  • Mage_Eav_Model_Resource_Form_Element_Collection
  • Mage_Eav_Model_Resource_Form_Fieldset
  • Mage_Eav_Model_Resource_Form_Fieldset_Collection
  • Mage_Eav_Model_Resource_Form_Type
  • Mage_Eav_Model_Resource_Form_Type_Collection
  • Mage_Eav_Model_Resource_Helper_Mysql4

Interfaces

  • Mage_Eav_Model_Entity_Attribute_Backend_Interface
  • Mage_Eav_Model_Entity_Attribute_Frontend_Interface
  • Mage_Eav_Model_Entity_Attribute_Interface
  • Mage_Eav_Model_Entity_Attribute_Source_Interface
  • Mage_Eav_Model_Entity_Increment_Interface
  • Mage_Eav_Model_Entity_Interface

Exceptions

  • Mage_Eav_Exception
  • Mage_Eav_Model_Entity_Attribute_Exception
  • Overview
  • Package
  • Class
  • Tree
   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 Entity Setup Model
  30:  *
  31:  * @category   Mage
  32:  * @package    Mage_Eav
  33:  * @author     Magento Core Team <core@magentocommerce.com>
  34:  */
  35: class Mage_Eav_Model_Entity_Setup extends Mage_Core_Model_Resource_Setup
  36: {
  37:     /**
  38:      * General Attribute Group Name
  39:      *
  40:      * @var string
  41:      */
  42:     protected $_generalGroupName        = 'General';
  43: 
  44:     /**
  45:      * Default attribute group name to id pairs
  46:      *
  47:      * @var array
  48:      */
  49:     public $defaultGroupIdAssociations  = array(
  50:         'General'   => 1
  51:     );
  52: 
  53:     /**
  54:      * Default attribute group name
  55:      *
  56:      * @var string
  57:      */
  58:     protected $_defaultGroupName         = 'Default';
  59: 
  60:     /**
  61:      * Default attribute set name
  62:      *
  63:      * @var string
  64:      */
  65:     protected $_defaultAttributeSetName  = 'Default';
  66: 
  67:     /**
  68:      * Clean cache
  69:      *
  70:      * @return Mage_Eav_Model_Entity_Setup
  71:      */
  72:     public function cleanCache()
  73:     {
  74:         Mage::app()->cleanCache(array('eav'));
  75:         return $this;
  76:     }
  77: 
  78:     /**
  79:      * Install Default Group Ids
  80:      *
  81:      * @return Mage_Eav_Model_Entity_Setup
  82:      */
  83:     public function installDefaultGroupIds()
  84:     {
  85:         $setIds = $this->getAllAttributeSetIds();
  86:         foreach ($this->defaultGroupIdAssociations as $defaultGroupName => $defaultGroupId) {
  87:             foreach ($setIds as $set) {
  88:                 $groupId = $this->getTableRow('eav/attribute_group',
  89:                     'attribute_group_name', $defaultGroupName, 'attribute_group_id', 'attribute_set_id', $set
  90:                 );
  91:                 if (!$groupId) {
  92:                     $groupId = $this->getTableRow('eav/attribute_group',
  93:                         'attribute_set_id', $set, 'attribute_group_id'
  94:                     );
  95:                 }
  96:                 $this->updateTableRow('eav/attribute_group',
  97:                     'attribute_group_id', $groupId, 'default_id', $defaultGroupId
  98:                 );
  99:             }
 100:         }
 101: 
 102:         return $this;
 103:     }
 104: 
 105: 
 106: /******************* ENTITY TYPES *****************/
 107: 
 108:     /**
 109:      * Add an entity type
 110:      *
 111:      * If already exists updates the entity type with params data
 112:      *
 113:      * @param string $code
 114:      * @param array $params
 115:      * @return Mage_Eav_Model_Entity_Setup
 116:      */
 117:     public function addEntityType($code, array $params)
 118:     {
 119:         $data = array(
 120:             'entity_type_code'              => $code,
 121:             'entity_model'                  => $params['entity_model'],
 122:             'attribute_model'               => $this->_getValue($params, 'attribute_model'),
 123:             'entity_table'                  => $this->_getValue($params, 'table', 'eav/entity'),
 124:             'value_table_prefix'            => $this->_getValue($params, 'table_prefix'),
 125:             'entity_id_field'               => $this->_getValue($params, 'id_field'),
 126:             'increment_model'               => $this->_getValue($params, 'increment_model'),
 127:             'increment_per_store'           => $this->_getValue($params, 'increment_per_store', 0),
 128:             'increment_pad_length'          => $this->_getValue($params, 'increment_pad_length', 8),
 129:             'increment_pad_char'            => $this->_getValue($params, 'increment_pad_char', 0),
 130:             'additional_attribute_table'    => $this->_getValue($params, 'additional_attribute_table'),
 131:             'entity_attribute_collection'   => $this->_getValue($params, 'entity_attribute_collection'),
 132:         );
 133: 
 134:         if ($this->getEntityType($code, 'entity_type_id')) {
 135:             $this->updateEntityType($code, $data);
 136:         } else {
 137:             $this->_conn->insert($this->getTable('eav/entity_type'), $data);
 138:         }
 139: 
 140:         if (!empty($params['default_group'])) {
 141:             $defaultGroup = $params['default_group'];
 142:         } else {
 143:             $defaultGroup = $this->_defaultGroupName;
 144:         }
 145: 
 146:         $this->addAttributeSet($code, $this->_defaultAttributeSetName);
 147:         $this->addAttributeGroup($code, $this->_defaultGroupName, $this->_generalGroupName);
 148: 
 149:         return $this;
 150:     }
 151: 
 152:     /**
 153:      * Update entity row
 154:      *
 155:      * @param string $code
 156:      * @param string $field
 157:      * @param string $value
 158:      * @return Mage_Eav_Model_Entity_Setup
 159:      */
 160:     public function updateEntityType($code, $field, $value = null)
 161:     {
 162:         $this->updateTableRow('eav/entity_type',
 163:             'entity_type_id', $this->getEntityTypeId($code), $field, $value
 164:         );
 165:         return $this;
 166:     }
 167: 
 168:     /**
 169:      * Retrieve Entity Type Data
 170:      *
 171:      * @param int|string $id
 172:      * @param string $field
 173:      * @return mixed
 174:      */
 175:     public function getEntityType($id, $field = null)
 176:     {
 177:         return $this->getTableRow('eav/entity_type',
 178:             is_numeric($id) ? 'entity_type_id' : 'entity_type_code', $id, $field
 179:         );
 180:     }
 181: 
 182:     /**
 183:      * Retrieve Entity Type Id By Id or Code
 184:      *
 185:      * @param mixed $entityTypeId
 186:      * @return int
 187:      */
 188:     public function getEntityTypeId($entityTypeId)
 189:     {
 190:         if (!is_numeric($entityTypeId)) {
 191:             $entityTypeId = $this->getEntityType($entityTypeId, 'entity_type_id');
 192:         }
 193:         if (!is_numeric($entityTypeId)) {
 194:             throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong entity ID'));
 195:         }
 196: 
 197:         return $entityTypeId;
 198:     }
 199: 
 200:     /**
 201:      * Remove entity type by Id or Code
 202:      *
 203:      * @param mixed $id
 204:      * @return Mage_Eav_Model_Entity_Setup
 205:      */
 206:     public function removeEntityType($id)
 207:     {
 208:         if (is_numeric($id)) {
 209:             $this->deleteTableRow('eav/entity_type', 'entity_type_id', $id);
 210:         } else {
 211:             $this->deleteTableRow('eav/entity_type', 'entity_type_code', (string)$id);
 212:         }
 213: 
 214:         return $this;
 215:     }
 216: 
 217: /******************* ATTRIBUTE SETS *****************/
 218: 
 219:     /**
 220:      * Retrieve Attribute Set Sort order
 221:      *
 222:      * @param mixed $entityTypeId
 223:      * @param int $sortOrder
 224:      * @return int
 225:      */
 226:     public function getAttributeSetSortOrder($entityTypeId, $sortOrder = null)
 227:     {
 228:         if (!is_numeric($sortOrder)) {
 229:             $bind   = array('entity_type_id' => $this->getEntityTypeId($entityTypeId));
 230:             $select = $this->_conn->select()
 231:                 ->from($this->getTable('eav/attribute_set'), 'MAX(sort_order)')
 232:                 ->where('entity_type_id = :entity_type_id');
 233: 
 234:             $sortOrder = $this->_conn->fetchOne($select, $bind) + 1;
 235:         }
 236: 
 237:         return $sortOrder;
 238:     }
 239: 
 240:     /**
 241:      * Add Attribute Set
 242:      *
 243:      * @param mixed $entityTypeId
 244:      * @param string $name
 245:      * @param int $sortOrder
 246:      * @return Mage_Eav_Model_Entity_Setup
 247:      */
 248:     public function addAttributeSet($entityTypeId, $name, $sortOrder = null)
 249:     {
 250:         $data = array(
 251:             'entity_type_id'        => $this->getEntityTypeId($entityTypeId),
 252:             'attribute_set_name'    => $name,
 253:             'sort_order'            => $this->getAttributeSetSortOrder($entityTypeId, $sortOrder),
 254:         );
 255: 
 256:         $setId = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id');
 257:         if ($setId) {
 258:             $this->updateAttributeSet($entityTypeId, $setId, $data);
 259:         } else {
 260:             $this->_conn->insert($this->getTable('eav/attribute_set'), $data);
 261: 
 262:             $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName);
 263:         }
 264: 
 265:         return $this;
 266:     }
 267: 
 268:     /**
 269:      * Update attribute set data
 270:      *
 271:      * @param mixed $entityTypeId
 272:      * @param int $id
 273:      * @param string $field
 274:      * @param mixed $value
 275:      * @return Mage_Eav_Model_Entity_Setup
 276:      */
 277:     public function updateAttributeSet($entityTypeId, $id, $field, $value = null)
 278:     {
 279:         $this->updateTableRow('eav/attribute_set',
 280:             'attribute_set_id', $this->getAttributeSetId($entityTypeId, $id),
 281:             $field, $value,
 282:             'entity_type_id', $this->getEntityTypeId($entityTypeId)
 283:         );
 284:         return $this;
 285:     }
 286: 
 287:     /**
 288:      * Retrieve Attribute set data by id or name
 289:      *
 290:      * @param mixed $entityTypeId
 291:      * @param mixed $id
 292:      * @param string $field
 293:      * @return mixed
 294:      */
 295:     public function getAttributeSet($entityTypeId, $id, $field = null)
 296:     {
 297:         return $this->getTableRow('eav/attribute_set',
 298:             is_numeric($id) ? 'attribute_set_id' : 'attribute_set_name', $id,
 299:             $field,
 300:             'entity_type_id', $this->getEntityTypeId($entityTypeId)
 301:         );
 302:     }
 303: 
 304:     /**
 305:      * Retrieve Attribute Set Id By Id or Name
 306:      *
 307:      * @throws Mage_Eav_Exception
 308:      * @param mixed $entityTypeId
 309:      * @param mixed $setId
 310:      * @return int
 311:      */
 312:     public function getAttributeSetId($entityTypeId, $setId)
 313:     {
 314:         if (!is_numeric($setId)) {
 315:             $setId = $this->getAttributeSet($entityTypeId, $setId, 'attribute_set_id');
 316:         }
 317:         if (!is_numeric($setId)) {
 318:             throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong attribute set ID'));
 319:         }
 320: 
 321:         return $setId;
 322:     }
 323: 
 324:     /**
 325:      * Remove Attribute Set
 326:      *
 327:      * @param mixed $entityTypeId
 328:      * @param mixed $id
 329:      * @return Mage_Eav_Model_Entity_Setup
 330:      */
 331:     public function removeAttributeSet($entityTypeId, $id)
 332:     {
 333:         $this->deleteTableRow('eav/attribute_set', 'attribute_set_id', $this->getAttributeSetId($entityTypeId, $id));
 334:         return $this;
 335:     }
 336: 
 337:     /**
 338:      * Set Default Attribute Set to Entity Type
 339:      *
 340:      * @param mixed $entityType
 341:      * @return Mage_Eav_Model_Entity_Setup
 342:      */
 343:     public function setDefaultSetToEntityType($entityType, $attributeSet = 'Default')
 344:     {
 345:         $entityTypeId = $this->getEntityTypeId($entityType);
 346:         $setId        = $this->getAttributeSetId($entityTypeId, $attributeSet);
 347:         $this->updateEntityType($entityTypeId, 'default_attribute_set_id', $setId);
 348:         return $this;
 349:     }
 350: 
 351:     /**
 352:      * Get identifiers of all attribute sets
 353:      *
 354:      * @return array
 355:      */
 356:     public function getAllAttributeSetIds($entityTypeId = null)
 357:     {
 358:         $select = $this->_conn->select()
 359:             ->from($this->getTable('eav/attribute_set'), 'attribute_set_id');
 360: 
 361:         $bind = array();
 362:         if ($entityTypeId !== null) {
 363:             $bind['entity_type_id'] = $this->getEntityTypeId($entityTypeId);
 364:             $select->where('entity_type_id = :entity_type_id');
 365:         }
 366: 
 367:         return $this->_conn->fetchCol($select, $bind);
 368:     }
 369: 
 370:     /**
 371:      * Retrieve Default Attribute Set for Entity Type
 372:      *
 373:      * @param string|int $entityType
 374:      * @return int
 375:      */
 376:     public function getDefaultAttributeSetId($entityType)
 377:     {
 378:         $bind = array('entity_type' => $entityType);
 379:         if (is_numeric($entityType)) {
 380:             $where = 'entity_type_id = :entity_type';
 381:         } else {
 382:             $where = 'entity_type_code = :entity_type';
 383:         }
 384:         $select = $this->getConnection()->select()
 385:             ->from($this->getTable('eav/entity_type'), 'default_attribute_set_id')
 386:             ->where($where);
 387: 
 388:         return $this->getConnection()->fetchOne($select, $bind);
 389:     }
 390: 
 391: /******************* ATTRIBUTE GROUPS *****************/
 392: 
 393:     /**
 394:      * Retrieve Attribute Group Sort order
 395:      *
 396:      * @param mixed $entityTypeId
 397:      * @param mixed $setId
 398:      * @param int $sortOrder
 399:      * @return int
 400:      */
 401:     public function getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder = null)
 402:     {
 403:         if (!is_numeric($sortOrder)) {
 404:             $bind   = array('attribute_set_id' => $this->getAttributeSetId($entityTypeId, $setId));
 405:             $select = $this->_conn->select()
 406:                 ->from($this->getTable('eav/attribute_group'), 'MAX(sort_order)')
 407:                 ->where('attribute_set_id = :attribute_set_id');
 408: 
 409:             $sortOrder = $this->_conn->fetchOne($select, $bind) + 1;
 410:         }
 411: 
 412:         return $sortOrder;
 413:     }
 414: 
 415:     /**
 416:      * Add Attribute Group
 417:      *
 418:      * @param mixed $entityTypeId
 419:      * @param mixed $setId
 420:      * @param string $name
 421:      * @param int $sortOrder
 422:      * @return Mage_Eav_Model_Entity_Setup
 423:      */
 424:     public function addAttributeGroup($entityTypeId, $setId, $name, $sortOrder = null)
 425:     {
 426:         $setId  = $this->getAttributeSetId($entityTypeId, $setId);
 427:         $data   = array(
 428:             'attribute_set_id'      => $setId,
 429:             'attribute_group_name'  => $name,
 430:         );
 431: 
 432:         if (isset($this->defaultGroupIdAssociations[$name])) {
 433:             $data['default_id'] = $this->defaultGroupIdAssociations[$name];
 434:         }
 435: 
 436:         if ($sortOrder !== null) {
 437:             $data['sort_order'] = $sortOrder;
 438:         }
 439: 
 440:         $groupId = $this->getAttributeGroup($entityTypeId, $setId, $name, 'attribute_group_id');
 441:         if ($groupId) {
 442:             $this->updateAttributeGroup($entityTypeId, $setId, $groupId, $data);
 443:         } else {
 444:             if ($sortOrder === null) {
 445:                 $data['sort_order'] = $this->getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder);
 446:             }
 447:             $this->_conn->insert($this->getTable('eav/attribute_group'), $data);
 448:         }
 449: 
 450:         return $this;
 451:     }
 452: 
 453:     /**
 454:      * Update Attribute Group Data
 455:      *
 456:      * @param mixed $entityTypeId
 457:      * @param mixed $setId
 458:      * @param mixed $id
 459:      * @param string $field
 460:      * @param mixed $value
 461:      * @return Mage_Eav_Model_Entity_Setup
 462:      */
 463:     public function updateAttributeGroup($entityTypeId, $setId, $id, $field, $value = null)
 464:     {
 465:         $this->updateTableRow('eav/attribute_group',
 466:             'attribute_group_id', $this->getAttributeGroupId($entityTypeId, $setId, $id),
 467:             $field, $value,
 468:             'attribute_set_id', $this->getAttributeSetId($entityTypeId, $setId)
 469:         );
 470: 
 471:         return $this;
 472:     }
 473: 
 474:     /**
 475:      * Retrieve Attribute Group Data
 476:      *
 477:      * @param mixed $entityTypeId
 478:      * @param mixed $setId
 479:      * @param mixed $id
 480:      * @param string $field
 481:      * @return mixed
 482:      */
 483:     public function getAttributeGroup($entityTypeId, $setId, $id, $field = null)
 484:     {
 485:         $searchId = $id;
 486:         if (is_numeric($id)) {
 487:             $searchField = 'attribute_group_id';
 488:         } else {
 489:             if (isset($this->defaultGroupIdAssociations[$id])) {
 490:                 $searchField = 'default_id';
 491:                 $searchId = $this->defaultGroupIdAssociations[$id];
 492:             } else {
 493:                 $searchField = 'attribute_group_name';
 494:             }
 495:         }
 496: 
 497:         return $this->getTableRow('eav/attribute_group',
 498:             $searchField, $searchId, $field,
 499:             'attribute_set_id', $this->getAttributeSetId($entityTypeId, $setId)
 500:         );
 501:     }
 502: 
 503:     /**
 504:      * Retrieve Attribute Group Id by Id or Name
 505:      *
 506:      * @param mixed $entityTypeId
 507:      * @param mixed $setId
 508:      * @param mixed $groupId
 509:      * @return Mage_Eav_Model_Entity_Setup
 510:      */
 511:     public function getAttributeGroupId($entityTypeId, $setId, $groupId)
 512:     {
 513:         if (!is_numeric($groupId)) {
 514:             $groupId = $this->getAttributeGroup($entityTypeId, $setId, $groupId, 'attribute_group_id');
 515:         }
 516: 
 517:         if (!is_numeric($groupId)) {
 518:             $groupId = $this->getDefaultAttributeGroupId($entityTypeId, $setId);
 519:         }
 520: 
 521:         if (!is_numeric($groupId)) {
 522:             throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong attribute group ID'));
 523:         }
 524:         return $groupId;
 525:     }
 526: 
 527:     /**
 528:      * Remove Attribute Group By Id or Name
 529:      *
 530:      * @param mixed $entityTypeId
 531:      * @param mixed $setId
 532:      * @param mixed $id
 533:      * @return Mage_Eav_Model_Entity_Setup
 534:      */
 535:     public function removeAttributeGroup($entityTypeId, $setId, $id)
 536:     {
 537:         $this->deleteTableRow(
 538:             'eav/attribute_group',
 539:             'attribute_group_id',
 540:             $this->getAttributeGroupId($entityTypeId, $setId, $id)
 541:         );
 542:         return $this;
 543:     }
 544: 
 545:     /**
 546:      * Retrieve Default Attribute Group Id By Entity Type and Attribute Set
 547:      *
 548:      * @param string|int $entityType
 549:      * @param int $attributeSetId
 550:      * @return int
 551:      */
 552:     public function getDefaultAttributeGroupId($entityType, $attributeSetId = null)
 553:     {
 554:         $entityType = $this->getEntityTypeId($entityType);
 555:         if (!is_numeric($attributeSetId)) {
 556:             $attributeSetId = $this->getDefaultAttributeSetId($entityType);
 557:         }
 558:         $bind   = array('attribute_set_id' => $attributeSetId);
 559:         $select = $this->getConnection()->select()
 560:             ->from($this->getTable('eav/attribute_group'), 'attribute_group_id')
 561:             ->where('attribute_set_id = :attribute_set_id')
 562:             ->order(array('default_id ' . Varien_Db_Select::SQL_DESC, 'sort_order'))
 563:             ->limit(1);
 564: 
 565:         return $this->getConnection()->fetchOne($select, $bind);
 566:     }
 567: 
 568: /******************* ATTRIBUTES *****************/
 569: 
 570:     /**
 571:      * Retrieve value from array by key or return default value
 572:      *
 573:      * @param array $array
 574:      * @param string $key
 575:      * @param string $default
 576:      * @return string
 577:      */
 578:     protected function _getValue($array, $key, $default = null)
 579:     {
 580:         if (isset($array[$key]) && is_bool($array[$key])) {
 581:             $array[$key] = (int) $array[$key];
 582:         }
 583:         return isset($array[$key]) ? $array[$key] : $default;
 584:     }
 585: 
 586:     /**
 587:      * Prepare attribute values to save
 588:      *
 589:      * @param array $attr
 590:      * @return array
 591:      */
 592:     protected function _prepareValues($attr)
 593:     {
 594:         $data = array(
 595:             'backend_model'   => $this->_getValue($attr, 'backend'),
 596:             'backend_type'    => $this->_getValue($attr, 'type', 'varchar'),
 597:             'backend_table'   => $this->_getValue($attr, 'table'),
 598:             'frontend_model'  => $this->_getValue($attr, 'frontend'),
 599:             'frontend_input'  => $this->_getValue($attr, 'input', 'text'),
 600:             'frontend_label'  => $this->_getValue($attr, 'label'),
 601:             'frontend_class'  => $this->_getValue($attr, 'frontend_class'),
 602:             'source_model'    => $this->_getValue($attr, 'source'),
 603:             'is_required'     => $this->_getValue($attr, 'required', 1),
 604:             'is_user_defined' => $this->_getValue($attr, 'user_defined', 0),
 605:             'default_value'   => $this->_getValue($attr, 'default'),
 606:             'is_unique'       => $this->_getValue($attr, 'unique', 0),
 607:             'note'            => $this->_getValue($attr, 'note'),
 608:             'is_global'       => $this->_getValue($attr, 'global',
 609:                                      Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
 610:                                  ),
 611:         );
 612: 
 613:         return $data;
 614:     }
 615: 
 616:     /**
 617:      * Validate attribute data before insert into table
 618:      *
 619:      * @param  array $data
 620:      * @throws Mage_Eav_Exception
 621:      * @return true
 622:      */
 623:     protected function _validateAttributeData($data)
 624:     {
 625:         $attributeCodeMaxLength = Mage_Eav_Model_Entity_Attribute::ATTRIBUTE_CODE_MAX_LENGTH;
 626: 
 627:         if (isset($data['attribute_code']) &&
 628:            !Zend_Validate::is($data['attribute_code'], 'StringLength', array('max' => $attributeCodeMaxLength)))
 629:         {
 630:             throw Mage::exception('Mage_Eav',
 631:                 Mage::helper('eav')->__('Maximum length of attribute code must be less then %s symbols', $attributeCodeMaxLength)
 632:             );
 633:         }
 634: 
 635:         return true;
 636:     }
 637: 
 638:     /**
 639:      * Add attribute to an entity type
 640:      *
 641:      * If attribute is system will add to all existing attribute sets
 642:      *
 643:      * @param string|integer $entityTypeId
 644:      * @param string $code
 645:      * @param array $attr
 646:      * @return Mage_Eav_Model_Entity_Setup
 647:      */
 648:     public function addAttribute($entityTypeId, $code, array $attr)
 649:     {
 650:         $entityTypeId = $this->getEntityTypeId($entityTypeId);
 651:         $data = array_merge(
 652:             array(
 653:                 'entity_type_id' => $entityTypeId,
 654:                 'attribute_code' => $code
 655:             ),
 656:             $this->_prepareValues($attr)
 657:          );
 658: 
 659:         $this->_validateAttributeData($data);
 660: 
 661:         $sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
 662:         $attributeId = $this->getAttribute($entityTypeId, $code, 'attribute_id');
 663:         if ($attributeId) {
 664:             $this->updateAttribute($entityTypeId, $attributeId, $data, null, $sortOrder);
 665:         } else {
 666:             $this->_insertAttribute($data);
 667:         }
 668: 
 669:         if (!empty($attr['group']) || empty($attr['user_defined'])) {
 670:             $select = $this->_conn->select()
 671:                 ->from($this->getTable('eav/attribute_set'))
 672:                 ->where('entity_type_id = :entity_type_id');
 673:             $sets = $this->_conn->fetchAll($select, array('entity_type_id' => $entityTypeId));
 674:             foreach ($sets as $set) {
 675:                 if (!empty($attr['group'])) {
 676:                     $this->addAttributeGroup($entityTypeId, $set['attribute_set_id'],
 677:                         $attr['group']);
 678:                     $this->addAttributeToSet($entityTypeId, $set['attribute_set_id'],
 679:                         $attr['group'], $code, $sortOrder);
 680:                 } else {
 681:                     $this->addAttributeToSet($entityTypeId, $set['attribute_set_id'],
 682:                         $this->_generalGroupName, $code, $sortOrder);
 683:                 }
 684:             }
 685:         }
 686: 
 687:         if (isset($attr['option']) && is_array($attr['option'])) {
 688:             $option = $attr['option'];
 689:             $option['attribute_id'] = $this->getAttributeId($entityTypeId, $code);
 690:             $this->addAttributeOption($option);
 691:         }
 692: 
 693:         return $this;
 694:     }
 695: 
 696:     /**
 697:      * Add Attribure Option
 698:      *
 699:      * @param array $option
 700:      */
 701:     public function addAttributeOption($option)
 702:     {
 703:         $optionTable        = $this->getTable('eav/attribute_option');
 704:         $optionValueTable   = $this->getTable('eav/attribute_option_value');
 705: 
 706:         if (isset($option['value'])) {
 707:             foreach ($option['value'] as $optionId => $values) {
 708:                 $intOptionId = (int) $optionId;
 709:                 if (!empty($option['delete'][$optionId])) {
 710:                     if ($intOptionId) {
 711:                         $condition = array('option_id =?' => $intOptionId);
 712:                         $this->_conn->delete($optionTable, $condition);
 713:                     }
 714:                     continue;
 715:                 }
 716: 
 717:                 if (!$intOptionId) {
 718:                     $data = array(
 719:                         'attribute_id'  => $option['attribute_id'],
 720:                         'sort_order'    => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
 721:                     );
 722:                     $this->_conn->insert($optionTable, $data);
 723:                     $intOptionId = $this->_conn->lastInsertId($optionTable);
 724:                 } else {
 725:                     $data = array(
 726:                         'sort_order'    => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
 727:                     );
 728:                     $this->_conn->update($optionTable, $data, array('option_id=?' => $intOptionId));
 729:                 }
 730: 
 731:                 // Default value
 732:                 if (!isset($values[0])) {
 733:                     Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
 734:                 }
 735:                 $condition = array('option_id =?' => $intOptionId);
 736:                 $this->_conn->delete($optionValueTable, $condition);
 737:                 foreach ($values as $storeId => $value) {
 738:                     $data = array(
 739:                         'option_id' => $intOptionId,
 740:                         'store_id'  => $storeId,
 741:                         'value'     => $value,
 742:                     );
 743:                     $this->_conn->insert($optionValueTable, $data);
 744:                 }
 745:             }
 746:         } else if (isset($option['values'])) {
 747:             foreach ($option['values'] as $sortOrder => $label) {
 748:                 // add option
 749:                 $data = array(
 750:                     'attribute_id' => $option['attribute_id'],
 751:                     'sort_order'   => $sortOrder,
 752:                 );
 753:                 $this->_conn->insert($optionTable, $data);
 754:                 $intOptionId = $this->_conn->lastInsertId($optionTable);
 755: 
 756:                 $data = array(
 757:                     'option_id' => $intOptionId,
 758:                     'store_id'  => 0,
 759:                     'value'     => $label,
 760:                 );
 761:                 $this->_conn->insert($optionValueTable, $data);
 762:             }
 763:         }
 764:     }
 765: 
 766:     /**
 767:      * Update Attribute data and Attribute additional data
 768:      *
 769:      * @param mixed $entityTypeId
 770:      * @param mixed $id
 771:      * @param string $field
 772:      * @param mixed $value
 773:      * @param int $sortOrder
 774:      * @return Mage_Eav_Model_Entity_Setup
 775:      */
 776:     public function updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null)
 777:     {
 778:         $this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
 779:         $this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
 780:         return $this;
 781:     }
 782: 
 783:     /**
 784:      * Update Attribute data
 785:      *
 786:      * @param mixed $entityTypeId
 787:      * @param mixed $id
 788:      * @param string $field
 789:      * @param mixed $value
 790:      * @param int $sortOrder
 791:      * @return Mage_Eav_Model_Entity_Setup
 792:      */
 793:     protected function _updateAttribute($entityTypeId, $id, $field, $value = null, $sortOrder = null)
 794:     {
 795:         if ($sortOrder !== null) {
 796:             $this->updateTableRow('eav/entity_attribute',
 797:                 'attribute_id', $this->getAttributeId($entityTypeId, $id),
 798:                 'sort_order', $sortOrder
 799:             );
 800:         }
 801: 
 802:         $attributeFields = $this->_getAttributeTableFields();
 803:         if (is_array($field)) {
 804:             $bind = array();
 805:             foreach ($field as $k => $v) {
 806:                 if (isset($attributeFields[$k])) {
 807:                     $bind[$k] = $this->getConnection()->prepareColumnValue($attributeFields[$k], $v);
 808:                 }
 809:             }
 810:             if (!$bind) {
 811:                 return $this;
 812:             }
 813:             $field = $bind;
 814:         } else {
 815:             if (!isset($attributeFields[$field])) {
 816:                 return $this;
 817:             }
 818:         }
 819: 
 820:         $this->updateTableRow('eav/attribute',
 821:             'attribute_id', $this->getAttributeId($entityTypeId, $id),
 822:             $field, $value,
 823:             'entity_type_id', $this->getEntityTypeId($entityTypeId)
 824:         );
 825: 
 826:         return $this;
 827:     }
 828: 
 829:     /**
 830:      * Update Attribute Additional data
 831:      *
 832:      * @param mixed $entityTypeId
 833:      * @param mixed $id
 834:      * @param string $field
 835:      * @param mixed $value
 836:      * @return Mage_Eav_Model_Entity_Setup
 837:      */
 838:     protected function _updateAttributeAdditionalData($entityTypeId, $id, $field, $value = null)
 839:     {
 840:         $additionalTable = $this->getEntityType($entityTypeId, 'additional_attribute_table');
 841:         if (!$additionalTable) {
 842:             return $this;
 843:         }
 844:         $additionalTableExists = $this->getConnection()->isTableExists($this->getTable($additionalTable));
 845:         if ($additionalTable && $additionalTableExists) {
 846:             $attributeFields = $this->getConnection()->describeTable($this->getTable($additionalTable));
 847:             if (is_array($field)) {
 848:                 $bind = array();
 849:                 foreach ($field as $k => $v) {
 850:                     if (isset($attributeFields[$k])) {
 851:                         $bind[$k] = $this->getConnection()->prepareColumnValue($attributeFields[$k], $v);
 852:                     }
 853:                 }
 854:                 if (!$bind) {
 855:                     return $this;
 856:                 }
 857:                 $field = $bind;
 858:             } else {
 859:                 if (!isset($attributeFields[$field])) {
 860:                     return $this;
 861:                 }
 862:             }
 863:             $this->updateTableRow($this->getTable($additionalTable),
 864:                 'attribute_id', $this->getAttributeId($entityTypeId, $id),
 865:                 $field, $value
 866:             );
 867:         }
 868: 
 869:         return $this;
 870:     }
 871: 
 872:     /**
 873:      * Retrieve Attribute Data By Id or Code
 874:      *
 875:      * @param mixed $entityTypeId
 876:      * @param mixed $id
 877:      * @param string $field
 878:      * @return mixed
 879:      */
 880:     public function getAttribute($entityTypeId, $id, $field = null)
 881:     {
 882:         $additionalTable    = $this->getEntityType($entityTypeId, 'additional_attribute_table');
 883:         $entityTypeId       = $this->getEntityTypeId($entityTypeId);
 884:         $idField            = is_numeric($id) ? 'attribute_id' : 'attribute_code';
 885:         if (!$additionalTable) {
 886:             return $this->getTableRow('eav/attribute', $idField, $id, $field, 'entity_type_id', $entityTypeId);
 887:         }
 888: 
 889:         $mainTable          = $this->getTable('eav/attribute');
 890:         if (empty($this->_setupCache[$mainTable][$entityTypeId][$id])) {
 891:             $additionalTable = $this->getTable($additionalTable);
 892:             $bind = array(
 893:                 'id'                => $id,
 894:                 'entity_type_id'    => $entityTypeId
 895:             );
 896:             $select = $this->_conn->select()
 897:                 ->from(array('main' => $mainTable))
 898:                 ->join(
 899:                     array('additional' => $additionalTable),
 900:                     'main.attribute_id = additional.attribute_id')
 901:                 ->where("main.{$idField} = :id")
 902:                 ->where('main.entity_type_id = :entity_type_id');
 903: 
 904:             $row = $this->_conn->fetchRow($select, $bind);
 905:             if (!$row) {
 906:                 $this->_setupCache[$mainTable][$entityTypeId][$id] = false;
 907:             } else {
 908:                 $this->_setupCache[$mainTable][$entityTypeId][$row['attribute_id']] = $row;
 909:                 $this->_setupCache[$mainTable][$entityTypeId][$row['attribute_code']] = $row;
 910:             }
 911:         }
 912: 
 913:         $row = $this->_setupCache[$mainTable][$entityTypeId][$id];
 914:         if ($field !== null) {
 915:             return isset($row[$field]) ? $row[$field] : false;
 916:         }
 917: 
 918:         return $row;
 919:     }
 920: 
 921:     /**
 922:      * Retrieve Attribute Id Data By Id or Code
 923:      *
 924:      * @param mixed $entityTypeId
 925:      * @param mixed $id
 926:      * @return int
 927:      */
 928:     public function getAttributeId($entityTypeId, $id)
 929:     {
 930:         if (!is_numeric($id)) {
 931:             $id = $this->getAttribute($entityTypeId, $id, 'attribute_id');
 932:         }
 933:         if (!is_numeric($id)) {
 934:             return false;
 935:         }
 936:         return $id;
 937:     }
 938: 
 939:     /**
 940:      * Return table name for eav attribute
 941:      *
 942:      * @param int|string $entityTypeId Entity Type id or Entity Type code
 943:      * @param int|string $id Attribute id or Attribute code
 944:      * @return string
 945:      */
 946:     public function getAttributeTable($entityTypeId, $id)
 947:     {
 948:         $entityKeyName    = is_numeric($entityTypeId) ? 'entity_type_id' : 'entity_type_code';
 949:         $attributeKeyName = is_numeric($id) ? 'attribute_id' : 'attribute_code';
 950: 
 951:         $bind = array(
 952:             'id'                => $id,
 953:             'entity_type_id'    => $entityTypeId
 954:         );
 955:         $select = $this->getConnection()->select()
 956:             ->from(
 957:                 array('entity_type' => $this->getTable('eav/entity_type')),
 958:                 array('entity_table'))
 959:             ->join(
 960:                 array('attribute' => $this->getTable('eav/attribute')),
 961:                 'attribute.entity_type_id = entity_type.entity_type_id',
 962:                 array('backend_type'))
 963:             ->where("entity_type.{$entityKeyName} = :entity_type_id")
 964:             ->where("attribute.{$attributeKeyName} = :id")
 965:             ->limit(1);
 966: 
 967:         $result = $this->getConnection()->fetchRow($select, $bind);
 968:         if ($result) {
 969:             $table = $this->getTable($result['entity_table']);
 970:             if ($result['backend_type'] != 'static') {
 971:                 $table .= '_' . $result['backend_type'];
 972:             }
 973:             return $table;
 974:         }
 975: 
 976:         return false;
 977:     }
 978: 
 979:     /**
 980:      * Remove Attribute
 981:      *
 982:      * @param mixed $entityTypeId
 983:      * @param mixed $code
 984:      * @return Mage_Eav_Model_Entity_Setup
 985:      */
 986:     public function removeAttribute($entityTypeId, $code)
 987:     {
 988:         $mainTable  = $this->getTable('eav/attribute');
 989:         $attribute  = $this->getAttribute($entityTypeId, $code);
 990:         if ($attribute) {
 991:             $this->deleteTableRow('eav/attribute', 'attribute_id', $attribute['attribute_id']);
 992:             if (isset($this->_setupCache[$mainTable][$attribute['entity_type_id']][$attribute['attribute_code']])) {
 993:                 unset($this->_setupCache[$mainTable][$attribute['entity_type_id']][$attribute['attribute_code']]);
 994:             }
 995:         }
 996:         return $this;
 997:     }
 998: 
 999:     /**
1000:      * Retrieve Attribute Sort Order
1001:      *
1002:      * @param mixed $entityTypeId
1003:      * @param mixed $setId
1004:      * @param mixed $groupId
1005:      * @param int $sortOrder
1006:      * @return Mage_Eav_Model_Entity_Setup
1007:      */
1008:     public function getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder = null)
1009:     {
1010:         if (!is_numeric($sortOrder)) {
1011:             $bind = array('attribute_group_id' => $this->getAttributeGroupId($entityTypeId, $setId, $groupId));
1012:             $select = $this->_conn->select()
1013:                 ->from($this->getTable('eav/entity_attribute'), 'MAX(sort_order)')
1014:                 ->where('attribute_group_id = :attribute_group_id');
1015: 
1016:             $sortOrder = $this->_conn->fetchOne($select, $bind) + 1;
1017:         }
1018: 
1019:         return $sortOrder;
1020:     }
1021: 
1022:     /**
1023:      * Add Attribute to All Groups on Attribute Set
1024:      *
1025:      * @param mixed $entityTypeId
1026:      * @param mixed $setId
1027:      * @param mixed $groupId
1028:      * @param mixed $attributeId
1029:      * @param int $sortOrder
1030:      * @return Mage_Eav_Model_Entity_Setup
1031:      */
1032:     public function addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)
1033:     {
1034:         $entityTypeId   = $this->getEntityTypeId($entityTypeId);
1035:         $setId          = $this->getAttributeSetId($entityTypeId, $setId);
1036:         $groupId        = $this->getAttributeGroupId($entityTypeId, $setId, $groupId);
1037:         $attributeId    = $this->getAttributeId($entityTypeId, $attributeId);
1038:         $table          = $this->getTable('eav/entity_attribute');
1039: 
1040:         $bind = array(
1041:             'attribute_set_id' => $setId,
1042:             'attribute_id'     => $attributeId
1043:         );
1044:         $select = $this->_conn->select()
1045:             ->from($table)
1046:             ->where('attribute_set_id = :attribute_set_id')
1047:             ->where('attribute_id = :attribute_id');
1048:         $result = $this->_conn->fetchRow($select, $bind);
1049: 
1050:         if ($result) {
1051:             if ($result['attribute_group_id'] != $groupId) {
1052:                 $where = array('entity_attribute_id =?' => $result['entity_attribute_id']);
1053:                 $data  = array('attribute_group_id' => $groupId);
1054:                 $this->_conn->update($table, $data, $where);
1055:             }
1056:         } else {
1057:             $data = array(
1058:                 'entity_type_id'        => $entityTypeId,
1059:                 'attribute_set_id'      => $setId,
1060:                 'attribute_group_id'    => $groupId,
1061:                 'attribute_id'          => $attributeId,
1062:                 'sort_order'            => $this->getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder),
1063:             );
1064: 
1065:             $this->_conn->insert($table, $data);
1066:         }
1067: 
1068:         return $this;
1069:     }
1070: 
1071:     /**
1072:      * Add or update attribute to group
1073:      *
1074:      * @param int|string $entityType
1075:      * @param int|string $setId
1076:      * @param int|string $groupId
1077:      * @param int|string $attributeId
1078:      * @param int $sortOrder
1079:      * @return Mage_Eav_Model_Entity_Setup
1080:      */
1081:     public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId, $sortOrder = null)
1082:     {
1083:         $entityType  = $this->getEntityTypeId($entityType);
1084:         $setId       = $this->getAttributeSetId($entityType, $setId);
1085:         $groupId     = $this->getAttributeGroupId($entityType, $setId, $groupId);
1086:         $attributeId = $this->getAttributeId($entityType, $attributeId);
1087: 
1088:         $data = array(
1089:             'entity_type_id'        => $entityType,
1090:             'attribute_set_id'      => $setId,
1091:             'attribute_group_id'    => $groupId,
1092:             'attribute_id'          => $attributeId,
1093:         );
1094: 
1095:         $bind = array(
1096:             'entity_type_id'    => $entityType,
1097:             'attribute_set_id'  => $setId,
1098:             'attribute_id'      => $attributeId
1099:         );
1100:         $select = $this->getConnection()->select()
1101:             ->from($this->getTable('eav/entity_attribute'))
1102:             ->where('entity_type_id = :entity_type_id')
1103:             ->where('attribute_set_id = :attribute_set_id')
1104:             ->where('attribute_id = :attribute_id');
1105:         $row = $this->getConnection()->fetchRow($select, $bind);
1106:         if ($row) {
1107:             // update
1108:             if ($sortOrder !== null) {
1109:                 $data['sort_order'] = $sortOrder;
1110:             }
1111: 
1112:             $this->getConnection()->update(
1113:                 $this->getTable('eav/entity_attribute'),
1114:                 $data,
1115:                 $this->getConnection()->quoteInto('entity_attribute_id=?', $row['entity_attribute_id'])
1116:             );
1117:         } else {
1118:             if ($sortOrder === null) {
1119:                 $select = $this->getConnection()->select()
1120:                     ->from($this->getTable('eav/entity_attribute'), 'MAX(sort_order)')
1121:                     ->where('entity_type_id = :entity_type_id')
1122:                     ->where('attribute_set_id = :attribute_set_id')
1123:                     ->where('attribute_id = :attribute_id');
1124: 
1125:                 $sortOrder = $this->getConnection()->fetchOne($select, $bind) + 10;
1126:             }
1127:             $sortOrder = is_numeric($sortOrder) ? $sortOrder : 1;
1128:             $data['sort_order'] = $sortOrder;
1129:             $this->getConnection()->insert($this->getTable('eav/entity_attribute'), $data);
1130:         }
1131: 
1132:         return $this;
1133:     }
1134: 
1135: /******************* BULK INSTALL *****************/
1136: 
1137:     /**
1138:      * Install entities
1139:      *
1140:      * @param array $entities
1141:      * @return Mage_Eav_Model_Entity_Setup
1142:      */
1143:     public function installEntities($entities = null)
1144:     {
1145:         $this->cleanCache();
1146: 
1147:         if ($entities === null) {
1148:             $entities = $this->getDefaultEntities();
1149:         }
1150: 
1151:         foreach ($entities as $entityName=>$entity) {
1152:             $this->addEntityType($entityName, $entity);
1153: 
1154:             $frontendPrefix = isset($entity['frontend_prefix']) ? $entity['frontend_prefix'] : '';
1155:             $backendPrefix  = isset($entity['backend_prefix']) ? $entity['backend_prefix'] : '';
1156:             $sourcePrefix   = isset($entity['source_prefix']) ? $entity['source_prefix'] : '';
1157: 
1158:             foreach ($entity['attributes'] as $attrCode => $attr) {
1159:                 if (!empty($attr['backend'])) {
1160:                     if ('_' === $attr['backend']) {
1161:                         $attr['backend'] = $backendPrefix;
1162:                     } elseif ('_' === $attr['backend']{0}) {
1163:                         $attr['backend'] = $backendPrefix.$attr['backend'];
1164:                     } else {
1165:                         $attr['backend'] = $attr['backend'];
1166:                     }
1167:                 }
1168:                 if (!empty($attr['frontend'])) {
1169:                     if ('_' === $attr['frontend']) {
1170:                         $attr['frontend'] = $frontendPrefix;
1171:                     } elseif ('_' === $attr['frontend']{0}) {
1172:                         $attr['frontend'] = $frontendPrefix.$attr['frontend'];
1173:                     } else {
1174:                         $attr['frontend'] = $attr['frontend'];
1175:                     }
1176:                 }
1177:                 if (!empty($attr['source'])) {
1178:                     if ('_' === $attr['source']) {
1179:                         $attr['source'] = $sourcePrefix;
1180:                     } elseif ('_' === $attr['source']{0}) {
1181:                         $attr['source'] = $sourcePrefix . $attr['source'];
1182:                     } else {
1183:                         $attr['source'] = $attr['source'];
1184:                     }
1185:                 }
1186: 
1187:                 $this->addAttribute($entityName, $attrCode, $attr);
1188:             }
1189:             $this->setDefaultSetToEntityType($entityName);
1190:         }
1191: 
1192:         return $this;
1193:     }
1194: 
1195: 
1196: /****************************** CREATE ENTITY TABLES ***********************************/
1197: 
1198:     /**
1199:      * Create entity tables
1200:      *
1201:      * @param string $baseName
1202:      * @param array $options
1203:      * - no-main
1204:      * - no-default-types
1205:      * - types
1206:      * @return unknown
1207:      */
1208:     public function createEntityTables($baseTableName, array $options = array())
1209:     {
1210:         $isNoCreateMainTable = $this->_getValue($options, 'no-main', false);
1211:         $isNoDefaultTypes    = $this->_getValue($options, 'no-default-types', false);
1212:         $customTypes         = $this->_getValue($options, 'types', array());
1213:         $tables              = array();
1214: 
1215:         if (!$isNoCreateMainTable) {
1216:             /**
1217:              * Create table main eav table
1218:              */
1219:             $connection = $this->getConnection();
1220:             $mainTable = $connection
1221:                 ->newTable($this->getTable($baseTableName))
1222:                 ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
1223:                     'identity'  => true,
1224:                     'nullable'  => false,
1225:                     'primary'   => true,
1226:                  ), 'Entity Id')
1227:                 ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1228:                     'unsigned'  => true,
1229:                     'nullable'  => false,
1230:                     'default'   => '0',
1231:                 ), 'Entity Type Id')
1232:                 ->addColumn('attribute_set_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1233:                     'unsigned'  => true,
1234:                     'nullable'  => false,
1235:                     'default'   => '0',
1236:                 ), 'Attribute Set Id')
1237:                 ->addColumn('increment_id', Varien_Db_Ddl_Table::TYPE_TEXT, 50, array(
1238:                     'nullable'  => false,
1239:                     'default'   => '',
1240:                 ), 'Increment Id')
1241:                 ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1242:                     'unsigned'  => true,
1243:                     'nullable'  => false,
1244:                     'default'   => '0',
1245:                 ), 'Store Id')
1246:                 ->addColumn('created_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
1247:                     'nullable'  => false,
1248:                 ), 'Created At')
1249:                 ->addColumn('updated_at', Varien_Db_Ddl_Table::TYPE_TIMESTAMP, null, array(
1250:                     'nullable'  => false,
1251:                 ), 'Updated At')
1252:                 ->addColumn('is_active', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1253:                     'unsigned'  => true,
1254:                     'nullable'  => false,
1255:                     'default'   => '1',
1256:                 ), 'Defines Is Entity Active')
1257:                 ->addIndex($this->getIdxName($baseTableName, array('entity_type_id')),
1258:                     array('entity_type_id'))
1259:                 ->addIndex($this->getIdxName($baseTableName, array('store_id')),
1260:                     array('store_id'))
1261:                 ->addForeignKey($this->getFkName($baseTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
1262:                     'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
1263:                     Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
1264:                 ->addForeignKey($this->getFkName($baseTableName, 'store_id', 'core/store', 'store_id'),
1265:                     'store_id', $this->getTable('core/store'), 'store_id',
1266:                     Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
1267:                 ->setComment('Eav Entity Main Table');
1268: 
1269:             $tables[$this->getTable($baseTableName)] = $mainTable;
1270:         }
1271: 
1272:         $types = array();
1273:         if (!$isNoDefaultTypes) {
1274:             $types = array(
1275:                 'datetime'  => array(Varien_Db_Ddl_Table::TYPE_DATETIME, null),
1276:                 'decimal'   => array(Varien_Db_Ddl_Table::TYPE_DECIMAL, '12,4'),
1277:                 'int'       => array(Varien_Db_Ddl_Table::TYPE_INTEGER, null),
1278:                 'text'      => array(Varien_Db_Ddl_Table::TYPE_TEXT, '64k'),
1279:                 'varchar'   => array(Varien_Db_Ddl_Table::TYPE_TEXT, '255'),
1280:                 'char'   => array(Varien_Db_Ddl_Table::TYPE_TEXT, '255')
1281:             );
1282:         }
1283: 
1284:         if (!empty($customTypes)) {
1285:             foreach ($customTypes as $type => $fieldType) {
1286:                 if (count($fieldType) != 2) {
1287:                     throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong type definition for %s', $type));
1288:                 }
1289:                 $types[$type] = $fieldType;
1290:             }
1291:         }
1292: 
1293:         /**
1294:          * Create table array($baseTableName, $type)
1295:          */
1296:         foreach ($types as $type => $fieldType) {
1297:             $eavTableName = array($baseTableName, $type);
1298: 
1299:             $eavTable = $connection->newTable($this->getTable($eavTableName));
1300:             $eavTable
1301:                 ->addColumn('value_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
1302:                     'identity'  => true,
1303:                     'nullable'  => false,
1304:                     'primary'   => true,
1305:                     ), 'Value Id')
1306:                 ->addColumn('entity_type_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1307:                     'unsigned'  => true,
1308:                     'nullable'  => false,
1309:                     'default'   => '0',
1310:                     ), 'Entity Type Id')
1311:                 ->addColumn('attribute_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1312:                     'unsigned'  => true,
1313:                     'nullable'  => false,
1314:                     'default'   => '0',
1315:                     ), 'Attribute Id')
1316:                 ->addColumn('store_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
1317:                     'unsigned'  => true,
1318:                     'nullable'  => false,
1319:                     'default'   => '0',
1320:                     ), 'Store Id')
1321:                 ->addColumn('entity_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
1322:                     'unsigned'  => true,
1323:                     'nullable'  => false,
1324:                     'default'   => '0',
1325:                     ), 'Entity Id')
1326:                 ->addColumn('value', $fieldType[0], $fieldType[1], array(
1327:                     'nullable'  => false,
1328:                     ), 'Attribute Value')
1329:                 ->addIndex($this->getIdxName($eavTableName, array('entity_type_id')),
1330:                     array('entity_type_id'))
1331:                 ->addIndex($this->getIdxName($eavTableName, array('attribute_id')),
1332:                     array('attribute_id'))
1333:                 ->addIndex($this->getIdxName($eavTableName, array('store_id')),
1334:                     array('store_id'))
1335:                 ->addIndex($this->getIdxName($eavTableName, array('entity_id')),
1336:                     array('entity_id'));
1337:             if ($type !== 'text') {
1338:                 $eavTable->addIndex($this->getIdxName($eavTableName, array('attribute_id', 'value')),
1339:                     array('attribute_id', 'value'));
1340:                 $eavTable->addIndex($this->getIdxName($eavTableName, array('entity_type_id', 'value')),
1341:                     array('entity_type_id', 'value'));
1342:             }
1343: 
1344:             $eavTable
1345:                 ->addForeignKey($this->getFkName($eavTableName, 'entity_id', $baseTableName, 'entity_id'),
1346:                     'entity_id', $this->getTable($baseTableName), 'entity_id',
1347:                     Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
1348:                 ->addForeignKey($this->getFkName($eavTableName, 'entity_type_id', 'eav/entity_type', 'entity_type_id'),
1349:                     'entity_type_id', $this->getTable('eav/entity_type'), 'entity_type_id',
1350:                     Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
1351:                 ->addForeignKey($this->getFkName($eavTableName, 'store_id', 'core/store', 'store_id'),
1352:                     'store_id', $this->getTable('core/store'), 'store_id',
1353:                     Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE)
1354:                 ->setComment('Eav Entity Value Table');
1355: 
1356:             $tables[$this->getTable($eavTableName)] = $eavTable;
1357:         }
1358: 
1359:         $connection->beginTransaction();
1360:         try {
1361:             foreach ($tables as $tableName => $table) {
1362:                 $connection->createTable($table);
1363:             }
1364:             $connection->commit();
1365:         } catch (Exception $e) {
1366:            $connection->rollBack();
1367:            throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Can\'t create table: %s', $tableName));
1368:         }
1369: 
1370:         return $this;
1371:     }
1372: 
1373:     /**
1374:      * Retrieve attribute table fields
1375:      *
1376:      * @return array
1377:      */
1378:     protected function _getAttributeTableFields()
1379:     {
1380:         return $this->getConnection()->describeTable($this->getTable('eav/attribute'));
1381:     }
1382: 
1383:     /**
1384:      * Insert attribute and filter data
1385:      *
1386:      * @param array $data
1387:      * @return Mage_Eav_Model_Entity_Setup
1388:      */
1389:     protected function _insertAttribute(array $data)
1390:     {
1391:         $bind   = array();
1392: 
1393:         $fields = $this->_getAttributeTableFields();
1394: 
1395:         foreach ($data as $k => $v) {
1396:             if (isset($fields[$k])) {
1397:                 $bind[$k] = $this->getConnection()->prepareColumnValue($fields[$k], $v);
1398:             }
1399:         }
1400:         if (!$bind) {
1401:             return $this;
1402:         }
1403: 
1404:         $this->getConnection()->insert($this->getTable('eav/attribute'), $bind);
1405:         $attributeId = $this->getConnection()->lastInsertId($this->getTable('eav/attribute'));
1406:         $this->_insertAttributeAdditionalData(
1407:             $data['entity_type_id'],
1408:             array_merge(array('attribute_id' => $attributeId), $data)
1409:         );
1410: 
1411:         return $this;
1412:     }
1413: 
1414:     /**
1415:      * Insert attribute additional data
1416:      *
1417:      * @param int $entityTypeId
1418:      * @param array $data
1419:      * @return Mage_Eav_Model_Entity_Setup
1420:      */
1421:     protected function _insertAttributeAdditionalData($entityTypeId, array $data)
1422:     {
1423:         $additionalTable = $this->getEntityType($entityTypeId, 'additional_attribute_table');
1424:         if (!$additionalTable) {
1425:             return $this;
1426:         }
1427:         $additionalTableExists = $this->getConnection()->isTableExists($this->getTable($additionalTable));
1428:         if ($additionalTable && $additionalTableExists) {
1429:             $bind   = array();
1430:             $fields = $this->getConnection()->describeTable($this->getTable($additionalTable));
1431:             foreach ($data as $k => $v) {
1432:                 if (isset($fields[$k])) {
1433:                     $bind[$k] = $this->getConnection()->prepareColumnValue($fields[$k], $v);
1434:                 }
1435:             }
1436:             if (!$bind) {
1437:                 return $this;
1438:             }
1439:             $this->getConnection()->insert($this->getTable($additionalTable), $bind);
1440:         }
1441: 
1442:         return $this;
1443:     }
1444: }
1445: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0