OXID eShop CE  4.8.11
 All Classes 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 {
14  protected $_aArray = array();
15 
22  protected $_blRemovedActive = false;
23 
29  private $_oBaseObject = null;
30 
36  private $_blValid = true;
37 
56  public function offsetExists( $offset )
57  {
58  if ( isset( $this->_aArray[$offset] ) ) {
59  return true;
60  } else {
61  return false;
62  }
63  }
64 
72  public function offsetGet( $offset )
73  {
74  if ( $this->offsetExists( $offset ) ) {
75  return $this->_aArray[$offset];
76  } else {
77  return false;
78  }
79  }
80 
89  public function offsetSet( $offset, $oBase )
90  {
91  if ( isset( $offset ) ) {
92  $this->_aArray[$offset] = & $oBase;
93  } else {
94  $sLongFieldName = $this->_getFieldLongName( 'oxid' );
95  if ( isset( $oBase->$sLongFieldName->value ) ) {
96  $sOxid = $oBase->$sLongFieldName->value;
97  $this->_aArray[$sOxid] = & $oBase;
98  } else {
99  $this->_aArray[] = & $oBase;
100  }
101  }
102 
103  }
104 
112  public function offsetUnset( $offset )
113  {
114  if (strcmp($offset, $this->key()) === 0) {
115  // #0002184: active element removed, next element will be prev / first
116  $this->_blRemovedActive = true;
117  }
118 
119  unset( $this->_aArray[$offset] );
120  }
121 
127  public function arrayKeys()
128  {
129  return array_keys( $this->_aArray );
130  }
131 
137  public function rewind()
138  {
139  $this->_blRemovedActive = false;
140  $this->_blValid = ( false !== reset( $this->_aArray ) );
141  }
142 
148  public function current()
149  {
150  return current( $this->_aArray );
151  }
152 
158  public function key()
159  {
160  return key( $this->_aArray );
161  }
162 
168  public function prev()
169  {
170  $oVar = prev($this->_aArray);
171  if ($oVar === false) {
172  // the first element, reset pointer
173  $oVar = reset($this->_aArray);
174  }
175  $this->_blRemovedActive = false;
176  return $oVar;
177  }
178 
184  public function next()
185  {
186  if ($this->_blRemovedActive === true && current($this->_aArray)) {
187  $oVar = $this->prev();
188  } else {
189  $oVar = next($this->_aArray);
190  }
191 
192  $this->_blValid = ( false !== $oVar );
193  }
194 
200  public function valid()
201  {
202  return $this->_blValid;
203  }
204 
210  public function count()
211  {
212  return count( $this->_aArray );
213  }
214 
220  public function clear()
221  {
222  /*
223  foreach ( $this->_aArray as $key => $sValue) {
224  unset( $this->_aArray[$key]);
225  }
226  reset( $this->_aArray);*/
227  $this->_aArray = array();
228  }
229 
237  public function assign( $aArray )
238  {
239  $this->_aArray = $aArray;
240  }
241 
247  public function reverse()
248  {
249  return array_reverse( $this->_aArray );
250  }
251 
263  protected $_sObjectsInListName = 'oxBase';
264 
270  protected $_sCoreTable = null;
271 
275  protected $_sShopID = null;
276 
280  protected $_aSqlLimit = array();
281 
287  public function __construct( $sObjectName = null )
288  {
289  $myConfig = $this->getConfig();
290  $this->_aSqlLimit[0] = 0;
291  $this->_aSqlLimit[1] = 0;
292  $this->_sShopID = $myConfig->getShopId();
293 
294  if ( $sObjectName ) {
295  $this->init( $sObjectName );
296  }
297  }
298 
306  public function __get( $sName )
307  {
308  if ( $sName == 'aList') {
309  return $this->_aArray;
310  }
311  }
312 
318  public function getArray()
319  {
320  return $this->_aArray;
321  }
322 
331  public function init($sObjectName, $sCoreTable = null)
332  {
333  $this->_sObjectsInListName = $sObjectName;
334  if ($sCoreTable) {
335  $this->_sCoreTable = $sCoreTable;
336  }
337  }
338 
344  public function getBaseObject()
345  {
346  if ( !$this->_oBaseObject ) {
347  $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
348  $this->_oBaseObject->setInList();
349  $this->_oBaseObject->init( $this->_sCoreTable );
350  }
351 
352  return $this->_oBaseObject;
353  }
354 
362  public function selectString( $sSql )
363  {
364  $this->clear();
365 
367  if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
368  $rs = $oDb->selectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0] );
369  } else {
370  $rs = $oDb->select( $sSql );
371  }
372 
373  if ($rs != false && $rs->recordCount() > 0) {
374 
375  $oSaved = clone $this->getBaseObject();
376 
377  while (!$rs->EOF) {
378 
379  $oListObject = clone $oSaved;
380 
381  $this->_assignElement($oListObject, $rs->fields);
382 
383  if ($oListObject->getId()) {
384  $this->_aArray[$oListObject->getId()] = $oListObject;
385  } else {
386  $this->_aArray[] = $oListObject;
387  }
388 
389  $rs->moveNext();
390  }
391  }
392  }
393 
394 
402  public function assignArray( $aData )
403  {
404  $this->clear();
405  if ( count( $aData ) ) {
406 
407  $oSaved = clone $this->getBaseObject();
408 
409  foreach ($aData as $aItem) {
410  $oListObject = clone $oSaved;
411  $this->_assignElement( $oListObject, $aItem );
412  if ( $oListObject->getId() ) {
413  $this->_aArray[ $oListObject->getId() ] = $oListObject;
414  } else {
415  $this->_aArray[] = $oListObject;
416  }
417  }
418  }
419  }
420 
421 
430  public function setSqlLimit( $iStart, $iRecords)
431  {
432  $this->_aSqlLimit[0] = $iStart;
433  $this->_aSqlLimit[1] = $iRecords;
434  }
435 
444  public function containsFieldValue($oVal, $sFieldName)
445  {
446  $sFieldName = $this->_getFieldLongName($sFieldName);
447  foreach ($this->_aArray as $obj) {
448  if ($obj->{$sFieldName}->value == $oVal) {
449  return true;
450  }
451  }
452 
453  return false;
454  }
455 
461  public function getList()
462  {
463  $oListObject =$this->getBaseObject();
464  $sFieldList = $oListObject->getSelectFields();
465  $sQ = "select $sFieldList from " . $oListObject->getViewName();
466  if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
467  $sQ .= " where $sActiveSnippet ";
468  }
469  $this->selectString($sQ);
470 
471  return $this;
472  }
473 
483  protected function _assignElement($oListObject, $aDbFields)
484  {
485  $oListObject->assign($aDbFields);
486  }
487 
495  protected function _getFieldLongName($sFieldName)
496  {
497  if ($this->_sCoreTable) {
498  return $this->_sCoreTable . '__' . $sFieldName;
499  }
500 
501  return $sFieldName;
502  }
503 
504 }