00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Eav_Model_Entity_Setup extends Mage_Core_Model_Resource_Setup
00036 {
00037 protected $_attributeTableFields;
00038 protected $_generalGroupName = 'General';
00039
00040 public $defaultGroupIdAssociations = array('General'=>1);
00041
00042
00043
00044
00045
00046
00047 public function cleanCache()
00048 {
00049 Mage::app()->cleanCache(array('eav'));
00050 return $this;
00051 }
00052
00053
00054
00055
00056
00057
00058 public function installDefaultGroupIds()
00059 {
00060 $setIds = $this->getAllAttributeSetIds();
00061 foreach ($this->defaultGroupIdAssociations as $defaultGroupName=>$defaultGroupId) {
00062 foreach ($setIds as $set) {
00063 $groupId = $this->getTableRow('eav/attribute_group',
00064 'attribute_group_name', $defaultGroupName, 'attribute_group_id', 'attribute_set_id', $set
00065 );
00066 if (!$groupId) {
00067 $groupId = $this->getTableRow('eav/attribute_group',
00068 'attribute_set_id', $set, 'attribute_group_id'
00069 );
00070 }
00071 $this->updateTableRow('eav/attribute_group',
00072 'attribute_group_id', $groupId,
00073 'default_id', $defaultGroupId
00074 );
00075 }
00076 }
00077 return $this;
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 public function addEntityType($code, array $params)
00093 {
00094 $data = array(
00095 'entity_type_code' => $code,
00096 'entity_model' => $params['entity_model'],
00097 'attribute_model' => isset($params['attribute_model']) ? $params['attribute_model'] : '',
00098 'entity_table' => isset($params['table']) ? $params['table'] : 'eav/entity',
00099 'increment_model' => isset($params['increment_model']) ? $params['increment_model'] : '',
00100 'increment_per_store' => isset($params['increment_per_store']) ? $params['increment_per_store'] : 0,
00101 );
00102
00103 if ($this->getEntityType($code, 'entity_type_id')) {
00104 $this->updateEntityType($code, $data);
00105 } else {
00106 $this->_conn->insert($this->getTable('eav/entity_type'), $data);
00107 }
00108
00109 $this->addAttributeSet($code, 'Default');
00110 $this->addAttributeGroup($code, 'Default', $this->_generalGroupName);
00111
00112 return $this;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 public function updateEntityType($code, $field, $value=null)
00124 {
00125 $this->updateTableRow('eav/entity_type',
00126 'entity_type_id', $this->getEntityTypeId($code),
00127 $field, $value
00128 );
00129 return $this;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 public function getEntityType($id, $field=null)
00140 {
00141 return $this->getTableRow('eav/entity_type',
00142 is_numeric($id) ? 'entity_type_id' : 'entity_type_code', $id,
00143 $field
00144 );
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 public function getEntityTypeId($entityTypeId)
00154 {
00155 if (!is_numeric($entityTypeId)) {
00156 $entityTypeId = $this->getEntityType($entityTypeId, 'entity_type_id');
00157 }
00158 if (!is_numeric($entityTypeId)) {
00159 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong entity ID'));
00160 }
00161 return $entityTypeId;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170 public function removeEntityType($id)
00171 {
00172 if (is_numeric($id)) {
00173 $this->deleteTableRow('eav/entity_type', 'entity_type_id', $id);
00174 }
00175 else {
00176 $this->deleteTableRow('eav/entity_type', 'entity_type_code', (string)$id);
00177 }
00178 return $this;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 public function getAttributeSetSortOrder($entityTypeId, $sortOrder=null)
00191 {
00192 if (!is_numeric($sortOrder)) {
00193 $sortOrder = $this->_conn->fetchOne("select max(sort_order)
00194 from ".$this->getTable('eav/attribute_set')."
00195 where entity_type_id=".$this->getEntityTypeId($entityTypeId)
00196 );
00197 $sortOrder++;
00198 }
00199 return $sortOrder;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 public function addAttributeSet($entityTypeId, $name, $sortOrder=null)
00211 {
00212 $data = array(
00213 'entity_type_id'=>$this->getEntityTypeId($entityTypeId),
00214 'attribute_set_name'=>$name,
00215 'sort_order'=>$this->getAttributeSetSortOrder($entityTypeId, $sortOrder),
00216 );
00217
00218 if ($id = $this->getAttributeSet($entityTypeId, $name, 'attribute_set_id')) {
00219 $this->updateAttributeSet($entityTypeId, $id, $data);
00220 } else {
00221 $this->_conn->insert($this->getTable('eav/attribute_set'), $data);
00222
00223 $this->addAttributeGroup($entityTypeId, $name, $this->_generalGroupName);
00224 }
00225
00226 return $this;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 public function updateAttributeSet($entityTypeId, $id, $field, $value=null)
00239 {
00240 $this->updateTableRow('eav/attribute_set',
00241 'attribute_set_id', $this->getAttributeSetId($entityTypeId, $id),
00242 $field, $value,
00243 'entity_type_id', $this->getEntityTypeId($entityTypeId)
00244 );
00245 return $this;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 public function getAttributeSet($entityTypeId, $id, $field=null)
00257 {
00258 return $this->getTableRow('eav/attribute_set',
00259 is_numeric($id) ? 'attribute_set_id' : 'attribute_set_name', $id,
00260 $field,
00261 'entity_type_id', $this->getEntityTypeId($entityTypeId)
00262 );
00263 }
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 public function getAttributeSetId($entityTypeId, $setId)
00274 {
00275 if (!is_numeric($setId)) {
00276 $setId = $this->getAttributeSet($entityTypeId, $setId, 'attribute_set_id');
00277 }
00278 if (!is_numeric($setId)) {
00279 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong attribute set ID'));
00280 }
00281 return $setId;
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291 public function removeAttributeSet($entityTypeId, $id)
00292 {
00293 $this->deleteTableRow('eav/attribute_set', 'attribute_set_id', $this->getAttributeSetId($entityTypeId, $id));
00294 return $this;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303 public function setDefaultSetToEntityType($entityType, $attributeSet = 'Default')
00304 {
00305 $entityTypeId = $this->getEntityTypeId($entityType);
00306 $setId = $this->getAttributeSetId($entityTypeId, $attributeSet);
00307 $this->updateEntityType($entityTypeId, 'default_attribute_set_id', $setId);
00308 return $this;
00309 }
00310
00311
00312
00313
00314
00315
00316 public function getAllAttributeSetIds($entityTypeId=null)
00317 {
00318 $where = '';
00319 if (!is_null($entityTypeId)) {
00320 $where = " WHERE `entity_type_id` = '" . $this->getEntityTypeId($entityTypeId) . "'";
00321 }
00322 $sql = "SELECT `attribute_set_id` FROM `{$this->getTable('eav/attribute_set')}`" . $where;
00323 return $this->_conn->fetchCol($sql);
00324 }
00325
00326
00327
00328
00329
00330
00331
00332 public function getDefaultAttributeSetId($entityType)
00333 {
00334 $select = $this->getConnection()->select()
00335 ->from($this->getTable('eav/entity_type'), 'default_attribute_set_id')
00336 ->where(is_numeric($entityType) ? 'entity_type_id=?' : 'entity_type_code=?', $entityType);
00337 return $this->getConnection()->fetchOne($select);
00338 }
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 public function getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder=null)
00351 {
00352 if (!is_numeric($sortOrder)) {
00353 $sortOrder = $this->_conn->fetchOne("select max(sort_order)
00354 from ".$this->getTable('eav/attribute_group')."
00355 where attribute_set_id=".$this->getAttributeSetId($entityTypeId, $setId)
00356 );
00357 $sortOrder++;
00358 }
00359 return $sortOrder;
00360 }
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 public function addAttributeGroup($entityTypeId, $setId, $name, $sortOrder=null)
00372 {
00373 $setId = $this->getAttributeSetId($entityTypeId, $setId);
00374 $data = array(
00375 'attribute_set_id'=>$setId,
00376 'attribute_group_name'=>$name,
00377 );
00378 if (isset($this->defaultGroupIdAssociations[$name])) {
00379 $data['default_id'] = $this->defaultGroupIdAssociations[$name];
00380 }
00381 if (!is_null($sortOrder)) {
00382 $data['sort_order'] = $sortOrder;
00383 }
00384
00385 if ($id = $this->getAttributeGroup($entityTypeId, $setId, $name, 'attribute_group_id')) {
00386 $this->updateAttributeGroup($entityTypeId, $setId, $id, $data);
00387 } else {
00388 if (is_null($sortOrder)) {
00389 $data['sort_order'] = $this->getAttributeGroupSortOrder($entityTypeId, $setId, $sortOrder);
00390 }
00391 $this->_conn->insert($this->getTable('eav/attribute_group'), $data);
00392 }
00393
00394 return $this;
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 public function updateAttributeGroup($entityTypeId, $setId, $id, $field, $value=null)
00408 {
00409 $this->updateTableRow('eav/attribute_group',
00410 'attribute_group_id', $this->getAttributeGroupId($entityTypeId, $setId, $id),
00411 $field, $value,
00412 'attribute_set_id', $this->getAttributeSetId($entityTypeId, $setId)
00413 );
00414 return $this;
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 public function getAttributeGroup($entityTypeId, $setId, $id, $field=null)
00427 {
00428 $searchId = $id;
00429 if (is_numeric($id)) {
00430 $searchField = 'attribute_group_id';
00431 } else {
00432 if (isset($this->defaultGroupIdAssociations[$id])) {
00433 $searchField = 'default_id';
00434 $searchId = $this->defaultGroupIdAssociations[$id];
00435 } else {
00436 $searchField = 'attribute_group_name';
00437 }
00438 }
00439
00440 return $this->getTableRow('eav/attribute_group',
00441 $searchField, $searchId, $field,
00442 'attribute_set_id', $this->getAttributeSetId($entityTypeId, $setId)
00443 );
00444 }
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 public function getAttributeGroupId($entityTypeId, $setId, $groupId)
00455 {
00456 if (!is_numeric($groupId)) {
00457 $groupId = $this->getAttributeGroup($entityTypeId, $setId, $groupId, 'attribute_group_id');
00458 }
00459 if (!is_numeric($groupId)) {
00460 throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Wrong attribute group ID'));
00461 }
00462 return $groupId;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 public function removeAttributeGroup($entityTypeId, $setId, $id)
00474 {
00475 $this->deleteTableRow(
00476 'eav/attribute_group',
00477 'attribute_group_id',
00478 $this->getAttributeGroupId($entityTypeId, $setId, $id)
00479 );
00480 return $this;
00481 }
00482
00483
00484
00485
00486
00487
00488
00489
00490 public function getDefaultAttributeGroupId($entityType, $attributeSetId = null)
00491 {
00492 $entityType = $this->getEntityTypeId($entityType);
00493 if (!is_numeric($attributeSetId)) {
00494 $attributeSetId = $this->getDefaultAttributeSetId($entityType);
00495 }
00496
00497 $select = $this->getConnection()->select()
00498 ->from($this->getTable('eav/attribute_group'), 'attribute_group_id')
00499 ->where('attribute_set_id=?', $attributeSetId)
00500 ->order('default_id DESC, sort_order')
00501 ->limit(1);
00502 return $this->getConnection()->fetchOne($select);
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 protected function _getValue($array, $key, $default = null)
00516 {
00517 return isset($array[$key]) ? $array[$key] : $default;
00518 }
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 public function addAttribute($entityTypeId, $code, array $attr)
00531 {
00532 $entityTypeId = $this->getEntityTypeId($entityTypeId);
00533 $data = array(
00534 'entity_type_id' => $entityTypeId,
00535 'attribute_code' => $code,
00536 'backend_model' => $this->_getValue($attr, 'backend', ''),
00537 'backend_type' => $this->_getValue($attr, 'type', 'varchar'),
00538 'backend_table' => $this->_getValue($attr, 'table', ''),
00539 'frontend_model' => $this->_getValue($attr, 'frontend', ''),
00540 'frontend_input' => $this->_getValue($attr, 'input', 'text'),
00541 'frontend_input_renderer' => $this->_getValue($attr, 'input_renderer', ''),
00542 'frontend_label' => $this->_getValue($attr, 'label', ''),
00543 'source_model' => $this->_getValue($attr, 'source', ''),
00544 'is_global' => $this->_getValue($attr, 'global', 1),
00545 'is_visible' => $this->_getValue($attr, 'visible', 1),
00546 'is_required' => $this->_getValue($attr, 'required', 1),
00547 'is_user_defined' => $this->_getValue($attr, 'user_defined', 0),
00548 'default_value' => $this->_getValue($attr, 'default', ''),
00549 'is_searchable' => $this->_getValue($attr, 'searchable', 0),
00550 'is_filterable' => $this->_getValue($attr, 'filterable', 0),
00551 'is_comparable' => $this->_getValue($attr, 'comparable', 0),
00552 'is_visible_on_front' => $this->_getValue($attr, 'visible_on_front', 0),
00553 'is_html_allowed_on_front' => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
00554 'is_visible_in_advanced_search'
00555 => $this->_getValue($attr, 'visible_in_advanced_search', 0),
00556 'is_used_for_price_rules' => $this->_getValue($attr, 'used_for_price_rules', 1),
00557 'is_filterable_in_search' => $this->_getValue($attr, 'filterable_in_search', 0),
00558 'used_in_product_listing' => $this->_getValue($attr, 'used_in_product_listing', 0),
00559 'used_for_sort_by' => $this->_getValue($attr, 'used_for_sort_by', 0),
00560 'is_unique' => $this->_getValue($attr, 'unique', 0),
00561 'apply_to' => $this->_getValue($attr, 'apply_to', ''),
00562 'is_configurable' => $this->_getValue($attr, 'is_configurable', 1),
00563 'note' => $this->_getValue($attr, 'note', ''),
00564 'position' => $this->_getValue($attr, 'position', 0),
00565 );
00566
00567 $sortOrder = isset($attr['sort_order']) ? $attr['sort_order'] : null;
00568
00569 if ($id = $this->getAttribute($entityTypeId, $code, 'attribute_id')) {
00570 $this->updateAttribute($entityTypeId, $id, $data, null, $sortOrder);
00571 } else {
00572 $this->_insertAttribute($data);
00573 }
00574
00575 if (!empty($attr['group'])) {
00576 $sets = $this->_conn->fetchAll('select * from '.$this->getTable('eav/attribute_set').' where entity_type_id=?', $entityTypeId);
00577 foreach ($sets as $set) {
00578 $this->addAttributeGroup($entityTypeId, $set['attribute_set_id'], $attr['group']);
00579 $this->addAttributeToSet($entityTypeId, $set['attribute_set_id'], $attr['group'], $code, $sortOrder);
00580 }
00581 }
00582 if (empty($attr['is_user_defined'])) {
00583 $sets = $this->_conn->fetchAll('select * from '.$this->getTable('eav/attribute_set').' where entity_type_id=?', $entityTypeId);
00584 foreach ($sets as $set) {
00585 $this->addAttributeToSet($entityTypeId, $set['attribute_set_id'], $this->_generalGroupName, $code, $sortOrder);
00586 }
00587 }
00588
00589 if (isset($attr['option']) && is_array($attr['option'])) {
00590 $option = $attr['option'];
00591 $option['attribute_id'] = $this->getAttributeId($entityTypeId, $code);
00592 $this->addAttributeOption($option);
00593 }
00594
00595 return $this;
00596 }
00597
00598
00599
00600
00601
00602
00603 public function addAttributeOption($option)
00604 {
00605 if (isset($option['value'])) {
00606 $optionTable = $this->getTable('eav/attribute_option');
00607 $optionValueTable = $this->getTable('eav/attribute_option_value');
00608
00609 foreach ($option['value'] as $optionId => $values) {
00610 $intOptionId = (int) $optionId;
00611 if (!empty($option['delete'][$optionId])) {
00612 if ($intOptionId) {
00613 $condition = $this->_conn->quoteInto('option_id=?', $intOptionId);
00614 $write->delete($optionTable, $condition);
00615 }
00616 continue;
00617 }
00618
00619 if (!$intOptionId) {
00620 $data = array(
00621 'attribute_id' => $option['attribute_id'],
00622 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00623 );
00624 $this->_conn->insert($optionTable, $data);
00625 $intOptionId = $this->_conn->lastInsertId();
00626 } else {
00627 $data = array(
00628 'sort_order' => isset($option['order'][$optionId]) ? $option['order'][$optionId] : 0,
00629 );
00630 $this->_conn->update($optionTable, $data, $this->_conn->quoteInto('option_id=?', $intOptionId));
00631 }
00632
00633
00634 if (!isset($values[0])) {
00635 Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
00636 }
00637
00638 $this->_conn->delete($optionValueTable, $this->_conn->quoteInto('option_id=?', $intOptionId));
00639 foreach ($values as $storeId => $value) {
00640 $data = array(
00641 'option_id' => $intOptionId,
00642 'store_id' => $storeId,
00643 'value' => $value,
00644 );
00645 $this->_conn->insert($optionValueTable, $data);
00646 }
00647 }
00648 }
00649 }
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661 public function updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)
00662 {
00663 if (!is_null($sortOrder)) {
00664 $this->updateTableRow('eav/entity_attribute',
00665 'attribute_id', $this->getAttributeId($entityTypeId, $id),
00666 'sort_order', $sortOrder
00667 );
00668 }
00669
00670 $attributeFields = $this->_getAttributeTableFields();
00671 if (is_array($field)) {
00672 $bind = array();
00673 foreach ($field as $k => $v) {
00674 if (isset($attributeFields[$k])) {
00675 $bind[$k] = $v;
00676 }
00677 }
00678 if (!$bind) {
00679 return $this;
00680 }
00681 $field = $bind;
00682 }
00683 else {
00684 if (!isset($attributeFields[$field])) {
00685 return $this;
00686 }
00687 }
00688
00689 $this->updateTableRow('eav/attribute',
00690 'attribute_id', $this->getAttributeId($entityTypeId, $id),
00691 $field, $value,
00692 'entity_type_id', $this->getEntityTypeId($entityTypeId)
00693 );
00694 return $this;
00695 }
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705 public function getAttribute($entityTypeId, $id, $field=null)
00706 {
00707 return $this->getTableRow('eav/attribute',
00708 is_numeric($id) ? 'attribute_id' : 'attribute_code', $id,
00709 $field,
00710 'entity_type_id', $this->getEntityTypeId($entityTypeId)
00711 );
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721 public function getAttributeId($entityTypeId, $id)
00722 {
00723 if (!is_numeric($id)) {
00724 $id = $this->getAttribute($entityTypeId, $id, 'attribute_id');
00725 }
00726 if (!is_numeric($id)) {
00727
00728 return false;
00729 }
00730 return $id;
00731 }
00732
00733
00734
00735
00736
00737
00738
00739
00740 public function getAttributeTable($entityTypeId, $id)
00741 {
00742 $entityKeyName = is_numeric($entityTypeId) ? 'entity_type_id' : 'entity_type_code';
00743 $attributeKeyName = is_numeric($id) ? 'attribute_id' : 'attribute_code';
00744
00745 $select = $this->getConnection()->select()
00746 ->from(
00747 array('e' => $this->getTable('eav/entity_type')),
00748 array('entity_table'))
00749 ->join(
00750 array('a' => $this->getTable('eav/attribute')),
00751 'a.entity_type_id=e.entity_type_id',
00752 array('backend_type'))
00753 ->where("e.{$entityKeyName}=?", $entityTypeId)
00754 ->where("a.{$attributeKeyName}=?", $id)
00755 ->limit(1);
00756 if ($result = $this->getConnection()->fetchRow($select)) {
00757 $table = $this->getTable($result['entity_table']);
00758 if ($result['backend_type'] != 'static') {
00759 $table .= '_' . $result['backend_type'];
00760 }
00761 return $table;
00762 }
00763
00764 return false;
00765 }
00766
00767
00768
00769
00770
00771
00772
00773
00774 public function removeAttribute($entityTypeId, $code)
00775 {
00776 $attributeId = $this->getAttributeId($entityTypeId, $code);
00777 if ($attributeId) {
00778 $this->deleteTableRow('eav/attribute', 'attribute_id', $attributeId);
00779 }
00780 return $this;
00781 }
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792 public function getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder=null)
00793 {
00794 if (!is_numeric($sortOrder)) {
00795 $sortOrder = $this->_conn->fetchOne("select max(sort_order)
00796 from ".$this->getTable('eav/entity_attribute')."
00797 where attribute_group_id=".$this->getAttributeGroupId($entityTypeId, $setId, $groupId)
00798 );
00799 $sortOrder++;
00800 }
00801 return $sortOrder;
00802 }
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814 public function addAttributeToSet($entityTypeId, $setId, $groupId, $attributeId, $sortOrder=null)
00815 {
00816 $entityTypeId = $this->getEntityTypeId($entityTypeId);
00817 $setId = $this->getAttributeSetId($entityTypeId, $setId);
00818 $groupId = $this->getAttributeGroupId($entityTypeId, $setId, $groupId);
00819 $attributeId = $this->getAttributeId($entityTypeId, $attributeId);
00820 $generalGroupId = $this->getAttributeGroupId($entityTypeId, $setId, $this->_generalGroupName);
00821
00822 $oldId = $this->_conn->fetchOne("select entity_attribute_id from ".$this->getTable('eav/entity_attribute')." where attribute_set_id=$setId and attribute_id=$attributeId");
00823 if ($oldId) {
00824 if ($groupId && $groupId != $generalGroupId) {
00825 $newGroupData = array('attribute_group_id'=>$groupId);
00826 $condition = $this->_conn->quoteInto('entity_attribute_id = ?', $oldId);
00827 $this->_conn->update($this->getTable('eav/entity_attribute'), $newGroupData, $condition);
00828 }
00829 return $this;
00830 }
00831 $this->_conn->insert($this->getTable('eav/entity_attribute'), array(
00832 'entity_type_id' =>$entityTypeId,
00833 'attribute_set_id' =>$setId,
00834 'attribute_group_id'=>$groupId,
00835 'attribute_id' =>$attributeId,
00836 'sort_order' =>$this->getAttributeSortOrder($entityTypeId, $setId, $groupId, $sortOrder),
00837 ));
00838
00839 return $this;
00840 }
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852 public function addAttributeToGroup($entityType, $setId, $groupId, $attributeId, $sortOrder = null)
00853 {
00854 $entityType = $this->getEntityTypeId($entityType);
00855 $setId = $this->getAttributeSetId($entityType, $setId);
00856 $groupId = $this->getAttributeGroupId($entityType, $setId, $groupId);
00857 $attributeId = $this->getAttributeId($entityType, $attributeId);
00858
00859 $bind = array(
00860 'entity_type_id' => $entityType,
00861 'attribute_set_id' => $setId,
00862 'attribute_group_id' => $groupId,
00863 'attribute_id' => $attributeId,
00864 );
00865
00866 $select = $this->getConnection()->select()
00867 ->from($this->getTable('eav/entity_attribute'))
00868 ->where('entity_type_id=?', $entityType)
00869 ->where('attribute_set_id=?', $setId)
00870 ->where('attribute_id=?', $attributeId);
00871 $row = $this->getConnection()->fetchRow($select);
00872 if ($row) {
00873
00874 if (!is_null($sortOrder)) {
00875 $bind['sort_order'] = $sortOrder;
00876 }
00877
00878 $this->getConnection()->update(
00879 $this->getTable('eav/entity_attribute'),
00880 $bind,
00881 $this->getConnection()->quoteInto('entity_attribute_id=?', $row['entity_attribute_id'])
00882 );
00883 }
00884 else {
00885 if (is_null($sortOrder)) {
00886 $select = $this->getConnection()->select()
00887 ->from($this->getTable('eav/entity_attribute'), 'MAX(sort_order) + 10')
00888 ->where('entity_type_id=?', $entityType)
00889 ->where('attribute_set_id=?', $setId)
00890 ->where('attribute_group_id=?', $groupId);
00891 $sortOrder = $this->getConnection()->fetchOne($select);
00892 }
00893 $bind['sort_order'] = $sortOrder;
00894 $this->getConnection()->insert($this->getTable('eav/entity_attribute'), $bind);
00895 }
00896
00897 return $this;
00898 }
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908 public function installEntities($entities=null)
00909 {
00910 $this->cleanCache();
00911
00912 if (is_null($entities)) {
00913 $entities = $this->getDefaultEntities();
00914 }
00915
00916 foreach ($entities as $entityName=>$entity) {
00917 $this->addEntityType($entityName, $entity);
00918
00919 $frontendPrefix = isset($entity['frontend_prefix']) ? $entity['frontend_prefix'] : '';
00920 $backendPrefix = isset($entity['backend_prefix']) ? $entity['backend_prefix'] : '';
00921 $sourcePrefix = isset($entity['source_prefix']) ? $entity['source_prefix'] : '';
00922
00923 foreach ($entity['attributes'] as $attrCode=>$attr) {
00924 if (!empty($attr['backend'])) {
00925 if ('_'===$attr['backend']) {
00926 $attr['backend'] = $backendPrefix;
00927 } elseif ('_'===$attr['backend']{0}) {
00928 $attr['backend'] = $backendPrefix.$attr['backend'];
00929 } else {
00930 $attr['backend'] = $attr['backend'];
00931 }
00932 }
00933 if (!empty($attr['frontend'])) {
00934 if ('_'===$attr['frontend']) {
00935 $attr['frontend'] = $frontendPrefix;
00936 } elseif ('_'===$attr['frontend']{0}) {
00937 $attr['frontend'] = $frontendPrefix.$attr['frontend'];
00938 } else {
00939 $attr['frontend'] = $attr['frontend'];
00940 }
00941 }
00942 if (!empty($attr['source'])) {
00943 if ('_'===$attr['source']) {
00944 $attr['source'] = $sourcePrefix;
00945 } elseif ('_'===$attr['source']{0}) {
00946 $attr['source'] = $sourcePrefix.$attr['source'];
00947 } else {
00948 $attr['source'] = $attr['source'];
00949 }
00950 }
00951
00952 $this->addAttribute($entityName, $attrCode, $attr);
00953 }
00954 $this->setDefaultSetToEntityType($entityName);
00955 }
00956
00957 return $this;
00958 }
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973 public function createEntityTables($baseName, array $options=array())
00974 {
00975 if (empty($options['no-main'])) {
00976 $sql = "
00977 DROP TABLE IF EXISTS `{$baseName}`;
00978 CREATE TABLE `{$baseName}` (
00979 `entity_id` int(10) unsigned NOT NULL auto_increment,
00980 `entity_type_id` smallint(8) unsigned NOT NULL default '0',
00981 `attribute_set_id` smallint(5) unsigned NOT NULL default '0',
00982 `increment_id` varchar(50) NOT NULL default '',
00983 `parent_id` int(10) unsigned NULL default '0',
00984 `store_id` smallint(5) unsigned NOT NULL default '0',
00985 `created_at` datetime NOT NULL default '0000-00-00 00:00:00',
00986 `updated_at` datetime NOT NULL default '0000-00-00 00:00:00',
00987 `is_active` tinyint(1) unsigned NOT NULL default '1',
00988 PRIMARY KEY (`entity_id`),
00989 CONSTRAINT `FK_{$baseName}_type` FOREIGN KEY (`entity_type_id`) REFERENCES `eav_entity_type` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE,
00990 CONSTRAINT `FK_{$baseName}_store` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
00991 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
00992 }
00993
00994 $types = array(
00995 'datetime'=>'datetime',
00996 'decimal'=>'decimal(12,4)',
00997 'int'=>'int',
00998 'text'=>'text',
00999 'varchar'=>'varchar(255)',
01000 );
01001 if (!empty($options['types']) && is_array($options['types'])) {
01002 if ($options['no-default-types']) {
01003 $types = array();
01004 }
01005 $types = array_merge($types, $options['types']);
01006 }
01007
01008 foreach ($types as $type=>$fieldType) {
01009 $sql .= "
01010 DROP TABLE IF EXISTS `{$baseName}_{$type}`;
01011 CREATE TABLE `{$baseName}_{$type}` (
01012 `value_id` int(11) NOT NULL auto_increment,
01013 `entity_type_id` smallint(8) unsigned NOT NULL default '0',
01014 `attribute_id` smallint(5) unsigned NOT NULL default '0',
01015 `store_id` smallint(5) unsigned NOT NULL default '0',
01016 `entity_id` int(10) unsigned NOT NULL default '0',
01017 `value` {$fieldType} NOT NULL,
01018 PRIMARY KEY (`value_id`),
01019 UNIQUE KEY `IDX_BASE` (`entity_type_id`,`entity_id`,`attribute_id`,`store_id`),
01020 ".($type!=='text' ? "
01021 KEY `value_by_attribute` (`attribute_id`,`value`),
01022 KEY `value_by_entity_type` (`entity_type_id`,`value`),
01023 " : "")."
01024 CONSTRAINT `FK_{$baseName}_{$type}` FOREIGN KEY (`entity_id`) REFERENCES `{$baseName}` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE,
01025 CONSTRAINT `FK_{$baseName}_{$type}_attribute` FOREIGN KEY (`attribute_id`) REFERENCES `eav_attribute` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE,
01026 CONSTRAINT `FK_{$baseName}_{$type}_entity_type` FOREIGN KEY (`entity_type_id`) REFERENCES `eav_entity_type` (`entity_type_id`) ON DELETE CASCADE ON UPDATE CASCADE,
01027 CONSTRAINT `FK_{$baseName}_{$type}_store` FOREIGN KEY (`store_id`) REFERENCES `core_store` (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE
01028 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
01029 }
01030
01031 try {
01032 $this->_conn->multi_query($sql);
01033 } catch (Exception $e) {
01034 throw $e;
01035 }
01036
01037 return $this;
01038 }
01039
01040
01041
01042
01043
01044
01045 protected function _getAttributeTableFields() {
01046 return $this->getConnection()->describeTable($this->getTable('eav/attribute'));
01047 }
01048
01049
01050
01051
01052
01053
01054 protected function _insertAttribute(array $data) {
01055 $bind = array();
01056 $fields = $this->_getAttributeTableFields();
01057
01058 foreach ($data as $k => $v) {
01059 if (isset($fields[$k])) {
01060 $bind[$k] = $v;
01061 }
01062 }
01063 if (!$bind) {
01064 return $this;
01065 }
01066
01067 $this->getConnection()->insert($this->getTable('eav/attribute'), $bind);
01068
01069 return $this;
01070 }
01071 }