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 
00022     protected $_blRemovedActive = false;
00023 
00029     private $_oBaseObject = null;
00030 
00036     private $_blValid = true;
00037 
00056     public function offsetExists( $offset )
00057     {
00058         if ( isset( $this->_aArray[$offset] ) ) {
00059             return true;
00060         } else {
00061             return false;
00062         }
00063     }
00064 
00072     public function offsetGet( $offset )
00073     {
00074         if ( $this->offsetExists( $offset ) ) {
00075             return $this->_aArray[$offset];
00076         } else {
00077             return false;
00078         }
00079     }
00080 
00089     public function offsetSet( $offset, $oBase )
00090     {
00091         if ( isset( $offset ) ) {
00092             $this->_aArray[$offset] = & $oBase;
00093         } else {
00094             $sLongFieldName = $this->_getFieldLongName( 'oxid' );
00095             if ( isset( $oBase->$sLongFieldName->value ) ) {
00096                 $sOxid = $oBase->$sLongFieldName->value;
00097                 $this->_aArray[$sOxid] = & $oBase;
00098             } else {
00099                 $this->_aArray[] = & $oBase;
00100             }
00101         }
00102 
00103     }
00104 
00112     public function offsetUnset( $offset )
00113     {
00114         if (strcmp($offset, $this->key()) === 0) {
00115             // #0002184: active element removed, next element will be prev / first
00116             $this->_blRemovedActive = true;
00117         }
00118 
00119         unset( $this->_aArray[$offset] );
00120     }
00121 
00127     public function arrayKeys()
00128     {
00129         return array_keys( $this->_aArray );
00130     }
00131 
00137     public function rewind()
00138     {
00139         $this->_blValid = ( false !== reset( $this->_aArray ) );
00140     }
00141 
00147     public function current()
00148     {
00149         return current( $this->_aArray );
00150     }
00151 
00157     public function key()
00158     {
00159         return key( $this->_aArray );
00160     }
00161 
00167     public function prev()
00168     {
00169         $oVar = prev($this->_aArray);
00170         if ($oVar === false) {
00171             // the first element, reset pointer
00172             $oVar = reset($this->_aArray);
00173         }
00174         $this->_blRemovedActive = false;
00175         return $oVar;
00176     }
00177 
00183     public function next()
00184     {
00185         if ($this->_blRemovedActive === true && current($this->_aArray)) {
00186             $oVar = $this->prev();
00187         } else {
00188             $oVar = next($this->_aArray);
00189         }
00190 
00191         $this->_blValid = ( false !== $oVar );
00192     }
00193 
00199     public function valid()
00200     {
00201         return $this->_blValid;
00202     }
00203 
00209     public function count()
00210     {
00211         return count( $this->_aArray );
00212     }
00213 
00219     public function clear()
00220     {
00221         /*
00222         foreach ( $this->_aArray as $key => $sValue) {
00223             unset( $this->_aArray[$key]);
00224         }
00225         reset( $this->_aArray);*/
00226         $this->_aArray = array();
00227     }
00228 
00236     public function assign( $aArray )
00237     {
00238         $this->_aArray = $aArray;
00239     }
00240 
00246     public function reverse()
00247     {
00248         return array_reverse( $this->_aArray );
00249     }
00250 
00262     protected $_sObjectsInListName = 'oxBase';
00263 
00269     protected $_sCoreTable = null;
00270 
00274     protected $_sShopID = null;
00275 
00279     protected $_aSqlLimit = array();
00280 
00286     public function __construct( $sObjectName = null )
00287     {
00288         $myConfig = $this->getConfig();
00289         $this->_aSqlLimit[0] = 0;
00290         $this->_aSqlLimit[1] = 0;
00291         $this->_sShopID   = $myConfig->getShopId();
00292 
00293         if ( $sObjectName ) {
00294             $this->init( $sObjectName );
00295         }
00296     }
00297 
00305     public function __get( $sName)
00306     {
00307         //throw new Exception( 'oxList Access to undefined variable '. $sName);
00308         //echo( "Access to ".$sName.PHP_EOL);
00309 
00310         // TMP
00311         if ( $sName == 'aList') {
00312             return $this->_aArray;
00313         }
00314     }
00315 
00321     public function getArray()
00322     {
00323         return $this->_aArray;
00324     }
00325 
00334     public function init($sObjectName, $sCoreTable = null)
00335     {
00336         $this->_sObjectsInListName = $sObjectName;
00337         if ($sCoreTable) {
00338             $this->_sCoreTable = $sCoreTable;
00339         }
00340     }
00341 
00347     public function getBaseObject()
00348     {
00349         if ( !$this->_oBaseObject ) {
00350             $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
00351             $this->_oBaseObject->setInList();
00352             $this->_oBaseObject->init( $this->_sCoreTable );
00353         }
00354 
00355         return $this->_oBaseObject;
00356     }
00357 
00365     public function selectString( $sSql)
00366     {
00367         $this->clear();
00368 
00369         if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00370             $rs = oxDb::getDb(true)->SelectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0]);
00371         } else {
00372             $rs = oxDb::getDb(true)->Execute( $sSql);
00373         }
00374 
00375         if ($rs != false && $rs->recordCount() > 0) {
00376 
00377             $oSaved = clone $this->getBaseObject();
00378 
00379             while (!$rs->EOF) {
00380 
00381                 $oListObject = clone $oSaved;
00382 
00383                 $this->_assignElement($oListObject, $rs->fields);
00384 
00385                 if ($oListObject->getId()) {
00386                     $this->_aArray[$oListObject->getId()] = $oListObject;
00387                 } else {
00388                     $this->_aArray[] = $oListObject;
00389                 }
00390 
00391                 $rs->moveNext();
00392             }
00393         }
00394     }
00395 
00404     public function setSqlLimit( $iStart, $iRecords)
00405     {
00406         $this->_aSqlLimit[0] = $iStart;
00407         $this->_aSqlLimit[1] = $iRecords;
00408     }
00409 
00418     public function containsFieldValue($oVal, $sFieldName)
00419     {
00420         $sFieldName = $this->_getFieldLongName($sFieldName);
00421         foreach ($this->_aArray as $obj) {
00422             if ($obj->{$sFieldName}->value == $oVal) {
00423                 return true;
00424             }
00425         }
00426 
00427         return false;
00428     }
00429 
00435     public function getList()
00436     {
00437         $oListObject =$this->getBaseObject();
00438         $sFieldList = $oListObject->getSelectFields();
00439         $sQ = "select $sFieldList from " . $oListObject->getViewName();
00440         if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
00441             $sQ .= " where $sActiveSnippet ";
00442         }
00443         $this->selectString($sQ);
00444 
00445         return $this;
00446     }
00447 
00457     protected function _assignElement($oListObject, $aDbFields)
00458     {
00459         $oListObject->assign($aDbFields);
00460     }
00461 
00469     protected function _getFieldLongName($sFieldName)
00470     {
00471         if ($this->_sCoreTable) {
00472             return $this->_sCoreTable . '__' . $sFieldName;
00473         }
00474 
00475         return $sFieldName;
00476     }
00477 
00478 }