OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxcmp_basket.php
Go to the documentation of this file.
1 <?php
2 
9 class oxcmp_basket extends oxView
10 {
11 
17  protected $_blIsComponent = true;
18 
24  protected $_sLastCallFnc = null;
25 
32  public $aRedirectParams = array('cnid', // category id
33  'mnid', // manufacturer id
34  'anid', // active article id
35  'tpl', // spec. template
36  'listtype', // list type
37  'searchcnid', // search category
38  'searchvendor', // search vendor
39  'searchmanufacturer', // search manufacturer
40  // @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
41  'searchtag', // search tag
42  // END deprecated
43  // @deprecated since v5.3 (2016-06-17); Listmania will be moved to an own module.
44  'searchrecomm', // search recomendation
45  'recommid' // recomm. list id
46  // END deprecated
47  );
48 
52  public function init()
53  {
54  $oConfig = $this->getConfig();
55  if ($oConfig->getConfigParam('blPsBasketReservationEnabled')) {
56  if ($oReservations = $this->getSession()->getBasketReservations()) {
57  if (!$oReservations->getTimeLeft()) {
58  $oBasket = $this->getSession()->getBasket();
59  if ($oBasket && $oBasket->getProductsCount()) {
60  $oBasket->deleteBasket();
61  }
62  }
63  $iLimit = (int) $oConfig->getConfigParam('iBasketReservationCleanPerRequest');
64  if (!$iLimit) {
65  $iLimit = 200;
66  }
67  $oReservations->discardUnusedReservations($iLimit);
68  }
69  }
70 
71  parent::init();
72 
73  // Basket exclude
74  if ($this->getConfig()->getConfigParam('blBasketExcludeEnabled')) {
75  if ($oBasket = $this->getSession()->getBasket()) {
76  $this->getParent()->setRootCatChanged($this->isRootCatChanged() && $oBasket->getContents());
77  }
78  }
79  }
80 
87  public function render()
88  {
89  // recalculating
90  if ($oBasket = $this->getSession()->getBasket()) {
91  $oBasket->calculateBasket(false);
92  }
93 
95 
96  return $oBasket;
97  }
98 
114  public function tobasket($sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false)
115  {
116  // adding to basket is not allowed ?
117  $myConfig = $this->getConfig();
118  if (oxRegistry::getUtils()->isSearchEngine()) {
119  return;
120  }
121 
122  // adding articles
123  if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
124 
125  $this->_setLastCallFnc('tobasket');
126 
127  $database = oxDb::getDb();
128  $database->startTransaction();
129  try {
130  $oBasketItem = $this->_addItems($aProducts);
131  //reserve active basket
132  if (oxRegistry::getConfig()->getConfigParam('blPsBasketReservationEnabled')) {
133  $basket = oxRegistry::getSession()->getBasket();
134  oxRegistry::getSession()->getBasketReservations()->reserveBasket($basket);
135  }
136  } catch (\Exception $exception) {
137  $database->rollbackTransaction();
138  unset($oBasketItem);
139  throw $exception;
140  }
141  $database->commitTransaction();
142 
143  // new basket item marker
144  if ($oBasketItem && $myConfig->getConfigParam('iNewBasketItemMessage') != 0) {
145  $oNewItem = new stdClass();
146  $oNewItem->sTitle = $oBasketItem->getTitle();
147  $oNewItem->sId = $oBasketItem->getProductId();
148  $oNewItem->dAmount = $oBasketItem->getAmount();
149  $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
150 
151  // passing article
152  oxRegistry::getSession()->setVariable('_newitem', $oNewItem);
153  }
154 
155 
156  // redirect to basket
157  return $this->_getRedirectUrl();
158  }
159  }
160 
172  public function changebasket(
173  $sProductId = null,
174  $dAmount = null,
175  $aSel = null,
176  $aPersParam = null,
177  $blOverride = true
178  ) {
179  if (!oxRegistry::getSession()->checkSessionChallenge()) {
180  return;
181  }
182 
183  // adding to basket is not allowed ?
184  if (oxRegistry::getUtils()->isSearchEngine()) {
185  return;
186  }
187 
188  // fetching item ID
189  if (!$sProductId) {
190  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
191 
192  if ($sBasketItemId) {
193  $oBasket = $this->getSession()->getBasket();
194  //take params
195  $aBasketContents = $oBasket->getContents();
196  $oItem = $aBasketContents[$sBasketItemId];
197 
198  $sProductId = isset($oItem) ? $oItem->getProductId() : null;
199  } else {
200  $sProductId = oxRegistry::getConfig()->getRequestParameter('aid');
201  }
202  }
203 
204  // fetching other needed info
205  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
206  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
207  $aPersParam = $aPersParam ? $aPersParam : oxRegistry::getConfig()->getRequestParameter('persparam');
208 
209  // adding articles
210  if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
211 
212  // information that last call was changebasket
213  $oBasket = $this->getSession()->getBasket();
214  $oBasket->onUpdate();
215 
216  $this->_setLastCallFnc('changebasket');
217 
218  $database = oxDb::getDb();
219  $database->startTransaction();
220  try {
221  $oBasketItem = $this->_addItems($aProducts);
222  // reserve active basket
223  if (oxRegistry::getConfig()->getConfigParam('blPsBasketReservationEnabled')) {
224  oxRegistry::getSession()->getBasketReservations()->reserveBasket($oBasket);
225  }
226  } catch (\Exception $exception) {
227  $database->rollbackTransaction();
228  unset($oBasketItem);
229  throw $exception;
230  }
231  $database->commitTransaction();
232  }
233 
234  }
235 
242  protected function _getRedirectUrl()
243  {
244 
245  // active class
246  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
247  $sClass = $sClass ? $sClass . '?' : 'start?';
248  $sPosition = '';
249 
250  // setting redirect parameters
251  foreach ($this->aRedirectParams as $sParamName) {
252  $sParamVal = oxRegistry::getConfig()->getRequestParameter($sParamName);
253  $sPosition .= $sParamVal ? $sParamName . '=' . $sParamVal . '&' : '';
254  }
255 
256  // special treatment
257  // search param
258  $sParam = rawurlencode(oxRegistry::getConfig()->getRequestParameter('searchparam', true));
259  $sPosition .= $sParam ? 'searchparam=' . $sParam . '&' : '';
260 
261  // current page number
262  $iPageNr = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
263  $sPosition .= ($iPageNr > 0) ? 'pgNr=' . $iPageNr . '&' : '';
264 
265  // reload and backbutton blocker
266  if ($this->getConfig()->getConfigParam('iNewBasketItemMessage') == 3) {
267 
268  // saving return to shop link to session
269  oxRegistry::getSession()->setVariable('_backtoshop', $sClass . $sPosition);
270 
271  // redirecting to basket
272  $sClass = 'basket?';
273  }
274 
275  return $sClass . $sPosition;
276  }
277 
290  protected function _getItems(
291  $sProductId = null,
292  $dAmount = null,
293  $aSel = null,
294  $aPersParam = null,
295  $blOverride = false
296  ) {
297  // collecting items to add
298  $aProducts = oxRegistry::getConfig()->getRequestParameter('aproducts');
299 
300  // collecting specified item
301  $sProductId = $sProductId ? $sProductId : oxRegistry::getConfig()->getRequestParameter('aid');
302  if ($sProductId) {
303 
304  // additionally fetching current product info
305  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
306 
307  // select lists
308  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
309 
310  // persistent parameters
311  if (empty($aPersParam)) {
312  $aPersParam = oxRegistry::getConfig()->getRequestParameter('persparam');
313  if (!is_array($aPersParam) || empty($aPersParam['details'])) {
314  $aPersParam = null;
315  }
316  }
317 
318  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
319 
320  $aProducts[$sProductId] = array('am' => $dAmount,
321  'sel' => $aSel,
322  'persparam' => $aPersParam,
323  'override' => $blOverride,
324  'basketitemid' => $sBasketItemId
325  );
326  }
327 
328  if (is_array($aProducts) && count($aProducts)) {
329 
330  if (oxRegistry::getConfig()->getRequestParameter('removeBtn') !== null) {
331  //setting amount to 0 if removing article from basket
332  foreach ($aProducts as $sProductId => $aProduct) {
333  if (isset($aProduct['remove']) && $aProduct['remove']) {
334  $aProducts[$sProductId]['am'] = 0;
335  } else {
336  unset ($aProducts[$sProductId]);
337  }
338  }
339  }
340 
341  return $aProducts;
342  }
343 
344  return false;
345  }
346 
355  protected function _addItems($aProducts)
356  {
357  $oActView = $this->getConfig()->getActiveView();
358  $sErrorDest = $oActView->getErrorDestination();
359 
360  $oBasket = $this->getSession()->getBasket();
361  $oBasketInfo = $oBasket->getBasketSummary();
362 
363  $basketItemAmounts = array();
364 
365  foreach ($aProducts as $sAddProductId => $aProductInfo) {
366 
367  $sProductId = isset($aProductInfo['aid']) ? $aProductInfo['aid'] : $sAddProductId;
368 
369  // collecting input
370  $oProduct = $oBasketInfo->aArticles[$sProductId];
371  $aProducts[$sAddProductId]['oldam'] = isset($oProduct) ? $oProduct : 0;
372 
373  $dAmount = isset($aProductInfo['am']) ? $aProductInfo['am'] : 0;
374  $aSelList = isset($aProductInfo['sel']) ? $aProductInfo['sel'] : null;
375  $aParams = $aProductInfo['persparam'];
376  $aPersParam = (isset($aParams) && is_array($aParams) && strlen($aParams['details'])) ? $aParams : null;
377  $blOverride = isset($aProductInfo['override']) ? $aProductInfo['override'] : null;
378  $blIsBundle = isset($aProductInfo['bundle']) ? true : false;
379  $sOldBasketItemId = isset($aProductInfo['basketitemid']) ? $aProductInfo['basketitemid'] : null;
380 
381  try {
382 
383  //If we already changed articles so they now exactly match existing ones,
384  //we need to make sure we get the amounts correct
385  if (isset($basketItemAmounts[$sOldBasketItemId])) {
386  $dAmount = $dAmount + $basketItemAmounts[$sOldBasketItemId];
387  }
388 
389  $oBasketItem = $oBasket->addToBasket(
390  $sProductId,
391  $dAmount,
392  $aSelList,
393  $aPersParam,
394  $blOverride,
395  $blIsBundle,
396  $sOldBasketItemId
397  );
398 
399  if (is_a($oBasketItem, 'oxBasketItem')) {
400  $basketItemId = $oBasketItem->getBasketItemKey();
401  }
402  if (!empty($basketItemId)) {
403  $basketItemAmounts[$basketItemId] += $dAmount;
404  }
405 
406  } catch (oxOutOfStockException $oEx) {
407  $oEx->setDestination($sErrorDest);
408  // #950 Change error destination to basket popup
409  if (!$sErrorDest && $this->getConfig()->getConfigParam('iNewBasketItemMessage') == 2) {
410  $sErrorDest = 'popup';
411  }
412  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
413  } catch (oxArticleInputException $oEx) {
414  //add to display at specific position
415  $oEx->setDestination($sErrorDest);
416  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
417  } catch (oxNoArticleException $oEx) {
418  //ignored, best solution F ?
419  }
420  if (!$oBasketItem) {
421  $oInfo = $oBasket->getBasketSummary();
422  $oProduct = $oInfo->aArticles[$sProductId];
423  $aProducts[$sAddProductId]['am'] = isset($oProduct) ? $oProduct : 0;
424  }
425  }
426 
427  //if basket empty remove posible gift card
428  if ($oBasket->getProductsCount() == 0) {
429  $oBasket->setCardId(null);
430  }
431 
432  // information that last call was tobasket
433  $this->_setLastCall($this->_getLastCallFnc(), $aProducts, $oBasketInfo);
434 
435  return $oBasketItem;
436  }
437 
445  protected function _setLastCall($sCallName, $aProductInfo, $aBasketInfo)
446  {
447  oxRegistry::getSession()->setVariable('aLastcall', array($sCallName => $aProductInfo));
448  }
449 
455  protected function _setLastCallFnc($sCallName)
456  {
457  $this->_sLastCallFnc = $sCallName;
458  }
459 
465  protected function _getLastCallFnc()
466  {
467  return $this->_sLastCallFnc;
468  }
469 
475  public function isRootCatChanged()
476  {
477  // in Basket
478  $oBasket = $this->getSession()->getBasket();
479  if ($oBasket->showCatChangeWarning()) {
480  $oBasket->setCatChangeWarningState(false);
481 
482  return true;
483  }
484 
485  // in Category, only then category is empty ant not equal to default category
486  $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
487  $sActCat = oxRegistry::getConfig()->getRequestParameter('cnid');
488  $oActCat = oxnew('oxcategory');
489  if ($sActCat && $sActCat != $sDefCat && $oActCat->load($sActCat)) {
490  $sActRoot = $oActCat->oxcategories__oxrootid->value;
491  if ($oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId()) {
492  return true;
493  }
494  }
495 
496  return false;
497  }
498 
507  public function executeuserchoice()
508  {
509 
510  // redirect to basket
511  if (oxRegistry::getConfig()->getRequestParameter("tobasket")) {
512  return "basket";
513  } else {
514  // clear basket
515  $this->getSession()->getBasket()->deleteBasket();
516  $this->getParent()->setRootCatChanged(false);
517  }
518  }
519 
520 }