oxdeliverysetlist.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxDeliverySetList extends oxList
00007 {
00012     private static $_instance = null;
00013 
00019     protected $_sUserId = null;
00020 
00026     protected $_sCountryId = null;
00027 
00033     protected $_oUser = null;
00034 
00040     protected $_sHomeCountry = null;
00041 
00048     public function __construct( $sObjectsInListName = 'oxdeliveryset')
00049     {
00050         $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
00051         parent::__construct( 'oxdeliveryset' );
00052     }
00053 
00061     public function setHomeCountry( $sHomeCountry )
00062     {
00063         if ( is_array( $sHomeCountry ) ) {
00064             $this->_sHomeCountry = current( $sHomeCountry );
00065         } else {
00066             $this->_sHomeCountry = $sHomeCountry;
00067         }
00068     }
00069 
00075     static function getInstance()
00076     {
00077         if ( defined('OXID_PHP_UNIT')) {
00078             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00079                 return $oClassMod;
00080             } else {
00081                  $inst = oxNew( 'oxDeliverySetList' );
00082                  modInstances::addMod( __CLASS__, $inst );
00083                  return $inst;
00084             }
00085         }
00086 
00087 
00088         if ( !isset( self::$_instance ) ) {
00089             // allow modules
00090             self::$_instance = oxNew( 'oxDeliverySetList' );
00091         }
00092         return self::$_instance;
00093     }
00094 
00095 
00109     protected function _getList( $oUser = null, $sCountryId = null )
00110     {
00111         // checking for current session user which gives additional restrictions for user itself, users group and country
00112         if ( $oUser === null ) {
00113             $oUser = $this->getUser();
00114         } else {
00115             //set user
00116             $this->setUser( $oUser );
00117         }
00118 
00119         $sUserId = $oUser ? $oUser->getId() : '';
00120 
00121         if ( $sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
00122 
00123             // chooseing delivery country if it is not set yet
00124             if ( !$sCountryId ) {
00125 
00126                 if ( $oUser ) {
00127                     $sCountryId = $oUser->getActiveCountry();
00128                 } else {
00129                     $sCountryId = $this->_sHomeCountry;
00130                 }
00131             }
00132 
00133             $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId ) );
00134             $this->_sUserId = $sUserId;
00135             $this->_sCountryId = $sCountryId;
00136         }
00137 
00138         $this->rewind();
00139 
00140         return $this;
00141     }
00142 
00143 
00152     protected function _getFilterSelect( $oUser, $sCountryId )
00153     {
00154         $sTable = getViewName( 'oxdeliveryset' );
00155         $sQ  = "select $sTable.* from $sTable ";
00156         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet().' ';
00157 
00158         // defining initial filter parameters
00159         $sUserId    = null;
00160         $aGroupIds  = null;
00161 
00162         // checking for current session user which gives additional restrictions for user itself, users group and country
00163         if ( $oUser ) {
00164 
00165             // user ID
00166             $sUserId = $oUser->getId();
00167 
00168             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00169             $aGroupIds = $oUser->getUserGroups();
00170         }
00171 
00172         $sIds = '';
00173         if ( count( $aGroupIds ) ) {
00174             foreach ( $aGroupIds as $oGroup ) {
00175                 $aIds[] = "'".$oGroup->getId()."'";
00176             }
00177             $sIds = implode(', ', $aIds);
00178         }
00179 
00180         $sCountrySql = $sCountryId?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID='$sCountryId')":'0';
00181         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID='$sUserId')":'0';
00182         $sGroupSql   = $sIds      ?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' and oxobject2delivery.OXOBJECTID in ($sIds) )":'0';
00183 
00184         $sQ .= "and (
00185             select
00186                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
00187                     $sCountrySql,
00188                     1) &&
00189                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
00190                     $sUserSql,
00191                     1) &&
00192                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
00193                     $sGroupSql,
00194                     1)
00195             )";
00196         //order by
00197         $sQ .= " order by $sTable.oxpos";
00198 
00199         return $sQ;
00200     }
00201 
00211     public function getDeliverySetList( $oUser, $sCountryId, $sDelSet = null )
00212     {
00213         $this->_getList( $oUser, $sCountryId );
00214 
00215         // if there is allready chosen delivery set we must start checking from it
00216         $aList = $this->_aArray;
00217         if ( $sDelSet && isset( $aList[$sDelSet] ) ) {
00218 
00219             //set it as first element
00220             $oDelSet = $aList[$sDelSet];
00221             unset( $aList[$sDelSet] );
00222 
00223             $aList = array_merge( array( $sDelSet => $oDelSet ), $aList );
00224         }
00225         return $aList;
00226     }
00227 
00242     public function getDeliverySetData( $sShipSet, $oUser, $oBasket )
00243     {
00244         $sActShipSet = null;
00245         $aActSets    = array();
00246         $aActPaymentList = array();
00247 
00248         if (!$oUser) {
00249             return;
00250         }
00251 
00252         $this->_getList( $oUser, $oUser->getActiveCountry() );
00253 
00254         // if there are no shipsets we dont need to load payments
00255         if ( $this->count() ) {
00256 
00257             // one selected ?
00258             if ( $sShipSet && !isset( $this->_aArray[$sShipSet] ) ) {
00259                 $sShipSet = null;
00260             }
00261 
00262             $oPayList = oxPaymentList::getInstance();
00263             $oDelList = oxDeliveryList::getInstance();
00264 
00265             $dBasketPrice = $oBasket->getDiscountProductsPrice()->getBruttoSum();
00266 
00267             // checking if these ship sets available (number if possible payment methods > 0)
00268             foreach ( $this as $sShipSetId => $oShipSet ) {
00269 
00270                 $aPaymentList = $oPayList->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00271                 if ( count( $aPaymentList ) ) {
00272 
00273                     // now checking for deliveries
00274                     if ( $oDelList->hasDeliveries( $oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId ) ) {
00275                         $aActSets[$sShipSetId] = $oShipSet;
00276 
00277                         if ( !$sShipSet || ( $sShipSetId == $sShipSet ) ) {
00278                             $sActShipSet = $sShipSet = $sShipSetId;
00279                             $aActPaymentList = $aPaymentList;
00280                             $oShipSet->blSelected = true;
00281                         }
00282                     }
00283                 }
00284             }
00285         }
00286 
00287         return array( $aActSets, $sActShipSet, $aActPaymentList );
00288     }
00289 
00295     public function getUser()
00296     {
00297         if ( !$this->_oUser ) {
00298             $this->_oUser = parent::getUser();
00299         }
00300 
00301         return $this->_oUser;
00302     }
00303 
00311     public function setUser( $oUser )
00312     {
00313         $this->_oUser = $oUser;
00314     }
00315 }

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5