OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxcmp_basket.php
Go to the documentation of this file.
1 <?php
2 
8 class oxcmp_basket extends oxView
9 {
10 
15  protected $_blIsComponent = true;
16 
21  protected $_sLastCallFnc = null;
22 
28  public $aRedirectParams = array( 'cnid', // category id
29  'mnid', // manufacturer id
30  'anid', // active article id
31  'tpl', // spec. template
32  'listtype', // list type
33  'searchcnid', // search category
34  'searchvendor',// search vendor
35  'searchmanufacturer',// search manufacturer
36  'searchtag', // search tag
37  'searchrecomm',// search recomendation
38  'recommid' // recomm. list id
39  );
40 
46  public function init()
47  {
48  $oConfig = $this->getConfig();
49  if ($oConfig->getConfigParam( 'blPsBasketReservationEnabled' )) {
50  if ($oReservations = $this->getSession()->getBasketReservations()) {
51  if (!$oReservations->getTimeLeft()) {
52  $oBasket = $this->getSession()->getBasket();
53  if ( $oBasket && $oBasket->getProductsCount() ) {
54  $oBasket->deleteBasket();
55  }
56  }
57  $iLimit = (int) $oConfig->getConfigParam( 'iBasketReservationCleanPerRequest' );
58  if (!$iLimit) {
59  $iLimit = 200;
60  }
61  $oReservations->discardUnusedReservations($iLimit);
62  }
63  }
64 
65  parent::init();
66 
67  // Basket exclude
68  if ( $this->getConfig()->getConfigParam( 'blBasketExcludeEnabled' ) ) {
69  if ( $oBasket = $this->getSession()->getBasket() ) {
70  $this->getParent()->setRootCatChanged( $this->isRootCatChanged() && $oBasket->getContents() );
71  }
72  }
73  }
74 
81  public function render()
82  {
83  // recalculating
84  if ( $oBasket = $this->getSession()->getBasket() ) {
85  $oBasket->calculateBasket( false );
86  }
87 
89 
90  return $oBasket;
91  }
92 
108  public function tobasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
109  {
110  // adding to basket is not allowed ?
111  $myConfig = $this->getConfig();
112  if ( oxRegistry::getUtils()->isSearchEngine() ) {
113  return;
114  }
115 
116  // adding articles
117  if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
118 
119  $this->_setLastCallFnc( 'tobasket' );
120  $oBasketItem = $this->_addItems( $aProducts );
121 
122  // new basket item marker
123  if ( $oBasketItem && $myConfig->getConfigParam( 'iNewBasketItemMessage' ) != 0 ) {
124  $oNewItem = new stdClass();
125  $oNewItem->sTitle = $oBasketItem->getTitle();
126  $oNewItem->sId = $oBasketItem->getProductId();
127  $oNewItem->dAmount = $oBasketItem->getAmount();
128  $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
129 
130  // passing article
131  oxSession::setVar( '_newitem', $oNewItem );
132  }
133 
134 
135  // redirect to basket
136  return $this->_getRedirectUrl();
137  }
138  }
139 
151  public function changebasket( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = true )
152  {
153  // adding to basket is not allowed ?
154  if ( oxRegistry::getUtils()->isSearchEngine() ) {
155  return;
156  }
157 
158  // fetching item ID
159  if (!$sProductId) {
160  $sBasketItemId = oxConfig::getParameter( 'bindex' );
161 
162  if ( $sBasketItemId ) {
163  $oBasket = $this->getSession()->getBasket();
164  //take params
165  $aBasketContents = $oBasket->getContents();
166  $sProductId = isset( $aBasketContents[$sBasketItemId] )?$aBasketContents[$sBasketItemId]->getProductId():null;
167  } else {
168  $sProductId = oxConfig::getParameter( 'aid' );
169  }
170  }
171 
172  // fetching other needed info
173  $dAmount = isset( $dAmount )?$dAmount:oxConfig::getParameter( 'am' );
174  $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
175  $aPersParam = $aPersParam?$aPersParam:oxConfig::getParameter( 'persparam' );
176 
177  // adding articles
178  if ( $aProducts = $this->_getItems( $sProductId, $dAmount, $aSel, $aPersParam, $blOverride ) ) {
179 
180  // information that last call was changebasket
181  $oBasket = $this->getSession()->getBasket();
182  $oBasket->onUpdate();
183 
184  $this->_setLastCallFnc( 'changebasket' );
185  $oBasketItem = $this->_addItems( $aProducts );
186  }
187 
188  }
189 
196  protected function _getRedirectUrl()
197  {
198 
199  // active class
200  $sClass = oxConfig::getParameter( 'cl' );
201  $sClass = $sClass?$sClass.'?':'start?';
202  $sPosition = '';
203 
204  // setting redirect parameters
205  foreach ( $this->aRedirectParams as $sParamName ) {
206  $sParamVal = oxConfig::getParameter( $sParamName );
207  $sPosition .= $sParamVal?$sParamName.'='.$sParamVal.'&':'';
208  }
209 
210  // special treatment
211  // search param
212  $sParam = rawurlencode( oxConfig::getParameter( 'searchparam', true ) );
213  $sPosition .= $sParam?'searchparam='.$sParam.'&':'';
214 
215  // current page number
216  $iPageNr = (int) oxConfig::getParameter( 'pgNr' );
217  $sPosition .= ( $iPageNr > 0 )?'pgNr='.$iPageNr.'&':'';
218 
219  // reload and backbutton blocker
220  if ( $this->getConfig()->getConfigParam( 'iNewBasketItemMessage' ) == 3 ) {
221 
222  // saving return to shop link to session
223  oxSession::setVar( '_backtoshop', $sClass.$sPosition );
224 
225  // redirecting to basket
226  $sClass = 'basket?';
227  }
228 
229  return $sClass.$sPosition;
230  }
231 
244  protected function _getItems( $sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false )
245  {
246  // collecting items to add
247  $aProducts = oxConfig::getParameter( 'aproducts' );
248 
249  // collecting specified item
250  $sProductId = $sProductId?$sProductId:oxConfig::getParameter( 'aid' );
251  if ( $sProductId ) {
252 
253  // additionally fething current product info
254  $dAmount = isset( $dAmount ) ? $dAmount : oxConfig::getParameter( 'am' );
255 
256  // select lists
257  $aSel = isset( $aSel )?$aSel:oxConfig::getParameter( 'sel' );
258 
259  // persistent parameters
260  if ( empty($aPersParam) ) {
261  $aPersParam = oxConfig::getParameter( 'persparam' );
262  if ( !is_array($aPersParam) || empty($aPersParam['details']) ) {
263  $aPersParam = null;
264  }
265  }
266 
267  $sBasketItemId = oxConfig::getParameter( 'bindex' );
268 
269  $aProducts[$sProductId] = array( 'am' => $dAmount,
270  'sel' => $aSel,
271  'persparam' => $aPersParam,
272  'override' => $blOverride,
273  'basketitemid' => $sBasketItemId
274  );
275  }
276 
277  if ( is_array( $aProducts ) && count( $aProducts ) ) {
278 
279  if (oxConfig::getParameter( 'removeBtn' ) !== null) {
280  //setting amount to 0 if removing article from basket
281  foreach ( $aProducts as $sProductId => $aProduct ) {
282  if ( isset($aProduct['remove']) && $aProduct['remove']) {
283  $aProducts[$sProductId]['am'] = 0;
284  } else {
285  unset ($aProducts[$sProductId]);
286  }
287  }
288  }
289 
290  return $aProducts;
291  }
292 
293  return false;
294  }
295 
304  protected function _addItems ( $aProducts )
305  {
306  $oActView = $this->getConfig()->getActiveView();
307  $sErrorDest = $oActView->getErrorDestination();
308 
309  $oBasket = $this->getSession()->getBasket();
310  $oBasketInfo = $oBasket->getBasketSummary();
311 
312  foreach ( $aProducts as $sAddProductId => $aProductInfo ) {
313 
314  $sProductId = isset( $aProductInfo['aid'] ) ? $aProductInfo['aid'] : $sAddProductId;
315 
316  // collecting input
317  $aProducts[$sAddProductId]['oldam'] = isset( $oBasketInfo->aArticles[$sProductId] ) ? $oBasketInfo->aArticles[$sProductId] : 0;
318 
319  $dAmount = isset( $aProductInfo['am'] )?$aProductInfo['am']:0;
320  $aSelList = isset( $aProductInfo['sel'] )?$aProductInfo['sel']:null;
321  $aPersParam = ( isset( $aProductInfo['persparam'] ) && is_array( $aProductInfo['persparam'] ) && strlen( $aProductInfo['persparam']['details'] ) )?$aProductInfo['persparam']:null;
322  $blOverride = isset( $aProductInfo['override'] )?$aProductInfo['override']:null;
323  $blIsBundle = isset( $aProductInfo['bundle'] )?true:false;
324  $sOldBasketItemId = isset( $aProductInfo['basketitemid'] )?$aProductInfo['basketitemid']:null;
325 
326  try {
327  $oBasketItem = $oBasket->addToBasket( $sProductId, $dAmount, $aSelList, $aPersParam, $blOverride, $blIsBundle, $sOldBasketItemId );
328  } catch ( oxOutOfStockException $oEx ) {
329  $oEx->setDestination( $sErrorDest );
330  // #950 Change error destination to basket popup
331  if ( !$sErrorDest && $this->getConfig()->getConfigParam( 'iNewBasketItemMessage') == 2) {
332  $sErrorDest = 'popup';
333  }
334  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
335  } catch ( oxArticleInputException $oEx ) {
336  //add to display at specific position
337  $oEx->setDestination( $sErrorDest );
338  oxRegistry::get("oxUtilsView")->addErrorToDisplay( $oEx, false, (bool) $sErrorDest, $sErrorDest );
339  } catch ( oxNoArticleException $oEx ) {
340  //ignored, best solution F ?
341  }
342  if ( !$oBasketItem ) {
343  $oInfo = $oBasket->getBasketSummary();
344  $aProducts[$sAddProductId]['am'] = isset( $oInfo->aArticles[$sProductId] ) ? $oInfo->aArticles[$sProductId] : 0;
345  }
346  }
347 
348  //if basket empty remove posible gift card
349  if ( $oBasket->getProductsCount() == 0 ) {
350  $oBasket->setCardId( null );
351  }
352 
353  // information that last call was tobasket
354  $this->_setLastCall( $this->_getLastCallFnc(), $aProducts, $oBasketInfo );
355 
356  return $oBasketItem;
357  }
358 
368  protected function _setLastCall( $sCallName, $aProductInfo, $aBasketInfo )
369  {
370  oxSession::setVar( 'aLastcall', array( $sCallName => $aProductInfo ) );
371  }
372 
380  protected function _setLastCallFnc( $sCallName )
381  {
382  $this->_sLastCallFnc = $sCallName;
383  }
384 
390  protected function _getLastCallFnc()
391  {
392  return $this->_sLastCallFnc;
393  }
394 
400  public function isRootCatChanged()
401  {
402  // in Basket
403  $oBasket = $this->getSession()->getBasket();
404  if ( $oBasket->showCatChangeWarning() ) {
405  $oBasket->setCatChangeWarningState( false );
406  return true;
407  }
408 
409  // in Category, only then category is empty ant not equal to default category
410  $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
411  $sActCat = oxConfig::getParameter( 'cnid' );
412  $oActCat = oxnew('oxcategory');
413  if ($sActCat && $sActCat!=$sDefCat && $oActCat->load($sActCat) ) {
414  $sActRoot = $oActCat->oxcategories__oxrootid->value;
415  if ( $oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId() ) {
416  return true;
417  }
418  }
419 
420  return false;
421  }
422 
431  public function executeuserchoice()
432  {
433 
434  // redirect to basket
435  if ( oxConfig::getParameter( "tobasket" ) ) {
436  return "basket";
437  } else {
438  // clear basket
439  $this->getSession()->getBasket()->deleteBasket();
440  $this->getParent()->setRootCatChanged( false );
441  }
442  }
443 
444 }