OXID eShop CE  4.9.6
 All Classes 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  'searchtag', // search tag
41  'searchrecomm', // search recomendation
42  'recommid' // recomm. list id
43  );
44 
48  public function init()
49  {
50  $oConfig = $this->getConfig();
51  if ($oConfig->getConfigParam('blPsBasketReservationEnabled')) {
52  if ($oReservations = $this->getSession()->getBasketReservations()) {
53  if (!$oReservations->getTimeLeft()) {
54  $oBasket = $this->getSession()->getBasket();
55  if ($oBasket && $oBasket->getProductsCount()) {
56  $oBasket->deleteBasket();
57  }
58  }
59  $iLimit = (int) $oConfig->getConfigParam('iBasketReservationCleanPerRequest');
60  if (!$iLimit) {
61  $iLimit = 200;
62  }
63  $oReservations->discardUnusedReservations($iLimit);
64  }
65  }
66 
67  parent::init();
68 
69  // Basket exclude
70  if ($this->getConfig()->getConfigParam('blBasketExcludeEnabled')) {
71  if ($oBasket = $this->getSession()->getBasket()) {
72  $this->getParent()->setRootCatChanged($this->isRootCatChanged() && $oBasket->getContents());
73  }
74  }
75  }
76 
83  public function render()
84  {
85  // recalculating
86  if ($oBasket = $this->getSession()->getBasket()) {
87  $oBasket->calculateBasket(false);
88  }
89 
91 
92  return $oBasket;
93  }
94 
110  public function tobasket($sProductId = null, $dAmount = null, $aSel = null, $aPersParam = null, $blOverride = false)
111  {
112  // adding to basket is not allowed ?
113  $myConfig = $this->getConfig();
114  if (oxRegistry::getUtils()->isSearchEngine()) {
115  return;
116  }
117 
118  // adding articles
119  if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
120 
121  $this->_setLastCallFnc('tobasket');
122  $oBasketItem = $this->_addItems($aProducts);
123 
124  // new basket item marker
125  if ($oBasketItem && $myConfig->getConfigParam('iNewBasketItemMessage') != 0) {
126  $oNewItem = new stdClass();
127  $oNewItem->sTitle = $oBasketItem->getTitle();
128  $oNewItem->sId = $oBasketItem->getProductId();
129  $oNewItem->dAmount = $oBasketItem->getAmount();
130  $oNewItem->dBundledAmount = $oBasketItem->getdBundledAmount();
131 
132  // passing article
133  oxRegistry::getSession()->setVariable('_newitem', $oNewItem);
134  }
135 
136 
137  // redirect to basket
138  return $this->_getRedirectUrl();
139  }
140  }
141 
153  public function changebasket(
154  $sProductId = null,
155  $dAmount = null,
156  $aSel = null,
157  $aPersParam = null,
158  $blOverride = true
159  ) {
160  // adding to basket is not allowed ?
161  if (oxRegistry::getUtils()->isSearchEngine()) {
162  return;
163  }
164 
165  // fetching item ID
166  if (!$sProductId) {
167  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
168 
169  if ($sBasketItemId) {
170  $oBasket = $this->getSession()->getBasket();
171  //take params
172  $aBasketContents = $oBasket->getContents();
173  $oItem = $aBasketContents[$sBasketItemId];
174 
175  $sProductId = isset($oItem) ? $oItem->getProductId() : null;
176  } else {
177  $sProductId = oxRegistry::getConfig()->getRequestParameter('aid');
178  }
179  }
180 
181  // fetching other needed info
182  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
183  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
184  $aPersParam = $aPersParam ? $aPersParam : oxRegistry::getConfig()->getRequestParameter('persparam');
185 
186  // adding articles
187  if ($aProducts = $this->_getItems($sProductId, $dAmount, $aSel, $aPersParam, $blOverride)) {
188 
189  // information that last call was changebasket
190  $oBasket = $this->getSession()->getBasket();
191  $oBasket->onUpdate();
192 
193  $this->_setLastCallFnc('changebasket');
194  $oBasketItem = $this->_addItems($aProducts);
195  }
196 
197  }
198 
205  protected function _getRedirectUrl()
206  {
207 
208  // active class
209  $sClass = oxRegistry::getConfig()->getRequestParameter('cl');
210  $sClass = $sClass ? $sClass . '?' : 'start?';
211  $sPosition = '';
212 
213  // setting redirect parameters
214  foreach ($this->aRedirectParams as $sParamName) {
215  $sParamVal = oxRegistry::getConfig()->getRequestParameter($sParamName);
216  $sPosition .= $sParamVal ? $sParamName . '=' . $sParamVal . '&' : '';
217  }
218 
219  // special treatment
220  // search param
221  $sParam = rawurlencode(oxRegistry::getConfig()->getRequestParameter('searchparam', true));
222  $sPosition .= $sParam ? 'searchparam=' . $sParam . '&' : '';
223 
224  // current page number
225  $iPageNr = (int) oxRegistry::getConfig()->getRequestParameter('pgNr');
226  $sPosition .= ($iPageNr > 0) ? 'pgNr=' . $iPageNr . '&' : '';
227 
228  // reload and backbutton blocker
229  if ($this->getConfig()->getConfigParam('iNewBasketItemMessage') == 3) {
230 
231  // saving return to shop link to session
232  oxRegistry::getSession()->setVariable('_backtoshop', $sClass . $sPosition);
233 
234  // redirecting to basket
235  $sClass = 'basket?';
236  }
237 
238  return $sClass . $sPosition;
239  }
240 
253  protected function _getItems(
254  $sProductId = null,
255  $dAmount = null,
256  $aSel = null,
257  $aPersParam = null,
258  $blOverride = false
259  ) {
260  // collecting items to add
261  $aProducts = oxRegistry::getConfig()->getRequestParameter('aproducts');
262 
263  // collecting specified item
264  $sProductId = $sProductId ? $sProductId : oxRegistry::getConfig()->getRequestParameter('aid');
265  if ($sProductId) {
266 
267  // additionally fething current product info
268  $dAmount = isset($dAmount) ? $dAmount : oxRegistry::getConfig()->getRequestParameter('am');
269 
270  // select lists
271  $aSel = isset($aSel) ? $aSel : oxRegistry::getConfig()->getRequestParameter('sel');
272 
273  // persistent parameters
274  if (empty($aPersParam)) {
275  $aPersParam = oxRegistry::getConfig()->getRequestParameter('persparam');
276  if (!is_array($aPersParam) || empty($aPersParam['details'])) {
277  $aPersParam = null;
278  }
279  }
280 
281  $sBasketItemId = oxRegistry::getConfig()->getRequestParameter('bindex');
282 
283  $aProducts[$sProductId] = array('am' => $dAmount,
284  'sel' => $aSel,
285  'persparam' => $aPersParam,
286  'override' => $blOverride,
287  'basketitemid' => $sBasketItemId
288  );
289  }
290 
291  if (is_array($aProducts) && count($aProducts)) {
292 
293  if (oxRegistry::getConfig()->getRequestParameter('removeBtn') !== null) {
294  //setting amount to 0 if removing article from basket
295  foreach ($aProducts as $sProductId => $aProduct) {
296  if (isset($aProduct['remove']) && $aProduct['remove']) {
297  $aProducts[$sProductId]['am'] = 0;
298  } else {
299  unset ($aProducts[$sProductId]);
300  }
301  }
302  }
303 
304  return $aProducts;
305  }
306 
307  return false;
308  }
309 
318  protected function _addItems($aProducts)
319  {
320  $oActView = $this->getConfig()->getActiveView();
321  $sErrorDest = $oActView->getErrorDestination();
322 
323  $oBasket = $this->getSession()->getBasket();
324  $oBasketInfo = $oBasket->getBasketSummary();
325 
326  foreach ($aProducts as $sAddProductId => $aProductInfo) {
327 
328  $sProductId = isset($aProductInfo['aid']) ? $aProductInfo['aid'] : $sAddProductId;
329 
330  // collecting input
331  $oProduct = $oBasketInfo->aArticles[$sProductId];
332  $aProducts[$sAddProductId]['oldam'] = isset($oProduct) ? $oProduct : 0;
333 
334  $dAmount = isset($aProductInfo['am']) ? $aProductInfo['am'] : 0;
335  $aSelList = isset($aProductInfo['sel']) ? $aProductInfo['sel'] : null;
336  $aParams = $aProductInfo['persparam'];
337  $aPersParam = (isset($aParams) && is_array($aParams) && strlen($aParams['details'])) ? $aParams : null;
338  $blOverride = isset($aProductInfo['override']) ? $aProductInfo['override'] : null;
339  $blIsBundle = isset($aProductInfo['bundle']) ? true : false;
340  $sOldBasketItemId = isset($aProductInfo['basketitemid']) ? $aProductInfo['basketitemid'] : null;
341 
342  try {
343  $oBasketItem = $oBasket->addToBasket(
344  $sProductId,
345  $dAmount,
346  $aSelList,
347  $aPersParam,
348  $blOverride,
349  $blIsBundle,
350  $sOldBasketItemId
351  );
352  } catch (oxOutOfStockException $oEx) {
353  $oEx->setDestination($sErrorDest);
354  // #950 Change error destination to basket popup
355  if (!$sErrorDest && $this->getConfig()->getConfigParam('iNewBasketItemMessage') == 2) {
356  $sErrorDest = 'popup';
357  }
358  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
359  } catch (oxArticleInputException $oEx) {
360  //add to display at specific position
361  $oEx->setDestination($sErrorDest);
362  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
363  } catch (oxNoArticleException $oEx) {
364  //ignored, best solution F ?
365  }
366  if (!$oBasketItem) {
367  $oInfo = $oBasket->getBasketSummary();
368  $oProduct = $oInfo->aArticles[$sProductId];
369  $aProducts[$sAddProductId]['am'] = isset($oProduct) ? $oProduct : 0;
370  }
371  }
372 
373  //if basket empty remove posible gift card
374  if ($oBasket->getProductsCount() == 0) {
375  $oBasket->setCardId(null);
376  }
377 
378  // information that last call was tobasket
379  $this->_setLastCall($this->_getLastCallFnc(), $aProducts, $oBasketInfo);
380 
381  return $oBasketItem;
382  }
383 
391  protected function _setLastCall($sCallName, $aProductInfo, $aBasketInfo)
392  {
393  oxRegistry::getSession()->setVariable('aLastcall', array($sCallName => $aProductInfo));
394  }
395 
401  protected function _setLastCallFnc($sCallName)
402  {
403  $this->_sLastCallFnc = $sCallName;
404  }
405 
411  protected function _getLastCallFnc()
412  {
413  return $this->_sLastCallFnc;
414  }
415 
421  public function isRootCatChanged()
422  {
423  // in Basket
424  $oBasket = $this->getSession()->getBasket();
425  if ($oBasket->showCatChangeWarning()) {
426  $oBasket->setCatChangeWarningState(false);
427 
428  return true;
429  }
430 
431  // in Category, only then category is empty ant not equal to default category
432  $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
433  $sActCat = oxRegistry::getConfig()->getRequestParameter('cnid');
434  $oActCat = oxnew('oxcategory');
435  if ($sActCat && $sActCat != $sDefCat && $oActCat->load($sActCat)) {
436  $sActRoot = $oActCat->oxcategories__oxrootid->value;
437  if ($oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId()) {
438  return true;
439  }
440  }
441 
442  return false;
443  }
444 
453  public function executeuserchoice()
454  {
455 
456  // redirect to basket
457  if (oxRegistry::getConfig()->getRequestParameter("tobasket")) {
458  return "basket";
459  } else {
460  // clear basket
461  $this->getSession()->getBasket()->deleteBasket();
462  $this->getParent()->setRootCatChanged(false);
463  }
464  }
465 
466 }