oxdiscountlist.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxDiscountList extends oxList
00009 {
00015     static protected $_instance = null;
00016 
00022     protected $_sUserId = null;
00023 
00029     protected $_blReload = true;
00030 
00036     public function __construct( $sObjectsInListName = 'oxdiscount' )
00037     {
00038         parent::__construct( 'oxdiscount' );
00039     }
00040 
00046     static public function getInstance()
00047     {
00048         // disable cashing for test modules
00049         if ( defined( 'OXID_PHP_UNIT' ) ) {
00050             static $inst = array();
00051             self::$_instance = $inst[oxClassCacheKey()];
00052         }
00053 
00054         if ( !isset( self::$_instance ) ) {
00055             // allow modules
00056             self::$_instance = oxNew( 'oxDiscountList' );
00057 
00058             if ( defined( 'OXID_PHP_UNIT' ) ) {
00059                 $inst[oxClassCacheKey()] = self::$_instance;
00060             }
00061         }
00062 
00063         return self::$_instance;
00064     }
00065 
00073     protected function _getList( $oUser = null )
00074     {
00075         $sUserId = $oUser?$oUser->getId():'';
00076 
00077         if ( $this->_blReload || $sUserId !== $this->_sUserId ) {
00078             // loading list
00079             $this->selectString( $this->_getFilterSelect( $oUser ) );
00080 
00081             // setting list proterties
00082             $this->_blReload = false;    // reload marker
00083             $this->_sUserId  = $sUserId; // discount list user id
00084         }
00085 
00086         // resetting array pointer
00087         $this->rewind();
00088 
00089         return $this;
00090     }
00091 
00099     public function getCountryId( $oUser )
00100     {
00101         $sCountryId = null;
00102         if ( $oUser ) {
00103             $sCountryId = $oUser->getActiveCountry();
00104         }
00105 
00106         return $sCountryId;
00107     }
00108 
00114     public function forceReload()
00115     {
00116         $this->_blReload = true;
00117     }
00118 
00126     protected function _getFilterSelect( $oUser )
00127     {
00128 
00129         $sTable = getViewName( 'oxdiscount' );
00130 
00131         $sQ  = "select ".$this->getBaseObject()->getSelectFields()." from $sTable ";
00132         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet().' ';
00133 
00134 
00135         // defining initial filter parameters
00136         $sUserId    = null;
00137         $sGroupIds  = null;
00138         $sCountryId = $this->getCountryId( $oUser );
00139 
00140         // checking for current session user which gives additional restrictions for user itself, users group and country
00141         if ( $oUser ) {
00142 
00143             // user ID
00144             $sUserId = $oUser->getId();
00145 
00146             // user group ids
00147             foreach ( $oUser->getUserGroups() as $oGroup ) {
00148                 if ( $sGroupIds ) {
00149                     $sGroupIds .= ', ';
00150                 }
00151                 $sGroupIds .= "'".$oGroup->getId()."'";
00152             }
00153         }
00154 
00155         $sCountrySql = $sCountryId?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' and oxobject2discount.OXOBJECTID='$sCountryId')":'0';
00156         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' and oxobject2discount.OXOBJECTID='$sUserId')":'0';
00157         $sGroupSql   = $sGroupIds ?"EXISTS(select oxobject2discount.oxid from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' and oxobject2discount.OXOBJECTID in ($sGroupIds) )":'0';
00158 
00159         $sQ .= "and (
00160             select
00161                 if(EXISTS(select 1 from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxcountry' LIMIT 1),
00162                         $sCountrySql,
00163                         1) &&
00164                 if(EXISTS(select 1 from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxuser' LIMIT 1),
00165                         $sUserSql,
00166                         1) &&
00167                 if(EXISTS(select 1 from oxobject2discount where oxobject2discount.OXDISCOUNTID=$sTable.OXID and oxobject2discount.oxtype='oxgroups' LIMIT 1),
00168                         $sGroupSql,
00169                         1)
00170             )";
00171 
00172         return $sQ;
00173     }
00174 
00183     public function getArticleDiscounts( $oArticle, $oUser = null )
00184     {
00185         $aList = array();
00186         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00187             if ( $oDiscount->isForArticle( $oArticle ) ) {
00188                 $aList[$oDiscount->getId()] = $oDiscount;
00189             }
00190         }
00191 
00192         return $aList;
00193     }
00194 
00204     public function getBasketItemDiscounts( $oArticle, $oBasket, $oUser = null )
00205     {
00206         $aList = array();
00207         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00208             if ( $oDiscount->isForBasketItem( $oArticle ) && $oDiscount->isForBasketAmount( $oBasket ) ) {
00209                 $aList[$oDiscount->getId()] = $oDiscount;
00210             }
00211         }
00212 
00213         return $aList;
00214     }
00215 
00224     public function getBasketDiscounts( $oBasket, $oUser = null )
00225     {
00226         $aList = array();
00227         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00228             if ( $oDiscount->isForBasket( $oBasket ) ) {
00229                 $aList[$oDiscount->getId()] = $oDiscount;
00230             }
00231         }
00232 
00233         return $aList;
00234     }
00235 
00245     public function getBasketItemBundleDiscounts( $oArticle, $oBasket, $oUser = null )
00246     {
00247         $aList = array();
00248         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00249             if ( $oDiscount->isForBundleItem( $oArticle, $oBasket ) && $oDiscount->isForBasketAmount($oBasket) ) {
00250                 $aList[$oDiscount->getId()] = $oDiscount;
00251             }
00252         }
00253 
00254         return $aList;
00255     }
00256 
00265     public function getBasketBundleDiscounts( $oBasket, $oUser = null )
00266     {
00267         $aList = array();
00268         foreach ( $this->_getList( $oUser ) as $oDiscount ) {
00269             if ( $oDiscount->isForBundleBasket( $oBasket ) ) {
00270                 $aList[$oDiscount->getId()] = $oDiscount;
00271             }
00272         }
00273 
00274         return $aList;
00275     }
00276 }

Generated on Thu Dec 4 12:04:56 2008 for OXID eShop CE by  doxygen 1.5.5