oxuserbasket.php

Go to the documentation of this file.
00001 <?php
00002 
00011 class oxUserBasket extends oxBase
00012 {
00013 
00019     protected $_aSkipSaveFields = array('oxcreate');
00020 
00026     protected $_sClassName = 'oxUserbasket';
00027 
00033     protected $_aBasketItems = null;
00034 
00040     protected $_blNewBasket = false;
00041 
00047     public function __construct()
00048     {
00049         parent::__construct();
00050         $this->init( 'oxuserbaskets' );
00051     }
00052 
00058     protected function _insert()
00059     {
00060         // marking basket as not new any more
00061         $this->_blNewBasket = false;
00062 
00063         if ( !isset( $this->oxuserbaskets__oxpublic->value ) ) {
00064             $this->oxuserbaskets__oxpublic = new oxField(1, oxField::T_RAW);
00065         }
00066 
00067         return parent::_insert();
00068     }
00069 
00076     public function setIsNewBasket()
00077     {
00078         $this->_blNewBasket = true;
00079     }
00080 
00086     public function getArticles()
00087     {
00088         $aRes = array();
00089         $aItems = $this->getItems();
00090         if ( is_array( $aItems ) ) {
00091             foreach ( $aItems as $sId => $oItem ) {
00092                 $oArticle = $oItem->getArticle( $sId );
00093                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList())] = $oArticle;
00094             }
00095         }
00096         return $aRes;
00097     }
00098 
00106     public function getItems( $blReload = false )
00107     {
00108         // cached ?
00109         if ( $this->_aBasketItems !== null && !$blReload ) {
00110             return $this->_aBasketItems;
00111         }
00112 
00113         // initializing
00114         $this->_aBasketItems = array();
00115 
00116         // loading basket items
00117         $oArticle  = oxNew( 'oxarticle' );
00118         $sViewName = $oArticle->getViewName();
00119 
00120         $sSelect  = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00121         $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00122         $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00123 
00124         $oItems = oxNew( 'oxlist' );
00125         $oItems->init( 'oxuserbasketitem' );
00126         $oItems->selectstring( $sSelect );
00127 
00128         foreach ( $oItems as $oItem ) {
00129             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList() );
00130             $this->_aBasketItems[$sKey] = $oItem;
00131         }
00132 
00133         return $this->_aBasketItems;
00134     }
00135 
00145     protected function _createItem( $sProductId, $aSelList = null )
00146     {
00147         $oNewItem = oxNew( 'oxuserbasketitem' );
00148         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00149         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00150 
00151         if ( !$aSelList ) {
00152             $oArticle = oxNew( 'oxArticle' );
00153             $oArticle->load( $sProductId );
00154             $aSelectLists = $oArticle->getSelectLists();
00155             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00156                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00157             }
00158         }
00159 
00160         $oNewItem->setSelList( $aSelList );
00161 
00162         return $oNewItem;
00163     }
00164 
00165 
00175     public function getItem( $sProductId, $aSelList)
00176     {
00177         // loading basket item list
00178         $aItems   = $this->getItems();
00179         $sItemKey = $this->_getItemKey( $sProductId, $aSelList );
00180 
00181         $oItem = null;
00182         // returning existing item
00183         if ( isset( $aItems[$sProductId] )) {
00184             $oItem = $aItems[$sProductId];
00185         } elseif ( isset( $aItems[$sItemKey] ) ) {
00186             $oItem = $aItems[$sItemKey];
00187         } else {
00188             $oItem = $this->_createItem( $sProductId, $aSelList );
00189         }
00190 
00191         return $oItem;
00192     }
00193 
00202     protected function _getItemKey( $sProductId, $aSel = null )
00203     {
00204         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00205         return md5( $sProductId.'|'.serialize( $aSel ) );
00206     }
00207 
00215     public function getItemCount( $blReload = false )
00216     {
00217         return count( $this->getItems( $blReload ) );
00218     }
00219 
00231     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false )
00232     {
00233         // basket info is only written in DB when something is in it
00234         if ( $this->_blNewBasket ) {
00235             $this->save();
00236         }
00237 
00238         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel ) ) ) {
00239 
00240             // if amount = 0 the means remove it
00241             if ( !$dAmount ) {
00242 
00243                 $oUserBasketItem->delete();
00244                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)])) {
00245                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] );
00246                 }
00247 
00248             } else { // updating object info and adding (if not yet added) item into basket items array
00249                 if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00250                     $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00251                 }
00252 
00253                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00254                 $oUserBasketItem->save();
00255 
00256                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] = $oUserBasketItem;
00257             }
00258 
00259             return $dAmount;
00260         }
00261     }
00262 
00270     public function delete( $sOXID = null )
00271     {
00272         if ( !$sOXID ) {
00273             $sOXID = $this->getId();
00274         }
00275 
00276         $blDelete = false;
00277         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00278             // cleaning up related data
00279             $sQ = "delete from oxuserbasketitems where oxbasketid = '$sOXID' ";
00280             oxDb::getDb()->execute( $sQ );
00281         }
00282         return $blDelete;
00283     }
00284 }

Generated on Fri Dec 19 14:20:29 2008 for OXID eShop CE by  doxygen 1.5.5