oxcmp_basket.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxcmp_basket extends oxView
00009 {
00010 
00015     protected $_blIsComponent = true;
00016 
00022     public $aRedirectParams = array( 'cnid',        // category id
00023                                      'mnid',        // manufacturer id
00024                                      'anid',        // active article id
00025                                      'tpl',         // spec. template
00026                                      'listtype',    // list type
00027                                      'searchcnid',  // search category
00028                                      'searchvendor',// search vendor
00029                                      'searchmanufacturer',// search manufacturer
00030                                      'searchtag',   // search tag
00031                                      'searchrecomm',// search recomendation
00032                                      'recommid'     // recomm. list id
00033                                     );
00034 
00040     public function init()
00041     {
00042         $oConfig = $this->getConfig();
00043         if ($oConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00044             if ($oReservations = $this->getSession()->getBasketReservations()) {
00045                 if (!$oReservations->getTimeLeft()) {
00046                     if ( $oBasket = $this->getSession()->getBasket() ) {
00047                         $oBasket->deleteBasket();
00048                     }
00049                 }
00050                 $iLimit = (int) $oConfig->getConfigParam( 'iBasketReservationCleanPerRequest' );
00051                 if (!$iLimit) {
00052                     $iLimit = 200;
00053                 }
00054                 $oReservations->discardUnusedReservations($iLimit);
00055             }
00056         }
00057         return parent::init();
00058     }
00059 
00066     public function render()
00067     {
00068         // recalculating
00069         if ( $oBasket = $this->getSession()->getBasket() ) {
00070             $oBasket->calculateBasket( false );
00071         }
00072 
00073         // Basket exclude
00074         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00075             $this->getParent()->addTplParam( 'scRootCatChanged', $this->isRootCatChanged() );
00076         }
00077 
00078         parent::render();
00079 
00080         return $oBasket;
00081     }
00082 
00098     public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00099     {
00100         // adding to basket is not allowed ?
00101         $myConfig = $this->getConfig();
00102         if ( oxUtils::getInstance()->isSearchEngine() ) {
00103             return;
00104         }
00105 
00106         // adding articles
00107         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00108 
00109             $this->_setLastCall( 'tobasket', $aProducts, $this->getSession()->getBasket()->getBasketSummary() );
00110             $oBasketItem = $this->_addItems( $aProducts );
00111 
00112             // new basket item marker
00113             if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
00114                 $oNewItem = new OxstdClass();
00115                 $oNewItem->sTitle  = $oBasketItem->getTitle();
00116                 $oNewItem->sId     = $oBasketItem->getProductId();
00117                 $oNewItem->dAmount = $oBasketItem->getAmount();
00118                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00119 
00120                 // passing article
00121                 oxSession::setVar( '_newitem', $oNewItem );
00122             }
00123         }
00124 
00125         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00126             // redirect to basket
00127             return $this->_getRedirectUrl();
00128         }
00129     }
00130 
00142     public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00143     {
00144         // adding to basket is not allowed ?
00145         if ( oxUtils::getInstance()->isSearchEngine() ) {
00146             return;
00147         }
00148 
00149         // fetching item ID
00150         if (!$sProductId) {
00151             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00152 
00153             if ( $sBasketItemId ) {
00154                 $oBasket = $this->getSession()->getBasket();
00155                 //take params
00156                 $aBasketContents = $oBasket->getContents();
00157                 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00158             } else {
00159                 $sProductId = oxConfig::getParameter( 'aid' );
00160             }
00161         }
00162 
00163         // fetching other needed info
00164         $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
00165         $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00166         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00167 
00168         // adding articles
00169         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00170 
00171             // information that last call was changebasket
00172             $oBasket = $this->getSession()->getBasket();
00173             $oBasket->onUpdate();
00174             $this->_setLastCall( 'changebasket', $aProducts, $oBasket->getBasketSummary() );
00175 
00176             $oBasketItem = $this->_addItems( $aProducts );
00177         }
00178 
00179     }
00180 
00195     public function wl_tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00196     {
00197         // adding to basket is not allowed ?
00198         if ( oxUtils::getInstance()->isSearchEngine() ) {
00199             return;
00200         }
00201 
00202         // collecting info
00203         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00204         $dAmount    = $dAmount?$dAmount:oxConfig::getParameter( 'am' );
00205         $aSel       = $aSel?$aSel:oxConfig::getParameter( 'sel' );
00206         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00207 
00208         // adding articles
00209         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00210 
00211             $oBasketItem = $this->_addItems( $aProducts );
00212             $oBasketItem->setWishArticleId( oxConfig::getParameter( 'anid' ) );
00213 
00214             // information that last call was tobasket
00215             $this->_setLastCall( 'tobasket', $aProducts, $this->getSession()->getBasket()->getBasketSummary() );
00216 
00217             // fetching user info
00218             $oUser = $this->getUser();
00219             $oWishUser = oxNew( 'oxuser' );
00220             $sUserId   = oxConfig::getParameter( 'owishid' )?oxConfig::getParameter( 'owishid' ):oxConfig::getParameter( 'wishid' );
00221             if ( $oWishUser->load( $sUserId ) && $oUser ) {
00222                 // checking if this user isn't assigned to delivery address list and if not - adding him this address
00223                 $oUser->addUserAddress( $oWishUser );
00224                 $oBasketItem->setWishId( $sUserId );
00225             }
00226         }
00227 
00228         return $this->_getRedirectUrl();
00229     }
00230 
00237     protected function _getRedirectUrl()
00238     {
00239 
00240         // active class
00241         $sClass = oxConfig::getParameter( 'cl' );
00242         $sClass = $sClass?$sClass.'?':'start?';
00243         $sPosition = '';
00244 
00245         // setting redirect parameters
00246         foreach ( $this->aRedirectParams as $sParamName ) {
00247             $sParamVal  = oxConfig::getParameter( $sParamName );
00248             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00249         }
00250 
00251         // special treatment
00252         // search param
00253         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00254         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00255 
00256         // current page number
00257         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00258         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00259 
00260         // reload and backbutton blocker
00261         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00262 
00263             // saving return to shop link to session
00264             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00265 
00266             // redirecting to basket
00267             $sClass = 'basket?';
00268         }
00269 
00270         return $sClass.$sPosition;
00271     }
00272 
00285     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00286     {
00287         // collecting items to add
00288         $aProducts = oxConfig::getParameter( 'aproducts' );
00289 
00290         // collecting specified item
00291         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00292         if ( $sProductId ) {
00293 
00294             // additionally fething current product info
00295             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00296 
00297             // select lists
00298             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00299 
00300             // persistent parameters
00301             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00302 
00303             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00304 
00305             $aProducts[$sProductId] = array( 'am' => $dAmount,
00306                                              'sel' => $aSel,
00307                                              'persparam' => $aPersParam,
00308                                              'override'  => $blOverride,
00309                                              'basketitemid' => $sBasketItemId
00310                                            );
00311         }
00312 
00313         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00314 
00315             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00316                 //setting amount to 0 if removing article from basket
00317                 foreach ( $aProducts as $sProductId => $aProduct ) {
00318                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00319                         $aProducts[$sProductId]['am'] = 0;
00320                     } else {
00321                         unset ($aProducts[$sProductId]);
00322                     }
00323                 }
00324             }
00325 
00326             return $aProducts;
00327         }
00328 
00329         return false;
00330     }
00331 
00340     protected function _addItems ( $aProducts )
00341     {
00342         $oActView   = $this->getConfig()->getActiveView();
00343         $sErrorDest = $oActView->getErrorDestination();
00344 
00345         $oBasket = $this->getSession()->getBasket();
00346         foreach ( $aProducts as $sProductId => $aProductInfo ) {
00347 
00348             // collecting input
00349 
00350             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sProductId;
00351 
00352             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00353             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00354             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00355             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00356             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00357             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00358 
00359             try {
00360                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00361             } catch ( oxOutOfStockException $oEx ) {
00362                 $oEx->setDestination( $sErrorDest );
00363                 // #950 Change error destination to basket popup
00364                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00365                     $sErrorDest = 'popup';
00366                 }
00367                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00368             } catch ( oxArticleInputException $oEx ) {
00369                 //add to display at specific position
00370                 $oEx->setDestination( $sErrorDest );
00371                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00372             } catch ( oxNoArticleException $oEx ) {
00373                 //ignored, best solution F ?
00374             }
00375         }
00376 
00377         return $oBasketItem;
00378     }
00379 
00389     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00390     {
00391         $aProducts = array();
00392 
00393         // collecting amounts info
00394         foreach ( $aProductInfo as $sProdId => $aProdData ) {
00395             $aProducts[$sProdId] = $aProdData;
00396             // setting previous amount
00397             $aProducts[$sProdId]['oldam'] = isset( $aBasketInfo->aArticles[$aProdData['aid']] ) ? $aBasketInfo->aArticles[$aProdData['aid']] : 0;
00398         }
00399 
00400         oxSession::setVar( 'aLastcall', array( $sCallName => $aProducts ) );
00401     }
00402 
00408     public function isRootCatChanged()
00409     {
00410         // in Basket
00411         $oBasket = $this->getSession()->getBasket();
00412         if ( $oBasket->showCatChangeWarning() ) {
00413             $oBasket->setCatChangeWarningState( false );
00414             return true;
00415         }
00416 
00417         // in Category, only then category is empty ant not equal to default category
00418         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00419         $sActCat = oxConfig::getParameter( 'cnid' );
00420         $oActCat = oxnew('oxcategory');
00421         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00422             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00423             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00424                 return true;
00425             }
00426         }
00427 
00428         return false;
00429     }
00430 
00439     public function executeuserchoice()
00440     {
00441         // redirect to basket
00442         if ( oxConfig::getParameter( "tobasket" ) ) {
00443             return "basket";
00444         } else {
00445             // clear basket
00446             $this->getSession()->getBasket()->deleteBasket();
00447         }
00448     }
00449 }