oxdeliverylist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDeliveryList extends oxList
00008 {
00013     private static $_instance = null;
00014 
00019     protected $_sUserId = null;
00020 
00025     protected $_blPerfLoadDelivery = null;
00026 
00031     protected $_aDeliveries = array();
00032 
00037     protected $_oUser = null;
00038 
00044     protected $_sHomeCountry = null;
00045 
00051     protected $_blCollectFittingDeliveriesSets = false;
00052 
00053 
00060     public function __construct( $sObjectsInListName = 'oxdelivery')
00061     {
00062         parent::__construct( 'oxdelivery' );
00063 
00064         // load or not delivery list
00065         $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
00066     }
00067 
00073     static function getInstance()
00074     {
00075         if ( defined('OXID_PHP_UNIT')) {
00076             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00077                 return $oClassMod;
00078             } else {
00079                  $inst = oxNew( 'oxDeliveryList' );
00080                  modInstances::addMod( __CLASS__, $inst );
00081                  return $inst;
00082             }
00083         }
00084 
00085 
00086         if ( !isset( self::$_instance ) ) {
00087             // allow modules
00088             self::$_instance = oxNew( 'oxDeliveryList' );
00089         }
00090         return self::$_instance;
00091     }
00092 
00100     public function setHomeCountry( $sHomeCountry )
00101     {
00102         if ( is_array( $sHomeCountry ) ) {
00103             $this->_sHomeCountry = current( $sHomeCountry );
00104         } else {
00105             $this->_sHomeCountry = $sHomeCountry;
00106         }
00107     }
00108 
00123     protected function _getList( $oUser = null, $sCountryId = null, $sDelSet = null )
00124     {
00125         // checking for current session user which gives additional restrictions for user itself, users group and country
00126         if ( $oUser === null ) {
00127             $oUser = $this->getUser();
00128         } else {
00129             //set user
00130             $this->setUser( $oUser );
00131         }
00132 
00133         $sUserId = $oUser ? $oUser->getId() : '';
00134 
00135         // chooseing delivery country if it is not set yet
00136         if ( !$sCountryId ) {
00137             if ( $oUser ) {
00138                 $sCountryId = $oUser->getActiveCountry();
00139             } else {
00140                 $sCountryId = $this->_sHomeCountry;
00141             }
00142         }
00143 
00144         if ( ( $sUserId.$sCountryId.$sDelSet ) !== $this->_sUserId ) {
00145 
00146             $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId, $sDelSet ) );
00147             $this->_sUserId = $sUserId.$sCountryId.$sDelSet;
00148         }
00149 
00150         $this->rewind();
00151 
00152         return $this;
00153     }
00154 
00164     protected function _getFilterSelect( $oUser, $sCountryId, $sDelSet )
00165     {
00166         $sTable = getViewName( 'oxdelivery' );
00167         $sQ  = "select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
00168         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and oxdel2delset.oxdelsetid = '$sDelSet' ";
00169 
00170         // do we need to check in filter starting param ?
00171         //$sQ .= "and ( $sTable.oxparam != 0 and $sTable.oxparamend != 0 ";
00172 
00173         $sQ .= "and $sTable.oxparamend != 0 ";
00174 
00175         // defining initial filter parameters
00176         $sUserId    = null;
00177         $aGroupIds  = null;
00178 
00179         // checking for current session user which gives additional restrictions for user itself, users group and country
00180         if ( $oUser ) {
00181 
00182             // user ID
00183             $sUserId = $oUser->getId();
00184 
00185             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00186             $aGroupIds = $oUser->getUserGroups();
00187         }
00188 
00189         $sIds = '';
00190         if ( count( $aGroupIds ) ) {
00191             foreach ( $aGroupIds as $oGroup ) {
00192                 $aIds[] = "'".$oGroup->getId()."'";
00193             }
00194             $sIds = implode(', ', $aIds);
00195         }
00196 
00197         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID='$sCountryId')" : '0';
00198         $sUserSql    = $sUserId    ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID='$sUserId')"   : '0';
00199         $sGroupSql   = $sIds       ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' and oxobject2delivery.OXOBJECTID in ($sIds) )"  : '0';
00200 
00201         $sQ .= "and (
00202             select
00203                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
00204                     $sCountrySql,
00205                     1) &&
00206                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
00207                     $sUserSql,
00208                     1) &&
00209                 if(EXISTS(select 1 from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
00210                     $sGroupSql,
00211                     1)
00212             )";
00213 
00214         $sQ .= " order by $sTable.oxsort ";
00215 
00216         return $sQ;
00217     }
00218 
00250     public function getDeliveryList( $oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null )
00251     {
00252         // ids of deliveries that doesnt fit for us to skip double check
00253         $aSkipDeliveries = array();
00254         $aDelSetList = oxDeliverySetList::getInstance()->getDeliverySetList( $oUser, $sDelCountry, $sDelSet );
00255 
00256         // must choose right delivery set to use its delivery list
00257         foreach ( $aDelSetList as $sDeliverySetId => $oDeliverySet ) {
00258 
00259             // loading delivery list to check if some of them fits
00260             $aDeliveries = $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00261             $blDelFound = false;
00262 
00263             foreach ( $aDeliveries as $sDeliveryId => $oDelivery ) {
00264 
00265                 // skipping that was checked and didn't fit before
00266                 if ( in_array( $sDeliveryId, $aSkipDeliveries ) ) {
00267                     continue;
00268                 }
00269 
00270                 $aSkipDeliveries[] = $sDeliveryId;
00271 
00272                 if ( $oDelivery->isForBasket( $oBasket ) ) {
00273 
00274                     // delivery fits conditions
00275                     $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
00276                     $blDelFound = true;
00277 
00278                     // unsetting from unfitting list
00279                     array_pop( $aSkipDeliveries );
00280 
00281                     // maybe checked "Stop processing after first match" ?
00282                     if ( $oDelivery->oxdelivery__oxfinalize->value ) {
00283                         break;
00284                     }
00285                 }
00286             }
00287 
00288             // found deliveryset and deliveries that fits
00289             if ( $blDelFound ) {
00290                 if ( $this->_blCollectFittingDeliveriesSets ) {
00291                     // collect only deliveries sets that fits deliveries
00292                     $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
00293                 } else {
00294                     // return collected fitting deliveries
00295                     oxSession::setVar( 'sShipSet', $sDeliverySetId );
00296                     return $this->_aDeliveries;
00297                 }
00298             }
00299         }
00300 
00301         //return deliveries sets if found
00302         if ( $this->_blCollectFittingDeliveriesSets && count($aFittingDelSets) ) {
00303 
00304             //reseting getting delivery sets list instead of delivieries before return
00305             $this->_blCollectFittingDeliveriesSets = false;
00306 
00307             //reset cache and list
00308             $this->setUser(null);
00309             $this->clear();
00310 
00311             return $aFittingDelSets;
00312         }
00313 
00314         // nothing what fits was found
00315         return array();
00316     }
00317 
00328     public function hasDeliveries( $oBasket, $oUser, $sDelCountry, $sDeliverySetId )
00329     {
00330         $blHas = false;
00331 
00332         // loading delivery list to check if some of them fits
00333         $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00334         foreach ( $this as $oDelivery ) {
00335 
00336             if ( $oDelivery->isForBasket( $oBasket ) ) {
00337                 $blHas = true;
00338                 break;
00339             }
00340         }
00341 
00342         return $blHas;
00343     }
00344     
00345 
00351     public function getUser()
00352     {
00353         if ( !$this->_oUser ) {
00354             $this->_oUser = parent::getUser();
00355         }
00356 
00357         return $this->_oUser;
00358     }
00359 
00367     public function setUser( $oUser )
00368     {
00369         $this->_oUser = $oUser;
00370     }
00371 
00380     public function setCollectFittingDeliveriesSets( $blCollectFittingDeliveriesSets = false )
00381     {
00382         $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
00383     }
00384 
00385 }

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