OXID eShop CE  4.10.1
 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  $oBasketItem = $this->_addItems($aProducts);
127 
128  // new basket item marker
129  if ($oBasketItem && $myConfig->getConfigParam('iNewBasketItemMessage') != 0) {
130  $oNewItem = new stdClass();
131  $oNewItem->sTitle = $oBasketItem->getTitle();
132  $oNewItem->sId = $oBasketItem->getProductId();
133  $oNewItem->dAmount = $oBasketItem->getAmount();
134  $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
135 
136  // passing article
137  oxRegistry::getSession()->setVariable('_newitem', $oNewItem);
138  }
139 
140 
141  // redirect to basket
142  return $this->_getRedirectUrl();
143  }
144  }
145 
157  public function changebasket(
158  $sProductId = null,
159  $dAmount = null,
160  $aSel = null,
161  $aPersParam = null,
162  $blOverride = true
163  ) {
164  // adding to basket is not allowed ?
165  if (oxRegistry::getUtils()->isSearchEngine()) {
166  return;
167  }
168 
169  // fetching item ID
170  if (!$sProductId) {
171  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
172 
173  if ($sBasketItemId) {
174  $oBasket = $this->getSession()->getBasket();
175  //take params
176  $aBasketContents = $oBasket->getContents();
177  $oItem = $aBasketContents[$sBasketItemId];
178 
179  $sProductId = isset($oItem) ? $oItem->getProductId() : null;
180  } else {
181  $sProductId = oxRegistry::getConfig()->getRequestParameter('aid');
182  }
183  }
184 
185  // fetching other needed info
186  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
187  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
188  $aPersParam = $aPersParam ? $aPersParam : oxRegistry::getConfig()->getRequestParameter('persparam');
189 
190  // adding articles
191  if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
192 
193  // information that last call was changebasket
194  $oBasket = $this->getSession()->getBasket();
195  $oBasket->onUpdate();
196 
197  $this->_setLastCallFnc('changebasket');
198  $oBasketItem = $this->_addItems($aProducts);
199  }
200 
201  }
202 
209  protected function _getRedirectUrl()
210  {
211 
212  // active class
213  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
214  $sClass = $sClass ? $sClass . '?' : 'start?';
215  $sPosition = '';
216 
217  // setting redirect parameters
218  foreach ($this->aRedirectParams as $sParamName) {
219  $sParamVal = oxRegistry::getConfig()->getRequestParameter($sParamName);
220  $sPosition .= $sParamVal ? $sParamName . '=' . $sParamVal . '&' : '';
221  }
222 
223  // special treatment
224  // search param
225  $sParam = rawurlencode(oxRegistry::getConfig()->getRequestParameter('searchparam', true));
226  $sPosition .= $sParam ? 'searchparam=' . $sParam . '&' : '';
227 
228  // current page number
229  $iPageNr = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
230  $sPosition .= ($iPageNr > 0) ? 'pgNr=' . $iPageNr . '&' : '';
231 
232  // reload and backbutton blocker
233  if ($this->getConfig()->getConfigParam('iNewBasketItemMessage') == 3) {
234 
235  // saving return to shop link to session
236  oxRegistry::getSession()->setVariable('_backtoshop', $sClass . $sPosition);
237 
238  // redirecting to basket
239  $sClass = 'basket?';
240  }
241 
242  return $sClass . $sPosition;
243  }
244 
257  protected function _getItems(
258  $sProductId = null,
259  $dAmount = null,
260  $aSel = null,
261  $aPersParam = null,
262  $blOverride = false
263  ) {
264  // collecting items to add
265  $aProducts = oxRegistry::getConfig()->getRequestParameter('aproducts');
266 
267  // collecting specified item
268  $sProductId = $sProductId ? $sProductId : oxRegistry::getConfig()->getRequestParameter('aid');
269  if ($sProductId) {
270 
271  // additionally fetching current product info
272  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
273 
274  // select lists
275  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
276 
277  // persistent parameters
278  if (empty($aPersParam)) {
279  $aPersParam = oxRegistry::getConfig()->getRequestParameter('persparam');
280  if (!is_array($aPersParam) || empty($aPersParam['details'])) {
281  $aPersParam = null;
282  }
283  }
284 
285  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
286 
287  $aProducts[$sProductId] = array('am' => $dAmount,
288  'sel' => $aSel,
289  'persparam' => $aPersParam,
290  'override' => $blOverride,
291  'basketitemid' => $sBasketItemId
292  );
293  }
294 
295  if (is_array($aProducts) && count($aProducts)) {
296 
297  if (oxRegistry::getConfig()->getRequestParameter('removeBtn') !== null) {
298  //setting amount to 0 if removing article from basket
299  foreach ($aProducts as $sProductId => $aProduct) {
300  if (isset($aProduct['remove']) && $aProduct['remove']) {
301  $aProducts[$sProductId]['am'] = 0;
302  } else {
303  unset ($aProducts[$sProductId]);
304  }
305  }
306  }
307 
308  return $aProducts;
309  }
310 
311  return false;
312  }
313 
322  protected function _addItems($aProducts)
323  {
324  $oActView = $this->getConfig()->getActiveView();
325  $sErrorDest = $oActView->getErrorDestination();
326 
327  $oBasket = $this->getSession()->getBasket();
328  $oBasketInfo = $oBasket->getBasketSummary();
329 
330  $basketItemAmounts = array();
331 
332  foreach ($aProducts as $sAddProductId => $aProductInfo) {
333 
334  $sProductId = isset($aProductInfo['aid']) ? $aProductInfo['aid'] : $sAddProductId;
335 
336  // collecting input
337  $oProduct = $oBasketInfo->aArticles[$sProductId];
338  $aProducts[$sAddProductId]['oldam'] = isset($oProduct) ? $oProduct : 0;
339 
340  $dAmount = isset($aProductInfo['am']) ? $aProductInfo['am'] : 0;
341  $aSelList = isset($aProductInfo['sel']) ? $aProductInfo['sel'] : null;
342  $aParams = $aProductInfo['persparam'];
343  $aPersParam = (isset($aParams) && is_array($aParams) && strlen($aParams['details'])) ? $aParams : null;
344  $blOverride = isset($aProductInfo['override']) ? $aProductInfo['override'] : null;
345  $blIsBundle = isset($aProductInfo['bundle']) ? true : false;
346  $sOldBasketItemId = isset($aProductInfo['basketitemid']) ? $aProductInfo['basketitemid'] : null;
347 
348  try {
349 
350  //If we already changed articles so they now exactly match existing ones,
351  //we need to make sure we get the amounts correct
352  if (isset($basketItemAmounts[$sOldBasketItemId])) {
353  $dAmount = $dAmount + $basketItemAmounts[$sOldBasketItemId];
354  }
355 
356  $oBasketItem = $oBasket->addToBasket(
357  $sProductId,
358  $dAmount,
359  $aSelList,
360  $aPersParam,
361  $blOverride,
362  $blIsBundle,
363  $sOldBasketItemId
364  );
365 
366  if (is_a($oBasketItem, 'oxBasketItem')) {
367  $basketItemId = $oBasketItem->getBasketItemKey();
368  }
369  if (!empty($basketItemId)) {
370  $basketItemAmounts[$basketItemId] += $dAmount;
371  }
372 
373  } catch (oxOutOfStockException $oEx) {
374  $oEx->setDestination($sErrorDest);
375  // #950 Change error destination to basket popup
376  if (!$sErrorDest && $this->getConfig()->getConfigParam('iNewBasketItemMessage') == 2) {
377  $sErrorDest = 'popup';
378  }
379  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
380  } catch (oxArticleInputException $oEx) {
381  //add to display at specific position
382  $oEx->setDestination($sErrorDest);
383  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
384  } catch (oxNoArticleException $oEx) {
385  //ignored, best solution F ?
386  }
387  if (!$oBasketItem) {
388  $oInfo = $oBasket->getBasketSummary();
389  $oProduct = $oInfo->aArticles[$sProductId];
390  $aProducts[$sAddProductId]['am'] = isset($oProduct) ? $oProduct : 0;
391  }
392  }
393 
394  //if basket empty remove posible gift card
395  if ($oBasket->getProductsCount() == 0) {
396  $oBasket->setCardId(null);
397  }
398 
399  // information that last call was tobasket
400  $this->_setLastCall($this->_getLastCallFnc(), $aProducts, $oBasketInfo);
401 
402  return $oBasketItem;
403  }
404 
412  protected function _setLastCall($sCallName, $aProductInfo, $aBasketInfo)
413  {
414  oxRegistry::getSession()->setVariable('aLastcall', array($sCallName => $aProductInfo));
415  }
416 
422  protected function _setLastCallFnc($sCallName)
423  {
424  $this->_sLastCallFnc = $sCallName;
425  }
426 
432  protected function _getLastCallFnc()
433  {
434  return $this->_sLastCallFnc;
435  }
436 
442  public function isRootCatChanged()
443  {
444  // in Basket
445  $oBasket = $this->getSession()->getBasket();
446  if ($oBasket->showCatChangeWarning()) {
447  $oBasket->setCatChangeWarningState(false);
448 
449  return true;
450  }
451 
452  // in Category, only then category is empty ant not equal to default category
453  $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
454  $sActCat = oxRegistry::getConfig()->getRequestParameter('cnid');
455  $oActCat = oxnew('oxcategory');
456  if ($sActCat && $sActCat != $sDefCat && $oActCat->load($sActCat)) {
457  $sActRoot = $oActCat->oxcategories__oxrootid->value;
458  if ($oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId()) {
459  return true;
460  }
461  }
462 
463  return false;
464  }
465 
474  public function executeuserchoice()
475  {
476 
477  // redirect to basket
478  if (oxRegistry::getConfig()->getRequestParameter("tobasket")) {
479  return "basket";
480  } else {
481  // clear basket
482  $this->getSession()->getBasket()->deleteBasket();
483  $this->getParent()->setRootCatChanged(false);
484  }
485  }
486 
487 }