OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxdiscountlist.php
Go to the documentation of this file.
1 <?php
2 
8 class oxDiscountList extends oxList
9 {
10 
16  protected $_sUserId = null;
17 
23  protected $_blReload = true;
24 
25 
31  protected $_hasSkipDiscountCategories = null;
32 
38  public function __construct()
39  {
40  parent::__construct('oxdiscount');
41  }
42 
52  protected function _getList($oUser = null)
53  {
54  $sUserId = $oUser ? $oUser->getId() : '';
55 
56  if ($this->_blReload || $sUserId !== $this->_sUserId) {
57  // loading list
58  $this->selectString($this->_getFilterSelect($oUser));
59 
60  // setting list proterties
61  $this->_blReload = false; // reload marker
62  $this->_sUserId = $sUserId; // discount list user id
63  }
64 
65  // resetting array pointer
66  $this->rewind();
67 
68  return $this;
69  }
70 
78  public function getCountryId($oUser)
79  {
80  $sCountryId = null;
81  if ($oUser) {
82  $sCountryId = $oUser->getActiveCountry();
83  }
84 
85  return $sCountryId;
86  }
87 
91  public function forceReload()
92  {
93  $this->_blReload = true;
94  }
95 
103  protected function _getFilterSelect($oUser)
104  {
105  $oBaseObject = $this->getBaseObject();
106 
107  $sTable = $oBaseObject->getViewName();
108  $sQ = "select " . $oBaseObject->getSelectFields() . " from $sTable ";
109  $sQ .= "where " . $oBaseObject->getSqlActiveSnippet() . ' ';
110 
111 
112  // defining initial filter parameters
113  $sUserId = null;
114  $sGroupIds = null;
115  $sCountryId = $this->getCountryId($oUser);
116  $oDb = oxDb::getDb();
117 
118  // checking for current session user which gives additional restrictions for user itself, users group and country
119  if ($oUser) {
120 
121  // user ID
122  $sUserId = $oUser->getId();
123 
124  // user group ids
125  foreach ($oUser->getUserGroups() as $oGroup) {
126  if ($sGroupIds) {
127  $sGroupIds .= ', ';
128  }
129  $sGroupIds .= $oDb->quote($oGroup->getId());
130  }
131  }
132 
133  $sUserTable = getViewName('oxuser');
134  $sGroupTable = getViewName('oxgroups');
135  $sCountryTable = getViewName('oxcountry');
136 
137  $sCountrySql = $sCountryId ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
138  $sUserSql = $sUserId ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
139  $sGroupSql = $sGroupIds ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )" : '0';
140 
141  $sQ .= "and (
142  select
143  if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
144  $sCountrySql,
145  1) &&
146  if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
147  $sUserSql,
148  1) &&
149  if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
150  $sGroupSql,
151  1)
152  )";
153 
154  return $sQ;
155  }
156 
165  public function getArticleDiscounts($oArticle, $oUser = null)
166  {
167  $aList = array();
168  $aDiscList = $this->_getList($oUser)->getArray();
169  foreach ($aDiscList as $oDiscount) {
170  if ($oDiscount->isForArticle($oArticle)) {
171  $aList[$oDiscount->getId()] = $oDiscount;
172  }
173  }
174 
175  return $aList;
176  }
177 
187  public function getBasketItemDiscounts($oArticle, $oBasket, $oUser = null)
188  {
189  $aList = array();
190  $aDiscList = $this->_getList($oUser)->getArray();
191  foreach ($aDiscList as $oDiscount) {
192  if ($oDiscount->isForBasketItem($oArticle) && $oDiscount->isForBasketAmount($oBasket)) {
193  $aList[$oDiscount->getId()] = $oDiscount;
194  }
195  }
196 
197  return $aList;
198  }
199 
208  public function getBasketDiscounts($oBasket, $oUser = null)
209  {
210  $aList = array();
211  $aDiscList = $this->_getList($oUser)->getArray();
212  foreach ($aDiscList as $oDiscount) {
213  if ($oDiscount->isForBasket($oBasket)) {
214  $aList[$oDiscount->getId()] = $oDiscount;
215  }
216  }
217 
218  return $aList;
219  }
220 
230  public function getBasketItemBundleDiscounts($oArticle, $oBasket, $oUser = null)
231  {
232  $aList = array();
233  $aDiscList = $this->_getList($oUser)->getArray();
234  foreach ($aDiscList as $oDiscount) {
235  if ($oDiscount->isForBundleItem($oArticle, $oBasket) && $oDiscount->isForBasketAmount($oBasket)) {
236  $aList[$oDiscount->getId()] = $oDiscount;
237  }
238  }
239 
240  return $aList;
241  }
242 
251  public function getBasketBundleDiscounts($oBasket, $oUser = null)
252  {
253  $aList = array();
254  $aDiscList = $this->_getList($oUser)->getArray();
255  foreach ($aDiscList as $oDiscount) {
256  if ($oDiscount->isForBundleBasket($oBasket)) {
257  $aList[$oDiscount->getId()] = $oDiscount;
258  }
259  }
260 
261  return $aList;
262  }
263 
269  public function hasSkipDiscountCategories()
270  {
271  if ($this->_hasSkipDiscountCategories === null || $this->_blReload) {
272  $sViewName = getViewName('oxcategories');
273  $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
274 
275  $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne($sQ);
276  }
277 
279  }
280 }