oxshop.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxShop extends oxI18n
00009 {
00010 
00016     protected $_sClassName = 'oxshop';
00017 
00023     protected $_aMultiShopTables = null;
00024 
00025 
00029     protected $_aQueries = array();
00030 
00034     protected $_aTables = null;
00035 
00039     protected $_blMultiShopInheritCategories = false;
00040 
00046     public function setTables($aTables)
00047     {
00048         $this->_aTables = $aTables;
00049     }
00050 
00056     public function getTables()
00057     {
00058         if (is_null($this->_aTables)) {
00059             $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
00060             $aTables = array_unique($aMultilangTables);
00061             $this->setTables($aTables);
00062         }
00063 
00064         return $this->_aTables;
00065     }
00066 
00072     public function setQueries($aQueries)
00073     {
00074         $this->_aQueries = $aQueries;
00075     }
00076 
00082     public function getQueries()
00083     {
00084         return $this->_aQueries;
00085     }
00086 
00092     public function addQuery($sQuery)
00093     {
00094         $this->_aQueries[] = $sQuery;
00095     }
00096 
00100     public function __construct()
00101     {
00102         parent::__construct();
00103 
00104 
00105         $this->init('oxshops');
00106 
00107         if ($iMax = $this->getConfig()->getConfigParam('iMaxShopId')) {
00108             $this->setMaxShopId($iMax);
00109         }
00110 
00111     }
00112 
00118     public function setMultiShopTables($aMultiShopTables)
00119     {
00120         $this->_aMultiShopTables = $aMultiShopTables;
00121     }
00122 
00128     public function getMultiShopTables()
00129     {
00130         if (is_null($this->_aMultiShopTables)) {
00131             $this->_aMultiShopTables = array();
00132         }
00133 
00134         return $this->_aMultiShopTables;
00135     }
00136 
00137 
00146     public function generateViews($blMultishopInheritCategories = false, $aMallInherit = null)
00147     {
00148         $this->_prepareViewsQueries();
00149         $blSuccess = $this->_runQueries();
00150 
00151         $this->_cleanInvalidViews();
00152 
00153         return $blSuccess;
00154     }
00155 
00164     protected function _getViewSelect($sTable, $iLang)
00165     {
00167         $oMetaData = oxNew('oxDbMetaDataHandler');
00168         $aFields = $oMetaData->getSinglelangFields($sTable, $iLang);
00169         foreach ($aFields as $sCoreField => $sField) {
00170             if ($sCoreField !== $sField) {
00171                 $aFields[$sCoreField] = $sField . ' AS ' . $sCoreField;
00172             }
00173         }
00174 
00175         return implode(',', $aFields);
00176     }
00177 
00185     protected function _getViewSelectMultilang($sTable)
00186     {
00187         $aFields = array();
00188 
00190         $oMetaData = oxNew('oxDbMetaDataHandler');
00191         $aTables = array_merge(array($sTable), $oMetaData->getAllMultiTables($sTable));
00192         foreach ($aTables as $sTableKey => $sTableName) {
00193             $aTableFields = $oMetaData->getFields($sTableName);
00194             foreach ($aTableFields as $sCoreField => $sField) {
00195                 if (!isset($aFields[$sCoreField])) {
00196                     $aFields[$sCoreField] = $sField;
00197                 }
00198             }
00199         }
00200 
00201         return implode(',', $aFields);
00202     }
00203 
00211     protected function _getViewJoinAll($sTable)
00212     {
00213         $sJoin = ' ';
00214         $oMetaData = oxNew('oxDbMetaDataHandler');
00215         $aTables = $oMetaData->getAllMultiTables($sTable);
00216         if (count($aTables)) {
00217             foreach ($aTables as $sTableKey => $sTableName) {
00218                 $sJoin .= "LEFT JOIN {$sTableName} USING (OXID) ";
00219             }
00220         }
00221 
00222         return $sJoin;
00223     }
00224 
00233     protected function _getViewJoinLang($sTable, $iLang)
00234     {
00235         $sJoin = ' ';
00236         $sLangTable = getLangTableName($sTable, $iLang);
00237         if ($sLangTable && $sLangTable !== $sTable) {
00238             $sJoin .= "LEFT JOIN {$sLangTable} USING (OXID) ";
00239         }
00240 
00241         return $sJoin;
00242     }
00243 
00244 
00250     public function getDefaultCategory()
00251     {
00252         return $this->oxshops__oxdefcat->value;
00253     }
00254 
00260     public function isProductiveMode()
00261     {
00262         return (bool) $this->oxshops__oxproductive->value;
00263     }
00264 
00268     protected function _cleanInvalidViews()
00269     {
00270         $oDb = oxDb::getDb();
00271         $oLang = oxRegistry::getLang();
00272         $aLanguages = $oLang->getLanguageIds($this->getId());
00273 
00274         $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
00275         $aMultishopTables = $this->getMultiShopTables();
00276 
00277         $oLang = oxRegistry::getLang();
00278         $aAllShopLanguages = $oLang->getAllShopLanguageIds();
00279 
00281         $oViewsValidator = oxNew('oxShopViewValidator');
00282 
00283         $oViewsValidator->setShopId($this->getId());
00284         $oViewsValidator->setLanguages($aLanguages);
00285         $oViewsValidator->setAllShopLanguages($aAllShopLanguages);
00286         $oViewsValidator->setMultiLangTables($aMultilangTables);
00287         $oViewsValidator->setMultiShopTables($aMultishopTables);
00288 
00289         $aViews = $oViewsValidator->getInvalidViews();
00290 
00291         foreach ($aViews as $sView) {
00292             $oDb->execute('DROP VIEW IF EXISTS ' . $sView);
00293         }
00294     }
00295 
00299     protected function _prepareViewsQueries()
00300     {
00301         $oLang = oxRegistry::getLang();
00302         $aLanguages = $oLang->getLanguageIds($this->getId());
00303 
00304         $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
00305         $aTables = $this->getTables();
00306         $iShopId = $this->getId();
00307         foreach ($aTables as $sTable) {
00308             $this->createViewQuery($sTable);
00309             if (in_array($sTable, $aMultilangTables)) {
00310                 $this->createViewQuery($sTable, $aLanguages);
00311             }
00312         }
00313     }
00314 
00321     public function createViewQuery($sTable, $aLanguages = null)
00322     {
00323         $sDefaultLangAddition = '';
00324         $sShopAddition = '';
00325         $sStart = 'CREATE OR REPLACE SQL SECURITY INVOKER VIEW';
00326 
00327         if (!is_array($aLanguages)) {
00328             $aLanguages = array(null => null);
00329         }
00330 
00331         foreach ($aLanguages as $iLang => $sLang) {
00332             $sLangAddition = $sLang === null ? $sDefaultLangAddition : "_{$sLang}";
00333 
00334             $sViewTable = "oxv_{$sTable}{$sLangAddition}";
00335 
00336             if ($sLang === null) {
00337                 $sFields = $this->_getViewSelectMultilang($sTable);
00338                 $sJoin = $this->_getViewJoinAll($sTable);
00339             } else {
00340                 $sFields = $this->_getViewSelect($sTable, $iLang);
00341                 $sJoin = $this->_getViewJoinLang($sTable, $iLang);
00342             }
00343 
00344             $sQuery = "{$sStart} `{$sViewTable}` AS SELECT {$sFields} FROM {$sTable}{$sJoin}";
00345             $this->addQuery($sQuery);
00346 
00347         }
00348 
00349     }
00350 
00357     protected function _runQueries()
00358     {
00359         $oDb = oxDb::getDb();
00360         $aQueries = $this->getQueries();
00361         $bSuccess = true;
00362         foreach ($aQueries as $sQuery) {
00363             if (!$oDb->execute($sQuery)) {
00364                 $bSuccess = false;
00365             }
00366         }
00367 
00368         return $bSuccess;
00369     }
00370 
00371 }