00001 <?php
00002
00008 class oxList extends oxSuperCfg implements ArrayAccess, Iterator, Countable
00009 {
00015 protected $_aArray = array();
00016
00022 private $_oBaseObject = null;
00023
00029 private $_blValid = true;
00030
00049 public function offsetExists( $offset )
00050 {
00051 if( isset( $this->_aArray[$offset] ) ) {
00052 return true;
00053 } else {
00054 return false;
00055 }
00056 }
00057
00065 public function offsetGet( $offset )
00066 {
00067 if( $this->offsetExists( $offset ) ) {
00068 return $this->_aArray[$offset];
00069 } else {
00070 return false;
00071 }
00072 }
00073
00082 public function offsetSet( $offset, $oBase )
00083 {
00084 if ( isset( $offset ) ) {
00085 $this->_aArray[$offset] = & $oBase;
00086 } else {
00087 $sLongFieldName = $this->_getFieldLongName( 'oxid' );
00088 if ( isset( $oBase->$sLongFieldName->value ) ) {
00089 $sOxid = $oBase->$sLongFieldName->value;
00090 $this->_aArray[$sOxid] = & $oBase;
00091 } else {
00092 $this->_aArray[] = & $oBase;
00093 }
00094 }
00095
00096 }
00097
00105 public function offsetUnset( $offset )
00106 {
00107 unset( $this->_aArray[$offset] );
00108 }
00109
00115 public function arrayKeys()
00116 {
00117 return array_keys( $this->_aArray );
00118 }
00119
00125 public function rewind()
00126 {
00127 $this->_blValid = ( false !== reset( $this->_aArray ) );
00128 }
00129
00135 public function current()
00136 {
00137 return current( $this->_aArray );
00138 }
00139
00145 public function key()
00146 {
00147 return key( $this->_aArray );
00148 }
00149
00155 public function next()
00156 {
00157 $this->_blValid = ( false !== next( $this->_aArray ) );
00158 }
00159
00165 public function valid()
00166 {
00167 return $this->_blValid;
00168 }
00169
00175 public function count()
00176 {
00177 return count( $this->_aArray );
00178 }
00179
00185 public function clear()
00186 {
00187
00188
00189
00190
00191
00192 $this->_aArray = array();
00193 }
00194
00202 public function assign( $aArray )
00203 {
00204 $this->_aArray = $aArray;
00205 }
00206
00212 public function reverse()
00213 {
00214 return array_reverse( $this->_aArray );
00215 }
00216
00228 protected $_sObjectsInListName = 'oxBase';
00229
00235 protected $_sCoreTable = null;
00236
00240 protected $_sShopID = null;
00241
00247 protected $_aAssignCallbackPrepend = null;
00248
00254 protected $_aAssignCallback = null;
00255
00259 protected $_aSqlLimit = array();
00260
00266 public function __construct( $sObjectName = null )
00267 {
00268 $myConfig = $this->getConfig();
00269 $this->_aSqlLimit[0] = 0;
00270 $this->_aSqlLimit[1] = 0;
00271 $this->_sShopID = $myConfig->getShopId();
00272
00273 if ( $sObjectName ) {
00274 $this->init( $sObjectName );
00275 }
00276 }
00277
00285 public function __get( $sName)
00286 {
00287
00288
00289
00290
00291 if( $sName == 'aList') {
00292 return $this->_aArray;
00293 }
00294 }
00295
00301 public function getArray()
00302 {
00303 return $this->_aArray;
00304 }
00305
00314 public function init($sObjectName, $sCoreTable = null)
00315 {
00316 $this->_sObjectsInListName = $sObjectName;
00317 if ($sCoreTable) {
00318 $this->_sCoreTable = $sCoreTable;
00319 }
00320 }
00321
00329 public function setAssignCallbackPrepend($aAssignCallbackPrepend)
00330 {
00331 $this->_aAssignCallbackPrepend = $aAssignCallbackPrepend;
00332 }
00333
00341 public function setAssignCallback( $aAssignCallBack)
00342 {
00343 $this->_aAssignCallback = $aAssignCallBack;
00344 }
00345
00351 public function getBaseObject()
00352 {
00353 if ( !$this->_oBaseObject ) {
00354 $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
00355 $this->_oBaseObject->init( $this->_sCoreTable );
00356 }
00357
00358 return $this->_oBaseObject;
00359 }
00360
00368 public function selectString( $sSql)
00369 {
00370
00371
00372 $this->clear();
00373
00374 if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00375 $rs = oxDb::getDb(true)->SelectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0]);
00376 } else {
00377 $rs = oxDb::getDb(true)->Execute( $sSql);
00378 }
00379
00380
00381 $oSaved = oxNew( $this->_sObjectsInListName);
00382
00383 $oSaved->init($this->getBaseObject()->getCoreTableName());
00384
00385 if ( $oSaved->isMultilang() ) {
00386 $oSaved->setLanguage( $this->getBaseObject()->getLanguage() );
00387 }
00388
00389 if ( $this->_aAssignCallbackPrepend ) {
00390 call_user_func( array( $oSaved, $this->_aAssignCallbackPrepend ) );
00391 }
00392
00393 if ($rs != false && $rs->recordCount() > 0) {
00394 while (!$rs->EOF) {
00395
00396 $oListObject = clone $oSaved;
00397
00398 $oListObject->assign( $rs->fields);
00399
00400 if ( $this->_aAssignCallBack ) {
00401 call_user_func( array( $oListObject, $this->_aAssignCallBack ) );
00402 }
00403
00404 if ($oListObject->getId()) {
00405 $this->_aArray[$oListObject->getId()] = $oListObject;
00406 } else {
00407 $this->_aArray[] = $oListObject;
00408 }
00409
00410 $rs->moveNext();
00411 }
00412 }
00413 }
00414
00423 public function setSqlLimit( $iStart, $iRecords)
00424 {
00425 $this->_aSqlLimit[0] = $iStart;
00426 $this->_aSqlLimit[1] = $iRecords;
00427 }
00428
00437 public function setObjectCallback( $aCallbackArray)
00438 {
00439 $this->_aAssignCallback = $aCallbackArray;
00440 }
00441
00450 public function containsFieldValue($oVal, $sFieldName)
00451 {
00452 $sFieldName = $this->_getFieldLongName($sFieldName);
00453 foreach ($this->_aArray as $obj) {
00454 if ($obj->{$sFieldName}->value == $oVal) {
00455 return true;
00456 }
00457 }
00458
00459 return false;
00460 }
00461
00467 public function getList()
00468 {
00469 $sFieldList = $this->getBaseObject()->getSelectFields();
00470 $sQ = "select $sFieldList from " . $this->getBaseObject()->getViewName();
00471 if ($sActiveSnippet = $this->getBaseObject()->getSqlActiveSnippet()) {
00472 $sQ .= " where $sActiveSnippet ";
00473 }
00474 $this->selectString($sQ);
00475 }
00476
00484 protected function _getFieldLongName($sFieldName)
00485 {
00486 if ($this->_sCoreTable) {
00487 return $this->_sCoreTable . '__' . $sFieldName;
00488 }
00489
00490 return $sFieldName;
00491 }
00492
00493 }