oxdeliverysetlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDeliverySetList extends oxList
00008 {
00009 
00015     protected $_sUserId = null;
00016 
00022     protected $_sCountryId = null;
00023 
00029     protected $_oUser = null;
00030 
00036     protected $_sHomeCountry = null;
00037 
00043     public function __construct()
00044     {
00045         $this->setHomeCountry($this->getConfig()->getConfigParam('aHomeCountry'));
00046         parent::__construct('oxdeliveryset');
00047     }
00048 
00054     public function setHomeCountry($sHomeCountry)
00055     {
00056         if (is_array($sHomeCountry)) {
00057             $this->_sHomeCountry = current($sHomeCountry);
00058         } else {
00059             $this->_sHomeCountry = $sHomeCountry;
00060         }
00061     }
00062 
00076     protected function _getList($oUser = null, $sCountryId = null)
00077     {
00078         // checking for current session user which gives additional restrictions for user itself, users group and country
00079         if ($oUser === null) {
00080             $oUser = $this->getUser();
00081         } else {
00082             //set user
00083             $this->setUser($oUser);
00084         }
00085 
00086         $sUserId = $oUser ? $oUser->getId() : '';
00087 
00088         if ($sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
00089 
00090             // choosing delivery country if it is not set yet
00091             if (!$sCountryId) {
00092 
00093                 if ($oUser) {
00094                     $sCountryId = $oUser->getActiveCountry();
00095                 } else {
00096                     $sCountryId = $this->_sHomeCountry;
00097                 }
00098             }
00099 
00100             $this->selectString($this->_getFilterSelect($oUser, $sCountryId));
00101             $this->_sUserId = $sUserId;
00102             $this->_sCountryId = $sCountryId;
00103         }
00104 
00105         $this->rewind();
00106 
00107         return $this;
00108     }
00109 
00110 
00119     protected function _getFilterSelect($oUser, $sCountryId)
00120     {
00121         $sTable = getViewName('oxdeliveryset');
00122         $sQ = "select $sTable.* from $sTable ";
00123         $sQ .= "where " . $this->getBaseObject()->getSqlActiveSnippet() . ' ';
00124 
00125         // defining initial filter parameters
00126         $sUserId = null;
00127         $aGroupIds = null;
00128 
00129         // checking for current session user which gives additional restrictions for user itself, users group and country
00130         if ($oUser) {
00131 
00132             // user ID
00133             $sUserId = $oUser->getId();
00134 
00135             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00136             $aGroupIds = $oUser->getUserGroups();
00137         }
00138 
00139         $aIds = array();
00140         if (count($aGroupIds)) {
00141             foreach ($aGroupIds as $oGroup) {
00142                 $aIds[] = $oGroup->getId();
00143             }
00144         }
00145 
00146         $sUserTable = getViewName('oxuser');
00147         $sGroupTable = getViewName('oxgroups');
00148         $sCountryTable = getViewName('oxcountry');
00149 
00150         $oDb = oxDb::getDb();
00151 
00152         $sCountrySql = $sCountryId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sCountryId) . ")" : '0';
00153         $sUserSql = $sUserId ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID=" . $oDb->quote($sUserId) . ")" : '0';
00154         $sGroupSql = count($aIds) ? "EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' and oxobject2delivery.OXOBJECTID in (" . implode(', ', oxDb::getInstance()->quoteArray($aIds)) . ") )" : '0';
00155 
00156         $sQ .= "and (
00157             select
00158                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
00159                     $sCountrySql,
00160                     1) &&
00161                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
00162                     $sUserSql,
00163                     1) &&
00164                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
00165                     $sGroupSql,
00166                     1)
00167             )";
00168 
00169         //order by
00170         $sQ .= " order by $sTable.oxpos";
00171 
00172         return $sQ;
00173     }
00174 
00184     public function getDeliverySetList($oUser, $sCountryId, $sDelSet = null)
00185     {
00186         $this->_getList($oUser, $sCountryId);
00187 
00188         // if there is already chosen delivery set we must start checking from it
00189         $aList = $this->_aArray;
00190         if ($sDelSet && isset($aList[$sDelSet])) {
00191 
00192             //set it as first element
00193             $oDelSet = $aList[$sDelSet];
00194             unset($aList[$sDelSet]);
00195 
00196             $aList = array_merge(array($sDelSet => $oDelSet), $aList);
00197         }
00198 
00199         return $aList;
00200     }
00201 
00216     public function getDeliverySetData($sShipSet, $oUser, $oBasket)
00217     {
00218         $sActShipSet = null;
00219         $aActSets = array();
00220         $aActPaymentList = array();
00221 
00222         if (!$oUser) {
00223             return;
00224         }
00225 
00226         $this->_getList($oUser, $oUser->getActiveCountry());
00227 
00228         // if there are no shipping sets we don't need to load payments
00229         if ($this->count()) {
00230 
00231             // one selected ?
00232             if ($sShipSet && !isset($this->_aArray[$sShipSet])) {
00233                 $sShipSet = null;
00234             }
00235 
00236             $oPayList = oxRegistry::get("oxPaymentList");
00237             $oDelList = oxRegistry::get("oxDeliveryList");
00238 
00239             $oCur = $this->getConfig()->getActShopCurrencyObject();
00240             $dBasketPrice = $oBasket->getPriceForPayment() / $oCur->rate;
00241 
00242             // checking if these ship sets available (number of possible payment methods > 0)
00243             foreach ($this as $sShipSetId => $oShipSet) {
00244 
00245                 $aPaymentList = $oPayList->getPaymentList($sShipSetId, $dBasketPrice, $oUser);
00246                 if (count($aPaymentList)) {
00247 
00248                     // now checking for deliveries
00249                     if ($oDelList->hasDeliveries($oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId)) {
00250                         $aActSets[$sShipSetId] = $oShipSet;
00251 
00252                         if (!$sShipSet || ($sShipSetId == $sShipSet)) {
00253                             $sActShipSet = $sShipSet = $sShipSetId;
00254                             $aActPaymentList = $aPaymentList;
00255                             $oShipSet->blSelected = true;
00256                         }
00257                     }
00258                 }
00259             }
00260         }
00261 
00262         return array($aActSets, $sActShipSet, $aActPaymentList);
00263     }
00264 
00270     public function getUser()
00271     {
00272         if (!$this->_oUser) {
00273             $this->_oUser = parent::getUser();
00274         }
00275 
00276         return $this->_oUser;
00277     }
00278 
00284     public function setUser($oUser)
00285     {
00286         $this->_oUser = $oUser;
00287     }
00288 
00293     public function loadNonRDFaDeliverySetList()
00294     {
00295         $sTable = getViewName('oxdeliveryset');
00296         $sSubSql = "SELECT * FROM oxobject2delivery WHERE oxobject2delivery.OXDELIVERYID = $sTable.OXID AND oxobject2delivery.OXTYPE = 'rdfadeliveryset'";
00297         $this->selectString("SELECT $sTable.* FROM $sTable WHERE NOT EXISTS($sSubSql) AND $sTable.OXACTIVE = 1");
00298     }
00299 
00306     public function loadRDFaDeliverySetList($sDelId = null)
00307     {
00308         $sTable = getViewName('oxdeliveryset');
00309         if ($sDelId) {
00310             $oDb = oxDb::getDb();
00311             $sSubSql = "( select $sTable.* from $sTable left join oxdel2delset on oxdel2delset.oxdelsetid=$sTable.oxid where " . $this->getBaseObject()->getSqlActiveSnippet() . " and oxdel2delset.oxdelid = " . $oDb->quote($sDelId) . " ) as $sTable";
00312         } else {
00313             $sSubSql = $sTable;
00314         }
00315         $sQ = "select $sTable.*, oxobject2delivery.oxobjectid from $sSubSql left join (select oxobject2delivery.* from oxobject2delivery where oxobject2delivery.oxtype = 'rdfadeliveryset' ) as oxobject2delivery on oxobject2delivery.oxdeliveryid=$sTable.oxid where " . $this->getBaseObject()->getSqlActiveSnippet() . " ";
00316         $this->selectString($sQ);
00317     }
00318 }