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         $iTime = oxUtilsDate::getInstance()->getTime();
00067         $this->oxuserbaskets__oxcreate = new oxField( $iTime );
00068         $this->oxuserbaskets__oxupdate = new oxField( $iTime );
00069 
00070         return parent::_insert();
00071     }
00072 
00079     public function setIsNewBasket()
00080     {
00081         $this->_blNewBasket = true;
00082         $iTime = oxUtilsDate::getInstance()->getTime();
00083         $this->oxuserbaskets__oxcreate = new oxField( $iTime );
00084         $this->oxuserbaskets__oxupdate = new oxField( $iTime );
00085     }
00086 
00092     public function isNewBasket()
00093     {
00094         return $this->_blNewBasket;
00095     }
00096 
00102     public function isEmpty()
00103     {
00104         if ( $this->isNewBasket() || $this->getItemCount() < 1 ) {
00105             return true;
00106         }
00107 
00108         return false;
00109     }
00110 
00116     public function getArticles()
00117     {
00118         $aRes = array();
00119         $aItems = $this->getItems();
00120         if ( is_array( $aItems ) ) {
00121             foreach ( $aItems as $sId => $oItem ) {
00122                 $oArticle = $oItem->getArticle( $sId );
00123                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList())] = $oArticle;
00124             }
00125         }
00126         return $aRes;
00127     }
00128 
00137     public function getItems( $blReload = false, $blActiveCheck = true )
00138     {
00139         // cached ?
00140         if ( $this->_aBasketItems !== null && !$blReload ) {
00141             return $this->_aBasketItems;
00142         }
00143 
00144         // initializing
00145         $this->_aBasketItems = array();
00146 
00147         // loading basket items
00148         $oArticle  = oxNew( 'oxarticle' );
00149         $sViewName = $oArticle->getViewName();
00150 
00151         $sSelect  = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00152         if ($blActiveCheck) {
00153             $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00154         }
00155         $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00156 
00157         $oItems = oxNew( 'oxlist' );
00158         $oItems->init( 'oxuserbasketitem' );
00159         $oItems->selectstring( $sSelect );
00160 
00161         foreach ( $oItems as $oItem ) {
00162             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList() );
00163             $this->_aBasketItems[$sKey] = $oItem;
00164         }
00165 
00166         return $this->_aBasketItems;
00167     }
00168 
00178     protected function _createItem( $sProductId, $aSelList = null )
00179     {
00180         $oNewItem = oxNew( 'oxuserbasketitem' );
00181         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00182         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00183 
00184         if ( !$aSelList ) {
00185             $oArticle = oxNew( 'oxArticle' );
00186             $oArticle->load( $sProductId );
00187             $aSelectLists = $oArticle->getSelectLists();
00188             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00189                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00190             }
00191         }
00192 
00193         $oNewItem->setSelList( $aSelList );
00194 
00195         return $oNewItem;
00196     }
00197 
00198 
00208     public function getItem( $sProductId, $aSelList)
00209     {
00210         // loading basket item list
00211         $aItems   = $this->getItems();
00212         $sItemKey = $this->_getItemKey( $sProductId, $aSelList );
00213 
00214         $oItem = null;
00215         // returning existing item
00216         if ( isset( $aItems[$sProductId] )) {
00217             $oItem = $aItems[$sProductId];
00218         } elseif ( isset( $aItems[$sItemKey] ) ) {
00219             $oItem = $aItems[$sItemKey];
00220         } else {
00221             $oItem = $this->_createItem( $sProductId, $aSelList );
00222         }
00223 
00224         return $oItem;
00225     }
00226 
00235     protected function _getItemKey( $sProductId, $aSel = null )
00236     {
00237         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00238         return md5( $sProductId.'|'.serialize( $aSel ) );
00239     }
00240 
00248     public function getItemCount( $blReload = false )
00249     {
00250         return count( $this->getItems( $blReload ) );
00251     }
00252 
00264     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false )
00265     {
00266         // basket info is only written in DB when something is in it
00267         if ( $this->_blNewBasket ) {
00268             $this->save();
00269         }
00270 
00271         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel ) ) ) {
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             if ( !$dAmount ) {
00278                 // if amount = 0 the means remove it
00279                 $oUserBasketItem->delete();
00280                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)])) {
00281                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] );
00282                 }
00283             } else {
00284                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00285                 $oUserBasketItem->save();
00286 
00287                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel)] = $oUserBasketItem;
00288             }
00289 
00290             //update timestamp
00291             $this->oxuserbaskets__oxupdate = new oxField(oxUtilsDate::getInstance()->getTime());
00292             $this->save();
00293 
00294             return $dAmount;
00295         }
00296     }
00297 
00305     public function delete( $sOXID = null )
00306     {
00307         if ( !$sOXID ) {
00308             $sOXID = $this->getId();
00309         }
00310 
00311         $blDelete = false;
00312         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00313             // cleaning up related data
00314             $sQ = "delete from oxuserbasketitems where oxbasketid = " . oxDb::getDb()->quote( $sOXID );
00315             oxDb::getDb()->execute( $sQ );
00316         }
00317         return $blDelete;
00318     }
00319 
00325     public function isVisible()
00326     {
00327         $oActivUser = $this->getConfig()->getUser();
00328         $sActivUserId = null;
00329         if ($oActivUser)
00330             $sActivUserId = $oActivUser->getId();
00331 
00332         $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
00333                               ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
00334 
00335         return $blIsVisible;
00336     }
00337 
00338 }