OXID eShop CE  4.8.11
 All Classes Files Functions Variables Pages
oxdeliverysetlist.php
Go to the documentation of this file.
1 <?php
2 
7 class oxDeliverySetList extends oxList
8 {
13  private static $_instance = null;
14 
20  protected $_sUserId = null;
21 
27  protected $_sCountryId = null;
28 
34  protected $_oUser = null;
35 
41  protected $_sHomeCountry = null;
42 
49  public function __construct( $sObjectsInListName = 'oxdeliveryset')
50  {
51  $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
52  parent::__construct( 'oxdeliveryset' );
53  }
54 
62  public function setHomeCountry( $sHomeCountry )
63  {
64  if ( is_array( $sHomeCountry ) ) {
65  $this->_sHomeCountry = current( $sHomeCountry );
66  } else {
67  $this->_sHomeCountry = $sHomeCountry;
68  }
69  }
70 
78  static function getInstance()
79  {
80  return oxRegistry::get("oxDeliverySetList");
81  }
82 
83 
97  protected function _getList( $oUser = null, $sCountryId = null )
98  {
99  // checking for current session user which gives additional restrictions for user itself, users group and country
100  if ( $oUser === null ) {
101  $oUser = $this->getUser();
102  } else {
103  //set user
104  $this->setUser( $oUser );
105  }
106 
107  $sUserId = $oUser ? $oUser->getId() : '';
108 
109  if ( $sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
110 
111  // chooseing delivery country if it is not set yet
112  if ( !$sCountryId ) {
113 
114  if ( $oUser ) {
115  $sCountryId = $oUser->getActiveCountry();
116  } else {
117  $sCountryId = $this->_sHomeCountry;
118  }
119  }
120 
121  $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId ) );
122  $this->_sUserId = $sUserId;
123  $this->_sCountryId = $sCountryId;
124  }
125 
126  $this->rewind();
127 
128  return $this;
129  }
130 
131 
140  protected function _getFilterSelect( $oUser, $sCountryId )
141  {
142  $sTable = getViewName( 'oxdeliveryset' );
143  $sQ = "select $sTable.* from $sTable ";
144  $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet().' ';
145 
146  // defining initial filter parameters
147  $sUserId = null;
148  $aGroupIds = null;
149 
150  // checking for current session user which gives additional restrictions for user itself, users group and country
151  if ( $oUser ) {
152 
153  // user ID
154  $sUserId = $oUser->getId();
155 
156  // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
157  $aGroupIds = $oUser->getUserGroups();
158  }
159 
160  $aIds = array();
161  if ( count( $aGroupIds ) ) {
162  foreach ( $aGroupIds as $oGroup ) {
163  $aIds[] = $oGroup->getId();
164  }
165  }
166 
167  $sUserTable = getViewName( 'oxuser' );
168  $sGroupTable = getViewName( 'oxgroups' );
169  $sCountryTable = getViewName( 'oxcountry' );
170 
171  $oDb = oxDb::getDb();
172 
173  $sCountrySql = $sCountryId?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")":'0';
174  $sUserSql = $sUserId ?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")":'0';
175  $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';
176 
177  $sQ .= "and (
178  select
179  if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
180  $sCountrySql,
181  1) &&
182  if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
183  $sUserSql,
184  1) &&
185  if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
186  $sGroupSql,
187  1)
188  )";
189 
190  //order by
191  $sQ .= " order by $sTable.oxpos";
192 
193  return $sQ;
194  }
195 
205  public function getDeliverySetList( $oUser, $sCountryId, $sDelSet = null )
206  {
207  $this->_getList( $oUser, $sCountryId );
208 
209  // if there is allready chosen delivery set we must start checking from it
210  $aList = $this->_aArray;
211  if ( $sDelSet && isset( $aList[$sDelSet] ) ) {
212 
213  //set it as first element
214  $oDelSet = $aList[$sDelSet];
215  unset( $aList[$sDelSet] );
216 
217  $aList = array_merge( array( $sDelSet => $oDelSet ), $aList );
218  }
219  return $aList;
220  }
221 
236  public function getDeliverySetData( $sShipSet, $oUser, $oBasket )
237  {
238  $sActShipSet = null;
239  $aActSets = array();
240  $aActPaymentList = array();
241 
242  if (!$oUser) {
243  return;
244  }
245 
246  $this->_getList( $oUser, $oUser->getActiveCountry() );
247 
248  // if there are no shipsets we dont need to load payments
249  if ( $this->count() ) {
250 
251  // one selected ?
252  if ( $sShipSet && !isset( $this->_aArray[$sShipSet] ) ) {
253  $sShipSet = null;
254  }
255 
256  $oPayList = oxRegistry::get("oxPaymentList");
257  $oDelList = oxRegistry::get("oxDeliveryList");
258 
259  $oCur = $this->getConfig()->getActShopCurrencyObject();
260  $dBasketPrice = $oBasket->getPriceForPayment() / $oCur->rate;
261 
262  // checking if these ship sets available (number of possible payment methods > 0)
263  foreach ( $this as $sShipSetId => $oShipSet ) {
264 
265  $aPaymentList = $oPayList->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
266  if ( count( $aPaymentList ) ) {
267 
268  // now checking for deliveries
269  if ( $oDelList->hasDeliveries( $oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId ) ) {
270  $aActSets[$sShipSetId] = $oShipSet;
271 
272  if ( !$sShipSet || ( $sShipSetId == $sShipSet ) ) {
273  $sActShipSet = $sShipSet = $sShipSetId;
274  $aActPaymentList = $aPaymentList;
275  $oShipSet->blSelected = true;
276  }
277  }
278  }
279  }
280  }
281 
282  return array( $aActSets, $sActShipSet, $aActPaymentList );
283  }
284 
290  public function getUser()
291  {
292  if ( !$this->_oUser ) {
293  $this->_oUser = parent::getUser();
294  }
295 
296  return $this->_oUser;
297  }
298 
306  public function setUser( $oUser )
307  {
308  $this->_oUser = $oUser;
309  }
310 
317  public function loadNonRDFaDeliverySetList()
318  {
319  $sTable = getViewName( 'oxdeliveryset' );
320  $sSubSql = "SELECT * FROM oxobject2delivery WHERE oxobject2delivery.OXDELIVERYID = $sTable.OXID AND oxobject2delivery.OXTYPE = 'rdfadeliveryset'";
321  $this->selectString( "SELECT $sTable.* FROM $sTable WHERE NOT EXISTS($sSubSql) AND $sTable.OXACTIVE = 1" );
322  }
323 
332  public function loadRDFaDeliverySetList($sDelId = null)
333  {
334  $sTable = getViewName( 'oxdeliveryset' );
335  if ($sDelId) {
336  $oDb = oxDb::getDb();
337  $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";
338  } else {
339  $sSubSql = $sTable;
340  }
341  $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()." ";
342  $this->selectString( $sQ );
343  }
344 
345 }