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->_blRemovedActive = false;
00140         $this->_blValid = ( false !== reset( $this->_aArray ) );
00141     }
00142 
00148     public function current()
00149     {
00150         return current( $this->_aArray );
00151     }
00152 
00158     public function key()
00159     {
00160         return key( $this->_aArray );
00161     }
00162 
00168     public function prev()
00169     {
00170         $oVar = prev($this->_aArray);
00171         if ($oVar === false) {
00172             // the first element, reset pointer
00173             $oVar = reset($this->_aArray);
00174         }
00175         $this->_blRemovedActive = false;
00176         return $oVar;
00177     }
00178 
00184     public function next()
00185     {
00186         if ($this->_blRemovedActive === true && current($this->_aArray)) {
00187             $oVar = $this->prev();
00188         } else {
00189             $oVar = next($this->_aArray);
00190         }
00191 
00192         $this->_blValid = ( false !== $oVar );
00193     }
00194 
00200     public function valid()
00201     {
00202         return $this->_blValid;
00203     }
00204 
00210     public function count()
00211     {
00212         return count( $this->_aArray );
00213     }
00214 
00220     public function clear()
00221     {
00222         /*
00223         foreach ( $this->_aArray as $key => $sValue) {
00224             unset( $this->_aArray[$key]);
00225         }
00226         reset( $this->_aArray);*/
00227         $this->_aArray = array();
00228     }
00229 
00237     public function assign( $aArray )
00238     {
00239         $this->_aArray = $aArray;
00240     }
00241 
00247     public function reverse()
00248     {
00249         return array_reverse( $this->_aArray );
00250     }
00251 
00263     protected $_sObjectsInListName = 'oxBase';
00264 
00270     protected $_sCoreTable = null;
00271 
00275     protected $_sShopID = null;
00276 
00280     protected $_aSqlLimit = array();
00281 
00287     public function __construct( $sObjectName = null )
00288     {
00289         $myConfig = $this->getConfig();
00290         $this->_aSqlLimit[0] = 0;
00291         $this->_aSqlLimit[1] = 0;
00292         $this->_sShopID   = $myConfig->getShopId();
00293 
00294         if ( $sObjectName ) {
00295             $this->init( $sObjectName );
00296         }
00297     }
00298 
00306     public function __get( $sName)
00307     {
00308         //throw new Exception( 'oxList Access to undefined variable '. $sName);
00309         //echo( "Access to ".$sName.PHP_EOL);
00310 
00311         // TMP
00312         if ( $sName == 'aList') {
00313             return $this->_aArray;
00314         }
00315     }
00316 
00322     public function getArray()
00323     {
00324         return $this->_aArray;
00325     }
00326 
00335     public function init($sObjectName, $sCoreTable = null)
00336     {
00337         $this->_sObjectsInListName = $sObjectName;
00338         if ($sCoreTable) {
00339             $this->_sCoreTable = $sCoreTable;
00340         }
00341     }
00342 
00348     public function getBaseObject()
00349     {
00350         if ( !$this->_oBaseObject ) {
00351             $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
00352             $this->_oBaseObject->setInList();
00353             $this->_oBaseObject->init( $this->_sCoreTable );
00354         }
00355 
00356         return $this->_oBaseObject;
00357     }
00358 
00366     public function selectString( $sSql )
00367     {
00368         $this->clear();
00369 
00370         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00371         if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00372             $rs = $oDb->selectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0] );
00373         } else {
00374             $rs = $oDb->select( $sSql );
00375         }
00376 
00377         if ($rs != false && $rs->recordCount() > 0) {
00378 
00379             $oSaved = clone $this->getBaseObject();
00380 
00381             while (!$rs->EOF) {
00382 
00383                 $oListObject = clone $oSaved;
00384 
00385                 $this->_assignElement($oListObject, $rs->fields);
00386 
00387                 if ($oListObject->getId()) {
00388                     $this->_aArray[$oListObject->getId()] = $oListObject;
00389                 } else {
00390                     $this->_aArray[] = $oListObject;
00391                 }
00392 
00393                 $rs->moveNext();
00394             }
00395         }
00396     }
00397 
00398 
00406     public function assignArray( $aData )
00407     {
00408         $this->clear();
00409         if ( count( $aData ) ) {
00410 
00411             $oSaved = clone $this->getBaseObject();
00412 
00413             foreach ($aData as $aItem) {
00414                 $oListObject = clone $oSaved;
00415                 $this->_assignElement( $oListObject, $aItem );
00416                 if ( $oListObject->getId() ) {
00417                     $this->_aArray[ $oListObject->getId() ] = $oListObject;
00418                 } else {
00419                     $this->_aArray[] = $oListObject;
00420                 }
00421             }
00422         }
00423     }
00424 
00425 
00434     public function setSqlLimit( $iStart, $iRecords)
00435     {
00436         $this->_aSqlLimit[0] = $iStart;
00437         $this->_aSqlLimit[1] = $iRecords;
00438     }
00439 
00448     public function containsFieldValue($oVal, $sFieldName)
00449     {
00450         $sFieldName = $this->_getFieldLongName($sFieldName);
00451         foreach ($this->_aArray as $obj) {
00452             if ($obj->{$sFieldName}->value == $oVal) {
00453                 return true;
00454             }
00455         }
00456 
00457         return false;
00458     }
00459 
00465     public function getList()
00466     {
00467         $oListObject =$this->getBaseObject();
00468         $sFieldList = $oListObject->getSelectFields();
00469         $sQ = "select $sFieldList from " . $oListObject->getViewName();
00470         if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
00471             $sQ .= " where $sActiveSnippet ";
00472         }
00473         $this->selectString($sQ);
00474 
00475         return $this;
00476     }
00477 
00487     protected function _assignElement($oListObject, $aDbFields)
00488     {
00489         $oListObject->assign($aDbFields);
00490     }
00491 
00499     protected function _getFieldLongName($sFieldName)
00500     {
00501         if ($this->_sCoreTable) {
00502             return $this->_sCoreTable . '__' . $sFieldName;
00503         }
00504 
00505         return $sFieldName;
00506     }
00507 
00508 }