OXID eShop CE  4.10.7
 All Classes Namespaces Files Functions Variables Pages
oxmodule.php
Go to the documentation of this file.
1 <?php
2 
9 class oxModule extends oxSuperCfg
10 {
11 
17  protected $_aModule = array();
18 
24  protected $_blMetadata = false;
25 
31  protected $_blRegistered = false;
32 
38  public function setModuleData($aModule)
39  {
40  $this->_aModule = $aModule;
41  }
42 
50  public function load($sModuleId)
51  {
52  $sModulePath = $this->getModuleFullPath($sModuleId);
53  $sMetadataPath = $sModulePath . "/metadata.php";
54 
55  if ($sModulePath && file_exists($sMetadataPath) && is_readable($sMetadataPath)) {
56  $aModule = array();
57  include $sMetadataPath;
58  $this->_aModule = $aModule;
59  $this->_blRegistered = true;
60  $this->_blMetadata = true;
61  $this->_aModule['active'] = $this->isActive();
62 
63  return true;
64  }
65 
66  return false;
67  }
68 
76  public function loadByDir($sModuleDir)
77  {
78  $sModuleId = null;
79  $aModulePaths = $this->getModulePaths();
80 
81  if (is_array($aModulePaths)) {
82  $sModuleId = array_search($sModuleDir, $aModulePaths);
83  }
84 
85  // if no module id defined, using module dir as id
86  if (!$sModuleId) {
87  $sModuleId = $sModuleDir;
88  }
89 
90  return $this->load($sModuleId);
91  }
92 
98  public function getDescription()
99  {
100  $iLang = oxRegistry::getLang()->getTplLanguage();
101 
102  return $this->getInfo("description", $iLang);
103  }
104 
110  public function getTitle()
111  {
112  $iLang = oxRegistry::getLang()->getTplLanguage();
113 
114  return $this->getInfo("title", $iLang);
115  }
116 
122  public function getId()
123  {
124  return $this->_aModule['id'];
125  }
126 
132  public function getExtensions()
133  {
134  return isset($this->_aModule['extend']) ? $this->_aModule['extend'] : array();
135  }
136 
142  public function getFiles()
143  {
144  return isset($this->_aModule['files']) ? $this->_aModule['files'] : array();
145  }
146 
154  public function getIdByPath($sModule)
155  {
156  $myConfig = $this->getConfig();
157  $aModulePaths = $myConfig->getConfigParam('aModulePaths');
158  $sModuleId = null;
159  if (is_array($aModulePaths)) {
160  foreach ($aModulePaths as $sId => $sPath) {
161  if (strpos($sModule, $sPath . "/") === 0) {
162  $sModuleId = $sId;
163  }
164  }
165  }
166  if (!$sModuleId) {
167  $sModuleId = substr($sModule, 0, strpos($sModule, "/"));
168  }
169  if (!$sModuleId) {
170  $sModuleId = $sModule;
171  }
172 
173  return $sModuleId;
174  }
175 
185  public function getInfo($sName, $iLang = null)
186  {
187  if (isset($this->_aModule[$sName])) {
188 
189  if ($iLang !== null && is_array($this->_aModule[$sName])) {
190  $sValue = null;
191 
192  $sLang = oxRegistry::getLang()->getLanguageAbbr($iLang);
193 
194  if (!empty($this->_aModule[$sName])) {
195  if (!empty($this->_aModule[$sName][$sLang])) {
196  $sValue = $this->_aModule[$sName][$sLang];
197  } elseif (!empty($this->_aModule['lang'])) {
198  // trying to get value according default language
199  $sValue = $this->_aModule[$sName][$this->_aModule['lang']];
200  } else {
201  // returning first array value
202  $sValue = reset($this->_aModule[$sName]);
203  }
204 
205  return $sValue;
206  }
207  } else {
208  return $this->_aModule[$sName];
209  }
210  }
211  }
212 
218  public function isActive()
219  {
220  $blActive = false;
221  $sId = $this->getId();
222  if (!is_null($sId)) {
223  $blActive = !$this->_isInDisabledList($sId);
224  if ($blActive && $this->hasExtendClass()) {
225  $blActive = $this->_isExtensionsActive();
226  }
227  }
228 
229  return $blActive;
230  }
231 
237  public function hasExtendClass()
238  {
239  $aExtensions = $this->getExtensions();
240 
241  return isset($aExtensions)
242  && is_array($aExtensions)
243  && !empty($aExtensions);
244  }
245 
251  public function isRegistered()
252  {
253  return $this->_blRegistered;
254  }
255 
261  public function hasMetadata()
262  {
263  return $this->_blMetadata;
264  }
265 
271  public function getMetadataPath()
272  {
273  $sModulePath = $this->getModuleFullPath();
274  if (substr($sModulePath, -1) != DIRECTORY_SEPARATOR) {
275  $sModulePath .= DIRECTORY_SEPARATOR;
276  }
277 
278  return $sModulePath . 'metadata.php';
279  }
280 
288  public function getModulePath($sModuleId = null)
289  {
290  if (!$sModuleId) {
291  $sModuleId = $this->getId();
292  }
293 
294  $aModulePaths = $this->getModulePaths();
295  $sModulePath = $aModulePaths[$sModuleId];
296 
297  // if still no module dir, try using module ID as dir name
298  if (!$sModulePath && is_dir($this->getConfig()->getModulesDir() . $sModuleId)) {
299  $sModulePath = $sModuleId;
300  }
301 
302  return $sModulePath;
303  }
304 
312  public function getModuleFullPath($sModuleId = null)
313  {
314  if (!$sModuleId) {
315  $sModuleId = $this->getId();
316  }
317 
318  if ($sModuleDir = $this->getModulePath($sModuleId)) {
319  return $this->getConfig()->getModulesDir() . $sModuleDir;
320  }
321 
322  return false;
323  }
324 
330  public function getModulePaths()
331  {
332  return $this->getConfig()->getConfigParam('aModulePaths');
333  }
334 
342  public function getTemplates($sModuleId = null)
343  {
344  if (is_null($sModuleId)) {
345  $sModuleId = $this->getId();
346  }
347 
348  if (!$sModuleId) {
349  return array();
350  }
351 
352  $sShopId = $this->getConfig()->getShopId();
353 
354  $aTemplates = oxDb::getDb()->getCol("SELECT oxtemplate FROM oxtplblocks WHERE oxmodule = '$sModuleId' AND oxshopid = '$sShopId'");
355 
356  return $aTemplates;
357  }
358 
367  protected function _countActivatedExtensions($aModuleExtensions, $aInstalledExtensions)
368  {
369  $iActive = 0;
370  foreach ($aModuleExtensions as $sClass => $mExtension) {
371  if (is_array($mExtension)) {
372  foreach ($mExtension as $sExtension) {
373  if ((isset($aInstalledExtensions[$sClass]) && in_array($sExtension, $aInstalledExtensions[$sClass]))) {
374  $iActive++;
375  }
376  }
377  } elseif ((isset($aInstalledExtensions[$sClass]) && in_array($mExtension, $aInstalledExtensions[$sClass]))) {
378  $iActive++;
379  }
380  }
381 
382  return $iActive;
383  }
384 
392  protected function _countExtensions($aModuleExtensions)
393  {
394  $iCount = 0;
395  foreach ($aModuleExtensions as $mExtensions) {
396  if (is_array($mExtensions)) {
397  $iCount += count($mExtensions);
398  } else {
399  $iCount++;
400  }
401  }
402 
403  return $iCount;
404  }
405 
411  protected function _isExtensionsActive()
412  {
413  $aModuleExtensions = $this->getExtensions();
414 
415  $aInstalledExtensions = $this->getConfig()->getModulesWithExtendedClass();
416  $iModuleExtensionsCount = $this->_countExtensions($aModuleExtensions);
417  $iActivatedModuleExtensionsCount = $this->_countActivatedExtensions($aModuleExtensions, $aInstalledExtensions);
418  $blActive = $iModuleExtensionsCount > 0 && $iActivatedModuleExtensionsCount == $iModuleExtensionsCount;
419 
420  return $blActive;
421  }
422 
430  protected function _isInDisabledList($sId)
431  {
432  $blInDisabledList = false;
433 
434  $aDisabledModules = (array) $this->getConfig()->getConfigParam('aDisabledModules');
435  if (in_array($sId, $aDisabledModules)) {
436  $blInDisabledList = true;
437  }
438 
439  return $blInDisabledList;
440  }
441 }