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 class Mage_Eav_Model_Mysql4_Entity_Attribute_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
00035 {
00036
00037
00038
00039
00040
00041 protected $_addSetInfoFlag = false;
00042
00043
00044
00045
00046 public function _construct()
00047 {
00048 $this->_init('eav/entity_attribute');
00049 }
00050
00051
00052
00053
00054
00055
00056 public function useLoadDataFields()
00057 {
00058 $this->getSelect()->reset(Zend_Db_Select::COLUMNS);
00059 $this->getSelect()->columns(array(
00060 'attribute_id',
00061 'entity_type_id',
00062 'attribute_code',
00063 'attribute_model',
00064 'backend_model',
00065 'backend_type',
00066 'backend_table',
00067 'frontend_input',
00068 'source_model',
00069 'is_global'
00070 ));
00071 return $this;
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 public function setEntityTypeFilter($typeId)
00081 {
00082 $this->getSelect()->where('main_table.entity_type_id=?', $typeId);
00083 return $this;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 public function setAttributeSetFilter($setId)
00093 {
00094 if (is_array($setId)) {
00095 if (!empty($setId)) {
00096 $this->join('entity_attribute', 'entity_attribute.attribute_id=main_table.attribute_id', 'attribute_id');
00097 $this->getSelect()->where('entity_attribute.attribute_set_id IN(?)', $setId);
00098 }
00099 }
00100 elseif($setId) {
00101 $this->join('entity_attribute', 'entity_attribute.attribute_id=main_table.attribute_id', '*');
00102 $this->getSelect()->where('entity_attribute.attribute_set_id=?', $setId);
00103 $this->setOrder('sort_order', 'asc');
00104 }
00105 return $this;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115 public function setAttributeSetsFilter(array $setIds)
00116 {
00117 $this->join('entity_attribute', 'entity_attribute.attribute_id=main_table.attribute_id', 'attribute_id');
00118 $this->getSelect()->distinct(true);
00119 $this->getSelect()->where('entity_attribute.attribute_set_id IN(?)', $setIds);
00120 $this->setOrder('sort_order', 'asc');
00121 return $this;
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 public function setInAllAttributeSetsFilter(array $setIds)
00131 {
00132 foreach ($setIds as $setId) {
00133 $setId = (int) $setId;
00134 if (!$setId) {
00135 continue;
00136 }
00137 $this->getSelect()->join(array('entity_attribute_'.$setId=>$this->getTable('entity_attribute')), 'entity_attribute_' . $setId . '.attribute_id=main_table.attribute_id and entity_attribute_' . $setId . '.attribute_set_id=' . $setId, 'attribute_id');
00138 }
00139
00140 $this->getSelect()->distinct(true);
00141 $this->setOrder('is_user_defined', 'asc');
00142 return $this;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function setAttributeSetExcludeFilter($setId)
00152 {
00153 $this->join('entity_attribute', 'entity_attribute.attribute_id=main_table.attribute_id', '*');
00154 $this->getSelect()->where('entity_attribute.attribute_set_id != ?', $setId);
00155 $this->setOrder('sort_order', 'asc');
00156 return $this;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165 public function setAttributesExcludeFilter($attributes)
00166 {
00167 $this->getSelect()->where('main_table.attribute_id NOT IN(?)', $attributes);
00168 return $this;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 public function setAttributeGroupFilter($groupId)
00178 {
00179 $this->join('entity_attribute', 'entity_attribute.attribute_id=main_table.attribute_id', '*');
00180 $this->getSelect()->where('entity_attribute.attribute_group_id=?', $groupId);
00181 $this->setOrder('sort_order', 'asc');
00182 return $this;
00183 }
00184
00185
00186
00187
00188
00189
00190 public function addAttributeGrouping()
00191 {
00192 $this->getSelect()->group('entity_attribute.attribute_id');
00193 return $this;
00194 }
00195
00196
00197
00198
00199
00200
00201 public function addVisibleFilter()
00202 {
00203 $this->getSelect()->where('main_table.is_visible=?', 1);
00204 return $this;
00205 }
00206
00207
00208
00209
00210
00211
00212 public function addIsFilterableFilter()
00213 {
00214 $this->getSelect()->where('main_table.is_filterable>0');
00215 return $this;
00216 }
00217
00218
00219
00220
00221
00222
00223 public function addIsFilterableInSearchFilter()
00224 {
00225 $this->getSelect()->where('main_table.is_filterable_in_search>0');
00226 return $this;
00227 }
00228
00229
00230
00231
00232
00233
00234 public function addIsUniqueFilter()
00235 {
00236 $this->getSelect()->where('main_table.is_unique>0');
00237 return $this;
00238 }
00239
00240
00241
00242
00243
00244
00245 public function addIsNotUniqueFilter()
00246 {
00247 $this->getSelect()->where('main_table.is_unique=0');
00248 return $this;
00249 }
00250
00251
00252
00253
00254
00255
00256 public function addIsSearchableFilter()
00257 {
00258 $this->getSelect()->where('main_table.is_searchable=1');
00259 return $this;
00260 }
00261
00262
00263
00264
00265
00266
00267 public function addHasOptionsFilter()
00268 {
00269 $this->getSelect()
00270 ->joinLeft(
00271 array('ao'=>$this->getTable('eav/attribute_option')), 'ao.attribute_id = main_table.attribute_id', 'option_id'
00272 )
00273 ->group('main_table.attribute_id')
00274 ->where('(main_table.frontend_input = ? and option_id > 0) or (main_table.frontend_input <> ?) or (main_table.is_user_defined = 0)', 'select', 'select');
00275
00276 return $this;
00277 }
00278
00279
00280
00281
00282
00283
00284 public function addDisplayInAdvancedSearchFilter(){
00285 $this->getSelect()
00286 ->where('main_table.is_visible_in_advanced_search = ?', 1);
00287
00288 return $this;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297 public function setFrontendInputTypeFilter($frontendInputType)
00298 {
00299 $this->getSelect()
00300 ->where('main_table.frontend_input = ?', $frontendInputType);
00301 return $this;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310 public function addSetInfo($flag=true)
00311 {
00312 $this->_addSetInfoFlag = $flag;
00313 return $this;
00314 }
00315
00316
00317
00318
00319
00320
00321 protected function _addSetInfo()
00322 {
00323 if ($this->_addSetInfoFlag) {
00324 $attributeIds = array();
00325 foreach ($this->_data as &$dataItem) {
00326 $attributeIds[] = $dataItem['attribute_id'];
00327 }
00328 $attributeToSetInfo = array();
00329
00330 if (count($attributeIds) > 0) {
00331 $select = $this->getConnection()->select()
00332 ->from(
00333 array('entity' => $this->getTable('entity_attribute')),
00334 array('attribute_id','attribute_set_id', 'attribute_group_id', 'sort_order')
00335 )
00336 ->joinLeft(
00337 array('group' => $this->getTable('attribute_group')),
00338 'entity.attribute_group_id=group.attribute_group_id',
00339 array('group_sort_order' => 'sort_order')
00340 )
00341 ->where('attribute_id IN (?)', $attributeIds);
00342 $result = $this->getConnection()->fetchAll($select);
00343
00344 foreach ($result as $row) {
00345 $data = array(
00346 'group_id' => $row['attribute_group_id'],
00347 'group_sort' => $row['group_sort_order'],
00348 'sort' => $row['sort_order']
00349 );
00350 $attributeToSetInfo[$row['attribute_id']][$row['attribute_set_id']] = $data;
00351 }
00352 }
00353
00354 foreach ($this->_data as &$attributeData) {
00355 if (isset($attributeToSetInfo[$attributeData['attribute_id']])) {
00356 $setInfo = $attributeToSetInfo[$attributeData['attribute_id']];
00357 } else {
00358 $setInfo = array();
00359 }
00360
00361 $attributeData['attribute_set_info'] = $setInfo;
00362 }
00363
00364 unset($attributeToSetInfo);
00365 unset($attributeIds);
00366 }
00367 return $this;
00368 }
00369
00370 protected function _afterLoadData()
00371 {
00372 $this->_addSetInfo();
00373 return parent::_afterLoadData();
00374 }
00375
00376
00377
00378
00379
00380
00381 public function checkConfigurableProducts()
00382 {
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399 return $this;
00400 }
00401
00402
00403
00404
00405
00406
00407
00408 public function setCodeFilter($code)
00409 {
00410 if (empty($code)) {
00411 return $this;
00412 }
00413 if (!is_array($code)) {
00414 $code = array($code);
00415 }
00416 $this->getSelect()->where('main_table.attribute_code IN(?)', $code);
00417 return $this;
00418 }
00419 }