oxdeliverysetlist.php

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

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