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 
00204     public function wl_tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00205     {
00206         // adding to basket is not allowed ?
00207         if ( oxUtils::getInstance()->isSearchEngine() ) {
00208             return;
00209         }
00210 
00211         // collecting info
00212         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00213         $dAmount    = $dAmount?$dAmount:oxConfig::getParameter( 'am' );
00214         $aSel       = $aSel?$aSel:oxConfig::getParameter( 'sel' );
00215         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00216 
00217         // adding articles
00218         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00219 
00220             $this->_setLastCallFnc( 'tobasket' );
00221             $oBasketItem = $this->_addItems( $aProducts );
00222             $oBasketItem->setWishArticleId( oxConfig::getParameter( 'anid' ) );
00223 
00224             // fetching user info
00225             $oUser = $this->getUser();
00226             $oWishUser = oxNew( 'oxuser' );
00227             $sUserId   = oxConfig::getParameter( 'owishid' )?oxConfig::getParameter( 'owishid' ):oxConfig::getParameter( 'wishid' );
00228             if ( $oWishUser->load( $sUserId ) && $oUser ) {
00229                 // checking if this user isn't assigned to delivery address list and if not - adding him this address
00230                 $oUser->addUserAddress( $oWishUser );
00231                 $oBasketItem->setWishId( $sUserId );
00232             }
00233         }
00234 
00235         return $this->_getRedirectUrl();
00236     }
00237 
00244     protected function _getRedirectUrl()
00245     {
00246 
00247         // active class
00248         $sClass = oxConfig::getParameter( 'cl' );
00249         $sClass = $sClass?$sClass.'?':'start?';
00250         $sPosition = '';
00251 
00252         // setting redirect parameters
00253         foreach ( $this->aRedirectParams as $sParamName ) {
00254             $sParamVal  = oxConfig::getParameter( $sParamName );
00255             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00256         }
00257 
00258         // special treatment
00259         // search param
00260         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00261         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00262 
00263         // current page number
00264         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00265         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00266 
00267         // reload and backbutton blocker
00268         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00269 
00270             // saving return to shop link to session
00271             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00272 
00273             // redirecting to basket
00274             $sClass = 'basket?';
00275         }
00276 
00277         return $sClass.$sPosition;
00278     }
00279 
00292     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00293     {
00294         // collecting items to add
00295         $aProducts = oxConfig::getParameter( 'aproducts' );
00296 
00297         // collecting specified item
00298         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00299         if ( $sProductId ) {
00300 
00301             // additionally fething current product info
00302             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00303 
00304             // select lists
00305             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00306 
00307             // persistent parameters
00308             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00309 
00310             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00311 
00312             $aProducts[$sProductId] = array( 'am' => $dAmount,
00313                                              'sel' => $aSel,
00314                                              'persparam' => $aPersParam,
00315                                              'override'  => $blOverride,
00316                                              'basketitemid' => $sBasketItemId
00317                                            );
00318         }
00319 
00320         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00321 
00322             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00323                 //setting amount to 0 if removing article from basket
00324                 foreach ( $aProducts as $sProductId => $aProduct ) {
00325                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00326                         $aProducts[$sProductId]['am'] = 0;
00327                     } else {
00328                         unset ($aProducts[$sProductId]);
00329                     }
00330                 }
00331             }
00332 
00333             return $aProducts;
00334         }
00335 
00336         return false;
00337     }
00338 
00347     protected function _addItems ( $aProducts )
00348     {
00349         $oActView   = $this->getConfig()->getActiveView();
00350         $sErrorDest = $oActView->getErrorDestination();
00351 
00352         $oBasket = $this->getSession()->getBasket();
00353         $oBasketInfo = $oBasket->getBasketSummary();
00354 
00355         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00356 
00357             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00358 
00359             // collecting input
00360             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00361 
00362             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00363             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00364             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00365             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00366             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00367             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00368 
00369             try {
00370                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00371             } catch ( oxOutOfStockException $oEx ) {
00372                 $oEx->setDestination( $sErrorDest );
00373                 // #950 Change error destination to basket popup
00374                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00375                     $sErrorDest = 'popup';
00376                 }
00377                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00378             } catch ( oxArticleInputException $oEx ) {
00379                 //add to display at specific position
00380                 $oEx->setDestination( $sErrorDest );
00381                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00382             } catch ( oxNoArticleException $oEx ) {
00383                 //ignored, best solution F ?
00384             }
00385             if ( !$oBasketItem ) {
00386                 $oInfo = $oBasket->getBasketSummary();
00387                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00388             }
00389         }
00390 
00391         // information that last call was tobasket
00392         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00393 
00394         return $oBasketItem;
00395     }
00396 
00406     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00407     {
00408         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00409     }
00410 
00418     protected function _setLastCallFnc( $sCallName )
00419     {
00420         $this->_sLastCallFnc = $sCallName;
00421     }
00422 
00428     protected function _getLastCallFnc()
00429     {
00430         return $this->_sLastCallFnc;
00431     }
00432 
00438     public function isRootCatChanged()
00439     {
00440         // in Basket
00441         $oBasket = $this->getSession()->getBasket();
00442         if ( $oBasket->showCatChangeWarning() ) {
00443             $oBasket->setCatChangeWarningState( false );
00444             return true;
00445         }
00446 
00447         // in Category, only then category is empty ant not equal to default category
00448         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00449         $sActCat = oxConfig::getParameter( 'cnid' );
00450         $oActCat = oxnew('oxcategory');
00451         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00452             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00453             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00454                 return true;
00455             }
00456         }
00457 
00458         return false;
00459     }
00460 
00469     public function executeuserchoice()
00470     {
00471         // redirect to basket
00472         if ( oxConfig::getParameter( "tobasket" ) ) {
00473             return "basket";
00474         } else {
00475             // clear basket
00476             $this->getSession()->getBasket()->deleteBasket();
00477             $this->getParent()->setRootCatChanged( false );
00478         }
00479     }
00480 }