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         if ( $sName == 'aList') {
00309             return $this->_aArray;
00310         }
00311     }
00312 
00318     public function getArray()
00319     {
00320         return $this->_aArray;
00321     }
00322 
00331     public function init($sObjectName, $sCoreTable = null)
00332     {
00333         $this->_sObjectsInListName = $sObjectName;
00334         if ($sCoreTable) {
00335             $this->_sCoreTable = $sCoreTable;
00336         }
00337     }
00338 
00344     public function getBaseObject()
00345     {
00346         if ( !$this->_oBaseObject ) {
00347             $this->_oBaseObject = oxNew( $this->_sObjectsInListName );
00348             $this->_oBaseObject->setInList();
00349             $this->_oBaseObject->init( $this->_sCoreTable );
00350         }
00351 
00352         return $this->_oBaseObject;
00353     }
00354 
00362     public function selectString( $sSql )
00363     {
00364         $this->clear();
00365 
00366         $oDb = oxDb::getDb( oxDb::FETCH_MODE_ASSOC );
00367         if ( $this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00368             $rs = $oDb->selectLimit( $sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0] );
00369         } else {
00370             $rs = $oDb->select( $sSql );
00371         }
00372 
00373         if ($rs != false && $rs->recordCount() > 0) {
00374 
00375             $oSaved = clone $this->getBaseObject();
00376 
00377             while (!$rs->EOF) {
00378 
00379                 $oListObject = clone $oSaved;
00380 
00381                 $this->_assignElement($oListObject, $rs->fields);
00382 
00383                 if ($oListObject->getId()) {
00384                     $this->_aArray[$oListObject->getId()] = $oListObject;
00385                 } else {
00386                     $this->_aArray[] = $oListObject;
00387                 }
00388 
00389                 $rs->moveNext();
00390             }
00391         }
00392     }
00393 
00394 
00402     public function assignArray( $aData )
00403     {
00404         $this->clear();
00405         if ( count( $aData ) ) {
00406 
00407             $oSaved = clone $this->getBaseObject();
00408 
00409             foreach ($aData as $aItem) {
00410                 $oListObject = clone $oSaved;
00411                 $this->_assignElement( $oListObject, $aItem );
00412                 if ( $oListObject->getId() ) {
00413                     $this->_aArray[ $oListObject->getId() ] = $oListObject;
00414                 } else {
00415                     $this->_aArray[] = $oListObject;
00416                 }
00417             }
00418         }
00419     }
00420 
00421 
00430     public function setSqlLimit( $iStart, $iRecords)
00431     {
00432         $this->_aSqlLimit[0] = $iStart;
00433         $this->_aSqlLimit[1] = $iRecords;
00434     }
00435 
00444     public function containsFieldValue($oVal, $sFieldName)
00445     {
00446         $sFieldName = $this->_getFieldLongName($sFieldName);
00447         foreach ($this->_aArray as $obj) {
00448             if ($obj->{$sFieldName}->value == $oVal) {
00449                 return true;
00450             }
00451         }
00452 
00453         return false;
00454     }
00455 
00461     public function getList()
00462     {
00463         $oListObject =$this->getBaseObject();
00464         $sFieldList = $oListObject->getSelectFields();
00465         $sQ = "select $sFieldList from " . $oListObject->getViewName();
00466         if ( $sActiveSnippet = $oListObject->getSqlActiveSnippet() ) {
00467             $sQ .= " where $sActiveSnippet ";
00468         }
00469         $this->selectString($sQ);
00470 
00471         return $this;
00472     }
00473 
00483     protected function _assignElement($oListObject, $aDbFields)
00484     {
00485         $oListObject->assign($aDbFields);
00486     }
00487 
00495     protected function _getFieldLongName($sFieldName)
00496     {
00497         if ($this->_sCoreTable) {
00498             return $this->_sCoreTable . '__' . $sFieldName;
00499         }
00500 
00501         return $sFieldName;
00502     }
00503 
00504 }