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