OXID eShop CE  4.9.6
 All Classes Files Functions Variables Pages
oxshop.php
Go to the documentation of this file.
1 <?php
2 
8 class oxShop extends oxI18n
9 {
10 
16  protected $_sClassName = 'oxshop';
17 
23  protected $_aMultiShopTables = null;
24 
25 
29  protected $_aQueries = array();
30 
34  protected $_aTables = null;
35 
39  protected $_blMultiShopInheritCategories = false;
40 
46  public function setTables($aTables)
47  {
48  $this->_aTables = $aTables;
49  }
50 
56  public function getTables()
57  {
58  if (is_null($this->_aTables)) {
59  $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
60  $aTables = array_unique($aMultilangTables);
61  $this->setTables($aTables);
62  }
63 
64  return $this->_aTables;
65  }
66 
72  public function setQueries($aQueries)
73  {
74  $this->_aQueries = $aQueries;
75  }
76 
82  public function getQueries()
83  {
84  return $this->_aQueries;
85  }
86 
92  public function addQuery($sQuery)
93  {
94  $this->_aQueries[] = $sQuery;
95  }
96 
100  public function __construct()
101  {
103 
104 
105  $this->init('oxshops');
106 
107  if ($iMax = $this->getConfig()->getConfigParam('iMaxShopId')) {
108  $this->setMaxShopId($iMax);
109  }
110 
111  }
112 
118  public function setMultiShopTables($aMultiShopTables)
119  {
120  $this->_aMultiShopTables = $aMultiShopTables;
121  }
122 
128  public function getMultiShopTables()
129  {
130  if (is_null($this->_aMultiShopTables)) {
131  $this->_aMultiShopTables = array();
132  }
133 
135  }
136 
137 
146  public function generateViews($blMultishopInheritCategories = false, $aMallInherit = null)
147  {
148  $this->_prepareViewsQueries();
149  $blSuccess = $this->_runQueries();
150 
151  $this->_cleanInvalidViews();
152 
153  return $blSuccess;
154  }
155 
164  protected function _getViewSelect($sTable, $iLang)
165  {
167  $oMetaData = oxNew('oxDbMetaDataHandler');
168  $aFields = $oMetaData->getSinglelangFields($sTable, $iLang);
169  foreach ($aFields as $sCoreField => $sField) {
170  if ($sCoreField !== $sField) {
171  $aFields[$sCoreField] = $sField . ' AS ' . $sCoreField;
172  }
173  }
174 
175  return implode(',', $aFields);
176  }
177 
185  protected function _getViewSelectMultilang($sTable)
186  {
187  $aFields = array();
188 
190  $oMetaData = oxNew('oxDbMetaDataHandler');
191  $aTables = array_merge(array($sTable), $oMetaData->getAllMultiTables($sTable));
192  foreach ($aTables as $sTableKey => $sTableName) {
193  $aTableFields = $oMetaData->getFields($sTableName);
194  foreach ($aTableFields as $sCoreField => $sField) {
195  if (!isset($aFields[$sCoreField])) {
196  $aFields[$sCoreField] = $sField;
197  }
198  }
199  }
200 
201  return implode(',', $aFields);
202  }
203 
211  protected function _getViewJoinAll($sTable)
212  {
213  $sJoin = ' ';
214  $oMetaData = oxNew('oxDbMetaDataHandler');
215  $aTables = $oMetaData->getAllMultiTables($sTable);
216  if (count($aTables)) {
217  foreach ($aTables as $sTableKey => $sTableName) {
218  $sJoin .= "LEFT JOIN {$sTableName} USING (OXID) ";
219  }
220  }
221 
222  return $sJoin;
223  }
224 
233  protected function _getViewJoinLang($sTable, $iLang)
234  {
235  $sJoin = ' ';
236  $sLangTable = getLangTableName($sTable, $iLang);
237  if ($sLangTable && $sLangTable !== $sTable) {
238  $sJoin .= "LEFT JOIN {$sLangTable} USING (OXID) ";
239  }
240 
241  return $sJoin;
242  }
243 
244 
250  public function getDefaultCategory()
251  {
252  return $this->oxshops__oxdefcat->value;
253  }
254 
260  public function isProductiveMode()
261  {
262  return (bool) $this->oxshops__oxproductive->value;
263  }
264 
268  protected function _cleanInvalidViews()
269  {
270  $oDb = oxDb::getDb();
271  $oLang = oxRegistry::getLang();
272  $aLanguages = $oLang->getLanguageIds($this->getId());
273 
274  $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
275  $aMultishopTables = $this->getMultiShopTables();
276 
277  $oLang = oxRegistry::getLang();
278  $aAllShopLanguages = $oLang->getAllShopLanguageIds();
279 
281  $oViewsValidator = oxNew('oxShopViewValidator');
282 
283  $oViewsValidator->setShopId($this->getId());
284  $oViewsValidator->setLanguages($aLanguages);
285  $oViewsValidator->setAllShopLanguages($aAllShopLanguages);
286  $oViewsValidator->setMultiLangTables($aMultilangTables);
287  $oViewsValidator->setMultiShopTables($aMultishopTables);
288 
289  $aViews = $oViewsValidator->getInvalidViews();
290 
291  foreach ($aViews as $sView) {
292  $oDb->execute('DROP VIEW IF EXISTS ' . $sView);
293  }
294  }
295 
299  protected function _prepareViewsQueries()
300  {
301  $oLang = oxRegistry::getLang();
302  $aLanguages = $oLang->getLanguageIds($this->getId());
303 
304  $aMultilangTables = oxRegistry::getLang()->getMultiLangTables();
305  $aTables = $this->getTables();
306  $iShopId = $this->getId();
307  foreach ($aTables as $sTable) {
308  $this->createViewQuery($sTable);
309  if (in_array($sTable, $aMultilangTables)) {
310  $this->createViewQuery($sTable, $aLanguages);
311  }
312  }
313  }
314 
321  public function createViewQuery($sTable, $aLanguages = null)
322  {
323  $sDefaultLangAddition = '';
324  $sShopAddition = '';
325  $sStart = 'CREATE OR REPLACE SQL SECURITY INVOKER VIEW';
326 
327  if (!is_array($aLanguages)) {
328  $aLanguages = array(null => null);
329  }
330 
331  foreach ($aLanguages as $iLang => $sLang) {
332  $sLangAddition = $sLang === null ? $sDefaultLangAddition : "_{$sLang}";
333 
334  $sViewTable = "oxv_{$sTable}{$sLangAddition}";
335 
336  if ($sLang === null) {
337  $sFields = $this->_getViewSelectMultilang($sTable);
338  $sJoin = $this->_getViewJoinAll($sTable);
339  } else {
340  $sFields = $this->_getViewSelect($sTable, $iLang);
341  $sJoin = $this->_getViewJoinLang($sTable, $iLang);
342  }
343 
344  $sQuery = "{$sStart} `{$sViewTable}` AS SELECT {$sFields} FROM {$sTable}{$sJoin}";
345  $this->addQuery($sQuery);
346 
347  }
348 
349  }
350 
357  protected function _runQueries()
358  {
359  $oDb = oxDb::getDb();
360  $aQueries = $this->getQueries();
361  $bSuccess = true;
362  foreach ($aQueries as $sQuery) {
363  if (!$oDb->execute($sQuery)) {
364  $bSuccess = false;
365  }
366  }
367 
368  return $bSuccess;
369  }
370 
371 }