oxdiscountlist.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxDiscountList extends oxList
00009 {
00010 
00016     protected $_sUserId = null;
00017 
00023     protected $_blReload = true;
00024 
00025 
00031     protected $_hasSkipDiscountCategories = null;
00032 
00038     public function __construct()
00039     {
00040         parent::__construct('oxdiscount');
00041     }
00042 
00052     protected function _getList($oUser = null)
00053     {
00054         $sUserId = $oUser ? $oUser->getId() : '';
00055 
00056         if ($this->_blReload || $sUserId !== $this->_sUserId) {
00057             // loading list
00058             $this->selectString($this->_getFilterSelect($oUser));
00059 
00060             // setting list proterties
00061             $this->_blReload = false; // reload marker
00062             $this->_sUserId = $sUserId; // discount list user id
00063         }
00064 
00065         // resetting array pointer
00066         $this->rewind();
00067 
00068         return $this;
00069     }
00070 
00078     public function getCountryId($oUser)
00079     {
00080         $sCountryId = null;
00081         if ($oUser) {
00082             $sCountryId = $oUser->getActiveCountry();
00083         }
00084 
00085         return $sCountryId;
00086     }
00087 
00091     public function forceReload()
00092     {
00093         $this->_blReload = true;
00094     }
00095 
00103     protected function _getFilterSelect($oUser)
00104     {
00105         $oBaseObject = $this->getBaseObject();
00106 
00107         $sTable = $oBaseObject->getViewName();
00108         $sQ = "select " . $oBaseObject->getSelectFields() . " from $sTable ";
00109         $sQ .= "where " . $oBaseObject->getSqlActiveSnippet() . ' ';
00110 
00111 
00112         // defining initial filter parameters
00113         $sUserId = null;
00114         $sGroupIds = null;
00115         $sCountryId = $this->getCountryId($oUser);
00116         $oDb = oxDb::getDb();
00117 
00118         // checking for current session user which gives additional restrictions for user itself, users group and country
00119         if ($oUser) {
00120 
00121             // user ID
00122             $sUserId = $oUser->getId();
00123 
00124             // user group ids
00125             foreach ($oUser->getUserGroups() as $oGroup) {
00126                 if ($sGroupIds) {
00127                     $sGroupIds .= ', ';
00128                 }
00129                 $sGroupIds .= $oDb->quote($oGroup->getId());
00130             }
00131         }
00132 
00133         $sUserTable = getViewName('oxuser');
00134         $sGroupTable = getViewName('oxgroups');
00135         $sCountryTable = getViewName('oxcountry');
00136 
00137         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
00138         $sUserSql = $sUserId ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
00139         $sGroupSql = $sGroupIds ? "EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )" : '0';
00140 
00141         $sQ .= "and (
00142             select
00143                 if(EXISTS(select 1 from oxobject2discount, $sCountryTable where $sCountryTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00144                         $sCountrySql,
00145                         1) &&
00146                 if(EXISTS(select 1 from oxobject2discount, $sUserTable where $sUserTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00147                         $sUserSql,
00148                         1) &&
00149                 if(EXISTS(select 1 from oxobject2discount, $sGroupTable where $sGroupTable.oxid=oxobject2discount.oxobjectid and oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00150                         $sGroupSql,
00151                         1)
00152             )";
00153 
00154         return $sQ;
00155     }
00156 
00165     public function getArticleDiscounts($oArticle, $oUser = null)
00166     {
00167         $aList = array();
00168         $aDiscList = $this->_getList($oUser)->getArray();
00169         foreach ($aDiscList as $oDiscount) {
00170             if ($oDiscount->isForArticle($oArticle)) {
00171                 $aList[$oDiscount->getId()] = $oDiscount;
00172             }
00173         }
00174 
00175         return $aList;
00176     }
00177 
00187     public function getBasketItemDiscounts($oArticle, $oBasket, $oUser = null)
00188     {
00189         $aList = array();
00190         $aDiscList = $this->_getList($oUser)->getArray();
00191         foreach ($aDiscList as $oDiscount) {
00192             if ($oDiscount->isForBasketItem($oArticle) && $oDiscount->isForBasketAmount($oBasket)) {
00193                 $aList[$oDiscount->getId()] = $oDiscount;
00194             }
00195         }
00196 
00197         return $aList;
00198     }
00199 
00208     public function getBasketDiscounts($oBasket, $oUser = null)
00209     {
00210         $aList = array();
00211         $aDiscList = $this->_getList($oUser)->getArray();
00212         foreach ($aDiscList as $oDiscount) {
00213             if ($oDiscount->isForBasket($oBasket)) {
00214                 $aList[$oDiscount->getId()] = $oDiscount;
00215             }
00216         }
00217 
00218         return $aList;
00219     }
00220 
00230     public function getBasketItemBundleDiscounts($oArticle, $oBasket, $oUser = null)
00231     {
00232         $aList = array();
00233         $aDiscList = $this->_getList($oUser)->getArray();
00234         foreach ($aDiscList as $oDiscount) {
00235             if ($oDiscount->isForBundleItem($oArticle, $oBasket) && $oDiscount->isForBasketAmount($oBasket)) {
00236                 $aList[$oDiscount->getId()] = $oDiscount;
00237             }
00238         }
00239 
00240         return $aList;
00241     }
00242 
00251     public function getBasketBundleDiscounts($oBasket, $oUser = null)
00252     {
00253         $aList = array();
00254         $aDiscList = $this->_getList($oUser)->getArray();
00255         foreach ($aDiscList as $oDiscount) {
00256             if ($oDiscount->isForBundleBasket($oBasket)) {
00257                 $aList[$oDiscount->getId()] = $oDiscount;
00258             }
00259         }
00260 
00261         return $aList;
00262     }
00263 
00269     public function hasSkipDiscountCategories()
00270     {
00271         if ($this->_hasSkipDiscountCategories === null || $this->_blReload) {
00272             $sViewName = getViewName('oxcategories');
00273             $sQ = "select 1 from {$sViewName} where {$sViewName}.oxactive = 1 and {$sViewName}.oxskipdiscounts = '1' ";
00274 
00275             $this->_hasSkipDiscountCategories = (bool) oxDb::getDb()->getOne($sQ);
00276         }
00277 
00278         return $this->_hasSkipDiscountCategories;
00279     }
00280 }