oxdeliverysetlist.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxDeliverySetList extends oxList
00008 {
00013     private static $_instance = null;
00014 
00020     protected $_sUserId = null;
00021 
00027     protected $_sCountryId = null;
00028 
00034     protected $_oUser = null;
00035 
00041     protected $_sHomeCountry = null;
00042 
00049     public function __construct( $sObjectsInListName = 'oxdeliveryset')
00050     {
00051         $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
00052         parent::__construct( 'oxdeliveryset' );
00053     }
00054 
00062     public function setHomeCountry( $sHomeCountry )
00063     {
00064         if ( is_array( $sHomeCountry ) ) {
00065             $this->_sHomeCountry = current( $sHomeCountry );
00066         } else {
00067             $this->_sHomeCountry = $sHomeCountry;
00068         }
00069     }
00070 
00078     static function getInstance()
00079     {
00080         return oxRegistry::get("oxDeliverySetList");
00081     }
00082 
00083 
00097     protected function _getList( $oUser = null, $sCountryId = null )
00098     {
00099         // checking for current session user which gives additional restrictions for user itself, users group and country
00100         if ( $oUser === null ) {
00101             $oUser = $this->getUser();
00102         } else {
00103             //set user
00104             $this->setUser( $oUser );
00105         }
00106 
00107         $sUserId = $oUser ? $oUser->getId() : '';
00108 
00109         if ( $sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
00110 
00111             // chooseing delivery country if it is not set yet
00112             if ( !$sCountryId ) {
00113 
00114                 if ( $oUser ) {
00115                     $sCountryId = $oUser->getActiveCountry();
00116                 } else {
00117                     $sCountryId = $this->_sHomeCountry;
00118                 }
00119             }
00120 
00121             $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId ) );
00122             $this->_sUserId = $sUserId;
00123             $this->_sCountryId = $sCountryId;
00124         }
00125 
00126         $this->rewind();
00127 
00128         return $this;
00129     }
00130 
00131 
00140     protected function _getFilterSelect( $oUser, $sCountryId )
00141     {
00142         $sTable = getViewName( 'oxdeliveryset' );
00143         $sQ  = "select $sTable.* from $sTable ";
00144         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet().' ';
00145 
00146         // defining initial filter parameters
00147         $sUserId    = null;
00148         $aGroupIds  = null;
00149 
00150         // checking for current session user which gives additional restrictions for user itself, users group and country
00151         if ( $oUser ) {
00152 
00153             // user ID
00154             $sUserId = $oUser->getId();
00155 
00156             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00157             $aGroupIds = $oUser->getUserGroups();
00158         }
00159 
00160         $aIds = array();
00161         if ( count( $aGroupIds ) ) {
00162             foreach ( $aGroupIds as $oGroup ) {
00163                 $aIds[] = $oGroup->getId();
00164             }
00165         }
00166 
00167         $sUserTable    = getViewName( 'oxuser' );
00168         $sGroupTable   = getViewName( 'oxgroups' );
00169         $sCountryTable = getViewName( 'oxcountry' );
00170 
00171         $oDb = oxDb::getDb();
00172 
00173         $sCountrySql = $sCountryId?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")":'0';
00174         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")":'0';
00175         $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';
00176 
00177         $sQ .= "and (
00178             select
00179                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
00180                     $sCountrySql,
00181                     1) &&
00182                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
00183                     $sUserSql,
00184                     1) &&
00185                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
00186                     $sGroupSql,
00187                     1)
00188             )";
00189 
00190         //order by
00191         $sQ .= " order by $sTable.oxpos";
00192 
00193         return $sQ;
00194     }
00195 
00205     public function getDeliverySetList( $oUser, $sCountryId, $sDelSet = null )
00206     {
00207         $this->_getList( $oUser, $sCountryId );
00208 
00209         // if there is allready chosen delivery set we must start checking from it
00210         $aList = $this->_aArray;
00211         if ( $sDelSet && isset( $aList[$sDelSet] ) ) {
00212 
00213             //set it as first element
00214             $oDelSet = $aList[$sDelSet];
00215             unset( $aList[$sDelSet] );
00216 
00217             $aList = array_merge( array( $sDelSet => $oDelSet ), $aList );
00218         }
00219         return $aList;
00220     }
00221 
00236     public function getDeliverySetData( $sShipSet, $oUser, $oBasket )
00237     {
00238         $sActShipSet = null;
00239         $aActSets    = array();
00240         $aActPaymentList = array();
00241 
00242         if (!$oUser) {
00243             return;
00244         }
00245 
00246         $this->_getList( $oUser, $oUser->getActiveCountry() );
00247 
00248         // if there are no shipsets we dont need to load payments
00249         if ( $this->count() ) {
00250 
00251             // one selected ?
00252             if ( $sShipSet && !isset( $this->_aArray[$sShipSet] ) ) {
00253                 $sShipSet = null;
00254             }
00255 
00256             $oPayList = oxRegistry::get("oxPaymentList");
00257             $oDelList = oxRegistry::get("oxDeliveryList");
00258 
00259             $oCur = $this->getConfig()->getActShopCurrencyObject();
00260             $dBasketPrice = $oBasket->getPriceForPayment() / $oCur->rate;
00261 
00262             // checking if these ship sets available (number of possible payment methods > 0)
00263             foreach ( $this as $sShipSetId => $oShipSet ) {
00264 
00265                 $aPaymentList = $oPayList->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00266                 if ( count( $aPaymentList ) ) {
00267 
00268                     // now checking for deliveries
00269                     if ( $oDelList->hasDeliveries( $oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId ) ) {
00270                         $aActSets[$sShipSetId] = $oShipSet;
00271 
00272                         if ( !$sShipSet || ( $sShipSetId == $sShipSet ) ) {
00273                             $sActShipSet = $sShipSet = $sShipSetId;
00274                             $aActPaymentList = $aPaymentList;
00275                             $oShipSet->blSelected = true;
00276                         }
00277                     }
00278                 }
00279             }
00280         }
00281 
00282         return array( $aActSets, $sActShipSet, $aActPaymentList );
00283     }
00284 
00290     public function getUser()
00291     {
00292         if ( !$this->_oUser ) {
00293             $this->_oUser = parent::getUser();
00294         }
00295 
00296         return $this->_oUser;
00297     }
00298 
00306     public function setUser( $oUser )
00307     {
00308         $this->_oUser = $oUser;
00309     }
00310 
00317     public function loadNonRDFaDeliverySetList()
00318     {
00319         $sTable = getViewName( 'oxdeliveryset' );
00320         $sSubSql = "SELECT * FROM oxobject2delivery WHERE oxobject2delivery.OXDELIVERYID = $sTable.OXID AND oxobject2delivery.OXTYPE = 'rdfadeliveryset'";
00321         $this->selectString( "SELECT $sTable.* FROM $sTable WHERE NOT EXISTS($sSubSql) AND $sTable.OXACTIVE = 1" );
00322     }
00323 
00332     public function loadRDFaDeliverySetList($sDelId = null)
00333     {
00334         $sTable = getViewName( 'oxdeliveryset' );
00335         if ($sDelId) {
00336             $oDb = oxDb::getDb();
00337             $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";
00338         } else {
00339             $sSubSql = $sTable;
00340         }
00341         $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()." ";
00342         $this->selectString( $sQ );
00343     }
00344 
00345 }