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',
00029 'mnid',
00030 'anid',
00031 'tpl',
00032 'listtype',
00033 'searchcnid',
00034 'searchvendor',
00035 'searchmanufacturer',
00036 'searchtag',
00037 'searchrecomm',
00038 'recommid'
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
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
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
00111 $myConfig = $this->getConfig();
00112 if ( oxRegistry::getUtils()->isSearchEngine() ) {
00113 return;
00114 }
00115
00116
00117 if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00118
00119 $this->_setLastCallFnc( 'tobasket' );
00120 $oBasketItem = $this->_addItems( $aProducts );
00121
00122
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
00131 oxSession::setVar( '_newitem', $oNewItem );
00132 }
00133
00134
00135
00136 return $this->_getRedirectUrl();
00137 }
00138 }
00139
00151 public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
00152 {
00153
00154 if ( oxRegistry::getUtils()->isSearchEngine() ) {
00155 return;
00156 }
00157
00158
00159 if (!$sProductId) {
00160 $sBasketItemId = oxConfig::getParameter( 'bindex' );
00161
00162 if ( $sBasketItemId ) {
00163 $oBasket = $this->getSession()->getBasket();
00164
00165 $aBasketContents = $oBasket->getContents();
00166 $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
00167 } else {
00168 $sProductId = oxConfig::getParameter( 'aid' );
00169 }
00170 }
00171
00172
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
00178 if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
00179
00180
00181 $oBasket = $this->getSession()->getBasket();
00182 $oBasket->onUpdate();
00183
00184 $this->_setLastCallFnc( 'changebasket' );
00185 $oBasketItem = $this->_addItems( $aProducts );
00186 }
00187
00188 }
00189
00196 protected function _getRedirectUrl()
00197 {
00198
00199
00200 $sClass = oxConfig::getParameter( 'cl' );
00201 $sClass = $sClass?$sClass.'?':'start?';
00202 $sPosition = '';
00203
00204
00205 foreach ( $this->aRedirectParams as $sParamName ) {
00206 $sParamVal = oxConfig::getParameter( $sParamName );
00207 $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
00208 }
00209
00210
00211
00212 $sParam = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
00213 $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
00214
00215
00216 $iPageNr = (int) oxConfig::getParameter( 'pgNr' );
00217 $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
00218
00219
00220 if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
00221
00222
00223 oxSession::setVar( '_backtoshop', $sClass.$sPosition );
00224
00225
00226 $sClass = 'basket?';
00227 }
00228
00229 return $sClass.$sPosition;
00230 }
00231
00244 protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
00245 {
00246
00247 $aProducts = oxConfig::getParameter( 'aproducts' );
00248
00249
00250 $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
00251 if ( $sProductId ) {
00252
00253
00254 $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
00255
00256
00257 $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
00258
00259
00260 if ( empty($aPersParam) ) {
00261 $aPersParam = oxConfig::getParameter( 'persparam' );
00262 if ( !is_array($aPersParam) || empty($aPersParam['details']) ) {
00263 $aPersParam = null;
00264 }
00265 }
00266
00267 $sBasketItemId = oxConfig::getParameter( 'bindex' );
00268
00269 $aProducts[$sProductId] = array( 'am' => $dAmount,
00270 'sel' => $aSel,
00271 'persparam' => $aPersParam,
00272 'override' => $blOverride,
00273 'basketitemid' => $sBasketItemId
00274 );
00275 }
00276
00277 if ( is_array( $aProducts ) && count( $aProducts ) ) {
00278
00279 if (oxConfig::getParameter( 'removeBtn' ) !== null) {
00280
00281 foreach ( $aProducts as $sProductId => $aProduct ) {
00282 if ( isset($aProduct['remove']) && $aProduct['remove']) {
00283 $aProducts[$sProductId]['am'] = 0;
00284 } else {
00285 unset ($aProducts[$sProductId]);
00286 }
00287 }
00288 }
00289
00290 return $aProducts;
00291 }
00292
00293 return false;
00294 }
00295
00304 protected function _addItems ( $aProducts )
00305 {
00306 $oActView = $this->getConfig()->getActiveView();
00307 $sErrorDest = $oActView->getErrorDestination();
00308
00309 $oBasket = $this->getSession()->getBasket();
00310 $oBasketInfo = $oBasket->getBasketSummary();
00311
00312 foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
00313
00314 $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
00315
00316
00317 $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
00318
00319 $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
00320 $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
00321 $aPersParam = ( isset( $aProductInfo['persparam'] ) && is_array( $aProductInfo['persparam'] ) && strlen( $aProductInfo['persparam']['details'] ) )?$aProductInfo['persparam']:null;
00322 $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
00323 $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
00324 $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
00325
00326 try {
00327 $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
00328 } catch ( oxOutOfStockException $oEx ) {
00329 $oEx->setDestination( $sErrorDest );
00330
00331 if ( !$sErrorDest && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
00332 $sErrorDest = 'popup';
00333 }
00334 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00335 } catch ( oxArticleInputException $oEx ) {
00336
00337 $oEx->setDestination( $sErrorDest );
00338 oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
00339 } catch ( oxNoArticleException $oEx ) {
00340
00341 }
00342 if ( !$oBasketItem ) {
00343 $oInfo = $oBasket->getBasketSummary();
00344 $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
00345 }
00346 }
00347
00348
00349 if ( $oBasket->getProductsCount() == 0 ) {
00350 $oBasket->setCardId( null );
00351 }
00352
00353
00354 $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
00355
00356 return $oBasketItem;
00357 }
00358
00368 protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
00369 {
00370 oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
00371 }
00372
00380 protected function _setLastCallFnc( $sCallName )
00381 {
00382 $this->_sLastCallFnc = $sCallName;
00383 }
00384
00390 protected function _getLastCallFnc()
00391 {
00392 return $this->_sLastCallFnc;
00393 }
00394
00400 public function isRootCatChanged()
00401 {
00402
00403 $oBasket = $this->getSession()->getBasket();
00404 if ( $oBasket->showCatChangeWarning() ) {
00405 $oBasket->setCatChangeWarningState( false );
00406 return true;
00407 }
00408
00409
00410 $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
00411 $sActCat = oxConfig::getParameter( 'cnid' );
00412 $oActCat = oxnew('oxcategory');
00413 if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
00414 $sActRoot = $oActCat->oxcategories__oxrootid->value;
00415 if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
00416 return true;
00417 }
00418 }
00419
00420 return false;
00421 }
00422
00431 public function executeuserchoice()
00432 {
00433
00434
00435 if ( oxConfig::getParameter( "tobasket" ) ) {
00436 return "basket";
00437 } else {
00438
00439 $this->getSession()->getBasket()->deleteBasket();
00440 $this->getParent()->setRootCatChanged( false );
00441 }
00442 }
00443
00444 }