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 
00064         parent::init();
00065 
00066         // Basket exclude
00067         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00068             if ( $oBasket = $this->getSession()->getBasket() ) {
00069                 $this->getParent()->setRootCatChanged( $this->isRootCatChanged() && $oBasket->getContents() );
00070             }
00071         }
00072     }
00073 
00080     public function render()
00081     {
00082         // recalculating
00083         if ( $oBasket = $this->getSession()->getBasket() ) {
00084             $oBasket->calculateBasket( false );
00085         }
00086 
00087         parent::render();
00088 
00089         return $oBasket;
00090     }
00091 
00107     public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00108     {
00109         // adding to basket is not allowed ?
00110         $myConfig = $this->getConfig();
00111         if ( oxUtils::getInstance()->isSearchEngine() ) {
00112             return;
00113         }
00114 
00115         // adding articles
00116         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00117 
00118             $this->_setLastCallFnc( 'tobasket' );
00119             $oBasketItem = $this->_addItems( $aProducts );
00120 
00121             // new basket item marker
00122             if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
00123                 $oNewItem = new OxstdClass();
00124                 $oNewItem->sTitle  = $oBasketItem->getTitle();
00125                 $oNewItem->sId     = $oBasketItem->getProductId();
00126                 $oNewItem->dAmount = $oBasketItem->getAmount();
00127                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00128 
00129                 // passing article
00130                 oxSession::setVar( '_newitem', $oNewItem );
00131             }
00132         }
00133 
00134         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00135             // redirect to basket
00136             return $this->_getRedirectUrl();
00137         }
00138     }
00139 
00151     public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00152     {
00153         // adding to basket is not allowed ?
00154         if ( oxUtils::getInstance()->isSearchEngine() ) {
00155             return;
00156         }
00157 
00158         // fetching item ID
00159         if (!$sProductId) {
00160             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00161 
00162             if ( $sBasketItemId ) {
00163                 $oBasket = $this->getSession()->getBasket();
00164                 //take params
00165                 $aBasketContents = $oBasket->getContents();
00166                 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00167             } else {
00168                 $sProductId = oxConfig::getParameter( 'aid' );
00169             }
00170         }
00171 
00172         // fetching other needed info
00173         $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
00174         $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00175         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00176 
00177         // adding articles
00178         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00179 
00180             // information that last call was changebasket
00181             $oBasket = $this->getSession()->getBasket();
00182             $oBasket->onUpdate();
00183 
00184             $this->_setLastCallFnc( 'changebasket' );
00185             $oBasketItem = $this->_addItems( $aProducts );
00186         }
00187 
00188     }
00189 
00205     public function wl_tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00206     {
00207         // adding to basket is not allowed ?
00208         if ( oxUtils::getInstance()->isSearchEngine() ) {
00209             return;
00210         }
00211 
00212         // collecting info
00213         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00214         $dAmount    = $dAmount?$dAmount:oxConfig::getParameter( 'am' );
00215         $aSel       = $aSel?$aSel:oxConfig::getParameter( 'sel' );
00216         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00217 
00218         // adding articles
00219         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00220 
00221             $this->_setLastCallFnc( 'tobasket' );
00222             $oBasketItem = $this->_addItems( $aProducts );
00223             $oBasketItem->setWishArticleId( oxConfig::getParameter( 'anid' ) );
00224 
00225             // fetching user info
00226             $oUser = $this->getUser();
00227             $oWishUser = oxNew( 'oxuser' );
00228             $sUserId   = oxConfig::getParameter( 'owishid' )?oxConfig::getParameter( 'owishid' ):oxConfig::getParameter( 'wishid' );
00229             if ( $oWishUser->load( $sUserId ) && $oUser ) {
00230                 // checking if this user isn't assigned to delivery address list and if not - adding him this address
00231                 $oUser->addUserAddress( $oWishUser );
00232                 $oBasketItem->setWishId( $sUserId );
00233             }
00234         }
00235 
00236         return $this->_getRedirectUrl();
00237     }
00238 
00245     protected function _getRedirectUrl()
00246     {
00247 
00248         // active class
00249         $sClass = oxConfig::getParameter( 'cl' );
00250         $sClass = $sClass?$sClass.'?':'start?';
00251         $sPosition = '';
00252 
00253         // setting redirect parameters
00254         foreach ( $this->aRedirectParams as $sParamName ) {
00255             $sParamVal  = oxConfig::getParameter( $sParamName );
00256             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00257         }
00258 
00259         // special treatment
00260         // search param
00261         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00262         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00263 
00264         // current page number
00265         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00266         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00267 
00268         // reload and backbutton blocker
00269         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00270 
00271             // saving return to shop link to session
00272             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00273 
00274             // redirecting to basket
00275             $sClass = 'basket?';
00276         }
00277 
00278         return $sClass.$sPosition;
00279     }
00280 
00293     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00294     {
00295         // collecting items to add
00296         $aProducts = oxConfig::getParameter( 'aproducts' );
00297 
00298         // collecting specified item
00299         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00300         if ( $sProductId ) {
00301 
00302             // additionally fething current product info
00303             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00304 
00305             // select lists
00306             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00307 
00308             // persistent parameters
00309             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00310 
00311             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00312 
00313             $aProducts[$sProductId] = array( 'am' => $dAmount,
00314                                              'sel' => $aSel,
00315                                              'persparam' => $aPersParam,
00316                                              'override'  => $blOverride,
00317                                              'basketitemid' => $sBasketItemId
00318                                            );
00319         }
00320 
00321         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00322 
00323             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00324                 //setting amount to 0 if removing article from basket
00325                 foreach ( $aProducts as $sProductId => $aProduct ) {
00326                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00327                         $aProducts[$sProductId]['am'] = 0;
00328                     } else {
00329                         unset ($aProducts[$sProductId]);
00330                     }
00331                 }
00332             }
00333 
00334             return $aProducts;
00335         }
00336 
00337         return false;
00338     }
00339 
00348     protected function _addItems ( $aProducts )
00349     {
00350         $oActView   = $this->getConfig()->getActiveView();
00351         $sErrorDest = $oActView->getErrorDestination();
00352 
00353         $oBasket = $this->getSession()->getBasket();
00354         $oBasketInfo = $oBasket->getBasketSummary();
00355 
00356         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00357 
00358             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00359 
00360             // collecting input
00361             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00362 
00363             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00364             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00365             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00366             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00367             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00368             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00369 
00370             try {
00371                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00372             } catch ( oxOutOfStockException $oEx ) {
00373                 $oEx->setDestination( $sErrorDest );
00374                 // #950 Change error destination to basket popup
00375                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00376                     $sErrorDest = 'popup';
00377                 }
00378                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00379             } catch ( oxArticleInputException $oEx ) {
00380                 //add to display at specific position
00381                 $oEx->setDestination( $sErrorDest );
00382                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00383             } catch ( oxNoArticleException $oEx ) {
00384                 //ignored, best solution F ?
00385             }
00386             if ( !$oBasketItem ) {
00387                 $oInfo = $oBasket->getBasketSummary();
00388                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00389             }
00390         }
00391 
00392         // information that last call was tobasket
00393         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00394 
00395         return $oBasketItem;
00396     }
00397 
00407     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00408     {
00409         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00410     }
00411 
00419     protected function _setLastCallFnc( $sCallName )
00420     {
00421         $this->_sLastCallFnc = $sCallName;
00422     }
00423 
00429     protected function _getLastCallFnc()
00430     {
00431         return $this->_sLastCallFnc;
00432     }
00433 
00439     public function isRootCatChanged()
00440     {
00441         // in Basket
00442         $oBasket = $this->getSession()->getBasket();
00443         if ( $oBasket->showCatChangeWarning() ) {
00444             $oBasket->setCatChangeWarningState( false );
00445             return true;
00446         }
00447 
00448         // in Category, only then category is empty ant not equal to default category
00449         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00450         $sActCat = oxConfig::getParameter( 'cnid' );
00451         $oActCat = oxnew('oxcategory');
00452         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00453             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00454             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00455                 return true;
00456             }
00457         }
00458 
00459         return false;
00460     }
00461 
00470     public function executeuserchoice()
00471     {
00472         // redirect to basket
00473         if ( oxConfig::getParameter( "tobasket" ) ) {
00474             return "basket";
00475         } else {
00476             // clear basket
00477             $this->getSession()->getBasket()->deleteBasket();
00478             $this->getParent()->setRootCatChanged( false );
00479         }
00480     }
00481 }