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 
00077     public function setIsNewBasket()
00078     {
00079         $this->_blNewBasket = true;
00080         $iTime = oxRegistry::get("oxUtilsDate")->getTime();
00081         $this->oxuserbaskets__oxupdate = new oxField($iTime);
00082     }
00083 
00089     public function isNewBasket()
00090     {
00091         return $this->_blNewBasket;
00092     }
00093 
00099     public function isEmpty()
00100     {
00101         if ($this->isNewBasket() || $this->getItemCount() < 1) {
00102             return true;
00103         }
00104 
00105         return false;
00106     }
00107 
00113     public function getArticles()
00114     {
00115         $aRes = array();
00116         $aItems = $this->getItems();
00117         if (is_array($aItems)) {
00118             foreach ($aItems as $sId => $oItem) {
00119                 $oArticle = $oItem->getArticle($sId);
00120                 $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList(), $oItem->getPersParams())] = $oArticle;
00121             }
00122         }
00123 
00124         return $aRes;
00125     }
00126 
00135     public function getItems($blReload = false, $blActiveCheck = true)
00136     {
00137         // cached ?
00138         if ($this->_aBasketItems !== null && !$blReload) {
00139             return $this->_aBasketItems;
00140         }
00141 
00142         // initializing
00143         $this->_aBasketItems = array();
00144 
00145         // loading basket items
00146         $oArticle = oxNew('oxarticle');
00147         $sViewName = $oArticle->getViewName();
00148 
00149         $sSelect = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
00150         if ($blActiveCheck) {
00151             $sSelect .= 'and ' . $oArticle->getSqlActiveSnippet() . ' ';
00152         }
00153         $sSelect .= "where oxuserbasketitems.oxbasketid = '" . $this->getId() . "' and $sViewName.oxid is not null ";
00154 
00155         $sSelect .= " order by oxartnum, oxsellist, oxpersparam ";
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(), $oItem->getPersParams());
00163             $this->_aBasketItems[$sKey] = $oItem;
00164         }
00165 
00166         return $this->_aBasketItems;
00167     }
00168 
00179     protected function _createItem($sProductId, $aSelList = null, $aPersParams = null)
00180     {
00181         $oNewItem = oxNew('oxuserbasketitem');
00182         $oNewItem->oxuserbasketitems__oxartid = new oxField($sProductId, oxField::T_RAW);
00183         $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
00184         if ($aPersParams && count($aPersParams)) {
00185             $oNewItem->setPersParams($aPersParams);
00186         }
00187 
00188         if (!$aSelList) {
00189             $oArticle = oxNew('oxArticle');
00190             $oArticle->load($sProductId);
00191             $aSelectLists = $oArticle->getSelectLists();
00192             if (($iSelCnt = count($aSelectLists))) {
00193                 $aSelList = array_fill(0, $iSelCnt, '0');
00194             }
00195         }
00196 
00197         $oNewItem->setSelList($aSelList);
00198 
00199         return $oNewItem;
00200     }
00201 
00202 
00213     public function getItem($sProductId, $aSelList, $aPersParams = null)
00214     {
00215         // loading basket item list
00216         $aItems = $this->getItems();
00217         $sItemKey = $this->_getItemKey($sProductId, $aSelList, $aPersParams);
00218         $oItem = null;
00219         // returning existing item
00220         if (isset($aItems[$sProductId])) {
00221             $oItem = $aItems[$sProductId];
00222         } elseif (isset($aItems[$sItemKey])) {
00223             $oItem = $aItems[$sItemKey];
00224         } else {
00225             $oItem = $this->_createItem($sProductId, $aSelList, $aPersParams);
00226         }
00227 
00228         return $oItem;
00229     }
00230 
00240     protected function _getItemKey($sProductId, $aSel = null, $aPersParam = null)
00241     {
00242         $aSel = ($aSel != null) ? $aSel : array(0 => '0');
00243 
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 
00326         return $blDelete;
00327     }
00328 
00334     public function isVisible()
00335     {
00336         $oActivUser = $this->getConfig()->getUser();
00337         $sActivUserId = null;
00338         if ($oActivUser) {
00339             $sActivUserId = $oActivUser->getId();
00340         }
00341 
00342         $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
00343                        ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
00344 
00345         return $blIsVisible;
00346     }
00347 }