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 ( oxUtils::getInstance()->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 OxstdClass();
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 ( oxUtils::getInstance()->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 
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             return $this->_getRedirectUrl();
00234         }
00235     }
00236 
00243     protected function _getRedirectUrl()
00244     {
00245 
00246         // active class
00247         $sClass = oxConfig::getParameter( 'cl' );
00248         $sClass = $sClass?$sClass.'?':'start?';
00249         $sPosition = '';
00250 
00251         // setting redirect parameters
00252         foreach ( $this->aRedirectParams as $sParamName ) {
00253             $sParamVal  = oxConfig::getParameter( $sParamName );
00254             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00255         }
00256 
00257         // special treatment
00258         // search param
00259         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00260         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00261 
00262         // current page number
00263         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00264         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00265 
00266         // reload and backbutton blocker
00267         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00268 
00269             // saving return to shop link to session
00270             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00271 
00272             // redirecting to basket
00273             $sClass = 'basket?';
00274         }
00275 
00276         return $sClass.$sPosition;
00277     }
00278 
00291     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00292     {
00293         // collecting items to add
00294         $aProducts = oxConfig::getParameter( 'aproducts' );
00295 
00296         // collecting specified item
00297         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00298         if ( $sProductId ) {
00299 
00300             // additionally fething current product info
00301             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00302 
00303             // select lists
00304             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00305 
00306             // persistent parameters
00307             if ( empty($aPersParam) ) {
00308                 $aPersParam = oxConfig::getParameter( 'persparam' );
00309                 if ( !is_array($aPersParam) || empty($aPersParam['details']) ) {
00310                     $aPersParam = null;
00311                 }
00312             }
00313 
00314             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00315 
00316             $aProducts[$sProductId] = array( 'am' => $dAmount,
00317                                              'sel' => $aSel,
00318                                              'persparam' => $aPersParam,
00319                                              'override'  => $blOverride,
00320                                              'basketitemid' => $sBasketItemId
00321                                            );
00322         }
00323 
00324         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00325 
00326             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00327                 //setting amount to 0 if removing article from basket
00328                 foreach ( $aProducts as $sProductId => $aProduct ) {
00329                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00330                         $aProducts[$sProductId]['am'] = 0;
00331                     } else {
00332                         unset ($aProducts[$sProductId]);
00333                     }
00334                 }
00335             }
00336 
00337             return $aProducts;
00338         }
00339 
00340         return false;
00341     }
00342 
00351     protected function _addItems ( $aProducts )
00352     {
00353         $oActView   = $this->getConfig()->getActiveView();
00354         $sErrorDest = $oActView->getErrorDestination();
00355 
00356         $oBasket = $this->getSession()->getBasket();
00357         $oBasketInfo = $oBasket->getBasketSummary();
00358 
00359         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00360 
00361             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00362 
00363             // collecting input
00364             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00365 
00366             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00367             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00368             $aPersParam = ( isset( $aProductInfo['persparam'] ) && is_array( $aProductInfo['persparam'] ) && strlen( $aProductInfo['persparam']['details'] ) )?$aProductInfo['persparam']:null;
00369             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00370             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00371             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00372 
00373             try {
00374                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00375             } catch ( oxOutOfStockException $oEx ) {
00376                 $oEx->setDestination( $sErrorDest );
00377                 // #950 Change error destination to basket popup
00378                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00379                     $sErrorDest = 'popup';
00380                 }
00381                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00382             } catch ( oxArticleInputException $oEx ) {
00383                 //add to display at specific position
00384                 $oEx->setDestination( $sErrorDest );
00385                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00386             } catch ( oxNoArticleException $oEx ) {
00387                 //ignored, best solution F ?
00388             }
00389             if ( !$oBasketItem ) {
00390                 $oInfo = $oBasket->getBasketSummary();
00391                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00392             }
00393         }
00394 
00395         // information that last call was tobasket
00396         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00397 
00398         return $oBasketItem;
00399     }
00400 
00410     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00411     {
00412         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00413     }
00414 
00422     protected function _setLastCallFnc( $sCallName )
00423     {
00424         $this->_sLastCallFnc = $sCallName;
00425     }
00426 
00432     protected function _getLastCallFnc()
00433     {
00434         return $this->_sLastCallFnc;
00435     }
00436 
00442     public function isRootCatChanged()
00443     {
00444         // in Basket
00445         $oBasket = $this->getSession()->getBasket();
00446         if ( $oBasket->showCatChangeWarning() ) {
00447             $oBasket->setCatChangeWarningState( false );
00448             return true;
00449         }
00450 
00451         // in Category, only then category is empty ant not equal to default category
00452         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00453         $sActCat = oxConfig::getParameter( 'cnid' );
00454         $oActCat = oxnew('oxcategory');
00455         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00456             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00457             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00458                 return true;
00459             }
00460         }
00461 
00462         return false;
00463     }
00464 
00473     public function executeuserchoice()
00474     {
00475         // redirect to basket
00476         if ( oxConfig::getParameter( "tobasket" ) ) {
00477             return "basket";
00478         } else {
00479             // clear basket
00480             $this->getSession()->getBasket()->deleteBasket();
00481             $this->getParent()->setRootCatChanged( false );
00482         }
00483     }
00484 }