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         $oDb = oxDb::getDb();
00166 
00167         $sTable = getViewName( 'oxdelivery' );
00168         $sQ  = "select $sTable.* from ( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
00169         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and oxdel2delset.oxdelsetid = ".$oDb->quote($sDelSet)." ";
00170 
00171         // defining initial filter parameters
00172         $sUserId    = null;
00173         $aGroupIds  = null;
00174 
00175         // checking for current session user which gives additional restrictions for user itself, users group and country
00176         if ( $oUser ) {
00177 
00178             // user ID
00179             $sUserId = $oUser->getId();
00180 
00181             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00182             $aGroupIds = $oUser->getUserGroups();
00183         }
00184 
00185         $aIds = array();
00186         if ( count( $aGroupIds ) ) {
00187             foreach ( $aGroupIds as $oGroup ) {
00188                 $aIds[] = $oGroup->getId();
00189             }
00190         }
00191 
00192         $sUserTable    = getViewName( 'oxuser' );
00193         $sGroupTable   = getViewName( 'oxgroups' );
00194         $sCountryTable = getViewName( 'oxcountry' );
00195 
00196         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")" : '0';
00197         $sUserSql    = $sUserId    ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")"   : '0';
00198         $sGroupSql   = count( $aIds ) ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' and oxobject2delivery.OXOBJECTID in (".implode(', ', oxDb::getInstance()->quoteArray($aIds) ).") )"  : '0';
00199 
00200         $sQ .= ") as $sTable where (
00201             select
00202                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
00203                     $sCountrySql,
00204                     1) &&
00205                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
00206                     $sUserSql,
00207                     1) &&
00208                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and 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             if ( $oDelivery->isForBasket( $oBasket ) ) {
00335                 $blHas = true;
00336                 break;
00337             }
00338         }
00339 
00340         return $blHas;
00341     }
00342     
00343 
00349     public function getUser()
00350     {
00351         if ( !$this->_oUser ) {
00352             $this->_oUser = parent::getUser();
00353         }
00354 
00355         return $this->_oUser;
00356     }
00357 
00365     public function setUser( $oUser )
00366     {
00367         $this->_oUser = $oUser;
00368     }
00369 
00378     public function setCollectFittingDeliveriesSets( $blCollectFittingDeliveriesSets = false )
00379     {
00380         $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
00381     }
00382 
00383 }