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 
00021     protected $_sLastCallFnc = null;
00022 
00028     public $aRedirectParams = array( 'cnid',        // category id
00029                                      'mnid',        // manufacturer id
00030                                      'anid',        // active article id
00031                                      'tpl',         // spec. template
00032                                      'listtype',    // list type
00033                                      'searchcnid',  // search category
00034                                      'searchvendor',// search vendor
00035                                      'searchmanufacturer',// search manufacturer
00036                                      'searchtag',   // search tag
00037                                      'searchrecomm',// search recomendation
00038                                      'recommid'     // recomm. list id
00039                                     );
00040 
00046     public function init()
00047     {
00048         $oConfig = $this->getConfig();
00049         if ($oConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
00050             if ($oReservations = $this->getSession()->getBasketReservations()) {
00051                 if (!$oReservations->getTimeLeft()) {
00052                     if ( $oBasket = $this->getSession()->getBasket() ) {
00053                         $oBasket->deleteBasket();
00054                     }
00055                 }
00056                 $iLimit = (int) $oConfig->getConfigParam( 'iBasketReservationCleanPerRequest' );
00057                 if (!$iLimit) {
00058                     $iLimit = 200;
00059                 }
00060                 $oReservations->discardUnusedReservations($iLimit);
00061             }
00062         }
00063         return parent::init();
00064     }
00065 
00072     public function render()
00073     {
00074         // recalculating
00075         if ( $oBasket = $this->getSession()->getBasket() ) {
00076             $oBasket->calculateBasket( false );
00077         }
00078 
00079         // Basket exclude
00080         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00081             $this->getParent()->addTplParam( 'scRootCatChanged', $this->isRootCatChanged() );
00082         }
00083 
00084         parent::render();
00085 
00086         return $oBasket;
00087     }
00088 
00104     public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00105     {
00106         // adding to basket is not allowed ?
00107         $myConfig = $this->getConfig();
00108         if ( oxUtils::getInstance()->isSearchEngine() ) {
00109             return;
00110         }
00111 
00112         // adding articles
00113         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00114 
00115             $this->_setLastCallFnc( 'tobasket' );
00116             $oBasketItem = $this->_addItems( $aProducts );
00117 
00118             // new basket item marker
00119             if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
00120                 $oNewItem = new OxstdClass();
00121                 $oNewItem->sTitle  = $oBasketItem->getTitle();
00122                 $oNewItem->sId     = $oBasketItem->getProductId();
00123                 $oNewItem->dAmount = $oBasketItem->getAmount();
00124                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00125 
00126                 // passing article
00127                 oxSession::setVar( '_newitem', $oNewItem );
00128             }
00129         }
00130 
00131         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00132             // redirect to basket
00133             return $this->_getRedirectUrl();
00134         }
00135     }
00136 
00148     public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00149     {
00150         // adding to basket is not allowed ?
00151         if ( oxUtils::getInstance()->isSearchEngine() ) {
00152             return;
00153         }
00154 
00155         // fetching item ID
00156         if (!$sProductId) {
00157             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00158 
00159             if ( $sBasketItemId ) {
00160                 $oBasket = $this->getSession()->getBasket();
00161                 //take params
00162                 $aBasketContents = $oBasket->getContents();
00163                 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00164             } else {
00165                 $sProductId = oxConfig::getParameter( 'aid' );
00166             }
00167         }
00168 
00169         // fetching other needed info
00170         $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
00171         $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00172         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00173 
00174         // adding articles
00175         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00176 
00177             // information that last call was changebasket
00178             $oBasket = $this->getSession()->getBasket();
00179             $oBasket->onUpdate();
00180 
00181             $this->_setLastCallFnc( 'changebasket' );
00182             $oBasketItem = $this->_addItems( $aProducts );
00183         }
00184 
00185     }
00186 
00201     public function wl_tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00202     {
00203         // adding to basket is not allowed ?
00204         if ( oxUtils::getInstance()->isSearchEngine() ) {
00205             return;
00206         }
00207 
00208         // collecting info
00209         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00210         $dAmount    = $dAmount?$dAmount:oxConfig::getParameter( 'am' );
00211         $aSel       = $aSel?$aSel:oxConfig::getParameter( 'sel' );
00212         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00213 
00214         // adding articles
00215         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00216 
00217             $this->_setLastCallFnc( 'tobasket' );
00218             $oBasketItem = $this->_addItems( $aProducts );
00219             $oBasketItem->setWishArticleId( oxConfig::getParameter( 'anid' ) );
00220 
00221             // fetching user info
00222             $oUser = $this->getUser();
00223             $oWishUser = oxNew( 'oxuser' );
00224             $sUserId   = oxConfig::getParameter( 'owishid' )?oxConfig::getParameter( 'owishid' ):oxConfig::getParameter( 'wishid' );
00225             if ( $oWishUser->load( $sUserId ) && $oUser ) {
00226                 // checking if this user isn't assigned to delivery address list and if not - adding him this address
00227                 $oUser->addUserAddress( $oWishUser );
00228                 $oBasketItem->setWishId( $sUserId );
00229             }
00230         }
00231 
00232         return $this->_getRedirectUrl();
00233     }
00234 
00241     protected function _getRedirectUrl()
00242     {
00243 
00244         // active class
00245         $sClass = oxConfig::getParameter( 'cl' );
00246         $sClass = $sClass?$sClass.'?':'start?';
00247         $sPosition = '';
00248 
00249         // setting redirect parameters
00250         foreach ( $this->aRedirectParams as $sParamName ) {
00251             $sParamVal  = oxConfig::getParameter( $sParamName );
00252             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00253         }
00254 
00255         // special treatment
00256         // search param
00257         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00258         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00259 
00260         // current page number
00261         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00262         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00263 
00264         // reload and backbutton blocker
00265         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00266 
00267             // saving return to shop link to session
00268             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00269 
00270             // redirecting to basket
00271             $sClass = 'basket?';
00272         }
00273 
00274         return $sClass.$sPosition;
00275     }
00276 
00289     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00290     {
00291         // collecting items to add
00292         $aProducts = oxConfig::getParameter( 'aproducts' );
00293 
00294         // collecting specified item
00295         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00296         if ( $sProductId ) {
00297 
00298             // additionally fething current product info
00299             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00300 
00301             // select lists
00302             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00303 
00304             // persistent parameters
00305             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00306 
00307             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00308 
00309             $aProducts[$sProductId] = array( 'am' => $dAmount,
00310                                              'sel' => $aSel,
00311                                              'persparam' => $aPersParam,
00312                                              'override'  => $blOverride,
00313                                              'basketitemid' => $sBasketItemId
00314                                            );
00315         }
00316 
00317         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00318 
00319             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00320                 //setting amount to 0 if removing article from basket
00321                 foreach ( $aProducts as $sProductId => $aProduct ) {
00322                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00323                         $aProducts[$sProductId]['am'] = 0;
00324                     } else {
00325                         unset ($aProducts[$sProductId]);
00326                     }
00327                 }
00328             }
00329 
00330             return $aProducts;
00331         }
00332 
00333         return false;
00334     }
00335 
00344     protected function _addItems ( $aProducts )
00345     {
00346         $oActView   = $this->getConfig()->getActiveView();
00347         $sErrorDest = $oActView->getErrorDestination();
00348 
00349         $oBasket = $this->getSession()->getBasket();
00350         $oBasketInfo = $oBasket->getBasketSummary();
00351 
00352         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00353 
00354             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00355 
00356             // collecting input
00357             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00358 
00359             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00360             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00361             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00362             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00363             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00364             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00365 
00366             try {
00367                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00368             } catch ( oxOutOfStockException $oEx ) {
00369                 $oEx->setDestination( $sErrorDest );
00370                 // #950 Change error destination to basket popup
00371                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00372                     $sErrorDest = 'popup';
00373                 }
00374                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00375             } catch ( oxArticleInputException $oEx ) {
00376                 //add to display at specific position
00377                 $oEx->setDestination( $sErrorDest );
00378                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00379             } catch ( oxNoArticleException $oEx ) {
00380                 //ignored, best solution F ?
00381             }
00382             if ( !$oBasketItem ) {
00383                 $oInfo = $oBasket->getBasketSummary();
00384                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00385             }
00386         }
00387 
00388         // information that last call was tobasket
00389         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00390 
00391         return $oBasketItem;
00392     }
00393 
00403     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00404     {
00405         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00406     }
00407 
00415     protected function _setLastCallFnc( $sCallName )
00416     {
00417         $this->_sLastCallFnc = $sCallName;
00418     }
00419 
00425     protected function _getLastCallFnc()
00426     {
00427         return $this->_sLastCallFnc;
00428     }
00429 
00435     public function isRootCatChanged()
00436     {
00437         // in Basket
00438         $oBasket = $this->getSession()->getBasket();
00439         if ( $oBasket->showCatChangeWarning() ) {
00440             $oBasket->setCatChangeWarningState( false );
00441             return true;
00442         }
00443 
00444         // in Category, only then category is empty ant not equal to default category
00445         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00446         $sActCat = oxConfig::getParameter( 'cnid' );
00447         $oActCat = oxnew('oxcategory');
00448         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00449             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00450             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00451                 return true;
00452             }
00453         }
00454 
00455         return false;
00456     }
00457 
00466     public function executeuserchoice()
00467     {
00468         // redirect to basket
00469         if ( oxConfig::getParameter( "tobasket" ) ) {
00470             return "basket";
00471         } else {
00472             // clear basket
00473             $this->getSession()->getBasket()->deleteBasket();
00474         }
00475     }
00476 }