oxuserbasket.php

Go to the documentation of this file.
00001 <?php
00002 
00011 class oxUserBasket extends oxBase
00012 {
00013 
00019     protected $_aSkipSaveFields = array( 'oxcreate', 'oxtimestamp' );
00020 
00026     protected $_sClassName = 'oxUserbasket';
00027 
00033     protected $_aBasketItems = null;
00034 
00040     protected $_blNewBasket = false;
00041 
00047     public function __construct()
00048     {
00049         parent::__construct();
00050         $this->init( 'oxuserbaskets' );
00051     }
00052 
00058     protected function _insert()
00059     {
00060         // marking basket as not new any more
00061         $this->_blNewBasket = false;
00062 
00063         if ( !isset( $this->oxuserbaskets__oxpublic->value ) ) {
00064             $this->oxuserbaskets__oxpublic = new oxField(1, oxField::T_RAW);
00065         }
00066 
00067         $iTime = oxRegistry::get("oxUtilsDate")->getTime();
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 = oxRegistry::get("oxUtilsDate")->getTime();
00083         $this->oxuserbaskets__oxupdate = new oxField( $iTime );
00084     }
00085 
00091     public function isNewBasket()
00092     {
00093         return $this->_blNewBasket;
00094     }
00095 
00101     public function isEmpty()
00102     {
00103         if ( $this->isNewBasket() || $this->getItemCount() < 1 ) {
00104             return true;
00105         }
00106 
00107         return false;
00108     }
00109 
00115     public function getArticles()
00116     {
00117         $aRes = array();
00118         $aItems = $this->getItems();
00119         if ( is_array( $aItems ) ) {
00120             foreach ( $aItems as $sId => $oItem ) {
00121                 $oArticle = $oItem->getArticle( $sId );
00122                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList(), $oItem->getPersParams())] = $oArticle;
00123             }
00124         }
00125         return $aRes;
00126     }
00127 
00136     public function getItems( $blReload = false, $blActiveCheck = true )
00137     {
00138         // cached ?
00139         if ( $this->_aBasketItems !== null && !$blReload ) {
00140             return $this->_aBasketItems;
00141         }
00142 
00143         // initializing
00144         $this->_aBasketItems = array();
00145 
00146         // loading basket items
00147         $oArticle  = oxNew( 'oxarticle' );
00148         $sViewName = $oArticle->getViewName();
00149 
00150         $sSelect  = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00151         if ($blActiveCheck) {
00152             $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
00153         }
00154         $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
00155 
00156         $sSelect .= " order by oxartnum, oxsellist, oxpersparam ";
00157 
00158         $oItems = oxNew( 'oxlist' );
00159         $oItems->init( 'oxuserbasketitem' );
00160         $oItems->selectstring( $sSelect );
00161 
00162         foreach ( $oItems as $oItem ) {
00163             $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList(), $oItem->getPersParams() );
00164             $this->_aBasketItems[$sKey] = $oItem;
00165         }
00166 
00167         return $this->_aBasketItems;
00168     }
00169 
00180     protected function _createItem( $sProductId, $aSelList = null, $aPersParams = null )
00181     {
00182         $oNewItem = oxNew( 'oxuserbasketitem' );
00183         $oNewItem->oxuserbasketitems__oxartid    = new oxField($sProductId, oxField::T_RAW);
00184         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00185         if ( $aPersParams && count($aPersParams) ) {
00186             $oNewItem->setPersParams( $aPersParams );
00187         }
00188 
00189         if ( !$aSelList ) {
00190             $oArticle = oxNew( 'oxArticle' );
00191             $oArticle->load( $sProductId );
00192             $aSelectLists = $oArticle->getSelectLists();
00193             if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
00194                 $aSelList = array_fill( 0, $iSelCnt, '0' );
00195             }
00196         }
00197 
00198         $oNewItem->setSelList( $aSelList );
00199 
00200         return $oNewItem;
00201     }
00202 
00203 
00214     public function getItem( $sProductId, $aSelList, $aPersParams = null)
00215     {
00216         // loading basket item list
00217         $aItems   = $this->getItems();
00218         $sItemKey = $this->_getItemKey( $sProductId, $aSelList, $aPersParams );
00219         $oItem = null;
00220         // returning existing item
00221         if ( isset( $aItems[$sProductId] )) {
00222             $oItem = $aItems[$sProductId];
00223         } elseif ( isset( $aItems[$sItemKey] ) ) {
00224             $oItem = $aItems[$sItemKey];
00225         } else {
00226             $oItem = $this->_createItem( $sProductId, $aSelList, $aPersParams );
00227         }
00228 
00229         return $oItem;
00230     }
00231 
00241     protected function _getItemKey( $sProductId, $aSel = null, $aPersParam = null )
00242     {
00243         $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
00244         return md5( $sProductId.'|'.serialize( $aSel ).'|'.serialize( $aPersParam ) );
00245     }
00246 
00254     public function getItemCount( $blReload = false )
00255     {
00256         return count( $this->getItems( $blReload ) );
00257     }
00258 
00271     public function addItemToBasket( $sProductId = null, $dAmount = null, $aSel = null, $blOverride = false, $aPersParam = null )
00272     {
00273         // basket info is only written in DB when something is in it
00274         if ( $this->_blNewBasket ) {
00275             $this->save();
00276         }
00277 
00278         if ( ( $oUserBasketItem = $this->getItem( $sProductId, $aSel, $aPersParam ) ) ) {
00279             // updating object info and adding (if not yet added) item into basket items array
00280             if ( !$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value) ) {
00281                 $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
00282             }
00283 
00284             if ( !$dAmount ) {
00285                 // if amount = 0 the means remove it
00286                 $oUserBasketItem->delete();
00287                 if ( isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)])) {
00288                     unset( $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)] );
00289                 }
00290             } else {
00291                 $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
00292                 $oUserBasketItem->save();
00293 
00294                 $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)] = $oUserBasketItem;
00295             }
00296 
00297             //update timestamp
00298             $this->oxuserbaskets__oxupdate = new oxField(oxRegistry::get("oxUtilsDate")->getTime());
00299             $this->save();
00300 
00301             return $dAmount;
00302         }
00303     }
00304 
00312     public function delete( $sOXID = null )
00313     {
00314         if ( !$sOXID ) {
00315             $sOXID = $this->getId();
00316         }
00317 
00318         $blDelete = false;
00319         if ( $sOXID && ( $blDelete = parent::delete( $sOXID ) ) ) {
00320             // cleaning up related data
00321             $oDb = oxDb::getDb();
00322             $sQ = "delete from oxuserbasketitems where oxbasketid = " . $oDb->quote( $sOXID );
00323             $oDb->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 }