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             // redirect to basket
00135             return $this->_getRedirectUrl();
00136         }
00137     }
00138 
00150     public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00151     {
00152         // adding to basket is not allowed ?
00153         if ( oxRegistry::getUtils()->isSearchEngine() ) {
00154             return;
00155         }
00156 
00157         // fetching item ID
00158         if (!$sProductId) {
00159             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00160 
00161             if ( $sBasketItemId ) {
00162                 $oBasket = $this->getSession()->getBasket();
00163                 //take params
00164                 $aBasketContents = $oBasket->getContents();
00165                 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00166             } else {
00167                 $sProductId = oxConfig::getParameter( 'aid' );
00168             }
00169         }
00170 
00171         // fetching other needed info
00172         $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
00173         $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00174         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00175 
00176         // adding articles
00177         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00178 
00179             // information that last call was changebasket
00180             $oBasket = $this->getSession()->getBasket();
00181             $oBasket->onUpdate();
00182 
00183             $this->_setLastCallFnc( 'changebasket' );
00184             $oBasketItem = $this->_addItems( $aProducts );
00185         }
00186 
00187     }
00188 
00195     protected function _getRedirectUrl()
00196     {
00197 
00198         // active class
00199         $sClass = oxConfig::getParameter( 'cl' );
00200         $sClass = $sClass?$sClass.'?':'start?';
00201         $sPosition = '';
00202 
00203         // setting redirect parameters
00204         foreach ( $this->aRedirectParams as $sParamName ) {
00205             $sParamVal  = oxConfig::getParameter( $sParamName );
00206             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00207         }
00208 
00209         // special treatment
00210         // search param
00211         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00212         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00213 
00214         // current page number
00215         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00216         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00217 
00218         // reload and backbutton blocker
00219         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00220 
00221             // saving return to shop link to session
00222             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00223 
00224             // redirecting to basket
00225             $sClass = 'basket?';
00226         }
00227 
00228         return $sClass.$sPosition;
00229     }
00230 
00243     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00244     {
00245         // collecting items to add
00246         $aProducts = oxConfig::getParameter( 'aproducts' );
00247 
00248         // collecting specified item
00249         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00250         if ( $sProductId ) {
00251 
00252             // additionally fething current product info
00253             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00254 
00255             // select lists
00256             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00257 
00258             // persistent parameters
00259             if ( empty($aPersParam) ) {
00260                 $aPersParam = oxConfig::getParameter( 'persparam' );
00261                 if ( !is_array($aPersParam) || empty($aPersParam['details']) ) {
00262                     $aPersParam = null;
00263                 }
00264             }
00265 
00266             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00267 
00268             $aProducts[$sProductId] = array( 'am' => $dAmount,
00269                                              'sel' => $aSel,
00270                                              'persparam' => $aPersParam,
00271                                              'override'  => $blOverride,
00272                                              'basketitemid' => $sBasketItemId
00273                                            );
00274         }
00275 
00276         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00277 
00278             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00279                 //setting amount to 0 if removing article from basket
00280                 foreach ( $aProducts as $sProductId => $aProduct ) {
00281                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00282                         $aProducts[$sProductId]['am'] = 0;
00283                     } else {
00284                         unset ($aProducts[$sProductId]);
00285                     }
00286                 }
00287             }
00288 
00289             return $aProducts;
00290         }
00291 
00292         return false;
00293     }
00294 
00303     protected function _addItems ( $aProducts )
00304     {
00305         $oActView   = $this->getConfig()->getActiveView();
00306         $sErrorDest = $oActView->getErrorDestination();
00307 
00308         $oBasket = $this->getSession()->getBasket();
00309         $oBasketInfo = $oBasket->getBasketSummary();
00310 
00311         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00312 
00313             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00314 
00315             // collecting input
00316             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00317 
00318             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00319             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00320             $aPersParam = ( isset( $aProductInfo['persparam'] ) && is_array( $aProductInfo['persparam'] ) && strlen( $aProductInfo['persparam']['details'] ) )?$aProductInfo['persparam']:null;
00321             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00322             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00323             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00324 
00325             try {
00326                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00327             } catch ( oxOutOfStockException $oEx ) {
00328                 $oEx->setDestination( $sErrorDest );
00329                 // #950 Change error destination to basket popup
00330                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00331                     $sErrorDest = 'popup';
00332                 }
00333                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00334             } catch ( oxArticleInputException $oEx ) {
00335                 //add to display at specific position
00336                 $oEx->setDestination( $sErrorDest );
00337                 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00338             } catch ( oxNoArticleException $oEx ) {
00339                 //ignored, best solution F ?
00340             }
00341             if ( !$oBasketItem ) {
00342                 $oInfo = $oBasket->getBasketSummary();
00343                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00344             }
00345         }
00346 
00347         //if basket empty remove posible gift card
00348         if ( $oBasket->getProductsCount() == 0 ) {
00349             $oBasket->setCardId( null );
00350         }
00351 
00352         // information that last call was tobasket
00353         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00354 
00355         return $oBasketItem;
00356     }
00357 
00367     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00368     {
00369         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00370     }
00371 
00379     protected function _setLastCallFnc( $sCallName )
00380     {
00381         $this->_sLastCallFnc = $sCallName;
00382     }
00383 
00389     protected function _getLastCallFnc()
00390     {
00391         return $this->_sLastCallFnc;
00392     }
00393 
00399     public function isRootCatChanged()
00400     {
00401         // in Basket
00402         $oBasket = $this->getSession()->getBasket();
00403         if ( $oBasket->showCatChangeWarning() ) {
00404             $oBasket->setCatChangeWarningState( false );
00405             return true;
00406         }
00407 
00408         // in Category, only then category is empty ant not equal to default category
00409         $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
00410         $sActCat = oxConfig::getParameter( 'cnid' );
00411         $oActCat = oxnew('oxcategory');
00412         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00413             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00414             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00415                 return true;
00416             }
00417         }
00418 
00419         return false;
00420     }
00421 
00430     public function executeuserchoice()
00431     {
00432         // redirect to basket
00433         if ( oxConfig::getParameter( "tobasket" ) ) {
00434             return "basket";
00435         } else {
00436             // clear basket
00437             $this->getSession()->getBasket()->deleteBasket();
00438             $this->getParent()->setRootCatChanged( false );
00439         }
00440     }
00441 }