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                     $oBasket = $this->getSession()->getBasket();
00053                     if ( $oBasket && $oBasket->getProductsCount() ) {
00054                         $oBasket->deleteBasket();
00055                     }
00056                 }
00057                 $iLimit = (int) $oConfig->getConfigParam( 'iBasketReservationCleanPerRequest' );
00058                 if (!$iLimit) {
00059                     $iLimit = 200;
00060                 }
00061                 $oReservations->discardUnusedReservations($iLimit);
00062             }
00063         }
00064 
00065         parent::init();
00066 
00067         // Basket exclude
00068         if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
00069             if ( $oBasket = $this->getSession()->getBasket() ) {
00070                 $this->getParent()->setRootCatChanged( $this->isRootCatChanged() && $oBasket->getContents() );
00071             }
00072         }
00073     }
00074 
00081     public function render()
00082     {
00083         // recalculating
00084         if ( $oBasket = $this->getSession()->getBasket() ) {
00085             $oBasket->calculateBasket( false );
00086         }
00087 
00088         parent::render();
00089 
00090         return $oBasket;
00091     }
00092 
00108     public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00109     {
00110         // adding to basket is not allowed ?
00111         $myConfig = $this->getConfig();
00112         if ( oxRegistry::getUtils()->isSearchEngine() ) {
00113             return;
00114         }
00115 
00116         // adding articles
00117         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00118 
00119             $this->_setLastCallFnc( 'tobasket' );
00120             $oBasketItem = $this->_addItems( $aProducts );
00121 
00122             // new basket item marker
00123             if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
00124                 $oNewItem = new stdClass();
00125                 $oNewItem->sTitle  = $oBasketItem->getTitle();
00126                 $oNewItem->sId     = $oBasketItem->getProductId();
00127                 $oNewItem->dAmount = $oBasketItem->getAmount();
00128                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00129 
00130                 // passing article
00131                 oxSession::setVar( '_newitem', $oNewItem );
00132             }
00133 
00134 
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 ( oxRegistry::getUtils()->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 
00196     protected function _getRedirectUrl()
00197     {
00198 
00199         // active class
00200         $sClass = oxConfig::getParameter( 'cl' );
00201         $sClass = $sClass?$sClass.'?':'start?';
00202         $sPosition = '';
00203 
00204         // setting redirect parameters
00205         foreach ( $this->aRedirectParams as $sParamName ) {
00206             $sParamVal  = oxConfig::getParameter( $sParamName );
00207             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00208         }
00209 
00210         // special treatment
00211         // search param
00212         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00213         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00214 
00215         // current page number
00216         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00217         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00218 
00219         // reload and backbutton blocker
00220         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00221 
00222             // saving return to shop link to session
00223             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00224 
00225             // redirecting to basket
00226             $sClass = 'basket?';
00227         }
00228 
00229         return $sClass.$sPosition;
00230     }
00231 
00244     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00245     {
00246         // collecting items to add
00247         $aProducts = oxConfig::getParameter( 'aproducts' );
00248 
00249         // collecting specified item
00250         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00251         if ( $sProductId ) {
00252 
00253             // additionally fething current product info
00254             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00255 
00256             // select lists
00257             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00258 
00259             // persistent parameters
00260             if ( empty($aPersParam) ) {
00261                 $aPersParam = oxConfig::getParameter( 'persparam' );
00262                 if ( !is_array($aPersParam) || empty($aPersParam['details']) ) {
00263                     $aPersParam = null;
00264                 }
00265             }
00266 
00267             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00268 
00269             $aProducts[$sProductId] = array( 'am' => $dAmount,
00270                                              'sel' => $aSel,
00271                                              'persparam' => $aPersParam,
00272                                              'override'  => $blOverride,
00273                                              'basketitemid' => $sBasketItemId
00274                                            );
00275         }
00276 
00277         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00278 
00279             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00280                 //setting amount to 0 if removing article from basket
00281                 foreach ( $aProducts as $sProductId => $aProduct ) {
00282                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00283                         $aProducts[$sProductId]['am'] = 0;
00284                     } else {
00285                         unset ($aProducts[$sProductId]);
00286                     }
00287                 }
00288             }
00289 
00290             return $aProducts;
00291         }
00292 
00293         return false;
00294     }
00295 
00304     protected function _addItems ( $aProducts )
00305     {
00306         $oActView   = $this->getConfig()->getActiveView();
00307         $sErrorDest = $oActView->getErrorDestination();
00308 
00309         $oBasket = $this->getSession()->getBasket();
00310         $oBasketInfo = $oBasket->getBasketSummary();
00311 
00312         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00313 
00314             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00315 
00316             // collecting input
00317             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00318 
00319             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00320             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00321             $aPersParam = ( isset( $aProductInfo['persparam'] ) && is_array( $aProductInfo['persparam'] ) && strlen( $aProductInfo['persparam']['details'] ) )?$aProductInfo['persparam']:null;
00322             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00323             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00324             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00325 
00326             try {
00327                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00328             } catch ( oxOutOfStockException $oEx ) {
00329                 $oEx->setDestination( $sErrorDest );
00330                 // #950 Change error destination to basket popup
00331                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00332                     $sErrorDest = 'popup';
00333                 }
00334                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00335             } catch ( oxArticleInputException $oEx ) {
00336                 //add to display at specific position
00337                 $oEx->setDestination( $sErrorDest );
00338                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00339             } catch ( oxNoArticleException $oEx ) {
00340                 //ignored, best solution F ?
00341             }
00342             if ( !$oBasketItem ) {
00343                 $oInfo = $oBasket->getBasketSummary();
00344                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00345             }
00346         }
00347 
00348         //if basket empty remove posible gift card
00349         if ( $oBasket->getProductsCount() == 0 ) {
00350             $oBasket->setCardId( null );
00351         }
00352 
00353         // information that last call was tobasket
00354         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00355 
00356         return $oBasketItem;
00357     }
00358 
00368     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00369     {
00370         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00371     }
00372 
00380     protected function _setLastCallFnc( $sCallName )
00381     {
00382         $this->_sLastCallFnc = $sCallName;
00383     }
00384 
00390     protected function _getLastCallFnc()
00391     {
00392         return $this->_sLastCallFnc;
00393     }
00394 
00400     public function isRootCatChanged()
00401     {
00402         // in Basket
00403         $oBasket = $this->getSession()->getBasket();
00404         if ( $oBasket->showCatChangeWarning() ) {
00405             $oBasket->setCatChangeWarningState( false );
00406             return true;
00407         }
00408 
00409         // in Category, only then category is empty ant not equal to default category
00410         $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
00411         $sActCat = oxConfig::getParameter( 'cnid' );
00412         $oActCat = oxnew('oxcategory');
00413         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00414             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00415             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00416                 return true;
00417             }
00418         }
00419 
00420         return false;
00421     }
00422 
00431     public function executeuserchoice()
00432     {
00433 
00434         // redirect to basket
00435         if ( oxConfig::getParameter( "tobasket" ) ) {
00436             return "basket";
00437         } else {
00438             // clear basket
00439             $this->getSession()->getBasket()->deleteBasket();
00440             $this->getParent()->setRootCatChanged( false );
00441         }
00442     }
00443 
00444 }