OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxattributelist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxAttributeList extends oxList
8 {
16  public function __construct( $sObjectsInListName = 'oxattribute')
17  {
18  parent::__construct( 'oxattribute');
19  }
20 
28  public function loadAttributesByIds( $aIds )
29  {
30  if (!count($aIds)) {
31  return null;
32  }
33 
34  foreach ($aIds as $iKey => $sVal) {
35  $aIds[$iKey] = oxDb::getInstance()->escapeString($sVal);
36  }
37 
38  $sAttrViewName = getViewName( 'oxattribute' );
39  $sViewName = getViewName( 'oxobject2attribute' );
40 
41  $sSelect = "select $sAttrViewName.oxid, $sAttrViewName.oxtitle, {$sViewName}.oxvalue, {$sViewName}.oxobjectid ";
42  $sSelect .= "from {$sViewName} left join $sAttrViewName on $sAttrViewName.oxid = {$sViewName}.oxattrid ";
43  $sSelect .= "where {$sViewName}.oxobjectid in ( '".implode("','", $aIds)."' ) ";
44  $sSelect .= "order by {$sViewName}.oxpos, $sAttrViewName.oxpos";
45 
46  return $this->_createAttributeListFromSql( $sSelect);
47  }
48 
56  protected function _createAttributeListFromSql( $sSelect )
57  {
58  $aAttributes = array();
59  $rs = oxDb::getDb()->select( $sSelect );
60  if ($rs != false && $rs->recordCount() > 0) {
61  while (!$rs->EOF) {
62  if ( !isset( $aAttributes[$rs->fields[0]])) {
63  $aAttributes[$rs->fields[0]] = new stdClass();
64  }
65 
66  $aAttributes[$rs->fields[0]]->title = $rs->fields[1];
67  if ( !isset( $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]])) {
68  $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]] = new stdClass();
69  }
70  $aAttributes[$rs->fields[0]]->aProd[$rs->fields[3]]->value = $rs->fields[2];
71  $rs->moveNext();
72  }
73  }
74  return $aAttributes;
75  }
76 
85  public function loadAttributes( $sArticleId, $sParentId = null )
86  {
87  if ( $sArticleId ) {
88 
90 
91  $sAttrViewName = getViewName( 'oxattribute' );
92  $sViewName = getViewName( 'oxobject2attribute' );
93 
94  $sSelect = "select {$sAttrViewName}.`oxid`, {$sAttrViewName}.`oxtitle`, o2a.`oxvalue` from {$sViewName} as o2a ";
95  $sSelect .= "left join {$sAttrViewName} on {$sAttrViewName}.oxid = o2a.oxattrid ";
96  $sSelect .= "where o2a.oxobjectid = '%s' and o2a.oxvalue != '' ";
97  $sSelect .= "order by o2a.oxpos, {$sAttrViewName}.oxpos";
98 
99  $aAttributes = $oDb->getAll( sprintf( $sSelect, $sArticleId ) );
100 
101  if ( $sParentId ) {
102  $aParentAttributes = $oDb->getAll( sprintf( $sSelect, $sParentId ));
103  $aAttributes = $this->_mergeAttributes( $aAttributes, $aParentAttributes );
104  }
105 
106  $this->assignArray( $aAttributes );
107  }
108 
109  }
110 
119  public function loadAttributesDisplayableInBasket( $sArtId, $sParentId = null )
120  {
121  if ( $sArtId ) {
122 
124 
125  $sAttrViewName = getViewName( 'oxattribute' );
126  $sViewName = getViewName( 'oxobject2attribute' );
127 
128  $sSelect = "select {$sAttrViewName}.*, o2a.* from {$sViewName} as o2a ";
129  $sSelect .= "left join {$sAttrViewName} on {$sAttrViewName}.oxid = o2a.oxattrid ";
130  $sSelect .= "where o2a.oxobjectid = '%s' and {$sAttrViewName}.oxdisplayinbasket = 1 and o2a.oxvalue != '' ";
131  $sSelect .= "order by o2a.oxpos, {$sAttrViewName}.oxpos";
132 
133  $aAttributes = $oDb->getAll( sprintf( $sSelect, $sArtId ) );
134 
135  if ( $sParentId ) {
136  $aParentAttributes = $oDb->getAll( sprintf( $sSelect, $sParentId ));
137  $aAttributes = $this->_mergeAttributes( $aAttributes, $aParentAttributes );
138  }
139 
140  $this->assignArray( $aAttributes );
141  }
142  }
143 
144 
154  public function getCategoryAttributes( $sCategoryId, $iLang )
155  {
156  $aSessionFilter = oxRegistry::getSession()->getVariable( 'session_attrfilter' );
157 
158  $oArtList = oxNew( "oxarticlelist");
159  $oArtList->loadCategoryIDs( $sCategoryId, $aSessionFilter );
160 
161  // Only if we have articles
162  if (count($oArtList) > 0 ) {
163  $oDb = oxDb::getDb();
164  $sArtIds = '';
165  foreach (array_keys($oArtList->getArray()) as $sId ) {
166  if ($sArtIds) {
167  $sArtIds .= ',';
168  }
169  $sArtIds .= $oDb->quote($sId);
170  }
171 
172  $sActCatQuoted = $oDb->quote( $sCategoryId );
173  $sAttTbl = getViewName( 'oxattribute', $iLang );
174  $sO2ATbl = getViewName( 'oxobject2attribute', $iLang );
175  $sC2ATbl = getViewName( 'oxcategory2attribute', $iLang );
176 
177  $sSelect = "SELECT DISTINCT att.oxid, att.oxtitle, o2a.oxvalue ".
178  "FROM $sAttTbl as att, $sO2ATbl as o2a ,$sC2ATbl as c2a ".
179  "WHERE att.oxid = o2a.oxattrid AND c2a.oxobjectid = $sActCatQuoted AND c2a.oxattrid = att.oxid AND o2a.oxvalue !='' AND o2a.oxobjectid IN ($sArtIds) ".
180  "ORDER BY c2a.oxsort , att.oxpos, att.oxtitle, o2a.oxvalue";
181 
182  $rs = $oDb->select( $sSelect );
183 
184  if ( $rs != false && $rs->recordCount() > 0 ) {
185  while ( !$rs->EOF && list( $sAttId, $sAttTitle, $sAttValue ) = $rs->fields ) {
186 
187  if ( !$this->offsetExists( $sAttId ) ) {
188 
189  $oAttribute = oxNew( "oxattribute" );
190  $oAttribute->setTitle( $sAttTitle );
191 
192  $this->offsetSet( $sAttId, $oAttribute );
193  $iLang = oxRegistry::getLang()->getBaseLanguage();
194  if ( isset( $aSessionFilter[$sCategoryId][$iLang][$sAttId] ) ) {
195  $oAttribute->setActiveValue( $aSessionFilter[$sCategoryId][$iLang][$sAttId] );
196  }
197 
198  } else {
199  $oAttribute = $this->offsetGet( $sAttId );
200  }
201 
202  $oAttribute->addValue( $sAttValue );
203  $rs->moveNext();
204  }
205  }
206  }
207 
208  return $this;
209  }
210 
219  protected function _mergeAttributes( $aAttributes, $aParentAttributes )
220  {
221 
222  if ( count( $aParentAttributes ) ) {
223  $aAttrIds = array();
224  foreach ( $aAttributes as $aAttribute ) {
225  $aAttrIds[] = $aAttribute['OXID'];
226  }
227 
228  foreach ( $aParentAttributes as $aAttribute ) {
229  if ( !in_array( $aAttribute['OXID'], $aAttrIds ) ) {
230  $aAttributes[] = $aAttribute;
231  }
232  }
233  }
234 
235  return $aAttributes;
236  }
237 
238 }