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 isNewBasket()
00086     {
00087         return $this->_blNewBasket;
00088     }
00089 
00095     public function isEmpty()
00096     {
00097         if ( $this->isNewBasket() || $this->getItemCount() < 1 ) {
00098             return true;
00099         }
00100 
00101         return false;
00102     }
00103 
00109     public function getArticles()
00110     {
00111         $aRes = array();
00112         $aItems = $this->getItems();
00113         if ( is_array( $aItems ) ) {
00114             foreach ( $aItems as $sId => $oItem ) {
00115                 $oArticle = $oItem->getArticle( $sId );
00116                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList())] = $oArticle;
00117             }
00118         }
00119         return $aRes;
00120     }
00121 
00129     public function getItems( $blReload = false )
00130     {
00131         // cached ?
00132         if ( $this->_aBasketItems !== null && !$blReload ) {
00133             return $this->_aBasketItems;
00134         }
00135 
00136         // initializing
00137         $this->_aBasketItems = array();
00138 
00139         // loading basket items
00140         $oArticle  = oxNew( 'oxarticle' );
00141         $sViewName = $oArticle->getViewName();
00142 
00143         $sSelect  = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00144         $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00145         $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00146 
00147         $oItems = oxNew( 'oxlist' );
00148         $oItems->init( 'oxuserbasketitem' );
00149         $oItems->selectstring( $sSelect );
00150 
00151         foreach ( $oItems as $oItem ) {
00152             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList() );
00153             $this->_aBasketItems[$sKey] = $oItem;
00154         }
00155 
00156         return $this->_aBasketItems;
00157     }
00158 
00168     protected function _createItem( $sProductId, $aSelList = null )
00169     {
00170         $oNewItem = oxNew( 'oxuserbasketitem' );
00171         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00172         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00173 
00174         if ( !$aSelList ) {
00175             $oArticle = oxNew( 'oxArticle' );
00176             $oArticle->load( $sProductId );
00177             $aSelectLists = $oArticle->getSelectLists();
00178             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00179                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00180             }
00181         }
00182 
00183         $oNewItem->setSelList( $aSelList );
00184 
00185         return $oNewItem;
00186     }
00187 
00188 
00198     public function getItem( $sProductId, $aSelList)
00199     {
00200         // loading basket item list
00201         $aItems   = $this->getItems();
00202         $sItemKey = $this->_getItemKey( $sProductId, $aSelList );
00203 
00204         $oItem = null;
00205         // returning existing item
00206         if ( isset( $aItems[$sProductId] )) {
00207             $oItem = $aItems[$sProductId];
00208         } elseif ( isset( $aItems[$sItemKey] ) ) {
00209             $oItem = $aItems[$sItemKey];
00210         } else {
00211             $oItem = $this->_createItem( $sProductId, $aSelList );
00212         }
00213 
00214         return $oItem;
00215     }
00216 
00225     protected function _getItemKey( $sProductId, $aSel = null )
00226     {
00227         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00228         return md5( $sProductId.'|'.serialize( $aSel ) );
00229     }
00230 
00238     public function getItemCount( $blReload = false )
00239     {
00240         return count( $this->getItems( $blReload ) );
00241     }
00242 
00254     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false )
00255     {
00256         // basket info is only written in DB when something is in it
00257         if ( $this->_blNewBasket ) {
00258             $this->save();
00259         }
00260 
00261         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel ) ) ) {
00262 
00263             // if amount = 0 the means remove it
00264             if ( !$dAmount ) {
00265 
00266                 $oUserBasketItem->delete();
00267                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)])) {
00268                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] );
00269                 }
00270 
00271             } else {
00272                 // updating object info and adding (if not yet added) item into basket items array
00273                 if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00274                     $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00275                 }
00276 
00277                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00278                 $oUserBasketItem->save();
00279 
00280                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] = $oUserBasketItem;
00281             }
00282 
00283             return $dAmount;
00284         }
00285     }
00286 
00294     public function delete( $sOXID = null )
00295     {
00296         if ( !$sOXID ) {
00297             $sOXID = $this->getId();
00298         }
00299 
00300         $blDelete = false;
00301         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00302             // cleaning up related data
00303             $sQ = "delete from oxuserbasketitems where oxbasketid = " . oxDb::getDb()->quote( $sOXID );
00304             oxDb::getDb()->execute( $sQ );
00305         }
00306         return $blDelete;
00307     }
00308 
00314     public function isVisible()
00315     {
00316         $oActivUser = $this->getConfig()->getUser();
00317         $sActivUserId = null;
00318         if ($oActivUser)
00319             $sActivUserId = $oActivUser->getId();
00320 
00321         $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
00322                               ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
00323 
00324         return $blIsVisible;
00325     }
00326 
00327 }

Generated by  doxygen 1.6.2