OXID eShop CE  4.8.10
 All Classes Files Functions Variables Pages
oxdeliverylist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxDeliveryList extends oxList
8 {
13  private static $_instance = null;
14 
19  protected $_sUserId = null;
20 
25  protected $_blPerfLoadDelivery = null;
26 
31  protected $_aDeliveries = array();
32 
37  protected $_oUser = null;
38 
44  protected $_sHomeCountry = null;
45 
52 
53 
60  public function __construct( $sObjectsInListName = 'oxdelivery')
61  {
62  parent::__construct( 'oxdelivery' );
63 
64  // load or not delivery list
65  $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
66  }
67 
75  static function getInstance()
76  {
77  return oxRegistry::get("oxDeliveryList");
78  }
79 
87  public function setHomeCountry( $sHomeCountry )
88  {
89  if ( is_array( $sHomeCountry ) ) {
90  $this->_sHomeCountry = current( $sHomeCountry );
91  } else {
92  $this->_sHomeCountry = $sHomeCountry;
93  }
94  }
95 
110  protected function _getList( $oUser = null, $sCountryId = null, $sDelSet = null )
111  {
112  // checking for current session user which gives additional restrictions for user itself, users group and country
113  if ( $oUser === null ) {
114  $oUser = $this->getUser();
115  } else {
116  //set user
117  $this->setUser( $oUser );
118  }
119 
120  $sUserId = $oUser ? $oUser->getId() : '';
121 
122  // chooseing delivery country if it is not set yet
123  if ( !$sCountryId ) {
124  if ( $oUser ) {
125  $sCountryId = $oUser->getActiveCountry();
126  } else {
127  $sCountryId = $this->_sHomeCountry;
128  }
129  }
130 
131  if ( ( $sUserId.$sCountryId.$sDelSet ) !== $this->_sUserId ) {
132 
133  $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId, $sDelSet ) );
134  $this->_sUserId = $sUserId.$sCountryId.$sDelSet;
135  }
136 
137  $this->rewind();
138 
139  return $this;
140  }
141 
151  protected function _getFilterSelect( $oUser, $sCountryId, $sDelSet )
152  {
153  $oDb = oxDb::getDb();
154 
155  $sTable = getViewName( 'oxdelivery' );
156  $sQ = "select $sTable.* from ( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
157  $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and oxdel2delset.oxdelsetid = ".$oDb->quote($sDelSet)." ";
158 
159  // defining initial filter parameters
160  $sUserId = null;
161  $aGroupIds = null;
162 
163  // checking for current session user which gives additional restrictions for user itself, users group and country
164  if ( $oUser ) {
165 
166  // user ID
167  $sUserId = $oUser->getId();
168 
169  // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
170  $aGroupIds = $oUser->getUserGroups();
171  }
172 
173  $aIds = array();
174  if ( count( $aGroupIds ) ) {
175  foreach ( $aGroupIds as $oGroup ) {
176  $aIds[] = $oGroup->getId();
177  }
178  }
179 
180  $sUserTable = getViewName( 'oxuser' );
181  $sGroupTable = getViewName( 'oxgroups' );
182  $sCountryTable = getViewName( 'oxcountry' );
183 
184  $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")" : '0';
185  $sUserSql = $sUserId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")" : '0';
186  $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';
187 
188  $sQ .= ") as $sTable where (
189  select
190  if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
191  $sCountrySql,
192  1) &&
193  if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
194  $sUserSql,
195  1) &&
196  if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
197  $sGroupSql,
198  1)
199  )";
200 
201  $sQ .= " order by $sTable.oxsort ";
202 
203  return $sQ;
204  }
205 
237  public function getDeliveryList( $oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null )
238  {
239  // ids of deliveries that doesnt fit for us to skip double check
240  $aSkipDeliveries = array();
241  $aDelSetList = oxRegistry::get("oxDeliverySetList")->getDeliverySetList( $oUser, $sDelCountry, $sDelSet );
242 
243  // must choose right delivery set to use its delivery list
244  foreach ( $aDelSetList as $sDeliverySetId => $oDeliverySet ) {
245 
246  // loading delivery list to check if some of them fits
247  $aDeliveries = $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
248  $blDelFound = false;
249 
250  foreach ( $aDeliveries as $sDeliveryId => $oDelivery ) {
251 
252  // skipping that was checked and didn't fit before
253  if ( in_array( $sDeliveryId, $aSkipDeliveries ) ) {
254  continue;
255  }
256 
257  $aSkipDeliveries[] = $sDeliveryId;
258 
259  if ( $oDelivery->isForBasket( $oBasket ) ) {
260 
261  // delivery fits conditions
262  $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
263  $blDelFound = true;
264 
265  // unsetting from unfitting list
266  array_pop( $aSkipDeliveries );
267 
268  // maybe checked "Stop processing after first match" ?
269  if ( $oDelivery->oxdelivery__oxfinalize->value ) {
270  break;
271  }
272  }
273  }
274 
275  // found deliveryset and deliveries that fits
276  if ( $blDelFound ) {
277  if ( $this->_blCollectFittingDeliveriesSets ) {
278  // collect only deliveries sets that fits deliveries
279  $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
280  } else {
281  // return collected fitting deliveries
282  oxSession::setVar( 'sShipSet', $sDeliverySetId );
283  return $this->_aDeliveries;
284  }
285  }
286  }
287 
288  //return deliveries sets if found
289  if ( $this->_blCollectFittingDeliveriesSets && count($aFittingDelSets) ) {
290 
291  //reseting getting delivery sets list instead of delivieries before return
292  $this->_blCollectFittingDeliveriesSets = false;
293 
294  //reset cache and list
295  $this->setUser(null);
296  $this->clear();
297 
298  return $aFittingDelSets;
299  }
300 
301  // nothing what fits was found
302  return array();
303  }
304 
315  public function hasDeliveries( $oBasket, $oUser, $sDelCountry, $sDeliverySetId )
316  {
317  $blHas = false;
318 
319  // loading delivery list to check if some of them fits
320  $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
321  foreach ( $this as $oDelivery ) {
322  if ( $oDelivery->isForBasket( $oBasket ) ) {
323  $blHas = true;
324  break;
325  }
326  }
327 
328  return $blHas;
329  }
330 
331 
337  public function getUser()
338  {
339  if ( !$this->_oUser ) {
340  $this->_oUser = parent::getUser();
341  }
342 
343  return $this->_oUser;
344  }
345 
353  public function setUser( $oUser )
354  {
355  $this->_oUser = $oUser;
356  }
357 
366  public function setCollectFittingDeliveriesSets( $blCollectFittingDeliveriesSets = false )
367  {
368  $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
369  }
370 
378  public function loadDeliveryListForProduct( $oProduct )
379  {
380  $oDb = oxDb::getDb();
381  $dPrice = $oDb->quote($oProduct->getPrice()->getBruttoPrice());
382  $dSize = $oDb->quote($oProduct->oxarticles__oxlength->value * $oProduct->oxarticles__oxwidth->value * $oProduct->oxarticles__oxheight->value);
383  $dWeight = $oProduct->oxarticles__oxweight->value;
384  $sTable = getViewName( 'oxdelivery' );
385  $sQ = "select $sTable.* from $sTable";
386  $sQ .= " where ".$this->getBaseObject()->getSqlActiveSnippet();
387  $sQ .= " and ($sTable.oxdeltype != 'a' || ( $sTable.oxparam <= 1 && $sTable.oxparamend >= 1))";
388  if ($dPrice) {
389  $sQ .= " and ($sTable.oxdeltype != 'p' || ( $sTable.oxparam <= $dPrice && $sTable.oxparamend >= $dPrice))";
390  }
391  if ($dSize) {
392  $sQ .= " and ($sTable.oxdeltype != 's' || ( $sTable.oxparam <= $dSize && $sTable.oxparamend >= $dSize))";
393  }
394  if ($dWeight) {
395  $sQ .= " and ($sTable.oxdeltype != 'w' || ( $sTable.oxparam <= $dWeight && $sTable.oxparamend >= $dWeight))";
396  }
397  $this->selectString($sQ);
398  }
399 
400 }