oxdeliverylist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDeliveryList extends oxList
00008 {
00009 
00015     protected $_sUserId = null;
00016 
00022     protected $_blPerfLoadDelivery = null;
00023 
00029     protected $_aDeliveries = array();
00030 
00036     protected $_oUser = null;
00037 
00043     protected $_sHomeCountry = null;
00044 
00051     protected $_blCollectFittingDeliveriesSets = false;
00052 
00053 
00059     public function __construct()
00060     {
00061         parent::__construct('oxdelivery');
00062 
00063         // load or not delivery list
00064         $this->setHomeCountry($this->getConfig()->getConfigParam('aHomeCountry'));
00065     }
00066 
00072     public function setHomeCountry($sHomeCountry)
00073     {
00074         if (is_array($sHomeCountry)) {
00075             $this->_sHomeCountry = current($sHomeCountry);
00076         } else {
00077             $this->_sHomeCountry = $sHomeCountry;
00078         }
00079     }
00080 
00095     protected function _getList($oUser = null, $sCountryId = null, $sDelSet = null)
00096     {
00097         // checking for current session user which gives additional restrictions for user itself, users group and country
00098         if ($oUser === null) {
00099             $oUser = $this->getUser();
00100         } else {
00101             //set user
00102             $this->setUser($oUser);
00103         }
00104 
00105         $sUserId = $oUser ? $oUser->getId() : '';
00106 
00107         // choosing delivery country if it is not set yet
00108         if (!$sCountryId) {
00109             if ($oUser) {
00110                 $sCountryId = $oUser->getActiveCountry();
00111             } else {
00112                 $sCountryId = $this->_sHomeCountry;
00113             }
00114         }
00115 
00116         if (($sUserId . $sCountryId . $sDelSet) !== $this->_sUserId) {
00117 
00118             $this->selectString($this->_getFilterSelect($oUser, $sCountryId, $sDelSet));
00119             $this->_sUserId = $sUserId . $sCountryId . $sDelSet;
00120         }
00121 
00122         $this->rewind();
00123 
00124         return $this;
00125     }
00126 
00136     protected function _getFilterSelect($oUser, $sCountryId, $sDelSet)
00137     {
00138         $oDb = oxDb::getDb();
00139 
00140         $sTable = getViewName('oxdelivery');
00141         $sQ = "select $sTable.* from ( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelid=$sTable.oxid ";
00142         $sQ .= "where " . $this->getBaseObject()->getSqlActiveSnippet() . " and oxdel2delset.oxdelsetid = " . $oDb->quote($sDelSet) . " ";
00143 
00144         // defining initial filter parameters
00145         $sUserId = null;
00146         $aGroupIds = null;
00147 
00148         // checking for current session user which gives additional restrictions for user itself, users group and country
00149         if ($oUser) {
00150 
00151             // user ID
00152             $sUserId = $oUser->getId();
00153 
00154             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00155             $aGroupIds = $oUser->getUserGroups();
00156         }
00157 
00158         $aIds = array();
00159         if (count($aGroupIds)) {
00160             foreach ($aGroupIds as $oGroup) {
00161                 $aIds[] = $oGroup->getId();
00162             }
00163         }
00164 
00165         $sUserTable = getViewName('oxuser');
00166         $sGroupTable = getViewName('oxgroups');
00167         $sCountryTable = getViewName('oxcountry');
00168 
00169         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
00170         $sUserSql = $sUserId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
00171         $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';
00172 
00173         $sQ .= ") as $sTable where (
00174             select
00175                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxcountry' LIMIT 1),
00176                     $sCountrySql,
00177                     1) &&
00178                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxuser' LIMIT 1),
00179                     $sUserSql,
00180                     1) &&
00181                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxgroups' LIMIT 1),
00182                     $sGroupSql,
00183                     1)
00184             )";
00185 
00186         $sQ .= " order by $sTable.oxsort ";
00187 
00188         return $sQ;
00189     }
00190 
00222     public function getDeliveryList($oBasket, $oUser = null, $sDelCountry = null, $sDelSet = null)
00223     {
00224         // ids of deliveries that does not fit for us to skip double check
00225         $aSkipDeliveries = array();
00226         $aDelSetList = oxRegistry::get("oxDeliverySetList")->getDeliverySetList($oUser, $sDelCountry, $sDelSet);
00227 
00228         // must choose right delivery set to use its delivery list
00229         foreach ($aDelSetList as $sDeliverySetId => $oDeliverySet) {
00230 
00231             // loading delivery list to check if some of them fits
00232             $aDeliveries = $this->_getList($oUser, $sDelCountry, $sDeliverySetId);
00233             $blDelFound = false;
00234 
00235             foreach ($aDeliveries as $sDeliveryId => $oDelivery) {
00236 
00237                 // skipping that was checked and didn't fit before
00238                 if (in_array($sDeliveryId, $aSkipDeliveries)) {
00239                     continue;
00240                 }
00241 
00242                 $aSkipDeliveries[] = $sDeliveryId;
00243 
00244                 if ($oDelivery->isForBasket($oBasket)) {
00245 
00246                     // delivery fits conditions
00247                     $this->_aDeliveries[$sDeliveryId] = $aDeliveries[$sDeliveryId];
00248                     $blDelFound = true;
00249 
00250                     // removing from unfitting list
00251                     array_pop($aSkipDeliveries);
00252 
00253                     // maybe checked "Stop processing after first match" ?
00254                     if ($oDelivery->oxdelivery__oxfinalize->value) {
00255                         break;
00256                     }
00257                 }
00258             }
00259 
00260             // found delivery set and deliveries that fits
00261             if ($blDelFound) {
00262                 if ($this->_blCollectFittingDeliveriesSets) {
00263                     // collect only deliveries sets that fits deliveries
00264                     $aFittingDelSets[$sDeliverySetId] = $oDeliverySet;
00265                 } else {
00266                     // return collected fitting deliveries
00267                     oxRegistry::getSession()->setVariable('sShipSet', $sDeliverySetId);
00268 
00269                     return $this->_aDeliveries;
00270                 }
00271             }
00272         }
00273 
00274         //return deliveries sets if found
00275         if ($this->_blCollectFittingDeliveriesSets && count($aFittingDelSets)) {
00276 
00277             //resetting getting delivery sets list instead of deliveries before return
00278             $this->_blCollectFittingDeliveriesSets = false;
00279 
00280             //reset cache and list
00281             $this->setUser(null);
00282             $this->clear();
00283 
00284             return $aFittingDelSets;
00285         }
00286 
00287         // nothing what fits was found
00288         return array();
00289     }
00290 
00301     public function hasDeliveries($oBasket, $oUser, $sDelCountry, $sDeliverySetId)
00302     {
00303         $blHas = false;
00304 
00305         // loading delivery list to check if some of them fits
00306         $this->_getList($oUser, $sDelCountry, $sDeliverySetId);
00307         foreach ($this as $oDelivery) {
00308             if ($oDelivery->isForBasket($oBasket)) {
00309                 $blHas = true;
00310                 break;
00311             }
00312         }
00313 
00314         return $blHas;
00315     }
00316 
00317     
00318 
00324     public function getUser()
00325     {
00326         if (!$this->_oUser) {
00327             $this->_oUser = parent::getUser();
00328         }
00329 
00330         return $this->_oUser;
00331     }
00332 
00338     public function setUser($oUser)
00339     {
00340         $this->_oUser = $oUser;
00341     }
00342 
00349     public function setCollectFittingDeliveriesSets($blCollectFittingDeliveriesSets = false)
00350     {
00351         $this->_blCollectFittingDeliveriesSets = $blCollectFittingDeliveriesSets;
00352     }
00353 
00359     public function loadDeliveryListForProduct($oProduct)
00360     {
00361         $oDb = oxDb::getDb();
00362         $dPrice = $oDb->quote($oProduct->getPrice()->getBruttoPrice());
00363         $dSize = $oDb->quote($oProduct->getSize());
00364         $dWeight = $oDb->quote($oProduct->getWeight());
00365 
00366         $sTable = getViewName('oxdelivery');
00367 
00368         $sQ = "select $sTable.* from $sTable";
00369         $sQ .= " where " . $this->getBaseObject()->getSqlActiveSnippet();
00370         $sQ .= " and ($sTable.oxdeltype != 'a' || ( $sTable.oxparam <= 1 && $sTable.oxparamend >= 1))";
00371         if ($dPrice) {
00372             $sQ .= " and ($sTable.oxdeltype != 'p' || ( $sTable.oxparam <= $dPrice && $sTable.oxparamend >= $dPrice))";
00373         }
00374         if ($dSize) {
00375             $sQ .= " and ($sTable.oxdeltype != 's' || ( $sTable.oxparam <= $dSize && $sTable.oxparamend >= $dSize))";
00376         }
00377         if ($dWeight) {
00378             $sQ .= " and ($sTable.oxdeltype != 'w' || ( $sTable.oxparam <= $dWeight && $sTable.oxparamend >= $dWeight))";
00379         }
00380         $this->selectString($sQ);
00381     }
00382 }