OXID eShop CE  4.9.9
 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 fetching 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  $basketItemAmounts = array();
327 
328  foreach ($aProducts as $sAddProductId => $aProductInfo) {
329 
330  $sProductId = isset($aProductInfo['aid']) ? $aProductInfo['aid'] : $sAddProductId;
331 
332  // collecting input
333  $oProduct = $oBasketInfo->aArticles[$sProductId];
334  $aProducts[$sAddProductId]['oldam'] = isset($oProduct) ? $oProduct : 0;
335 
336  $dAmount = isset($aProductInfo['am']) ? $aProductInfo['am'] : 0;
337  $aSelList = isset($aProductInfo['sel']) ? $aProductInfo['sel'] : null;
338  $aParams = $aProductInfo['persparam'];
339  $aPersParam = (isset($aParams) && is_array($aParams) && strlen($aParams['details'])) ? $aParams : null;
340  $blOverride = isset($aProductInfo['override']) ? $aProductInfo['override'] : null;
341  $blIsBundle = isset($aProductInfo['bundle']) ? true : false;
342  $sOldBasketItemId = isset($aProductInfo['basketitemid']) ? $aProductInfo['basketitemid'] : null;
343 
344  try {
345 
346  //If we already changed articles so they now exactly match existing ones,
347  //we need to make sure we get the amounts correct
348  if (isset($basketItemAmounts[$sOldBasketItemId])) {
349  $dAmount = $dAmount + $basketItemAmounts[$sOldBasketItemId];
350  }
351 
352  $oBasketItem = $oBasket->addToBasket(
353  $sProductId,
354  $dAmount,
355  $aSelList,
356  $aPersParam,
357  $blOverride,
358  $blIsBundle,
359  $sOldBasketItemId
360  );
361 
362  if (is_a($oBasketItem, 'oxBasketItem')) {
363  $basketItemId = $oBasketItem->getBasketItemKey();
364  }
365  if (!empty($basketItemId)) {
366  $basketItemAmounts[$basketItemId] += $dAmount;
367  }
368 
369  } catch (oxOutOfStockException $oEx) {
370  $oEx->setDestination($sErrorDest);
371  // #950 Change error destination to basket popup
372  if (!$sErrorDest && $this->getConfig()->getConfigParam('iNewBasketItemMessage') == 2) {
373  $sErrorDest = 'popup';
374  }
375  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
376  } catch (oxArticleInputException $oEx) {
377  //add to display at specific position
378  $oEx->setDestination($sErrorDest);
379  oxRegistry::get("oxUtilsView")->addErrorToDisplay($oEx, false, (bool) $sErrorDest, $sErrorDest);
380  } catch (oxNoArticleException $oEx) {
381  //ignored, best solution F ?
382  }
383  if (!$oBasketItem) {
384  $oInfo = $oBasket->getBasketSummary();
385  $oProduct = $oInfo->aArticles[$sProductId];
386  $aProducts[$sAddProductId]['am'] = isset($oProduct) ? $oProduct : 0;
387  }
388  }
389 
390  //if basket empty remove posible gift card
391  if ($oBasket->getProductsCount() == 0) {
392  $oBasket->setCardId(null);
393  }
394 
395  // information that last call was tobasket
396  $this->_setLastCall($this->_getLastCallFnc(), $aProducts, $oBasketInfo);
397 
398  return $oBasketItem;
399  }
400 
408  protected function _setLastCall($sCallName, $aProductInfo, $aBasketInfo)
409  {
410  oxRegistry::getSession()->setVariable('aLastcall', array($sCallName => $aProductInfo));
411  }
412 
418  protected function _setLastCallFnc($sCallName)
419  {
420  $this->_sLastCallFnc = $sCallName;
421  }
422 
428  protected function _getLastCallFnc()
429  {
430  return $this->_sLastCallFnc;
431  }
432 
438  public function isRootCatChanged()
439  {
440  // in Basket
441  $oBasket = $this->getSession()->getBasket();
442  if ($oBasket->showCatChangeWarning()) {
443  $oBasket->setCatChangeWarningState(false);
444 
445  return true;
446  }
447 
448  // in Category, only then category is empty ant not equal to default category
449  $sDefCat = oxRegistry::getConfig()->getActiveShop()->oxshops__oxdefcat->value;
450  $sActCat = oxRegistry::getConfig()->getRequestParameter('cnid');
451  $oActCat = oxnew('oxcategory');
452  if ($sActCat && $sActCat != $sDefCat && $oActCat->load($sActCat)) {
453  $sActRoot = $oActCat->oxcategories__oxrootid->value;
454  if ($oBasket->getBasketRootCatId() && $sActRoot != $oBasket->getBasketRootCatId()) {
455  return true;
456  }
457  }
458 
459  return false;
460  }
461 
470  public function executeuserchoice()
471  {
472 
473  // redirect to basket
474  if (oxRegistry::getConfig()->getRequestParameter("tobasket")) {
475  return "basket";
476  } else {
477  // clear basket
478  $this->getSession()->getBasket()->deleteBasket();
479  $this->getParent()->setRootCatChanged(false);
480  }
481  }
482 
483 }