oxdeliverylist.php

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

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