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