oxlist.php
Go to the documentation of this file.00001 <?php
00002
00007 class oxList extends oxSuperCfg implements ArrayAccess, Iterator, Countable
00008 {
00014 protected $_aArray = array();
00015
00021 private $_oBaseObject = null;
00022
00028 private $_blValid = true;
00029
00048 public function offsetExists( $offset )
00049 {
00050 if ( isset( $this->_aArray[$offset] ) ) {
00051 return true;
00052 } else {
00053 return false;
00054 }
00055 }
00056
00064 public function offsetGet( $offset )
00065 {
00066 if ( $this->offsetExists( $offset ) ) {
00067 return $this->_aArray[$offset];
00068 } else {
00069 return false;
00070 }
00071 }
00072
00081 public function offsetSet( $offset, $oBase )
00082 {
00083 if ( isset( $offset ) ) {
00084 $this->_aArray[$offset] = & $oBase;
00085 } else {
00086 $sLongFieldName = $this->_getFieldLongName( 'oxid' );
00087 if ( isset( $oBase->$sLongFieldName->value ) ) {
00088 $sOxid = $oBase->$sLongFieldName->value;
00089 $this->_aArray[$sOxid] = & $oBase;
00090 } else {
00091 $this->_aArray[] = & $oBase;
00092 }
00093 }
00094
00095 }
00096
00104 public function offsetUnset( $offset )
00105 {
00106 unset( $this->_aArray[$offset] );
00107 }
00108
00114 public function arrayKeys()
00115 {
00116 return array_keys( $this->_aArray );
00117 }
00118
00124 public function rewind()
00125 {
00126 $this->_blValid = ( false !== reset( $this->_aArray ) );
00127 }
00128
00134 public function current()
00135 {
00136 return current( $this->_aArray );
00137 }
00138
00144 public function key()
00145 {
00146 return key( $this->_aArray );
00147 }
00148
00154 public function next()
00155 {
00156 $this->_blValid = ( false !== next( $this->_aArray ) );
00157 }
00158
00164 public function valid()
00165 {
00166 return $this->_blValid;
00167 }
00168
00174 public function count()
00175 {
00176 return count( $this->_aArray );
00177 }
00178
00184 public function clear()
00185 {
00186
00187
00188
00189
00190
00191 $this->_aArray = array();
00192 }
00193
00201 public function assign( $aArray )
00202 {
00203 $this->_aArray = $aArray;
00204 }
00205
00211 public function reverse()
00212 {
00213 return array_reverse( $this->_aArray );
00214 }
00215
00227 protected $_sObjectsInListName = 'oxBase';
00228
00234 protected $_sCoreTable = null;
00235
00239 protected $_sShopID = null;
00240
00248 protected $_aAssignCallbackPrepend = null;
00249
00257 protected $_aAssignCallback = null;
00258
00262 protected $_aSqlLimit = array();
00263
00269 public function __construct( $sObjectName = null )
00270 {
00271 $myConfig = $this->getConfig();
00272 $this->_aSqlLimit[0] = 0;
00273 $this->_aSqlLimit[1] = 0;
00274 $this->_sShopID = $myConfig->getShopId();
00275
00276 if ( $sObjectName ) {
00277 $this->init( $sObjectName );
00278 }
00279 }
00280
00288 public function __get( $sName)
00289 {
00290
00291
00292
00293
00294 if ( $sName == 'aList') {
00295 return $this->_aArray;
00296 }
00297 }
00298
00304 public function getArray()
00305 {
00306 return $this->_aArray;
00307 }
00308
00317 public function init($sObjectName, $sCoreTable = null)
00318 {
00319 $this->_sObjectsInListName = $sObjectName;
00320 if ($sCoreTable) {
00321 $this->_sCoreTable = $sCoreTable;
00322 }
00323 }
00324
00334 public function setAssignCallbackPrepend($aAssignCallbackPrepend)
00335 {
00336 $this->_aAssignCallbackPrepend = $aAssignCallbackPrepend;
00337 }
00338
00348 public function setAssignCallback( $aAssignCallBack)
00349 {
00350 $this->_aAssignCallback = $aAssignCallBack;
00351 }
00352
00358 public function getBaseObject()
00359 {
00360 if ( !$this->_oBaseObject ) {
00361 $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
00362 $this->_oBaseObject->setInList();
00363 $this->_oBaseObject->init( $this->_sCoreTable );
00364 }
00365
00366 return $this->_oBaseObject;
00367 }
00368
00376 public function selectString( $sSql)
00377 {
00378 $this->clear();
00379
00380 if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00381 $rs = oxDb::getDb(true)->SelectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0]);
00382 } else {
00383 $rs = oxDb::getDb(true)->Execute( $sSql);
00384 }
00385
00386 $oSaved = clone $this->getBaseObject();
00387
00388
00389 if ( $this->_aAssignCallbackPrepend && is_callable($this->_aAssignCallbackPrepend)) {
00390 call_user_func( $this->_aAssignCallbackPrepend, $oSaved);
00391 }
00392
00393
00394 if ($rs != false && $rs->recordCount() > 0) {
00395 while (!$rs->EOF) {
00396
00397 $oListObject = clone $oSaved;
00398
00399 $this->_assignElement($oListObject, $rs->fields);
00400
00401
00402 if ( $this->_aAssignCallback ) {
00403 call_user_func( $this->_aAssignCallback, $oListObject );
00404 }
00405
00406
00407 if ($oListObject->getId()) {
00408 $this->_aArray[$oListObject->getId()] = $oListObject;
00409 } else {
00410 $this->_aArray[] = $oListObject;
00411 }
00412
00413 $rs->moveNext();
00414 }
00415 }
00416 }
00417
00426 public function setSqlLimit( $iStart, $iRecords)
00427 {
00428 $this->_aSqlLimit[0] = $iStart;
00429 $this->_aSqlLimit[1] = $iRecords;
00430 }
00431
00440 public function containsFieldValue($oVal, $sFieldName)
00441 {
00442 $sFieldName = $this->_getFieldLongName($sFieldName);
00443 foreach ($this->_aArray as $obj) {
00444 if ($obj->{$sFieldName}->value == $oVal) {
00445 return true;
00446 }
00447 }
00448
00449 return false;
00450 }
00451
00457 public function getList()
00458 {
00459 $oListObject =$this->getBaseObject();
00460 $sFieldList = $oListObject->getSelectFields();
00461 $sQ = "select $sFieldList from " . $oListObject->getViewName();
00462 if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
00463 $sQ .= " where $sActiveSnippet ";
00464 }
00465 $this->selectString($sQ);
00466
00467 return $this;
00468 }
00469
00479 protected function _assignElement($oListObject, $aDbFields)
00480 {
00481 $oListObject->assign($aDbFields);
00482 }
00483
00491 protected function _getFieldLongName($sFieldName)
00492 {
00493 if ($this->_sCoreTable) {
00494 return $this->_sCoreTable . '__' . $sFieldName;
00495 }
00496
00497 return $sFieldName;
00498 }
00499
00500 }