OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxdeliverylist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxDeliveryList extends oxList
8 {
9 
15  protected $_sUserId = null;
16 
22  protected $_blPerfLoadDelivery = null;
23 
29  protected $_aDeliveries = array();
30 
36  protected $_oUser = null;
37 
43  protected $_sHomeCountry = null;
44 
52 
53 
59  public function __construct()
60  {
61  parent::__construct('oxdelivery');
62 
63  // load or not delivery list
64  $this->setHomeCountry($this->getConfig()->getConfigParam('aHomeCountry'));
65  }
66 
72  public function setHomeCountry($sHomeCountry)
73  {
74  if (is_array($sHomeCountry)) {
75  $this->_sHomeCountry = current($sHomeCountry);
76  } else {
77  $this->_sHomeCountry = $sHomeCountry;
78  }
79  }
80 
95  protected function _getList($oUser = null, $sCountryId = null, $sDelSet = null)
96  {
97  // checking for current session user which gives additional restrictions for user itself, users group and country
98  if ($oUser === null) {
99  $oUser = $this->getUser();
100  } else {
101  //set user
102  $this->setUser($oUser);
103  }
104 
105  $sUserId = $oUser ? $oUser->getId() : '';
106 
107  // choosing delivery country if it is not set yet
108  if (!$sCountryId) {
109  if ($oUser) {
110  $sCountryId = $oUser->getActiveCountry();
111  } else {
112  $sCountryId = $this->_sHomeCountry;
113  }
114  }
115 
116  if (($sUserId . $sCountryId . $sDelSet) !== $this->_sUserId) {
117 
118  $this->selectString($this->_getFilterSelect($oUser, $sCountryId, $sDelSet));
119  $this->_sUserId = $sUserId . $sCountryId . $sDelSet;
120  }
121 
122  $this->rewind();
123 
124  return $this;
125  }
126 
136  protected function _getFilterSelect($oUser, $sCountryId, $sDelSet)
137  {
138  $oDb = oxDb::getDb();
139 
140  $sTable = getViewName('oxdelivery');
141  $sQ = "select $sTable.* from ( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
142  $sQ .= "where " . $this->getBaseObject()->getSqlActiveSnippet() . " and oxdel2delset.oxdelsetid = " . $oDb->quote($sDelSet) . " ";
143 
144  // defining initial filter parameters
145  $sUserId = null;
146  $aGroupIds = null;
147 
148  // checking for current session user which gives additional restrictions for user itself, users group and country
149  if ($oUser) {
150 
151  // user ID
152  $sUserId = $oUser->getId();
153 
154  // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
155  $aGroupIds = $oUser->getUserGroups();
156  }
157 
158  $aIds = array();
159  if (count($aGroupIds)) {
160  foreach ($aGroupIds as $oGroup) {
161  $aIds[] = $oGroup->getId();
162  }
163  }
164 
165  $sUserTable = getViewName('oxuser');
166  $sGroupTable = getViewName('oxgroups');
167  $sCountryTable = getViewName('oxcountry');
168 
169  $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
170  $sUserSql = $sUserId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
171  $sGroupSql = count($aIds) ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' and oxobject2delivery.OXOBJECTID in (" . implode(', ', oxDb::getInstance()->quoteArray($aIds)) . ") )" : '0';
172 
173  $sQ .= ") as $sTable where (
174  select
175  if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
176  $sCountrySql,
177  1) &&
178  if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
179  $sUserSql,
180  1) &&
181  if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
182  $sGroupSql,
183  1)
184  )";
185 
186  $sQ .= " order by $sTable.oxsort ";
187 
188  return $sQ;
189  }
190 
222  public function getDeliveryList($oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null)
223  {
224  // ids of deliveries that does not fit for us to skip double check
225  $aSkipDeliveries = array();
226  $aDelSetList = oxRegistry::get("oxDeliverySetList")->getDeliverySetList($oUser, $sDelCountry, $sDelSet);
227 
228  // must choose right delivery set to use its delivery list
229  foreach ($aDelSetList as $sDeliverySetId => $oDeliverySet) {
230 
231  // loading delivery list to check if some of them fits
232  $aDeliveries = $this->_getList($oUser, $sDelCountry, $sDeliverySetId);
233  $blDelFound = false;
234 
235  foreach ($aDeliveries as $sDeliveryId => $oDelivery) {
236 
237  // skipping that was checked and didn't fit before
238  if (in_array($sDeliveryId, $aSkipDeliveries)) {
239  continue;
240  }
241 
242  $aSkipDeliveries[] = $sDeliveryId;
243 
244  if ($oDelivery->isForBasket($oBasket)) {
245 
246  // delivery fits conditions
247  $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
248  $blDelFound = true;
249 
250  // removing from unfitting list
251  array_pop($aSkipDeliveries);
252 
253  // maybe checked "Stop processing after first match" ?
254  if ($oDelivery->oxdelivery__oxfinalize->value) {
255  break;
256  }
257  }
258  }
259 
260  // found delivery set and deliveries that fits
261  if ($blDelFound) {
262  if ($this->_blCollectFittingDeliveriesSets) {
263  // collect only deliveries sets that fits deliveries
264  $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
265  } else {
266  // return collected fitting deliveries
267  oxRegistry::getSession()->setVariable('sShipSet', $sDeliverySetId);
268 
269  return $this->_aDeliveries;
270  }
271  }
272  }
273 
274  //return deliveries sets if found
275  if ($this->_blCollectFittingDeliveriesSets && count($aFittingDelSets)) {
276 
277  //resetting getting delivery sets list instead of deliveries before return
278  $this->_blCollectFittingDeliveriesSets = false;
279 
280  //reset cache and list
281  $this->setUser(null);
282  $this->clear();
283 
284  return $aFittingDelSets;
285  }
286 
287  // nothing what fits was found
288  return array();
289  }
290 
301  public function hasDeliveries($oBasket, $oUser, $sDelCountry, $sDeliverySetId)
302  {
303  $blHas = false;
304 
305  // loading delivery list to check if some of them fits
306  $this->_getList($oUser, $sDelCountry, $sDeliverySetId);
307  foreach ($this as $oDelivery) {
308  if ($oDelivery->isForBasket($oBasket)) {
309  $blHas = true;
310  break;
311  }
312  }
313 
314  return $blHas;
315  }
316 
317 
318 
324  public function getUser()
325  {
326  if (!$this->_oUser) {
327  $this->_oUser = parent::getUser();
328  }
329 
330  return $this->_oUser;
331  }
332 
338  public function setUser($oUser)
339  {
340  $this->_oUser = $oUser;
341  }
342 
349  public function setCollectFittingDeliveriesSets($blCollectFittingDeliveriesSets = false)
350  {
351  $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
352  }
353 
359  public function loadDeliveryListForProduct($oProduct)
360  {
361  $oDb = oxDb::getDb();
362  $dPrice = $oDb->quote($oProduct->getPrice()->getBruttoPrice());
363  $dSize = $oDb->quote($oProduct->getSize());
364  $dWeight = $oDb->quote($oProduct->getWeight());
365 
366  $sTable = getViewName('oxdelivery');
367 
368  $sQ = "select $sTable.* from $sTable";
369  $sQ .= " where " . $this->getBaseObject()->getSqlActiveSnippet();
370  $sQ .= " and ($sTable.oxdeltype != 'a' || ( $sTable.oxparam <= 1 && $sTable.oxparamend >= 1))";
371  if ($dPrice) {
372  $sQ .= " and ($sTable.oxdeltype != 'p' || ( $sTable.oxparam <= $dPrice && $sTable.oxparamend >= $dPrice))";
373  }
374  if ($dSize) {
375  $sQ .= " and ($sTable.oxdeltype != 's' || ( $sTable.oxparam <= $dSize && $sTable.oxparamend >= $dSize))";
376  }
377  if ($dWeight) {
378  $sQ .= " and ($sTable.oxdeltype != 'w' || ( $sTable.oxparam <= $dWeight && $sTable.oxparamend >= $dWeight))";
379  }
380  $this->selectString($sQ);
381  }
382 }