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         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00370         if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00371             $rs = $oDb->selectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0] );
00372         } else {
00373             $rs = $oDb->select( $sSql );
00374         }
00375 
00376         if ($rs != false && $rs->recordCount() > 0) {
00377 
00378             $oSaved = clone $this->getBaseObject();
00379 
00380             while (!$rs->EOF) {
00381 
00382                 $oListObject = clone $oSaved;
00383 
00384                 $this->_assignElement($oListObject, $rs->fields);
00385 
00386                 if ($oListObject->getId()) {
00387                     $this->_aArray[$oListObject->getId()] = $oListObject;
00388                 } else {
00389                     $this->_aArray[] = $oListObject;
00390                 }
00391 
00392                 $rs->moveNext();
00393             }
00394         }
00395     }
00396 
00405     public function setSqlLimit( $iStart, $iRecords)
00406     {
00407         $this->_aSqlLimit[0] = $iStart;
00408         $this->_aSqlLimit[1] = $iRecords;
00409     }
00410 
00419     public function containsFieldValue($oVal, $sFieldName)
00420     {
00421         $sFieldName = $this->_getFieldLongName($sFieldName);
00422         foreach ($this->_aArray as $obj) {
00423             if ($obj->{$sFieldName}->value == $oVal) {
00424                 return true;
00425             }
00426         }
00427 
00428         return false;
00429     }
00430 
00436     public function getList()
00437     {
00438         $oListObject =$this->getBaseObject();
00439         $sFieldList = $oListObject->getSelectFields();
00440         $sQ = "select $sFieldList from " . $oListObject->getViewName();
00441         if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
00442             $sQ .= " where $sActiveSnippet ";
00443         }
00444         $this->selectString($sQ);
00445 
00446         return $this;
00447     }
00448 
00458     protected function _assignElement($oListObject, $aDbFields)
00459     {
00460         $oListObject->assign($aDbFields);
00461     }
00462 
00470     protected function _getFieldLongName($sFieldName)
00471     {
00472         if ($this->_sCoreTable) {
00473             return $this->_sCoreTable . '__' . $sFieldName;
00474         }
00475 
00476         return $sFieldName;
00477     }
00478 
00479 }