OXID eShop CE  4.9.8
 All Classes Files Functions Variables Pages
oxdeliverysetlist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxDeliverySetList extends oxList
8 {
9 
15  protected $_sUserId = null;
16 
22  protected $_sCountryId = null;
23 
29  protected $_oUser = null;
30 
36  protected $_sHomeCountry = null;
37 
43  public function __construct()
44  {
45  $this->setHomeCountry($this->getConfig()->getConfigParam('aHomeCountry'));
46  parent::__construct('oxdeliveryset');
47  }
48 
54  public function setHomeCountry($sHomeCountry)
55  {
56  if (is_array($sHomeCountry)) {
57  $this->_sHomeCountry = current($sHomeCountry);
58  } else {
59  $this->_sHomeCountry = $sHomeCountry;
60  }
61  }
62 
76  protected function _getList($oUser = null, $sCountryId = null)
77  {
78  // checking for current session user which gives additional restrictions for user itself, users group and country
79  if ($oUser === null) {
80  $oUser = $this->getUser();
81  } else {
82  //set user
83  $this->setUser($oUser);
84  }
85 
86  $sUserId = $oUser ? $oUser->getId() : '';
87 
88  if ($sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
89 
90  // choosing delivery country if it is not set yet
91  if (!$sCountryId) {
92 
93  if ($oUser) {
94  $sCountryId = $oUser->getActiveCountry();
95  } else {
96  $sCountryId = $this->_sHomeCountry;
97  }
98  }
99 
100  $this->selectString($this->_getFilterSelect($oUser, $sCountryId));
101  $this->_sUserId = $sUserId;
102  $this->_sCountryId = $sCountryId;
103  }
104 
105  $this->rewind();
106 
107  return $this;
108  }
109 
110 
119  protected function _getFilterSelect($oUser, $sCountryId)
120  {
121  $sTable = getViewName('oxdeliveryset');
122  $sQ = "select $sTable.* from $sTable ";
123  $sQ .= "where " . $this->getBaseObject()->getSqlActiveSnippet() . ' ';
124 
125  // defining initial filter parameters
126  $sUserId = null;
127  $aGroupIds = null;
128 
129  // checking for current session user which gives additional restrictions for user itself, users group and country
130  if ($oUser) {
131 
132  // user ID
133  $sUserId = $oUser->getId();
134 
135  // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
136  $aGroupIds = $oUser->getUserGroups();
137  }
138 
139  $aIds = array();
140  if (count($aGroupIds)) {
141  foreach ($aGroupIds as $oGroup) {
142  $aIds[] = $oGroup->getId();
143  }
144  }
145 
146  $sUserTable = getViewName('oxuser');
147  $sGroupTable = getViewName('oxgroups');
148  $sCountryTable = getViewName('oxcountry');
149 
150  $oDb = oxDb::getDb();
151 
152  $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
153  $sUserSql = $sUserId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
154  $sGroupSql = count($aIds) ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' and oxobject2delivery.OXOBJECTID in (" . implode(', ', oxDb::getInstance()->quoteArray($aIds)) . ") )" : '0';
155 
156  $sQ .= "and (
157  select
158  if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
159  $sCountrySql,
160  1) &&
161  if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
162  $sUserSql,
163  1) &&
164  if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
165  $sGroupSql,
166  1)
167  )";
168 
169  //order by
170  $sQ .= " order by $sTable.oxpos";
171 
172  return $sQ;
173  }
174 
184  public function getDeliverySetList($oUser, $sCountryId, $sDelSet = null)
185  {
186  $this->_getList($oUser, $sCountryId);
187 
188  // if there is already chosen delivery set we must start checking from it
189  $aList = $this->_aArray;
190  if ($sDelSet && isset($aList[$sDelSet])) {
191 
192  //set it as first element
193  $oDelSet = $aList[$sDelSet];
194  unset($aList[$sDelSet]);
195 
196  $aList = array_merge(array($sDelSet => $oDelSet), $aList);
197  }
198 
199  return $aList;
200  }
201 
216  public function getDeliverySetData($sShipSet, $oUser, $oBasket)
217  {
218  $sActShipSet = null;
219  $aActSets = array();
220  $aActPaymentList = array();
221 
222  if (!$oUser) {
223  return;
224  }
225 
226  $this->_getList($oUser, $oUser->getActiveCountry());
227 
228  // if there are no shipping sets we don't need to load payments
229  if ($this->count()) {
230 
231  // one selected ?
232  if ($sShipSet && !isset($this->_aArray[$sShipSet])) {
233  $sShipSet = null;
234  }
235 
236  $oPayList = oxRegistry::get("oxPaymentList");
237  $oDelList = oxRegistry::get("oxDeliveryList");
238 
239  $oCur = $this->getConfig()->getActShopCurrencyObject();
240  $dBasketPrice = $oBasket->getPriceForPayment() / $oCur->rate;
241 
242  // checking if these ship sets available (number of possible payment methods > 0)
243  foreach ($this as $sShipSetId => $oShipSet) {
244 
245  $aPaymentList = $oPayList->getPaymentList($sShipSetId, $dBasketPrice, $oUser);
246  if (count($aPaymentList)) {
247 
248  // now checking for deliveries
249  if ($oDelList->hasDeliveries($oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId)) {
250  $aActSets[$sShipSetId] = $oShipSet;
251 
252  if (!$sShipSet || ($sShipSetId == $sShipSet)) {
253  $sActShipSet = $sShipSet = $sShipSetId;
254  $aActPaymentList = $aPaymentList;
255  $oShipSet->blSelected = true;
256  }
257  }
258  }
259  }
260  }
261 
262  return array($aActSets, $sActShipSet, $aActPaymentList);
263  }
264 
270  public function getUser()
271  {
272  if (!$this->_oUser) {
273  $this->_oUser = parent::getUser();
274  }
275 
276  return $this->_oUser;
277  }
278 
284  public function setUser($oUser)
285  {
286  $this->_oUser = $oUser;
287  }
288 
293  public function loadNonRDFaDeliverySetList()
294  {
295  $sTable = getViewName('oxdeliveryset');
296  $sSubSql = "SELECT * FROM oxobject2delivery WHERE oxobject2delivery.OXDELIVERYID = $sTable.OXID AND oxobject2delivery.OXTYPE = 'rdfadeliveryset'";
297  $this->selectString("SELECT $sTable.* FROM $sTable WHERE NOT EXISTS($sSubSql) AND $sTable.OXACTIVE = 1");
298  }
299 
306  public function loadRDFaDeliverySetList($sDelId = null)
307  {
308  $sTable = getViewName('oxdeliveryset');
309  if ($sDelId) {
310  $oDb = oxDb::getDb();
311  $sSubSql = "( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelsetid=$sTable.oxid where " . $this->getBaseObject()->getSqlActiveSnippet() . " and oxdel2delset.oxdelid = " . $oDb->quote($sDelId) . " ) as $sTable";
312  } else {
313  $sSubSql = $sTable;
314  }
315  $sQ = "select $sTable.*, oxobject2delivery.oxobjectid from $sSubSql left join (select oxobject2delivery.* from oxobject2delivery where oxobject2delivery.oxtype = 'rdfadeliveryset' ) as oxobject2delivery on oxobject2delivery.oxdeliveryid=$sTable.oxid where " . $this->getBaseObject()->getSqlActiveSnippet() . " ";
316  $this->selectString($sQ);
317  }
318 }