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         // do we need to check in filter starting param ?
00172         //$sQ .= "and ( $sTable.oxparam != 0 and $sTable.oxparamend != 0 ";
00173 
00174         $sQ .= "and $sTable.oxparamend != 0 ";
00175 
00176         // defining initial filter parameters
00177         $sUserId    = null;
00178         $aGroupIds  = null;
00179 
00180         // checking for current session user which gives additional restrictions for user itself, users group and country
00181         if ( $oUser ) {
00182 
00183             // user ID
00184             $sUserId = $oUser->getId();
00185 
00186             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00187             $aGroupIds = $oUser->getUserGroups();
00188         }
00189 
00190         $aIds = array();
00191         if ( count( $aGroupIds ) ) {
00192             foreach ( $aGroupIds as $oGroup ) {
00193                 $aIds[] = $oGroup->getId();
00194             }
00195         }
00196 
00197         $sUserTable    = getViewName( 'oxuser' );
00198         $sGroupTable   = getViewName( 'oxgroups' );
00199         $sCountryTable = getViewName( 'oxcountry' );
00200 
00201         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")" : '0';
00202         $sUserSql    = $sUserId    ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")"   : '0';
00203         $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';
00204 
00205         $sQ .= ") as $sTable where (
00206             select
00207                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
00208                     $sCountrySql,
00209                     1) &&
00210                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
00211                     $sUserSql,
00212                     1) &&
00213                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
00214                     $sGroupSql,
00215                     1)
00216             )";
00217 
00218         $sQ .= " order by $sTable.oxsort ";
00219 
00220         return $sQ;
00221     }
00222 
00254     public function getDeliveryList( $oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null )
00255     {
00256         // ids of deliveries that doesnt fit for us to skip double check
00257         $aSkipDeliveries = array();
00258         $aDelSetList = oxDeliverySetList::getInstance()->getDeliverySetList( $oUser, $sDelCountry, $sDelSet );
00259 
00260         // must choose right delivery set to use its delivery list
00261         foreach ( $aDelSetList as $sDeliverySetId => $oDeliverySet ) {
00262 
00263             // loading delivery list to check if some of them fits
00264             $aDeliveries = $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00265             $blDelFound = false;
00266 
00267             foreach ( $aDeliveries as $sDeliveryId => $oDelivery ) {
00268 
00269                 // skipping that was checked and didn't fit before
00270                 if ( in_array( $sDeliveryId, $aSkipDeliveries ) ) {
00271                     continue;
00272                 }
00273 
00274                 $aSkipDeliveries[] = $sDeliveryId;
00275 
00276                 if ( $oDelivery->isForBasket( $oBasket ) ) {
00277 
00278                     // delivery fits conditions
00279                     $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
00280                     $blDelFound = true;
00281 
00282                     // unsetting from unfitting list
00283                     array_pop( $aSkipDeliveries );
00284 
00285                     // maybe checked "Stop processing after first match" ?
00286                     if ( $oDelivery->oxdelivery__oxfinalize->value ) {
00287                         break;
00288                     }
00289                 }
00290             }
00291 
00292             // found deliveryset and deliveries that fits
00293             if ( $blDelFound ) {
00294                 if ( $this->_blCollectFittingDeliveriesSets ) {
00295                     // collect only deliveries sets that fits deliveries
00296                     $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
00297                 } else {
00298                     // return collected fitting deliveries
00299                     oxSession::setVar( 'sShipSet', $sDeliverySetId );
00300                     return $this->_aDeliveries;
00301                 }
00302             }
00303         }
00304 
00305         //return deliveries sets if found
00306         if ( $this->_blCollectFittingDeliveriesSets && count($aFittingDelSets) ) {
00307 
00308             //reseting getting delivery sets list instead of delivieries before return
00309             $this->_blCollectFittingDeliveriesSets = false;
00310 
00311             //reset cache and list
00312             $this->setUser(null);
00313             $this->clear();
00314 
00315             return $aFittingDelSets;
00316         }
00317 
00318         // nothing what fits was found
00319         return array();
00320     }
00321 
00332     public function hasDeliveries( $oBasket, $oUser, $sDelCountry, $sDeliverySetId )
00333     {
00334         $blHas = false;
00335 
00336         // loading delivery list to check if some of them fits
00337         $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00338         foreach ( $this as $oDelivery ) {
00339             if ( $oDelivery->isForBasket( $oBasket ) ) {
00340                 $blHas = true;
00341                 break;
00342             }
00343         }
00344 
00345         return $blHas;
00346     }
00347     
00348 
00354     public function getUser()
00355     {
00356         if ( !$this->_oUser ) {
00357             $this->_oUser = parent::getUser();
00358         }
00359 
00360         return $this->_oUser;
00361     }
00362 
00370     public function setUser( $oUser )
00371     {
00372         $this->_oUser = $oUser;
00373     }
00374 
00383     public function setCollectFittingDeliveriesSets( $blCollectFittingDeliveriesSets = false )
00384     {
00385         $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
00386     }
00387 
00388 }