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             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00308 
00309             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00310 
00311             $aProducts[$sProductId] = array( 'am' => $dAmount,
00312                                              'sel' => $aSel,
00313                                              'persparam' => $aPersParam,
00314                                              'override'  => $blOverride,
00315                                              'basketitemid' => $sBasketItemId
00316                                            );
00317         }
00318 
00319         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00320 
00321             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00322                 //setting amount to 0 if removing article from basket
00323                 foreach ( $aProducts as $sProductId => $aProduct ) {
00324                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00325                         $aProducts[$sProductId]['am'] = 0;
00326                     } else {
00327                         unset ($aProducts[$sProductId]);
00328                     }
00329                 }
00330             }
00331 
00332             return $aProducts;
00333         }
00334 
00335         return false;
00336     }
00337 
00346     protected function _addItems ( $aProducts )
00347     {
00348         $oActView   = $this->getConfig()->getActiveView();
00349         $sErrorDest = $oActView->getErrorDestination();
00350 
00351         $oBasket = $this->getSession()->getBasket();
00352         $oBasketInfo = $oBasket->getBasketSummary();
00353 
00354         foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00355 
00356             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00357 
00358             // collecting input
00359             $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00360 
00361             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00362             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00363             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00364             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00365             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00366             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00367 
00368             try {
00369                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00370             } catch ( oxOutOfStockException $oEx ) {
00371                 $oEx->setDestination( $sErrorDest );
00372                 // #950 Change error destination to basket popup
00373                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00374                     $sErrorDest = 'popup';
00375                 }
00376                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00377             } catch ( oxArticleInputException $oEx ) {
00378                 //add to display at specific position
00379                 $oEx->setDestination( $sErrorDest );
00380                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00381             } catch ( oxNoArticleException $oEx ) {
00382                 //ignored, best solution F ?
00383             }
00384             if ( !$oBasketItem ) {
00385                 $oInfo = $oBasket->getBasketSummary();
00386                 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00387             }
00388         }
00389 
00390         // information that last call was tobasket
00391         $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00392 
00393         return $oBasketItem;
00394     }
00395 
00405     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00406     {
00407         oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00408     }
00409 
00417     protected function _setLastCallFnc( $sCallName )
00418     {
00419         $this->_sLastCallFnc = $sCallName;
00420     }
00421 
00427     protected function _getLastCallFnc()
00428     {
00429         return $this->_sLastCallFnc;
00430     }
00431 
00437     public function isRootCatChanged()
00438     {
00439         // in Basket
00440         $oBasket = $this->getSession()->getBasket();
00441         if ( $oBasket->showCatChangeWarning() ) {
00442             $oBasket->setCatChangeWarningState( false );
00443             return true;
00444         }
00445 
00446         // in Category, only then category is empty ant not equal to default category
00447         $sDefCat = oxConfig::getInstance()->getActiveShop()->oxshops__oxdefcat->value;
00448         $sActCat = oxConfig::getParameter( 'cnid' );
00449         $oActCat = oxnew('oxcategory');
00450         if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00451             $sActRoot = $oActCat->oxcategories__oxrootid->value;
00452             if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00453                 return true;
00454             }
00455         }
00456 
00457         return false;
00458     }
00459 
00468     public function executeuserchoice()
00469     {
00470         // redirect to basket
00471         if ( oxConfig::getParameter( "tobasket" ) ) {
00472             return "basket";
00473         } else {
00474             // clear basket
00475             $this->getSession()->getBasket()->deleteBasket();
00476             $this->getParent()->setRootCatChanged( false );
00477         }
00478     }
00479 }