oxuserbasket.php

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

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5