OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxmoduleinstaller.php
Go to the documentation of this file.
1 <?php
2 
10 {
11 
15  protected $_oModuleCache;
16 
22  public function __construct(oxModuleCache $oxModuleCache = null)
23  {
24  $this->setModuleCache($oxModuleCache);
25  }
26 
32  public function setModuleCache($oModuleCache)
33  {
34  $this->_oModuleCache = $oModuleCache;
35  }
36 
42  public function getModuleCache()
43  {
44  return $this->_oModuleCache;
45  }
46 
54  public function activate(oxModule $oModule)
55  {
56  $blResult = false;
57 
58  $sModuleId = $oModule->getId();
59 
60  if ($sModuleId) {
61  $this->_addExtensions($oModule);
62  $this->_removeFromDisabledList($sModuleId);
63 
64  $this->_addTemplateBlocks($oModule->getInfo("blocks"), $sModuleId);
65  $this->_addModuleFiles($oModule->getInfo("files"), $sModuleId);
66  $this->_addTemplateFiles($oModule->getInfo("templates"), $sModuleId);
67  $this->_addModuleSettings($oModule->getInfo("settings"), $sModuleId);
68  $this->_addModuleVersion($oModule->getInfo("version"), $sModuleId);
69  $this->_addModuleEvents($oModule->getInfo("events"), $sModuleId);
70 
71  //resets cache
72  if ($this->getModuleCache()) {
73  $this->getModuleCache()->resetCache();
74 
75  }
76 
77  $this->_callEvent('onActivate', $oModule->getId());
78 
79  $blResult = true;
80  }
81 
82  return $blResult;
83  }
84 
92  public function deactivate(oxModule $oModule)
93  {
94  $blResult = false;
95 
96  $sModuleId = $oModule->getId();
97 
98  if ($sModuleId) {
99  $sModuleId = $oModule->getId();
100  $this->_callEvent('onDeactivate', $sModuleId);
101 
102  $this->_addToDisabledList($sModuleId);
103 
104  //removing recoverable options
105  $this->_deleteBlock($sModuleId);
106  $this->_deleteTemplateFiles($sModuleId);
107  $this->_deleteModuleFiles($sModuleId);
108  $this->_deleteModuleEvents($sModuleId);
109  $this->_deleteModuleVersions($sModuleId);
110 
111  //resets cache
112  if ($this->getModuleCache()) {
113  $this->getModuleCache()->resetCache();
114  }
115 
116  $blResult = true;
117  }
118 
119  return $blResult;
120  }
121 
127  public function getModulesWithExtendedClass()
128  {
129  return $this->getConfig()->getModulesWithExtendedClass();
130  }
131 
139  public function buildModuleChains($aModuleArray)
140  {
141  $aModules = array();
142  if (is_array($aModuleArray)) {
143  foreach ($aModuleArray as $sClass => $aModuleChain) {
144  $aModules[$sClass] = implode('&', $aModuleChain);
145  }
146  }
147 
148  return $aModules;
149  }
150 
160  public function diffModuleArrays($aAllModuleArray, $aRemModuleArray)
161  {
162  if (is_array($aAllModuleArray) && is_array($aRemModuleArray)) {
163  foreach ($aAllModuleArray as $sClass => $aModuleChain) {
164  if (!is_array($aModuleChain)) {
165  $aModuleChain = array($aModuleChain);
166  }
167  if (isset($aRemModuleArray[$sClass])) {
168  if (!is_array($aRemModuleArray[$sClass])) {
169  $aRemModuleArray[$sClass] = array($aRemModuleArray[$sClass]);
170  }
171  $aAllModuleArray[$sClass] = array();
172  foreach ($aModuleChain as $sModule) {
173  if (!in_array($sModule, $aRemModuleArray[$sClass])) {
174  $aAllModuleArray[$sClass][] = $sModule;
175  }
176  }
177  if (!count($aAllModuleArray[$sClass])) {
178  unset ($aAllModuleArray[$sClass]);
179  }
180  } else {
181  $aAllModuleArray[$sClass] = $aModuleChain;
182  }
183  }
184 
185  }
186 
187  return $aAllModuleArray;
188  }
189 
195  protected function _addToDisabledList($sModuleId)
196  {
197  $aDisabledModules = (array) $this->getConfig()->getConfigParam('aDisabledModules');
198 
199  $aModules = array_merge($aDisabledModules, array($sModuleId));
200  $aModules = array_unique($aModules);
201 
202  $this->_saveToConfig('aDisabledModules', $aModules, 'arr');
203  }
204 
210  protected function _deleteModule($sModuleId)
211  {
212  $aExt = $this->getConfig()->getModulesWithExtendedClass();
213 
214  $aUpdatedExt = $this->diffModuleArrays($aExt, $sModuleId);
215  $aUpdatedExt = $this->buildModuleChains($aUpdatedExt);
216 
217  $this->getConfig()->saveShopConfVar('aarr', 'aModules', $aUpdatedExt);
218  }
219 
225  protected function _deleteBlock($sModuleId)
226  {
227  $oDb = oxDb::getDb();
228  $sShopId = $this->getConfig()->getShopId();
229  $oDb->execute("DELETE FROM `oxtplblocks` WHERE `oxmodule` =" . $oDb->quote($sModuleId) . " AND `oxshopid` = " . $oDb->quote($sShopId));
230  }
231 
237  protected function _deleteTemplateFiles($sModuleId)
238  {
239  $aTemplates = (array) $this->getConfig()->getConfigParam('aModuleTemplates');
240  unset($aTemplates[$sModuleId]);
241 
242  $this->_saveToConfig('aModuleTemplates', $aTemplates);
243  }
244 
250  protected function _deleteModuleFiles($sModuleId)
251  {
252  $aFiles = (array) $this->getConfig()->getConfigParam('aModuleFiles');
253  unset($aFiles[$sModuleId]);
254 
255  $this->_saveToConfig('aModuleFiles', $aFiles);
256  }
257 
263  protected function _deleteModuleEvents($sModuleId)
264  {
265  $aEvents = (array) $this->getConfig()->getConfigParam('aModuleEvents');
266  unset($aEvents[$sModuleId]);
267 
268  $this->_saveToConfig('aModuleEvents', $aEvents);
269  }
270 
276  protected function _deleteModuleVersions($sModuleId)
277  {
278  $aVersions = (array) $this->getConfig()->getConfigParam('aModuleVersions');
279  unset($aVersions[$sModuleId]);
280 
281  $this->_saveToConfig('aModuleVersions', $aVersions);
282  }
283 
289  protected function _addExtensions(oxModule $oModule)
290  {
291  $aModules = $this->_removeNotUsedExtensions($this->getModulesWithExtendedClass(), $oModule);
292 
293  if ($oModule->hasExtendClass()) {
294  $aAddModules = $oModule->getExtensions();
295  $aModules = $this->_mergeModuleArrays($aModules, $aAddModules);
296  }
297 
298  $aModules = $this->buildModuleChains($aModules);
299 
300  $this->_saveToConfig('aModules', $aModules);
301  }
302 
312  protected function _mergeModuleArrays($aAllModuleArray, $aAddModuleArray)
313  {
314  if (is_array($aAllModuleArray) && is_array($aAddModuleArray)) {
315  foreach ($aAddModuleArray as $sClass => $aModuleChain) {
316  if (!is_array($aModuleChain)) {
317  $aModuleChain = array($aModuleChain);
318  }
319  if (isset($aAllModuleArray[$sClass])) {
320  foreach ($aModuleChain as $sModule) {
321  if (!in_array($sModule, $aAllModuleArray[$sClass])) {
322  $aAllModuleArray[$sClass][] = $sModule;
323  }
324  }
325  } else {
326  $aAllModuleArray[$sClass] = $aModuleChain;
327  }
328  }
329  }
330 
331  return $aAllModuleArray;
332  }
333 
339  protected function _removeFromDisabledList($sModuleId)
340  {
341  $aDisabledModules = (array) $this->getConfig()->getConfigParam('aDisabledModules');
342 
343  if (isset($aDisabledModules) && is_array($aDisabledModules)) {
344  $aDisabledModules = array_diff($aDisabledModules, array($sModuleId));
345  $this->_saveToConfig('aDisabledModules', $aDisabledModules, 'arr');
346  }
347  }
348 
355  protected function _addTemplateBlocks($aModuleBlocks, $sModuleId)
356  {
357  $sShopId = $this->getConfig()->getShopId();
358  $oDb = oxDb::getDb();
359 
360  if (is_array($aModuleBlocks)) {
361 
362  foreach ($aModuleBlocks as $aValue) {
363  $sOxId = oxUtilsObject::getInstance()->generateUId();
364 
365  $sTemplate = $aValue["template"];
366  $iPosition = $aValue["position"] ? $aValue["position"] : 1;
367  $sBlock = $aValue["block"];
368  $sFile = $aValue["file"];
369 
370  $sSql = "INSERT INTO `oxtplblocks` (`OXID`, `OXACTIVE`, `OXSHOPID`, `OXTEMPLATE`, `OXBLOCKNAME`, `OXPOS`, `OXFILE`, `OXMODULE`)
371  VALUES ('{$sOxId}', 1, '{$sShopId}', " . $oDb->quote($sTemplate) . ", " . $oDb->quote($sBlock) . ", " . $oDb->quote($iPosition) . ", " . $oDb->quote($sFile) . ", '{$sModuleId}')";
372 
373  $oDb->execute($sSql);
374  }
375  }
376  }
377 
384  protected function _addModuleFiles($aModuleFiles, $sModuleId)
385  {
386  $aFiles = (array) $this->getConfig()->getConfigParam('aModuleFiles');
387 
388  if (is_array($aModuleFiles)) {
389  $aFiles[$sModuleId] = array_change_key_case($aModuleFiles, CASE_LOWER);
390  }
391 
392  $this->_saveToConfig('aModuleFiles', $aFiles);
393  }
394 
401  protected function _addTemplateFiles($aModuleTemplates, $sModuleId)
402  {
403  $aTemplates = (array) $this->getConfig()->getConfigParam('aModuleTemplates');
404  if (is_array($aModuleTemplates)) {
405  $aTemplates[$sModuleId] = $aModuleTemplates;
406  }
407 
408  $this->_saveToConfig('aModuleTemplates', $aTemplates);
409  }
410 
417  protected function _addModuleSettings($aModuleSettings, $sModuleId)
418  {
419  $this->_removeNotUsedSettings($aModuleSettings, $sModuleId);
420  $oConfig = $this->getConfig();
421  $sShopId = $oConfig->getShopId();
422  $oDb = oxDb::getDb();
423 
424  if (is_array($aModuleSettings)) {
425 
426  foreach ($aModuleSettings as $aValue) {
427  $sOxId = oxUtilsObject::getInstance()->generateUId();
428 
429  $sModule = 'module:' . $sModuleId;
430  $sName = $aValue["name"];
431  $sType = $aValue["type"];
432  $sValue = is_null($oConfig->getConfigParam($sName)) ? $aValue["value"] : $oConfig->getConfigParam($sName);
433  $sGroup = $aValue["group"];
434 
435  $sConstraints = "";
436  if ($aValue["constraints"]) {
437  $sConstraints = $aValue["constraints"];
438  } elseif ($aValue["constrains"]) {
439  $sConstraints = $aValue["constrains"];
440  }
441 
442  $iPosition = $aValue["position"] ? $aValue["position"] : 1;
443 
444  $oConfig->setConfigParam($sName, $sValue);
445  $oConfig->saveShopConfVar($sType, $sName, $sValue, $sShopId, $sModule);
446 
447  $sDeleteSql = "DELETE FROM `oxconfigdisplay` WHERE OXCFGMODULE=" . $oDb->quote($sModule) . " AND OXCFGVARNAME=" . $oDb->quote($sName);
448  $sInsertSql = "INSERT INTO `oxconfigdisplay` (`OXID`, `OXCFGMODULE`, `OXCFGVARNAME`, `OXGROUPING`, `OXVARCONSTRAINT`, `OXPOS`) " .
449  "VALUES ('{$sOxId}', " . $oDb->quote($sModule) . ", " . $oDb->quote($sName) . ", " . $oDb->quote($sGroup) . ", " . $oDb->quote($sConstraints) . ", " . $oDb->quote($iPosition) . ")";
450 
451  $oDb->execute($sDeleteSql);
452  $oDb->execute($sInsertSql);
453  }
454  }
455  }
456 
463  protected function _addModuleEvents($aModuleEvents, $sModuleId)
464  {
465  $aEvents = (array) $this->getConfig()->getConfigParam('aModuleEvents');
466  if (is_array($aEvents)) {
467  $aEvents[$sModuleId] = $aModuleEvents;
468  }
469 
470  $this->_saveToConfig('aModuleEvents', $aEvents);
471  }
472 
479  protected function _addModuleVersion($sModuleVersion, $sModuleId)
480  {
481  $aVersions = (array) $this->getConfig()->getConfigParam('aModuleVersions');
482  if (is_array($aVersions)) {
483  $aVersions[$sModuleId] = $sModuleVersion;
484  }
485 
486  $this->_saveToConfig('aModuleVersions', $aVersions);
487  }
488 
495  protected function _callEvent($sEvent, $sModuleId)
496  {
497  $aModuleEvents = (array) $this->getConfig()->getConfigParam('aModuleEvents');
498 
499  if (isset($aModuleEvents[$sModuleId], $aModuleEvents[$sModuleId][$sEvent])) {
500  $mEvent = $aModuleEvents[$sModuleId][$sEvent];
501 
502  if (is_callable($mEvent)) {
503  call_user_func($mEvent);
504  }
505  }
506  }
507 
508 
517  protected function _removeNotUsedExtensions($aInstalledExtensions, oxModule $oModule)
518  {
519  $aModuleExtensions = $oModule->getExtensions();
520 
521  $aInstalledModuleExtensions = $this->_filterModuleArray($aInstalledExtensions, $oModule->getId());
522 
523  if (count($aInstalledModuleExtensions)) {
524  $aGarbage = $this->_getModuleExtensionsGarbage($aModuleExtensions, $aInstalledModuleExtensions);
525 
526  if (count($aGarbage)) {
527  $aInstalledExtensions = $this->_removeGarbage($aInstalledExtensions, $aGarbage);
528  }
529  }
530 
531  return $aInstalledExtensions;
532  }
533 
542  protected function _getModuleExtensionsGarbage($aModuleMetaDataExtensions, $aModuleInstalledExtensions)
543  {
544  $aGarbage = $aModuleInstalledExtensions;
545 
546  foreach ($aModuleMetaDataExtensions as $sClassName => $sClassPath) {
547  if (isset($aGarbage[$sClassName])) {
548  unset($aGarbage[$sClassName][array_search($sClassPath, $aGarbage[$sClassName])]);
549  if (count($aGarbage[$sClassName]) == 0) {
550  unset($aGarbage[$sClassName]);
551  }
552  }
553  }
554 
555  return $aGarbage;
556  }
557 
566  protected function _removeGarbage($aInstalledExtensions, $aGarbage)
567  {
568  foreach ($aGarbage as $sClassName => $aClassPaths) {
569  foreach ($aClassPaths as $sClassPath) {
570  if (isset($aInstalledExtensions[$sClassName])) {
571  unset($aInstalledExtensions[$sClassName][array_search($sClassPath, $aInstalledExtensions[$sClassName])]);
572  if (count($aInstalledExtensions[$sClassName]) == 0) {
573  unset($aInstalledExtensions[$sClassName]);
574  }
575  }
576  }
577  }
578 
579  return $aInstalledExtensions;
580  }
581 
588  protected function _removeNotUsedSettings($aModuleSettings, $sModuleId)
589  {
590  $aModuleConfigs = $this->_getModuleConfigs($sModuleId);
591  $aModuleSettings = $this->_parseModuleSettings($aModuleSettings);
592 
593  $aConfigsToRemove = array_diff($aModuleConfigs, $aModuleSettings);
594  if (!empty($aConfigsToRemove)) {
595  $this->_removeModuleConfigs($sModuleId, $aConfigsToRemove);
596  }
597  }
598 
606  protected function _getModuleConfigs($sModuleId)
607  {
608  $oDb = oxDb::getDb();
609  $sQuotedShopId = $oDb->quote($this->getConfig()->getShopId());
610  $sQuotedModuleId = $oDb->quote('module:' . $sModuleId);
611 
612  $sModuleConfigsQuery = "SELECT oxvarname FROM oxconfig WHERE oxmodule = $sQuotedModuleId AND oxshopid = $sQuotedShopId";
613 
614  return $oDb->getCol($sModuleConfigsQuery);
615  }
616 
624  protected function _parseModuleSettings($aModuleSettings)
625  {
626  $aSettings = array();
627 
628  if (is_array($aModuleSettings)) {
629  foreach ($aModuleSettings as $aSetting) {
630  $aSettings[] = $aSetting['name'];
631  }
632  }
633 
634  return $aSettings;
635  }
636 
643  protected function _removeModuleConfigs($sModuleId, $aConfigsToRemove)
644  {
645  $oDb = oxDb::getDb();
646  $sQuotedShopId = $oDb->quote($this->getConfig()->getShopId());
647  $sQuotedModuleId = $oDb->quote('module:' . $sModuleId);
648 
649  $aQuotedConfigsToRemove = array_map(array($oDb, 'quote'), $aConfigsToRemove);
650  $sDeleteSql = "DELETE
651  FROM `oxconfig`
652  WHERE oxmodule = $sQuotedModuleId AND
653  oxshopid = $sQuotedShopId AND
654  oxvarname IN (" . implode(", ", $aQuotedConfigsToRemove) . ")";
655 
656  $oDb->execute($sDeleteSql);
657  }
658 
667  protected function _filterModuleArray($aModules, $sModuleId)
668  {
669  $aFilteredModules = array();
670  foreach ($aModules as $sClass => $aExtend) {
671  foreach ($aExtend as $sExtendPath) {
672  if (strpos($sExtendPath, $sModuleId . "/") === 0) {
673  $aFilteredModules[$sClass][] = $sExtendPath;
674  }
675  }
676  }
677 
678  return $aFilteredModules;
679  }
680 
688  protected function _saveToConfig($sVariableName, $sVariableValue, $sVariableType = 'aarr')
689  {
690  $oConfig = $this->getConfig();
691  $oConfig->setConfigParam($sVariableName, $sVariableValue);
692  $oConfig->saveShopConfVar($sVariableType, $sVariableName, $sVariableValue);
693  }
694 }