OXID eShop CE  4.8.12
 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 
79  public function setIsNewBasket()
80  {
81  $this->_blNewBasket = true;
82  $iTime = oxRegistry::get("oxUtilsDate")->getTime();
83  $this->oxuserbaskets__oxupdate = new oxField( $iTime );
84  }
85 
91  public function isNewBasket()
92  {
93  return $this->_blNewBasket;
94  }
95 
101  public function isEmpty()
102  {
103  if ( $this->isNewBasket() || $this->getItemCount() < 1 ) {
104  return true;
105  }
106 
107  return false;
108  }
109 
115  public function getArticles()
116  {
117  $aRes = array();
118  $aItems = $this->getItems();
119  if ( is_array( $aItems ) ) {
120  foreach ( $aItems as $sId => $oItem ) {
121  $oArticle = $oItem->getArticle( $sId );
122  $aRes[$this->_getItemKey($oArticle->getId(), $oItem->getSelList(), $oItem->getPersParams())] = $oArticle;
123  }
124  }
125  return $aRes;
126  }
127 
136  public function getItems( $blReload = false, $blActiveCheck = true )
137  {
138  // cached ?
139  if ( $this->_aBasketItems !== null && !$blReload ) {
140  return $this->_aBasketItems;
141  }
142 
143  // initializing
144  $this->_aBasketItems = array();
145 
146  // loading basket items
147  $oArticle = oxNew( 'oxarticle' );
148  $sViewName = $oArticle->getViewName();
149 
150  $sSelect = "select oxuserbasketitems.* from oxuserbasketitems left join $sViewName on oxuserbasketitems.oxartid = $sViewName.oxid ";
151  if ($blActiveCheck) {
152  $sSelect .= 'and '.$oArticle->getSqlActiveSnippet().' ';
153  }
154  $sSelect .= "where oxuserbasketitems.oxbasketid = '".$this->getId()."' and $sViewName.oxid is not null ";
155 
156  $sSelect .= " order by oxartnum, oxsellist, oxpersparam ";
157 
158  $oItems = oxNew( 'oxlist' );
159  $oItems->init( 'oxuserbasketitem' );
160  $oItems->selectstring( $sSelect );
161 
162  foreach ( $oItems as $oItem ) {
163  $sKey = $this->_getItemKey( $oItem->oxuserbasketitems__oxartid->value, $oItem->getSelList(), $oItem->getPersParams() );
164  $this->_aBasketItems[$sKey] = $oItem;
165  }
166 
167  return $this->_aBasketItems;
168  }
169 
180  protected function _createItem( $sProductId, $aSelList = null, $aPersParams = null )
181  {
182  $oNewItem = oxNew( 'oxuserbasketitem' );
183  $oNewItem->oxuserbasketitems__oxartid = new oxField($sProductId, oxField::T_RAW);
184  $oNewItem->oxuserbasketitems__oxbasketid = new oxField($this->getId(), oxField::T_RAW);
185  if ( $aPersParams && count($aPersParams) ) {
186  $oNewItem->setPersParams( $aPersParams );
187  }
188 
189  if ( !$aSelList ) {
190  $oArticle = oxNew( 'oxArticle' );
191  $oArticle->load( $sProductId );
192  $aSelectLists = $oArticle->getSelectLists();
193  if ( ( $iSelCnt = count( $aSelectLists ) ) ) {
194  $aSelList = array_fill( 0, $iSelCnt, '0' );
195  }
196  }
197 
198  $oNewItem->setSelList( $aSelList );
199 
200  return $oNewItem;
201  }
202 
203 
214  public function getItem( $sProductId, $aSelList, $aPersParams = null)
215  {
216  // loading basket item list
217  $aItems = $this->getItems();
218  $sItemKey = $this->_getItemKey( $sProductId, $aSelList, $aPersParams );
219  $oItem = null;
220  // returning existing item
221  if ( isset( $aItems[$sProductId] )) {
222  $oItem = $aItems[$sProductId];
223  } elseif ( isset( $aItems[$sItemKey] ) ) {
224  $oItem = $aItems[$sItemKey];
225  } else {
226  $oItem = $this->_createItem( $sProductId, $aSelList, $aPersParams );
227  }
228 
229  return $oItem;
230  }
231 
241  protected function _getItemKey( $sProductId, $aSel = null, $aPersParam = null )
242  {
243  $aSel = ( $aSel != null) ? $aSel : array (0=>'0');
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  return $blDelete;
326  }
327 
333  public function isVisible()
334  {
335  $oActivUser = $this->getConfig()->getUser();
336  $sActivUserId = null;
337  if ($oActivUser)
338  $sActivUserId = $oActivUser->getId();
339 
340  $blIsVisible = (bool) ($this->oxuserbaskets__oxpublic->value) ||
341  ($sActivUserId && ($this->oxuserbaskets__oxuserid->value == $sActivUserId));
342 
343  return $blIsVisible;
344  }
345 
346 }