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         // set oxcreate
00067         $this->oxuserbaskets__oxcreate = new oxField( oxUtilsDate::getInstance()->getTime() );
00068 
00069         return parent::_insert();
00070     }
00071 
00078     public function setIsNewBasket()
00079     {
00080         $this->_blNewBasket = true;
00081     }
00082 
00088     public function isNewBasket()
00089     {
00090         return $this->_blNewBasket;
00091     }
00092 
00098     public function isEmpty()
00099     {
00100         if ( $this->isNewBasket() || $this->getItemCount() < 1 ) {
00101             return true;
00102         }
00103 
00104         return false;
00105     }
00106 
00112     public function getArticles()
00113     {
00114         $aRes = array();
00115         $aItems = $this->getItems();
00116         if ( is_array( $aItems ) ) {
00117             foreach ( $aItems as $sId => $oItem ) {
00118                 $oArticle = $oItem->getArticle( $sId );
00119                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList())] = $oArticle;
00120             }
00121         }
00122         return $aRes;
00123     }
00124 
00132     public function getItems( $blReload = false )
00133     {
00134         // cached ?
00135         if ( $this->_aBasketItems !== null && !$blReload ) {
00136             return $this->_aBasketItems;
00137         }
00138 
00139         // initializing
00140         $this->_aBasketItems = array();
00141 
00142         // loading basket items
00143         $oArticle  = oxNew( 'oxarticle' );
00144         $sViewName = $oArticle->getViewName();
00145 
00146         $sSelect  = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00147         $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00148         $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00149 
00150         $oItems = oxNew( 'oxlist' );
00151         $oItems->init( 'oxuserbasketitem' );
00152         $oItems->selectstring( $sSelect );
00153 
00154         foreach ( $oItems as $oItem ) {
00155             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList() );
00156             $this->_aBasketItems[$sKey] = $oItem;
00157         }
00158 
00159         return $this->_aBasketItems;
00160     }
00161 
00171     protected function _createItem( $sProductId, $aSelList = null )
00172     {
00173         $oNewItem = oxNew( 'oxuserbasketitem' );
00174         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00175         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00176 
00177         if ( !$aSelList ) {
00178             $oArticle = oxNew( 'oxArticle' );
00179             $oArticle->load( $sProductId );
00180             $aSelectLists = $oArticle->getSelectLists();
00181             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00182                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00183             }
00184         }
00185 
00186         $oNewItem->setSelList( $aSelList );
00187 
00188         return $oNewItem;
00189     }
00190 
00191 
00201     public function getItem( $sProductId, $aSelList)
00202     {
00203         // loading basket item list
00204         $aItems   = $this->getItems();
00205         $sItemKey = $this->_getItemKey( $sProductId, $aSelList );
00206 
00207         $oItem = null;
00208         // returning existing item
00209         if ( isset( $aItems[$sProductId] )) {
00210             $oItem = $aItems[$sProductId];
00211         } elseif ( isset( $aItems[$sItemKey] ) ) {
00212             $oItem = $aItems[$sItemKey];
00213         } else {
00214             $oItem = $this->_createItem( $sProductId, $aSelList );
00215         }
00216 
00217         return $oItem;
00218     }
00219 
00228     protected function _getItemKey( $sProductId, $aSel = null )
00229     {
00230         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00231         return md5( $sProductId.'|'.serialize( $aSel ) );
00232     }
00233 
00241     public function getItemCount( $blReload = false )
00242     {
00243         return count( $this->getItems( $blReload ) );
00244     }
00245 
00257     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false )
00258     {
00259         // basket info is only written in DB when something is in it
00260         if ( $this->_blNewBasket ) {
00261             $this->save();
00262         }
00263 
00264         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel ) ) ) {
00265 
00266             // if amount = 0 the means remove it
00267             if ( !$dAmount ) {
00268 
00269                 $oUserBasketItem->delete();
00270                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)])) {
00271                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] );
00272                 }
00273 
00274             } else {
00275                 // updating object info and adding (if not yet added) item into basket items array
00276                 if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00277                     $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00278                 }
00279 
00280                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00281                 $oUserBasketItem->save();
00282 
00283                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] = $oUserBasketItem;
00284             }
00285 
00286             return $dAmount;
00287         }
00288     }
00289 
00297     public function delete( $sOXID = null )
00298     {
00299         if ( !$sOXID ) {
00300             $sOXID = $this->getId();
00301         }
00302 
00303         $blDelete = false;
00304         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00305             // cleaning up related data
00306             $sQ = "delete from oxuserbasketitems where oxbasketid = " . oxDb::getDb()->quote( $sOXID );
00307             oxDb::getDb()->execute( $sQ );
00308         }
00309         return $blDelete;
00310     }
00311 
00317     public function isVisible()
00318     {
00319         $oActivUser = $this->getConfig()->getUser();
00320         $sActivUserId = null;
00321         if ($oActivUser)
00322             $sActivUserId = $oActivUser->getId();
00323 
00324         $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
00325                               ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
00326 
00327         return $blIsVisible;
00328     }
00329 
00330 }

Generated by  doxygen 1.6.2