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(), $oItem->getPersParams())] = $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         $sSelect .= " order by oxartnum, oxsellist, oxpersparam ";
00158 
00159         $oItems = oxNew( 'oxlist' );
00160         $oItems->init( 'oxuserbasketitem' );
00161         $oItems->selectstring( $sSelect );
00162 
00163         foreach ( $oItems as $oItem ) {
00164             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList(), $oItem->getPersParams() );
00165             $this->_aBasketItems[$sKey] = $oItem;
00166         }
00167 
00168         return $this->_aBasketItems;
00169     }
00170 
00181     protected function _createItem( $sProductId, $aSelList = null, $aPersParams = null )
00182     {
00183         $oNewItem = oxNew( 'oxuserbasketitem' );
00184         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00185         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00186         if ( $aPersParams && count($aPersParams) ) {
00187             $oNewItem->setPersParams( $aPersParams );
00188         }
00189 
00190         if ( !$aSelList ) {
00191             $oArticle = oxNew( 'oxArticle' );
00192             $oArticle->load( $sProductId );
00193             $aSelectLists = $oArticle->getSelectLists();
00194             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00195                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00196             }
00197         }
00198 
00199         $oNewItem->setSelList( $aSelList );
00200 
00201         return $oNewItem;
00202     }
00203 
00204 
00215     public function getItem( $sProductId, $aSelList, $aPersParams = null)
00216     {
00217         // loading basket item list
00218         $aItems   = $this->getItems();
00219         $sItemKey = $this->_getItemKey( $sProductId, $aSelList, $aPersParams );
00220         $oItem = null;
00221         // returning existing item
00222         if ( isset( $aItems[$sProductId] )) {
00223             $oItem = $aItems[$sProductId];
00224         } elseif ( isset( $aItems[$sItemKey] ) ) {
00225             $oItem = $aItems[$sItemKey];
00226         } else {
00227             $oItem = $this->_createItem( $sProductId, $aSelList, $aPersParams );
00228         }
00229 
00230         return $oItem;
00231     }
00232 
00242     protected function _getItemKey( $sProductId, $aSel = null, $aPersParam = null )
00243     {
00244         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00245         return md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ) );
00246     }
00247 
00255     public function getItemCount( $blReload = false )
00256     {
00257         return count( $this->getItems( $blReload ) );
00258     }
00259 
00272     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false, $aPersParam = null )
00273     {
00274         // basket info is only written in DB when something is in it
00275         if ( $this->_blNewBasket ) {
00276             $this->save();
00277         }
00278 
00279         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel, $aPersParam ) ) ) {
00280             // updating object info and adding (if not yet added) item into basket items array
00281             if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00282                 $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00283             }
00284 
00285             if ( !$dAmount ) {
00286                 // if amount = 0 the means remove it
00287                 $oUserBasketItem->delete();
00288                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)])) {
00289                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)] );
00290                 }
00291             } else {
00292                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00293                 $oUserBasketItem->save();
00294 
00295                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)] = $oUserBasketItem;
00296             }
00297 
00298             //update timestamp
00299             $this->oxuserbaskets__oxupdate = new oxField(oxUtilsDate::getInstance()->getTime());
00300             $this->save();
00301 
00302             return $dAmount;
00303         }
00304     }
00305 
00313     public function delete( $sOXID = null )
00314     {
00315         if ( !$sOXID ) {
00316             $sOXID = $this->getId();
00317         }
00318 
00319         $blDelete = false;
00320         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00321             // cleaning up related data
00322             $sQ = "delete from oxuserbasketitems where oxbasketid = " . oxDb::getDb()->quote( $sOXID );
00323             oxDb::getDb()->execute( $sQ );
00324         }
00325         return $blDelete;
00326     }
00327 
00333     public function isVisible()
00334     {
00335         $oActivUser = $this->getConfig()->getUser();
00336         $sActivUserId = null;
00337         if ($oActivUser)
00338             $sActivUserId = $oActivUser->getId();
00339 
00340         $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
00341                               ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
00342 
00343         return $blIsVisible;
00344     }
00345 
00346 }