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 
00022     public $aRedirectParams = array( 'cnid',        // category id
00023                                      'mnid',        // manufacturer id
00024                                      'anid',        // active article id
00025                                      'tpl',         // spec. template
00026                                      'listtype',    // list type
00027                                      'searchcnid',  // search category
00028                                      'searchvendor',// search vendor
00029                                      'searchmanufacturer',// search manufacturer
00030                                      'searchtag',   // search tag
00031                                      'searchrecomm',// search recomendation
00032                                      'recommid'     // recomm. list id
00033                                     );
00034 
00041     public function render()
00042     {
00043         // recalculating
00044         if ( $oBasket = $this->getSession()->getBasket() ) {
00045             $oBasket->calculateBasket( false );
00046         }
00047 
00048         parent::render();
00049 
00050         return $oBasket;
00051     }
00052 
00068     public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00069     {
00070         // adding to basket is not allowed ?
00071         $myConfig = $this->getConfig();
00072         if ( oxUtils::getInstance()->isSearchEngine() ) {
00073             return;
00074         }
00075 
00076         // adding articles
00077         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00078 
00079             $this->_setLastCall( 'tobasket', $aProducts, $this->getSession()->getBasket()->getBasketSummary() );
00080             $oBasketItem = $this->_addItems( $aProducts );
00081 
00082             // new basket item marker
00083             if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
00084                 $oNewItem = new OxstdClass();
00085                 $oNewItem->sTitle  = $oBasketItem->getTitle();
00086                 $oNewItem->sId     = $oBasketItem->getProductId();
00087                 $oNewItem->dAmount = $oBasketItem->getAmount();
00088                 $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
00089 
00090                 // passing article
00091                 oxSession::setVar( '_newitem', $oNewItem );
00092             }
00093         }
00094 
00095         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00096             // redirect to basket
00097             return $this->_getRedirectUrl();
00098         }
00099     }
00100 
00112     public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00113     {
00114         // adding to basket is not allowed ?
00115         if ( oxUtils::getInstance()->isSearchEngine() ) {
00116             return;
00117         }
00118 
00119         // fetching item ID
00120         if (!$sProductId) {
00121             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00122 
00123             if ( $sBasketItemId ) {
00124                 $oBasket = $this->getSession()->getBasket();
00125                 //take params
00126                 $aBasketContents = $oBasket->getContents();
00127                 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00128             } else {
00129                 $sProductId = oxConfig::getParameter( 'aid' );
00130             }
00131         }
00132 
00133         // fetching other needed info
00134         $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
00135         $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00136         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00137 
00138         // adding articles
00139         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00140 
00141             // information that last call was changebasket
00142             $oBasket = $this->getSession()->getBasket();
00143             $oBasket->onUpdate();
00144             $this->_setLastCall( 'changebasket', $aProducts, $oBasket->getBasketSummary() );
00145 
00146             $oBasketItem = $this->_addItems( $aProducts );
00147         }
00148 
00149     }
00150 
00165     public function wl_tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00166     {
00167         // adding to basket is not allowed ?
00168         if ( oxUtils::getInstance()->isSearchEngine() ) {
00169             return;
00170         }
00171 
00172         // collecting info
00173         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00174         $dAmount    = $dAmount?$dAmount:oxConfig::getParameter( 'am' );
00175         $aSel       = $aSel?$aSel:oxConfig::getParameter( 'sel' );
00176         $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00177 
00178         // adding articles
00179         if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00180 
00181             $oBasketItem = $this->_addItems( $aProducts );
00182             $oBasketItem->setWishArticleId( oxConfig::getParameter( 'anid' ) );
00183 
00184             // information that last call was tobasket
00185             $this->_setLastCall( 'tobasket', $aProducts, $this->getSession()->getBasket()->getBasketSummary() );
00186 
00187             // fetching user info
00188             $oUser = $this->getUser();
00189             $oWishUser = oxNew( 'oxuser' );
00190             $sUserId   = oxConfig::getParameter( 'owishid' )?oxConfig::getParameter( 'owishid' ):oxConfig::getParameter( 'wishid' );
00191             if ( $oWishUser->load( $sUserId ) && $oUser ) {
00192                 // checking if this user isn't assigned to delivery address list and if not - adding him this address
00193                 $oUser->addUserAddress( $oWishUser );
00194                 $oBasketItem->setWishId( $sUserId );
00195             }
00196         }
00197 
00198         return $this->_getRedirectUrl();
00199     }
00200 
00207     protected function _getRedirectUrl()
00208     {
00209 
00210         // active class
00211         $sClass = oxConfig::getParameter( 'cl' );
00212         $sClass = $sClass?$sClass.'?':'start?';
00213         $sPosition = '';
00214 
00215         // setting redirect parameters
00216         foreach ( $this->aRedirectParams as $sParamName ) {
00217             $sParamVal  = oxConfig::getParameter( $sParamName );
00218             $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00219         }
00220 
00221         // special treatment
00222         // search param
00223         $sParam     = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00224         $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00225 
00226         // current page number
00227         $iPageNr    = (int) oxConfig::getParameter( 'pgNr' );
00228         $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00229 
00230         // reload and backbutton blocker
00231         if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00232 
00233             // saving return to shop link to session
00234             oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00235 
00236             // redirecting to basket
00237             $sClass = 'basket?';
00238         }
00239 
00240         return $sClass.$sPosition;
00241     }
00242 
00255     protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00256     {
00257         // collecting items to add
00258         $aProducts = oxConfig::getParameter( 'aproducts' );
00259 
00260         // collecting specified item
00261         $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00262         if ( $sProductId ) {
00263 
00264             // additionally fething current product info
00265             $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00266 
00267             // select lists
00268             $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00269 
00270             // persistent parameters
00271             $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
00272 
00273             $sBasketItemId = oxConfig::getParameter( 'bindex' );
00274 
00275             $aProducts[$sProductId] = array( 'am' => $dAmount,
00276                                              'sel' => $aSel,
00277                                              'persparam' => $aPersParam,
00278                                              'override'  => $blOverride,
00279                                              'basketitemid' => $sBasketItemId
00280                                            );
00281         }
00282 
00283         if ( is_array( $aProducts ) && count( $aProducts ) ) {
00284 
00285             if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00286                 //setting amount to 0 if removing article from basket
00287                 foreach ( $aProducts as $sProductId => $aProduct ) {
00288                     if ( isset($aProduct['remove']) && $aProduct['remove']) {
00289                         $aProducts[$sProductId]['am'] = 0;
00290                     } else {
00291                         unset ($aProducts[$sProductId]);
00292                     }
00293                 }
00294             }
00295 
00296             return $aProducts;
00297         }
00298 
00299         return false;
00300     }
00301 
00310     protected function _addItems ( $aProducts )
00311     {
00312         $oActView   = $this->getConfig()->getActiveView();
00313         $sErrorDest = $oActView->getErrorDestination();
00314 
00315         $oBasket = $this->getSession()->getBasket();
00316         foreach ( $aProducts as $sProductId => $aProductInfo ) {
00317 
00318             // collecting input
00319 
00320             $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sProductId;
00321 
00322             $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00323             $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00324             $aPersParam = isset( $aProductInfo['persparam'] )?$aProductInfo['persparam']:null;
00325             $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00326             $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00327             $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00328 
00329             try {
00330                 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00331             } catch ( oxOutOfStockException $oEx ) {
00332                 $oEx->setDestination( $sErrorDest );
00333                 // #950 Change error destination to basket popup
00334                 if ( !$sErrorDest  && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00335                     $sErrorDest = 'popup';
00336                 }
00337                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00338             } catch ( oxArticleInputException $oEx ) {
00339                 //add to display at specific position
00340                 $oEx->setDestination( $sErrorDest );
00341                 oxUtilsView::getInstance()->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00342             } catch ( oxNoArticleException $oEx ) {
00343                 //ignored, best solution F ?
00344             }
00345         }
00346 
00347         return $oBasketItem;
00348     }
00349 
00359     protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00360     {
00361         $aProducts = array();
00362 
00363         // collecting amounts info
00364         foreach ( $aProductInfo as $sProdId => $aProdData ) {
00365             $aProducts[$sProdId] = $aProdData;
00366             // setting previous amount
00367             $aProducts[$sProdId]['oldam'] = isset( $aBasketInfo->aArticles[$aProdData['aid']] ) ? $aBasketInfo->aArticles[$aProdData['aid']] : 0;
00368         }
00369 
00370         oxSession::setVar( 'aLastcall', array( $sCallName => $aProducts ) );
00371     }
00372 }

Generated by  doxygen 1.6.2