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 
00075     static function getInstance()
00076     {
00077         return oxRegistry::get("oxDeliveryList");
00078     }
00079 
00087     public function setHomeCountry( $sHomeCountry )
00088     {
00089         if ( is_array( $sHomeCountry ) ) {
00090             $this->_sHomeCountry = current( $sHomeCountry );
00091         } else {
00092             $this->_sHomeCountry = $sHomeCountry;
00093         }
00094     }
00095 
00110     protected function _getList( $oUser = null, $sCountryId = null, $sDelSet = 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         // chooseing delivery country if it is not set yet
00123         if ( !$sCountryId ) {
00124             if ( $oUser ) {
00125                 $sCountryId = $oUser->getActiveCountry();
00126             } else {
00127                 $sCountryId = $this->_sHomeCountry;
00128             }
00129         }
00130 
00131         if ( ( $sUserId.$sCountryId.$sDelSet ) !== $this->_sUserId ) {
00132 
00133             $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId, $sDelSet ) );
00134             $this->_sUserId = $sUserId.$sCountryId.$sDelSet;
00135         }
00136 
00137         $this->rewind();
00138 
00139         return $this;
00140     }
00141 
00151     protected function _getFilterSelect( $oUser, $sCountryId, $sDelSet )
00152     {
00153         $oDb = oxDb::getDb();
00154 
00155         $sTable = getViewName( 'oxdelivery' );
00156         $sQ  = "select $sTable.* from ( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
00157         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet()." and oxdel2delset.oxdelsetid = ".$oDb->quote($sDelSet)." ";
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         $aIds = array();
00174         if ( count( $aGroupIds ) ) {
00175             foreach ( $aGroupIds as $oGroup ) {
00176                 $aIds[] = $oGroup->getId();
00177             }
00178         }
00179 
00180         $sUserTable    = getViewName( 'oxuser' );
00181         $sGroupTable   = getViewName( 'oxgroups' );
00182         $sCountryTable = getViewName( 'oxcountry' );
00183 
00184         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")" : '0';
00185         $sUserSql    = $sUserId    ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")"   : '0';
00186         $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';
00187 
00188         $sQ .= ") as $sTable where (
00189             select
00190                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
00191                     $sCountrySql,
00192                     1) &&
00193                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
00194                     $sUserSql,
00195                     1) &&
00196                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
00197                     $sGroupSql,
00198                     1)
00199             )";
00200 
00201         $sQ .= " order by $sTable.oxsort ";
00202 
00203         return $sQ;
00204     }
00205 
00237     public function getDeliveryList( $oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null )
00238     {
00239         // ids of deliveries that doesnt fit for us to skip double check
00240         $aSkipDeliveries = array();
00241         $aDelSetList = oxRegistry::get("oxDeliverySetList")->getDeliverySetList( $oUser, $sDelCountry, $sDelSet );
00242 
00243         // must choose right delivery set to use its delivery list
00244         foreach ( $aDelSetList as $sDeliverySetId => $oDeliverySet ) {
00245 
00246             // loading delivery list to check if some of them fits
00247             $aDeliveries = $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00248             $blDelFound = false;
00249 
00250             foreach ( $aDeliveries as $sDeliveryId => $oDelivery ) {
00251 
00252                 // skipping that was checked and didn't fit before
00253                 if ( in_array( $sDeliveryId, $aSkipDeliveries ) ) {
00254                     continue;
00255                 }
00256 
00257                 $aSkipDeliveries[] = $sDeliveryId;
00258 
00259                 if ( $oDelivery->isForBasket( $oBasket ) ) {
00260 
00261                     // delivery fits conditions
00262                     $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
00263                     $blDelFound = true;
00264 
00265                     // unsetting from unfitting list
00266                     array_pop( $aSkipDeliveries );
00267 
00268                     // maybe checked "Stop processing after first match" ?
00269                     if ( $oDelivery->oxdelivery__oxfinalize->value ) {
00270                         break;
00271                     }
00272                 }
00273             }
00274 
00275             // found deliveryset and deliveries that fits
00276             if ( $blDelFound ) {
00277                 if ( $this->_blCollectFittingDeliveriesSets ) {
00278                     // collect only deliveries sets that fits deliveries
00279                     $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
00280                 } else {
00281                     // return collected fitting deliveries
00282                     oxSession::setVar( 'sShipSet', $sDeliverySetId );
00283                     return $this->_aDeliveries;
00284                 }
00285             }
00286         }
00287 
00288         //return deliveries sets if found
00289         if ( $this->_blCollectFittingDeliveriesSets && count($aFittingDelSets) ) {
00290 
00291             //reseting getting delivery sets list instead of delivieries before return
00292             $this->_blCollectFittingDeliveriesSets = false;
00293 
00294             //reset cache and list
00295             $this->setUser(null);
00296             $this->clear();
00297 
00298             return $aFittingDelSets;
00299         }
00300 
00301         // nothing what fits was found
00302         return array();
00303     }
00304 
00315     public function hasDeliveries( $oBasket, $oUser, $sDelCountry, $sDeliverySetId )
00316     {
00317         $blHas = false;
00318 
00319         // loading delivery list to check if some of them fits
00320         $this->_getList( $oUser, $sDelCountry, $sDeliverySetId );
00321         foreach ( $this as $oDelivery ) {
00322             if ( $oDelivery->isForBasket( $oBasket ) ) {
00323                 $blHas = true;
00324                 break;
00325             }
00326         }
00327 
00328         return $blHas;
00329     }
00330     
00331 
00337     public function getUser()
00338     {
00339         if ( !$this->_oUser ) {
00340             $this->_oUser = parent::getUser();
00341         }
00342 
00343         return $this->_oUser;
00344     }
00345 
00353     public function setUser( $oUser )
00354     {
00355         $this->_oUser = $oUser;
00356     }
00357 
00366     public function setCollectFittingDeliveriesSets( $blCollectFittingDeliveriesSets = false )
00367     {
00368         $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
00369     }
00370 
00378     public function loadDeliveryListForProduct( $oProduct )
00379     {
00380         $oDb = oxDb::getDb();
00381         $dPrice  = $oDb->quote($oProduct->getPrice()->getBruttoPrice());
00382         $dSize   = $oDb->quote($oProduct->oxarticles__oxlength->value * $oProduct->oxarticles__oxwidth->value * $oProduct->oxarticles__oxheight->value);
00383         $dWeight = $oProduct->oxarticles__oxweight->value;
00384         $sTable  = getViewName( 'oxdelivery' );
00385         $sQ = "select $sTable.* from $sTable";
00386         $sQ .= " where ".$this->getBaseObject()->getSqlActiveSnippet();
00387         $sQ .= " and ($sTable.oxdeltype != 'a' || ( $sTable.oxparam <= 1 && $sTable.oxparamend >= 1))";
00388         if ($dPrice) {
00389             $sQ .= " and ($sTable.oxdeltype != 'p' || ( $sTable.oxparam <= $dPrice && $sTable.oxparamend >= $dPrice))";
00390         }
00391         if ($dSize) {
00392             $sQ .= " and ($sTable.oxdeltype != 's' || ( $sTable.oxparam <= $dSize && $sTable.oxparamend >= $dSize))";
00393         }
00394         if ($dWeight) {
00395             $sQ .= " and ($sTable.oxdeltype != 'w' || ( $sTable.oxparam <= $dWeight && $sTable.oxparamend >= $dWeight))";
00396         }
00397         $this->selectString($sQ);
00398     }
00399 
00400 }