oxlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxList extends oxSuperCfg implements ArrayAccess, Iterator, Countable
00008 {
00009 
00015     protected $_aArray = array();
00016 
00023     protected $_blRemovedActive = false;
00024 
00030     private $_oBaseObject = null;
00031 
00037     private $_blValid = true;
00038 
00057     public function offsetExists($offset)
00058     {
00059         if (isset($this->_aArray[$offset])) {
00060             return true;
00061         } else {
00062             return false;
00063         }
00064     }
00065 
00073     public function offsetGet($offset)
00074     {
00075         if ($this->offsetExists($offset)) {
00076             return $this->_aArray[$offset];
00077         } else {
00078             return false;
00079         }
00080     }
00081 
00088     public function offsetSet($offset, $oBase)
00089     {
00090         if (isset($offset)) {
00091             $this->_aArray[$offset] = & $oBase;
00092         } else {
00093             $sLongFieldName = $this->_getFieldLongName('oxid');
00094             if (isset($oBase->$sLongFieldName->value)) {
00095                 $sOxid = $oBase->$sLongFieldName->value;
00096                 $this->_aArray[$sOxid] = & $oBase;
00097             } else {
00098                 $this->_aArray[] = & $oBase;
00099             }
00100         }
00101 
00102     }
00103 
00109     public function offsetUnset($offset)
00110     {
00111         if (strcmp($offset, $this->key()) === 0) {
00112             // #0002184: active element removed, next element will be prev / first
00113             $this->_blRemovedActive = true;
00114         }
00115 
00116         unset($this->_aArray[$offset]);
00117     }
00118 
00124     public function arrayKeys()
00125     {
00126         return array_keys($this->_aArray);
00127     }
00128 
00132     public function rewind()
00133     {
00134         $this->_blRemovedActive = false;
00135         $this->_blValid = (false !== reset($this->_aArray));
00136     }
00137 
00143     public function current()
00144     {
00145         return current($this->_aArray);
00146     }
00147 
00153     public function key()
00154     {
00155         return key($this->_aArray);
00156     }
00157 
00163     public function prev()
00164     {
00165         $oVar = prev($this->_aArray);
00166         if ($oVar === false) {
00167             // the first element, reset pointer
00168             $oVar = reset($this->_aArray);
00169         }
00170         $this->_blRemovedActive = false;
00171 
00172         return $oVar;
00173     }
00174 
00178     public function next()
00179     {
00180         if ($this->_blRemovedActive === true && current($this->_aArray)) {
00181             $oVar = $this->prev();
00182         } else {
00183             $oVar = next($this->_aArray);
00184         }
00185 
00186         $this->_blValid = (false !== $oVar);
00187     }
00188 
00194     public function valid()
00195     {
00196         return $this->_blValid;
00197     }
00198 
00204     public function count()
00205     {
00206         return count($this->_aArray);
00207     }
00208 
00212     public function clear()
00213     {
00214         /*
00215         foreach ( $this->_aArray as $key => $sValue) {
00216             unset( $this->_aArray[$key]);
00217         }
00218         reset( $this->_aArray);*/
00219         $this->_aArray = array();
00220     }
00221 
00227     public function assign($aArray)
00228     {
00229         $this->_aArray = $aArray;
00230     }
00231 
00237     public function reverse()
00238     {
00239         return array_reverse($this->_aArray);
00240     }
00241 
00253     protected $_sObjectsInListName = 'oxBase';
00254 
00260     protected $_sCoreTable = null;
00261 
00265     protected $_sShopID = null;
00266 
00270     protected $_aSqlLimit = array();
00271 
00277     public function __construct($sObjectName = null)
00278     {
00279         $myConfig = $this->getConfig();
00280         $this->_aSqlLimit[0] = 0;
00281         $this->_aSqlLimit[1] = 0;
00282         $this->_sShopID = $myConfig->getShopId();
00283 
00284         if ($sObjectName) {
00285             $this->init($sObjectName);
00286         }
00287     }
00288 
00296     public function __get($sName)
00297     {
00298         if ($sName == 'aList') {
00299             return $this->_aArray;
00300         }
00301     }
00302 
00308     public function getArray()
00309     {
00310         return $this->_aArray;
00311     }
00312 
00319     public function init($sObjectName, $sCoreTable = null)
00320     {
00321         $this->_sObjectsInListName = $sObjectName;
00322         if ($sCoreTable) {
00323             $this->_sCoreTable = $sCoreTable;
00324         }
00325     }
00326 
00332     public function getBaseObject()
00333     {
00334         if (!$this->_oBaseObject) {
00335             $this->_oBaseObject = oxNew($this->_sObjectsInListName);
00336             $this->_oBaseObject->setInList();
00337             $this->_oBaseObject->init($this->_sCoreTable);
00338         }
00339 
00340         return $this->_oBaseObject;
00341     }
00342 
00348     public function setBaseObject($oObject)
00349     {
00350         $this->_oBaseObject = $oObject;
00351     }
00352 
00358     public function selectString($sSql)
00359     {
00360         $this->clear();
00361 
00362         $oDb = oxDb::getDb(oxDb::FETCH_MODE_ASSOC);
00363         if ($this->_aSqlLimit[0] || $this->_aSqlLimit[1]) {
00364             $rs = $oDb->selectLimit($sSql, $this->_aSqlLimit[1], $this->_aSqlLimit[0]);
00365         } else {
00366             $rs = $oDb->select($sSql);
00367         }
00368 
00369         if ($rs != false && $rs->recordCount() > 0) {
00370 
00371             $oSaved = clone $this->getBaseObject();
00372 
00373             while (!$rs->EOF) {
00374 
00375                 $oListObject = clone $oSaved;
00376 
00377                 $this->_assignElement($oListObject, $rs->fields);
00378 
00379                 $this->add($oListObject);
00380 
00381                 $rs->moveNext();
00382             }
00383         }
00384     }
00385 
00391     public function add($oObject)
00392     {
00393         if ($oObject->getId()) {
00394             $this->_aArray[$oObject->getId()] = $oObject;
00395         } else {
00396             $this->_aArray[] = $oObject;
00397         }
00398     }
00399 
00405     public function assignArray($aData)
00406     {
00407         $this->clear();
00408         if (count($aData)) {
00409 
00410             $oSaved = clone $this->getBaseObject();
00411 
00412             foreach ($aData as $aItem) {
00413                 $oListObject = clone $oSaved;
00414                 $this->_assignElement($oListObject, $aItem);
00415                 if ($oListObject->getId()) {
00416                     $this->_aArray[$oListObject->getId()] = $oListObject;
00417                 } else {
00418                     $this->_aArray[] = $oListObject;
00419                 }
00420             }
00421         }
00422     }
00423 
00424 
00431     public function setSqlLimit($iStart, $iRecords)
00432     {
00433         $this->_aSqlLimit[0] = $iStart;
00434         $this->_aSqlLimit[1] = $iRecords;
00435     }
00436 
00445     public function containsFieldValue($oVal, $sFieldName)
00446     {
00447         $sFieldName = $this->_getFieldLongName($sFieldName);
00448         foreach ($this->_aArray as $obj) {
00449             if ($obj->{$sFieldName}->value == $oVal) {
00450                 return true;
00451             }
00452         }
00453 
00454         return false;
00455     }
00456 
00462     public function getList()
00463     {
00464         $oListObject = $this->getBaseObject();
00465         $sFieldList = $oListObject->getSelectFields();
00466         $sQ = "select $sFieldList from " . $oListObject->getViewName();
00467         if ($sActiveSnippet = $oListObject->getSqlActiveSnippet()) {
00468             $sQ .= " where $sActiveSnippet ";
00469         }
00470         $this->selectString($sQ);
00471 
00472         return $this;
00473     }
00474 
00482     protected function _assignElement($oListObject, $aDbFields)
00483     {
00484         $oListObject->assign($aDbFields);
00485     }
00486 
00494     protected function _getFieldLongName($sFieldName)
00495     {
00496         if ($this->_sCoreTable) {
00497             return $this->_sCoreTable . '__' . $sFieldName;
00498         }
00499 
00500         return $sFieldName;
00501     }
00502 }