OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxlist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxList extends oxSuperCfg implements ArrayAccess, Iterator, Countable
8 {
9 
15  protected $_aArray = array();
16 
23  protected $_blRemovedActive = false;
24 
30  private $_oBaseObject = null;
31 
37  private $_blValid = true;
38 
57  public function offsetExists($offset)
58  {
59  if (isset($this->_aArray[$offset])) {
60  return true;
61  } else {
62  return false;
63  }
64  }
65 
73  public function offsetGet($offset)
74  {
75  if ($this->offsetExists($offset)) {
76  return $this->_aArray[$offset];
77  } else {
78  return false;
79  }
80  }
81 
88  public function offsetSet($offset, $oBase)
89  {
90  if (isset($offset)) {
91  $this->_aArray[$offset] = & $oBase;
92  } else {
93  $sLongFieldName = $this->_getFieldLongName('oxid');
94  if (isset($oBase->$sLongFieldName->value)) {
95  $sOxid = $oBase->$sLongFieldName->value;
96  $this->_aArray[$sOxid] = & $oBase;
97  } else {
98  $this->_aArray[] = & $oBase;
99  }
100  }
101 
102  }
103 
109  public function offsetUnset($offset)
110  {
111  if (strcmp($offset, $this->key()) === 0) {
112  // #0002184: active element removed, next element will be prev / first
113  $this->_blRemovedActive = true;
114  }
115 
116  unset($this->_aArray[$offset]);
117  }
118 
124  public function arrayKeys()
125  {
126  return array_keys($this->_aArray);
127  }
128 
132  public function rewind()
133  {
134  $this->_blRemovedActive = false;
135  $this->_blValid = (false !== reset($this->_aArray));
136  }
137 
143  public function current()
144  {
145  return current($this->_aArray);
146  }
147 
153  public function key()
154  {
155  return key($this->_aArray);
156  }
157 
163  public function prev()
164  {
165  $oVar = prev($this->_aArray);
166  if ($oVar === false) {
167  // the first element, reset pointer
168  $oVar = reset($this->_aArray);
169  }
170  $this->_blRemovedActive = false;
171 
172  return $oVar;
173  }
174 
178  public function next()
179  {
180  if ($this->_blRemovedActive === true && current($this->_aArray)) {
181  $oVar = $this->prev();
182  } else {
183  $oVar = next($this->_aArray);
184  }
185 
186  $this->_blValid = (false !== $oVar);
187  }
188 
194  public function valid()
195  {
196  return $this->_blValid;
197  }
198 
204  public function count()
205  {
206  return count($this->_aArray);
207  }
208 
212  public function clear()
213  {
214  /*
215  foreach ( $this->_aArray as $key => $sValue) {
216  unset( $this->_aArray[$key]);
217  }
218  reset( $this->_aArray);*/
219  $this->_aArray = array();
220  }
221 
227  public function assign($aArray)
228  {
229  $this->_aArray = $aArray;
230  }
231 
237  public function reverse()
238  {
239  return array_reverse($this->_aArray);
240  }
241 
253  protected $_sObjectsInListName = 'oxBase';
254 
260  protected $_sCoreTable = null;
261 
265  protected $_sShopID = null;
266 
270  protected $_aSqlLimit = array();
271 
277  public function __construct($sObjectName = null)
278  {
279  $myConfig = $this->getConfig();
280  $this->_aSqlLimit[0] = 0;
281  $this->_aSqlLimit[1] = 0;
282  $this->_sShopID = $myConfig->getShopId();
283 
284  if ($sObjectName) {
285  $this->init($sObjectName);
286  }
287  }
288 
296  public function __get($sName)
297  {
298  if ($sName == 'aList') {
299  return $this->_aArray;
300  }
301  }
302 
308  public function getArray()
309  {
310  return $this->_aArray;
311  }
312 
319  public function init($sObjectName, $sCoreTable = null)
320  {
321  $this->_sObjectsInListName = $sObjectName;
322  if ($sCoreTable) {
323  $this->_sCoreTable = $sCoreTable;
324  }
325  }
326 
332  public function getBaseObject()
333  {
334  if (!$this->_oBaseObject) {
335  $this->_oBaseObject = oxNew($this->_sObjectsInListName);
336  $this->_oBaseObject->setInList();
337  $this->_oBaseObject->init($this->_sCoreTable);
338  }
339 
340  return $this->_oBaseObject;
341  }
342 
348  public function setBaseObject($oObject)
349  {
350  $this->_oBaseObject = $oObject;
351  }
352 
358  public function selectString($sSql)
359  {
360  $this->clear();
361 
363  if ($this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
364  $rs = $oDb->selectLimit($sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0]);
365  } else {
366  $rs = $oDb->select($sSql);
367  }
368 
369  if ($rs != false && $rs->recordCount() > 0) {
370 
371  $oSaved = clone $this->getBaseObject();
372 
373  while (!$rs->EOF) {
374 
375  $oListObject = clone $oSaved;
376 
377  $this->_assignElement($oListObject, $rs->fields);
378 
379  $this->add($oListObject);
380 
381  $rs->moveNext();
382  }
383  }
384  }
385 
391  public function add($oObject)
392  {
393  if ($oObject->getId()) {
394  $this->_aArray[$oObject->getId()] = $oObject;
395  } else {
396  $this->_aArray[] = $oObject;
397  }
398  }
399 
405  public function assignArray($aData)
406  {
407  $this->clear();
408  if (count($aData)) {
409 
410  $oSaved = clone $this->getBaseObject();
411 
412  foreach ($aData as $aItem) {
413  $oListObject = clone $oSaved;
414  $this->_assignElement($oListObject, $aItem);
415  if ($oListObject->getId()) {
416  $this->_aArray[$oListObject->getId()] = $oListObject;
417  } else {
418  $this->_aArray[] = $oListObject;
419  }
420  }
421  }
422  }
423 
424 
431  public function setSqlLimit($iStart, $iRecords)
432  {
433  $this->_aSqlLimit[0] = $iStart;
434  $this->_aSqlLimit[1] = $iRecords;
435  }
436 
445  public function containsFieldValue($oVal, $sFieldName)
446  {
447  $sFieldName = $this->_getFieldLongName($sFieldName);
448  foreach ($this->_aArray as $obj) {
449  if ($obj->{$sFieldName}->value == $oVal) {
450  return true;
451  }
452  }
453 
454  return false;
455  }
456 
462  public function getList()
463  {
464  $oListObject = $this->getBaseObject();
465  $sFieldList = $oListObject->getSelectFields();
466  $sQ = "select $sFieldList from " . $oListObject->getViewName();
467  if ($sActiveSnippet = $oListObject->getSqlActiveSnippet()) {
468  $sQ .= " where $sActiveSnippet ";
469  }
470  $this->selectString($sQ);
471 
472  return $this;
473  }
474 
482  protected function _assignElement($oListObject, $aDbFields)
483  {
484  $oListObject->assign($aDbFields);
485  }
486 
494  protected function _getFieldLongName($sFieldName)
495  {
496  if ($this->_sCoreTable) {
497  return $this->_sCoreTable . '__' . $sFieldName;
498  }
499 
500  return $sFieldName;
501  }
502 }