oxcmp_basket.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxcmp_basket extends oxView
00010 {
00011 
00017     protected $_blIsComponent = true;
00018 
00024     protected $_sLastCallFnc = null;
00025 
00032     public $aRedirectParams = array('cnid', // category id
00033                                     'mnid', // manufacturer id
00034                                     'anid', // active article id
00035                                     'tpl', // spec. template
00036                                     'listtype', // list type
00037                                     'searchcnid', // search category
00038                                     'searchvendor', // search vendor
00039                                     'searchmanufacturer', // search manufacturer
00040                                     'searchtag', // search tag
00041                                     'searchrecomm', // search recomendation
00042                                     'recommid' // recomm. list id
00043     );
00044 
00048     public function init()
00049     {
00050         $oConfig = $this->getConfig();
00051         if ($oConfig->getConfigParam('blPsBasketReservationEnabled')) {
00052             if ($oReservations = $this->getSession()->getBasketReservations()) {
00053                 if (!$oReservations->getTimeLeft()) {
00054                     $oBasket = $this->getSession()->getBasket();
00055                     if ($oBasket && $oBasket->getProductsCount()) {
00056                         $oBasket->deleteBasket();
00057                     }
00058                 }
00059                 $iLimit = (int) $oConfig->getConfigParam('iBasketReservationCleanPerRequest');
00060                 if (!$iLimit) {
00061                     $iLimit = 200;
00062                 }
00063                 $oReservations->discardUnusedReservations($iLimit);
00064             }
00065         }
00066 
00067         parent::init();
00068 
00069         // Basket exclude
00070         if ($this->getConfig()->getConfigParam('blBasketExcludeEnabled')) {
00071             if ($oBasket = $this->getSession()->getBasket()) {
00072                 $this->getParent()->setRootCatChanged($this->isRootCatChanged() && $oBasket->getContents());
00073             }
00074         }
00075     }
00076 
00083     public function render()
00084     {
00085         // recalculating
00086         if ($oBasket = $this->getSession()->getBasket()) {
00087             $oBasket->calculateBasket(false);
00088         }
00089 
00090         parent::render();
00091 
00092         return $oBasket;
00093     }
00094 
00110     public function tobasket($sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false)
00111     {
00112         // adding to basket is not allowed ?
00113         $myConfig = $this->getConfig();
00114         if (oxRegistry::getUtils()->isSearchEngine()) {
00115             return;
00116         }
00117 
00118         // adding articles
00119         if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
00120 
00121             $this->_setLastCallFnc('tobasket');
00122             $oBasketItem = $this->_addItems($aProducts);
00123 
00124             // new basket item marker
00125             if ($oBasketItem && $myConfig->getConfigParam('iNewBasketItemMessage') != 0) {
00126                 $oNewItem = new stdClass();
00127                 $oNewItem->sTitle = $oBasketItem->getTitle();
00128                 $oNewItem->sId = $oBasketItem->getProductId();
00129                 $oNewItem->dAmount = $oBasketItem->getAmount();
00130                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00131 
00132                 // passing article
00133                 oxRegistry::getSession()->setVariable('_newitem', $oNewItem);
00134             }
00135 
00136 
00137             // redirect to basket
00138             return $this->_getRedirectUrl();
00139         }
00140     }
00141 
00153     public function changebasket(
00154         $sProductId = null,
00155         $dAmount = null,
00156         $aSel = null,
00157         $aPersParam = null,
00158         $blOverride = true
00159     ) {
00160         // adding to basket is not allowed ?
00161         if (oxRegistry::getUtils()->isSearchEngine()) {
00162             return;
00163         }
00164 
00165         // fetching item ID
00166         if (!$sProductId) {
00167             $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
00168 
00169             if ($sBasketItemId) {
00170                 $oBasket = $this->getSession()->getBasket();
00171                 //take params
00172                 $aBasketContents = $oBasket->getContents();
00173                 $oItem = $aBasketContents[$sBasketItemId];
00174 
00175                 $sProductId = isset($oItem) ? $oItem->getProductId() : null;
00176             } else {
00177                 $sProductId = oxRegistry::getConfig()->getRequestParameter('aid');
00178             }
00179         }
00180 
00181         // fetching other needed info
00182         $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
00183         $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
00184         $aPersParam = $aPersParam ? $aPersParam : oxRegistry::getConfig()->getRequestParameter('persparam');
00185 
00186         // adding articles
00187         if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
00188 
00189             // information that last call was changebasket
00190             $oBasket = $this->getSession()->getBasket();
00191             $oBasket->onUpdate();
00192 
00193             $this->_setLastCallFnc('changebasket');
00194             $oBasketItem = $this->_addItems($aProducts);
00195         }
00196 
00197     }
00198 
00205     protected function _getRedirectUrl()
00206     {
00207 
00208         // active class
00209         $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
00210         $sClass = $sClass ? $sClass . '?' : 'start?';
00211         $sPosition = '';
00212 
00213         // setting redirect parameters
00214         foreach ($this->aRedirectParams as $sParamName) {
00215             $sParamVal = oxRegistry::getConfig()->getRequestParameter($sParamName);
00216             $sPosition .= $sParamVal ? $sParamName . '=' . $sParamVal . '&' : '';
00217         }
00218 
00219         // special treatment
00220         // search param
00221         $sParam = rawurlencode(oxRegistry::getConfig()->getRequestParameter('searchparam', true));
00222         $sPosition .= $sParam ? 'searchparam=' . $sParam . '&' : '';
00223 
00224         // current page number
00225         $iPageNr = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
00226         $sPosition .= ($iPageNr > 0) ? 'pgNr=' . $iPageNr . '&' : '';
00227 
00228         // reload and backbutton blocker
00229         if ($this->getConfig()->getConfigParam('iNewBasketItemMessage') == 3) {
00230 
00231             // saving return to shop link to session
00232             oxRegistry::getSession()->setVariable('_backtoshop', $sClass . $sPosition);
00233 
00234             // redirecting to basket
00235             $sClass = 'basket?';
00236         }
00237 
00238         return $sClass . $sPosition;
00239     }
00240 
00253     protected function _getItems(
00254         $sProductId = null,
00255         $dAmount = null,
00256         $aSel = null,
00257         $aPersParam = null,
00258         $blOverride = false
00259     ) {
00260         // collecting items to add
00261         $aProducts = oxRegistry::getConfig()->getRequestParameter('aproducts');
00262 
00263         // collecting specified item
00264         $sProductId = $sProductId ? $sProductId : oxRegistry::getConfig()->getRequestParameter('aid');
00265         if ($sProductId) {
00266 
00267             // additionally fething current product info
00268             $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
00269 
00270             // select lists
00271             $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
00272 
00273             // persistent parameters
00274             if (empty($aPersParam)) {
00275                 $aPersParam = oxRegistry::getConfig()->getRequestParameter('persparam');
00276                 if (!is_array($aPersParam) || empty($aPersParam['details'])) {
00277                     $aPersParam = null;
00278                 }
00279             }
00280 
00281             $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
00282 
00283             $aProducts[$sProductId] = array('am'           => $dAmount,
00284                                             'sel'          => $aSel,
00285                                             'persparam'    => $aPersParam,
00286                                             'override'     => $blOverride,
00287                                             'basketitemid' => $sBasketItemId
00288             );
00289         }
00290 
00291         if (is_array($aProducts) && count($aProducts)) {
00292 
00293             if (oxRegistry::getConfig()->getRequestParameter('removeBtn') !== null) {
00294                 //setting amount to 0 if removing article from basket
00295                 foreach ($aProducts as $sProductId => $aProduct) {
00296                     if (isset($aProduct['remove']) && $aProduct['remove']) {
00297                         $aProducts[$sProductId]['am'] = 0;
00298                     } else {
00299                         unset ($aProducts[$sProductId]);
00300                     }
00301                 }
00302             }
00303 
00304             return $aProducts;
00305         }
00306 
00307         return false;
00308     }
00309 
00318     protected function _addItems($aProducts)
00319     {
00320         $oActView = $this->getConfig()->getActiveView();
00321         $sErrorDest = $oActView->getErrorDestination();
00322 
00323         $oBasket = $this->getSession()->getBasket();
00324         $oBasketInfo = $oBasket->getBasketSummary();
00325 
00326         foreach ($aProducts as $sAddProductId => $aProductInfo) {
00327 
00328             $sProductId = isset($aProductInfo['aid']) ? $aProductInfo['aid'] : $sAddProductId;
00329 
00330             // collecting input
00331             $oProduct = $oBasketInfo->aArticles[$sProductId];
00332             $aProducts[$sAddProductId]['oldam'] = isset($oProduct) ? $oProduct : 0;
00333 
00334             $dAmount = isset($aProductInfo['am']) ? $aProductInfo['am'] : 0;
00335             $aSelList = isset($aProductInfo['sel']) ? $aProductInfo['sel'] : null;
00336             $aParams = $aProductInfo['persparam'];
00337             $aPersParam = (isset($aParams) && is_array($aParams) && strlen($aParams['details'])) ? $aParams : null;
00338             $blOverride = isset($aProductInfo['override']) ? $aProductInfo['override'] : null;
00339             $blIsBundle = isset($aProductInfo['bundle']) ? true : false;
00340             $sOldBasketItemId = isset($aProductInfo['basketitemid']) ? $aProductInfo['basketitemid'] : null;
00341 
00342             try {
00343                 $oBasketItem = $oBasket->addToBasket(
00344                     $sProductId,
00345                     $dAmount,
00346                     $aSelList,
00347                     $aPersParam,
00348                     $blOverride,
00349                     $blIsBundle,
00350                     $sOldBasketItemId
00351                 );
00352             } catch (oxOutOfStockException $oEx) {
00353                 $oEx->setDestination($sErrorDest);
00354                 // #950 Change error destination to basket popup
00355                 if (!$sErrorDest && $this->getConfig()->getConfigParam('iNewBasketItemMessage') == 2) {
00356                     $sErrorDest = 'popup';
00357                 }
00358                 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
00359             } catch (oxArticleInputException $oEx) {
00360                 //add to display at specific position
00361                 $oEx->setDestination($sErrorDest);
00362                 oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
00363             } catch (oxNoArticleException $oEx) {
00364                 //ignored, best solution F ?
00365             }
00366             if (!$oBasketItem) {
00367                 $oInfo = $oBasket->getBasketSummary();
00368                 $oProduct = $oInfo->aArticles[$sProductId];
00369                 $aProducts[$sAddProductId]['am'] = isset($oProduct) ? $oProduct : 0;
00370             }
00371         }
00372 
00373         //if basket empty remove posible gift card
00374         if ($oBasket->getProductsCount() == 0) {
00375             $oBasket->setCardId(null);
00376         }
00377 
00378         // information that last call was tobasket
00379         $this->_setLastCall($this->_getLastCallFnc(), $aProducts, $oBasketInfo);
00380 
00381         return $oBasketItem;
00382     }
00383 
00391     protected function _setLastCall($sCallName, $aProductInfo, $aBasketInfo)
00392     {
00393         oxRegistry::getSession()->setVariable('aLastcall', array($sCallName => $aProductInfo));
00394     }
00395 
00401     protected function _setLastCallFnc($sCallName)
00402     {
00403         $this->_sLastCallFnc = $sCallName;
00404     }
00405 
00411     protected function _getLastCallFnc()
00412     {
00413         return $this->_sLastCallFnc;
00414     }
00415 
00421     public function isRootCatChanged()
00422     {
00423         // in Basket
00424         $oBasket = $this->getSession()->getBasket();
00425         if ($oBasket->showCatChangeWarning()) {
00426             $oBasket->setCatChangeWarningState(false);
00427 
00428             return true;
00429         }
00430 
00431         // in Category, only then category is empty ant not equal to default category
00432         $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
00433         $sActCat = oxRegistry::getConfig()->getRequestParameter('cnid');
00434         $oActCat = oxnew('oxcategory');
00435         if ($sActCat && $sActCat != $sDefCat && $oActCat->load($sActCat)) {
00436             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00437             if ($oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId()) {
00438                 return true;
00439             }
00440         }
00441 
00442         return false;
00443     }
00444 
00453     public function executeuserchoice()
00454     {
00455 
00456         // redirect to basket
00457         if (oxRegistry::getConfig()->getRequestParameter("tobasket")) {
00458             return "basket";
00459         } else {
00460             // clear basket
00461             $this->getSession()->getBasket()->deleteBasket();
00462             $this->getParent()->setRootCatChanged(false);
00463         }
00464     }
00465 
00466 }