OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxuserbasket.php
Go to the documentation of this file.
1 <?php
2 
11 class oxUserBasket extends oxBase
12 {
13 
19  protected $_aSkipSaveFields = array('oxcreate', 'oxtimestamp');
20 
26  protected $_sClassName = 'oxUserbasket';
27 
33  protected $_aBasketItems = null;
34 
40  protected $_blNewBasket = false;
41 
47  public function __construct()
48  {
50  $this->init('oxuserbaskets');
51  }
52 
58  protected function _insert()
59  {
60  // marking basket as not new any more
61  $this->_blNewBasket = false;
62 
63  if (!isset($this->oxuserbaskets__oxpublic->value)) {
64  $this->oxuserbaskets__oxpublic = new oxField(1, oxField::T_RAW);
65  }
66 
67  $iTime = oxRegistry::get("oxUtilsDate")->getTime();
68  $this->oxuserbaskets__oxupdate = new oxField($iTime);
69 
70  return parent::_insert();
71  }
72 
77  public function setIsNewBasket()
78  {
79  $this->_blNewBasket = true;
80  $iTime = oxRegistry::get("oxUtilsDate")->getTime();
81  $this->oxuserbaskets__oxupdate = new oxField($iTime);
82  }
83 
89  public function isNewBasket()
90  {
91  return $this->_blNewBasket;
92  }
93 
99  public function isEmpty()
100  {
101  if ($this->isNewBasket() || $this->getItemCount() < 1) {
102  return true;
103  }
104 
105  return false;
106  }
107 
113  public function getArticles()
114  {
115  $aRes = array();
116  $aItems = $this->getItems();
117  if (is_array($aItems)) {
118  foreach ($aItems as $sId => $oItem) {
119  $oArticle = $oItem->getArticle($sId);
120  $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList(), $oItem->getPersParams())] = $oArticle;
121  }
122  }
123 
124  return $aRes;
125  }
126 
135  public function getItems($blReload = false, $blActiveCheck = true)
136  {
137  // cached ?
138  if ($this->_aBasketItems !== null && !$blReload) {
139  return $this->_aBasketItems;
140  }
141 
142  // initializing
143  $this->_aBasketItems = array();
144 
145  // loading basket items
146  $oArticle = oxNew('oxarticle');
147  $sViewName = $oArticle->getViewName();
148 
149  $sSelect = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
150  if ($blActiveCheck) {
151  $sSelect .= 'and ' . $oArticle->getSqlActiveSnippet() . ' ';
152  }
153  $sSelect .= "where oxuserbasketitems.oxbasketid = '" . $this->getId() . "' and $sViewName.oxid is not null ";
154 
155  $sSelect .= " order by oxartnum, oxsellist, oxpersparam ";
156 
157  $oItems = oxNew('oxlist');
158  $oItems->init('oxuserbasketitem');
159  $oItems->selectstring($sSelect);
160 
161  foreach ($oItems as $oItem) {
162  $sKey = $this->_getItemKey($oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList(), $oItem->getPersParams());
163  $this->_aBasketItems[$sKey] = $oItem;
164  }
165 
166  return $this->_aBasketItems;
167  }
168 
179  protected function _createItem($sProductId, $aSelList = null, $aPersParams = null)
180  {
181  $oNewItem = oxNew('oxuserbasketitem');
182  $oNewItem->oxuserbasketitems__oxartid = new oxField($sProductId, oxField::T_RAW);
183  $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
184  if ($aPersParams && count($aPersParams)) {
185  $oNewItem->setPersParams($aPersParams);
186  }
187 
188  if (!$aSelList) {
189  $oArticle = oxNew('oxArticle');
190  $oArticle->load($sProductId);
191  $aSelectLists = $oArticle->getSelectLists();
192  if (($iSelCnt = count($aSelectLists))) {
193  $aSelList = array_fill(0, $iSelCnt, '0');
194  }
195  }
196 
197  $oNewItem->setSelList($aSelList);
198 
199  return $oNewItem;
200  }
201 
202 
213  public function getItem($sProductId, $aSelList, $aPersParams = null)
214  {
215  // loading basket item list
216  $aItems = $this->getItems();
217  $sItemKey = $this->_getItemKey($sProductId, $aSelList, $aPersParams);
218  $oItem = null;
219  // returning existing item
220  if (isset($aItems[$sProductId])) {
221  $oItem = $aItems[$sProductId];
222  } elseif (isset($aItems[$sItemKey])) {
223  $oItem = $aItems[$sItemKey];
224  } else {
225  $oItem = $this->_createItem($sProductId, $aSelList, $aPersParams);
226  }
227 
228  return $oItem;
229  }
230 
240  protected function _getItemKey($sProductId, $aSel = null, $aPersParam = null)
241  {
242  $aSel = ($aSel != null) ? $aSel : array(0 => '0');
243 
244  return md5($sProductId . '|' . serialize($aSel) . '|' . serialize($aPersParam));
245  }
246 
254  public function getItemCount($blReload = false)
255  {
256  return count($this->getItems($blReload));
257  }
258 
271  public function addItemToBasket($sProductId = null, $dAmount = null, $aSel = null, $blOverride = false, $aPersParam = null)
272  {
273  // basket info is only written in DB when something is in it
274  if ($this->_blNewBasket) {
275  $this->save();
276  }
277 
278  if (($oUserBasketItem = $this->getItem($sProductId, $aSel, $aPersParam))) {
279  // updating object info and adding (if not yet added) item into basket items array
280  if (!$blOverride && !empty($oUserBasketItem->oxuserbasketitems__oxamount->value)) {
281  $dAmount += $oUserBasketItem->oxuserbasketitems__oxamount->value;
282  }
283 
284  if (!$dAmount) {
285  // if amount = 0 the means remove it
286  $oUserBasketItem->delete();
287  if (isset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)])) {
288  unset($this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)]);
289  }
290  } else {
291  $oUserBasketItem->oxuserbasketitems__oxamount = new oxField($dAmount, oxField::T_RAW);
292  $oUserBasketItem->save();
293 
294  $this->_aBasketItems[$this->_getItemKey($sProductId, $aSel, $aPersParam)] = $oUserBasketItem;
295  }
296 
297  //update timestamp
298  $this->oxuserbaskets__oxupdate = new oxField(oxRegistry::get("oxUtilsDate")->getTime());
299  $this->save();
300 
301  return $dAmount;
302  }
303  }
304 
312  public function delete($sOXID = null)
313  {
314  if (!$sOXID) {
315  $sOXID = $this->getId();
316  }
317 
318  $blDelete = false;
319  if ($sOXID && ($blDelete = parent::delete($sOXID))) {
320  // cleaning up related data
321  $oDb = oxDb::getDb();
322  $sQ = "delete from oxuserbasketitems where oxbasketid = " . $oDb->quote($sOXID);
323  $oDb->execute($sQ);
324  }
325 
326  return $blDelete;
327  }
328 
334  public function isVisible()
335  {
336  $oActivUser = $this->getConfig()->getUser();
337  $sActivUserId = null;
338  if ($oActivUser) {
339  $sActivUserId = $oActivUser->getId();
340  }
341 
342  $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
343  ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
344 
345  return $blIsVisible;
346  }
347 }