oxdeliverysetlist.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxDeliverySetList extends oxList
00007 {
00012     private static $_instance = null;
00013 
00019     protected $_sUserId = null;
00020 
00026     protected $_sCountryId = null;
00027 
00033     protected $_oUser = null;
00034 
00040     protected $_sHomeCountry = null;
00041 
00048     public function __construct( $sObjectsInListName = 'oxdeliveryset')
00049     {
00050         $this->setHomeCountry( $this->getConfig()->getConfigParam( 'aHomeCountry' ) );
00051         parent::__construct( 'oxdeliveryset' );
00052     }
00053 
00061     public function setHomeCountry( $sHomeCountry )
00062     {
00063         if ( is_array( $sHomeCountry ) ) {
00064             $this->_sHomeCountry = current( $sHomeCountry );
00065         } else {
00066             $this->_sHomeCountry = $sHomeCountry;
00067         }
00068     }
00069 
00075     static function getInstance()
00076     {
00077         if ( defined('OXID_PHP_UNIT')) {
00078             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00079                 return $oClassMod;
00080             } else {
00081                  $inst = oxNew( 'oxDeliverySetList' );
00082                  modInstances::addMod( __CLASS__, $inst );
00083                  return $inst;
00084             }
00085         }
00086 
00087 
00088         if ( !isset( self::$_instance ) ) {
00089             // allow modules
00090             self::$_instance = oxNew( 'oxDeliverySetList' );
00091         }
00092         return self::$_instance;
00093     }
00094 
00095 
00109     protected function _getList( $oUser = null, $sCountryId = null )
00110     {
00111         // checking for current session user which gives additional restrictions for user itself, users group and country
00112         if ( $oUser === null ) {
00113             $oUser = $this->getUser();
00114         } else {
00115             //set user
00116             $this->setUser( $oUser );
00117         }
00118 
00119         $sUserId = $oUser ? $oUser->getId() : '';
00120 
00121         if ( $sUserId !== $this->_sUserId || $sCountryId !== $this->_sCountryId) {
00122 
00123             // chooseing delivery country if it is not set yet
00124             if ( !$sCountryId ) {
00125 
00126                 if ( $oUser ) {
00127                     $sCountryId = $oUser->getActiveCountry();
00128                 } else {
00129                     $sCountryId = $this->_sHomeCountry;
00130                 }
00131             }
00132 
00133             $this->selectString( $this->_getFilterSelect( $oUser, $sCountryId ) );
00134             $this->_sUserId = $sUserId;
00135             $this->_sCountryId = $sCountryId;
00136         }
00137 
00138         $this->rewind();
00139 
00140         return $this;
00141     }
00142 
00143 
00152     protected function _getFilterSelect( $oUser, $sCountryId )
00153     {
00154         $sTable = getViewName( 'oxdeliveryset' );
00155         $sQ  = "select $sTable.* from $sTable ";
00156         $sQ .= "where ".$this->getBaseObject()->getSqlActiveSnippet().' ';
00157 
00158         // defining initial filter parameters
00159         $sUserId    = null;
00160         $aGroupIds  = null;
00161 
00162         // checking for current session user which gives additional restrictions for user itself, users group and country
00163         if ( $oUser ) {
00164 
00165             // user ID
00166             $sUserId = $oUser->getId();
00167 
00168             // user groups ( maybe would be better to fetch by function oxuser::getUserGroups() ? )
00169             $aGroupIds = $oUser->getUserGroups();
00170         }
00171 
00172         $aIds = array();
00173         if ( count( $aGroupIds ) ) {
00174             foreach ( $aGroupIds as $oGroup ) {
00175                 $aIds[] = $oGroup->getId();
00176             }
00177         }
00178 
00179         $sUserTable    = getViewName( 'oxuser' );
00180         $sGroupTable   = getViewName( 'oxgroups' );
00181         $sCountryTable = getViewName( 'oxcountry' );
00182 
00183         $oDb = oxDb::getDb();
00184 
00185         $sCountrySql = $sCountryId?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sCountryId).")":'0';
00186         $sUserSql    = $sUserId   ?"EXISTS(select oxobject2delivery.oxid from oxobject2delivery where oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' and oxobject2delivery.OXOBJECTID=".$oDb->quote($sUserId).")":'0';
00187         $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';
00188 
00189         $sQ .= "and (
00190             select
00191                 if(EXISTS(select 1 from oxobject2delivery, $sCountryTable where $sCountryTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelset' LIMIT 1),
00192                     $sCountrySql,
00193                     1) &&
00194                 if(EXISTS(select 1 from oxobject2delivery, $sUserTable where $sUserTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetu' LIMIT 1),
00195                     $sUserSql,
00196                     1) &&
00197                 if(EXISTS(select 1 from oxobject2delivery, $sGroupTable where $sGroupTable.oxid=oxobject2delivery.oxobjectid and oxobject2delivery.oxdeliveryid=$sTable.OXID and oxobject2delivery.oxtype='oxdelsetg' LIMIT 1),
00198                     $sGroupSql,
00199                     1)
00200             )";
00201 
00202         //order by
00203         $sQ .= " order by $sTable.oxpos";
00204 
00205         return $sQ;
00206     }
00207 
00217     public function getDeliverySetList( $oUser, $sCountryId, $sDelSet = null )
00218     {
00219         $this->_getList( $oUser, $sCountryId );
00220 
00221         // if there is allready chosen delivery set we must start checking from it
00222         $aList = $this->_aArray;
00223         if ( $sDelSet && isset( $aList[$sDelSet] ) ) {
00224 
00225             //set it as first element
00226             $oDelSet = $aList[$sDelSet];
00227             unset( $aList[$sDelSet] );
00228 
00229             $aList = array_merge( array( $sDelSet => $oDelSet ), $aList );
00230         }
00231         return $aList;
00232     }
00233 
00248     public function getDeliverySetData( $sShipSet, $oUser, $oBasket )
00249     {
00250         $sActShipSet = null;
00251         $aActSets    = array();
00252         $aActPaymentList = array();
00253 
00254         if (!$oUser) {
00255             return;
00256         }
00257 
00258         $this->_getList( $oUser, $oUser->getActiveCountry() );
00259 
00260         // if there are no shipsets we dont need to load payments
00261         if ( $this->count() ) {
00262 
00263             // one selected ?
00264             if ( $sShipSet && !isset( $this->_aArray[$sShipSet] ) ) {
00265                 $sShipSet = null;
00266             }
00267 
00268             $oPayList = oxPaymentList::getInstance();
00269             $oDelList = oxDeliveryList::getInstance();
00270 
00271             $oCur = $this->getConfig()->getActShopCurrencyObject();
00272             $dBasketPrice = $oBasket->getPriceForPayment() / $oCur->rate;
00273 
00274             // checking if these ship sets available (number of possible payment methods > 0)
00275             foreach ( $this as $sShipSetId => $oShipSet ) {
00276 
00277                 $aPaymentList = $oPayList->getPaymentList( $sShipSetId, $dBasketPrice, $oUser );
00278                 if ( count( $aPaymentList ) ) {
00279 
00280                     // now checking for deliveries
00281                     if ( $oDelList->hasDeliveries( $oBasket, $oUser, $oUser->getActiveCountry(), $sShipSetId ) ) {
00282                         $aActSets[$sShipSetId] = $oShipSet;
00283 
00284                         if ( !$sShipSet || ( $sShipSetId == $sShipSet ) ) {
00285                             $sActShipSet = $sShipSet = $sShipSetId;
00286                             $aActPaymentList = $aPaymentList;
00287                             $oShipSet->blSelected = true;
00288                         }
00289                     }
00290                 }
00291             }
00292         }
00293 
00294         return array( $aActSets, $sActShipSet, $aActPaymentList );
00295     }
00296 
00302     public function getUser()
00303     {
00304         if ( !$this->_oUser ) {
00305             $this->_oUser = parent::getUser();
00306         }
00307 
00308         return $this->_oUser;
00309     }
00310 
00318     public function setUser( $oUser )
00319     {
00320         $this->_oUser = $oUser;
00321     }
00322 }