00001 <?php
00002
00007 class oxModule extends oxSuperCfg
00008 {
00014 protected $_aModule = array();
00015
00021 protected $_blMetadata = false;
00022
00028 protected $_blRegistered = false;
00029
00035 protected $_blFile = false;
00036
00042 protected $_blLegacy = false;
00043
00051 public function setModuleData( $aModule )
00052 {
00053 $this->_aModule = $aModule;
00054 }
00055
00063 public function load( $sModuleId )
00064 {
00065 if ( $this->loadModule($sModuleId) ) return true;
00066
00067 if ( $this->loadLegacyModule($sModuleId) ) return true;
00068
00069 if ( $this->loadUnregisteredModule($sModuleId) ) return true;
00070
00071 return false;
00072 }
00073
00081 public function loadByDir( $sModuleDir )
00082 {
00083 $sModuleId = null;
00084 $aModulePaths = $this->getModulePaths();
00085
00086 if ( is_array($aModulePaths) ) {
00087 $sModuleId = array_search( $sModuleDir, $aModulePaths);
00088 }
00089
00090
00091 if ( !$sModuleId ) {
00092 $sModuleId = $sModuleDir;
00093 }
00094
00095 return $this->load( $sModuleId );
00096 }
00097
00105 public function loadModule( $sModuleId )
00106 {
00107 $sModuleDir = $this->getModulePath( $sModuleId );
00108
00109 $sFilePath = $this->getConfig()->getModulesDir() . $sModuleDir . "/metadata.php";
00110 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00111 $aModule = array();
00112 include $sFilePath;
00113 $this->_aModule = $aModule;
00114 $this->_blLegacy = false;
00115 $this->_blRegistered = true;
00116 $this->_blMetadata = true;
00117 $this->_blFile = false;
00118 $this->_aModule['active'] = $this->isActive() || !$this->isExtended();
00119 return true;
00120 }
00121 return false;
00122 }
00123
00131 public function loadLegacyModule( $sModuleId )
00132 {
00133 $aLegacyModules = $this->getLegacyModules();
00134 $sModuleDir = $this->getModulePath( $sModuleId );
00135
00136
00137 if ( isset( $aLegacyModules[$sModuleId] ) ) {
00138 $this->_aModule = $aLegacyModules[$sModuleId];
00139 $this->_blLegacy = true;
00140 $this->_blRegistered = true;
00141 $this->_blMetadata = false;
00142 $this->_blFile = empty( $sModuleDir );
00143 $this->_aModule['active'] = $this->isActive();
00144 return true;
00145 }
00146 return false;
00147 }
00148
00156 public function loadUnregisteredModule( $sModuleId )
00157 {
00158 $oConfig = $this->getConfig();
00159 $aModules = $this->getAllModules();
00160
00161 $sModuleDir = $this->getModulePath( $sModuleId );
00162
00163 $sFilePath = $oConfig->getModulesDir() . $sModuleDir ;
00164 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00165 $this->_aModule = array();
00166 $this->_aModule['id'] = $sModuleId;
00167 $this->_aModule['title'] = $sModuleId;
00168 $this->_aModule['extend'] = $this->buildModuleChains($this->filterModuleArray($aModules, $sModuleId));
00169 $this->_blLegacy = true;
00170 $this->_blRegistered = false;
00171 $this->_blMetadata = false;
00172 $this->_blFile = !is_dir($oConfig->getModulesDir() . $sModuleId);
00173 $this->_aModule['active'] = $this->isActive();
00174 return true;
00175 }
00176 return false;
00177 }
00178
00184 public function getDescription()
00185 {
00186 $iLang = oxRegistry::getLang()->getTplLanguage();
00187
00188 return $this->getInfo( "description", $iLang );
00189 }
00190
00196 public function getTitle()
00197 {
00198 $iLang = oxRegistry::getLang()->getTplLanguage();
00199
00200 return $this->getInfo( "title", $iLang );
00201 }
00202
00208 public function getId()
00209 {
00210 return $this->_aModule['id'];
00211 }
00212
00220 public function getIdByPath($sModule)
00221 {
00222 $myConfig = $this->getConfig();
00223 $aModulePaths = $myConfig->getConfigParam( 'aModulePaths' );
00224 $sModuleId = null;
00225 if (is_array( $aModulePaths )) {
00226 foreach ($aModulePaths as $sId => $sPath) {
00227 if (strpos($sModule, $sPath."/") === 0 ) {
00228 $sModuleId = $sId;
00229 }
00230 }
00231 }
00232 if (!$sModuleId) {
00233 $sModuleId = substr( $sModule, 0, strpos( $sModule, "/" ) );
00234 }
00235 if (!$sModuleId) {
00236 $sModuleId = $sModule;
00237 }
00238 return $sModuleId;
00239 }
00240
00250 public function getInfo( $sName, $iLang = null )
00251 {
00252 if (isset($this->_aModule[$sName])) {
00253
00254 if ( $iLang !== null && is_array($this->_aModule[$sName]) ) {
00255 $sValue = null;
00256
00257 $sLang = oxRegistry::getLang()->getLanguageAbbr( $iLang );
00258
00259 if ( !empty($this->_aModule[$sName]) ) {
00260 if ( !empty( $this->_aModule[$sName][$sLang] ) ) {
00261 $sValue = $this->_aModule[$sName][$sLang];
00262 } elseif ( !empty($this->_aModule['lang']) ) {
00263
00264 $sValue = $this->_aModule[$sName][$this->_aModule['lang']];
00265 } else {
00266
00267 $sValue = reset( $this->_aModule[$sName] );
00268 }
00269
00270 return $sValue;
00271 }
00272 } else {
00273 return $this->_aModule[$sName];
00274 }
00275 }
00276 }
00277
00283 public function isActive()
00284 {
00285 $blActive = false;
00286 $sId = $this->getId();
00287 if (isset($sId)) {
00288 if ( is_array($this->_aModule['extend']) && !empty($this->_aModule['extend']) ) {
00289 $aAddModules = $this->_aModule['extend'];
00290 $aInstalledModules = $this->getAllModules();
00291 $iClCount = count($aAddModules);
00292 $iActive = 0;
00293
00294 foreach ($aAddModules as $sClass => $sModule) {
00295 if ( (isset($aInstalledModules[$sClass]) && in_array($sModule, $aInstalledModules[$sClass])) ) {
00296 $iActive ++;
00297 }
00298 }
00299 $blActive = $iClCount > 0 && $iActive == $iClCount;
00300
00301 $aDisabledModules = $this->getDisabledModules();
00302 if ( $blActive && ( is_array($aDisabledModules) && in_array($sId, $aDisabledModules) ) ) {
00303 $blActive = false;
00304 }
00305 } else {
00306
00307 $aDisabledModules = $this->getDisabledModules();
00308 if ( is_array($aDisabledModules) && !in_array($sId, $aDisabledModules) ) {
00309 $blActive = true;
00310 }
00311 }
00312 }
00313
00314 return $blActive;
00315 }
00316
00322 public function isExtended()
00323 {
00324 if ($this->hasMetadata() && !empty($this->_aModule['extend'])) {
00325 return true;
00326 }
00327
00328 return false;
00329 }
00330
00336 public function isLegacy()
00337 {
00338 return $this->_blLegacy;
00339 }
00340
00346 public function isRegistered()
00347 {
00348 return $this->_blRegistered;
00349 }
00350
00356 public function hasMetadata()
00357 {
00358 return $this->_blMetadata;
00359 }
00360
00366 public function isFile()
00367 {
00368 return $this->_blFile;
00369 }
00370
00376 public function activate()
00377 {
00378 if (isset($this->_aModule['extend']) && is_array($this->_aModule['extend'])) {
00379 $oConfig = oxRegistry::getConfig();
00380 $aAddModules = $this->_aModule['extend'];
00381 $sModuleId = $this->getId();
00382
00383 $aInstalledModules = $this->getAllModules();
00384 $aDisabledModules = $this->getDisabledModules();
00385
00386 $aModules = $this->mergeModuleArrays($aInstalledModules, $aAddModules);
00387 $aModules = $this->buildModuleChains($aModules);
00388
00389 $oConfig->setConfigParam('aModules', $aModules);
00390 $oConfig->saveShopConfVar('aarr', 'aModules', $aModules);
00391
00392 if ( isset($aDisabledModules) && is_array($aDisabledModules) ) {
00393 $aDisabledModules = array_diff($aDisabledModules, array($sModuleId));
00394 $oConfig->setConfigParam('aDisabledModules', $aDisabledModules);
00395 $oConfig->saveShopConfVar('arr', 'aDisabledModules', $aDisabledModules);
00396 }
00397
00398
00399 if ( !$this->_hasInstalledTemplateBlocks($sModuleId) ) {
00400
00401 $this->_addTemplateBlocks( $this->getInfo("blocks") );
00402 } else {
00403
00404 $this->_changeBlockStatus( $sModuleId, "1" );
00405 }
00406
00407
00408 $this->_addModuleFiles($this->getInfo("files") );
00409
00410
00411 $this->_addTemplateFiles($this->getInfo("templates") );
00412
00413
00414 $this->_addModuleSettings($this->getInfo("settings"));
00415
00416
00417 $this->_addModuleVersion($this->getInfo("version"));
00418
00419
00420 $this->_addModuleEvents($this->getInfo("events"));
00421
00422
00423 $this->_resetCache();
00424
00425
00426 $this->_callEvent('onActivate', $sModuleId);
00427
00428 return true;
00429 }
00430 return false;
00431 }
00432
00440 public function deactivate($sModuleId = null)
00441 {
00442 $oConfig = $this->getConfig();
00443 if (!isset($sModuleId)) {
00444 $sModuleId = $this->getId();
00445 }
00446 if (isset($sModuleId)) {
00447
00448 $this->_callEvent('onDeactivate', $sModuleId);
00449
00450 $aDisabledModules = $this->getDisabledModules();
00451
00452 if (!is_array($aDisabledModules)) {
00453 $aDisabledModules = array();
00454 }
00455 $aModules = array_merge($aDisabledModules, array($sModuleId));
00456 $aModules = array_unique($aModules);
00457
00458 $oConfig->saveShopConfVar('arr', 'aDisabledModules', $aModules);
00459 $oConfig->setConfigParam('aDisabledModules', $aModules);
00460
00461
00462 $this->_changeBlockStatus( $sModuleId );
00463
00464
00465 $this->_resetCache();
00466
00467
00468 return true;
00469 }
00470 return false;
00471 }
00472
00481 protected function _callEvent($sEvent, $sModuleId)
00482 {
00483 $aModuleEvents = $this->getModuleEvents();
00484
00485 if (isset($aModuleEvents[$sModuleId], $aModuleEvents[$sModuleId][$sEvent])) {
00486 $mEvent = $aModuleEvents[$sModuleId][$sEvent];
00487
00488 if (is_callable($mEvent)) {
00489 call_user_func($mEvent);
00490 }
00491 }
00492 }
00493
00502 protected function _changeBlockStatus( $sModule, $iStatus = 0 )
00503 {
00504 $oDb = oxDb::getDb();
00505 $sShopId = $this->getConfig()->getShopId();
00506 $oDb->execute("UPDATE oxtplblocks SET oxactive = '".(int) $iStatus."' WHERE oxmodule =". $oDb->quote($sModule)."AND oxshopid = '$sShopId'");
00507 }
00508
00514 protected function _resetCache()
00515 {
00516 $aTemplates = $this->getTemplates();
00517 $oUtils = oxRegistry::getUtils();
00518 $oUtils->resetTemplateCache($aTemplates);
00519 $oUtils->resetLanguageCache();
00520 $oUtils->resetMenuCache();
00521
00522 $oUtilsObject = oxUtilsObject::getInstance();
00523 $oUtilsObject->resetModuleVars();
00524 }
00525
00526
00534 public function buildModuleChains($aModuleArray)
00535 {
00536 $aModules = array();
00537 if (is_array($aModuleArray)) {
00538 foreach ($aModuleArray as $sClass => $aModuleChain) {
00539 $aModules[$sClass] = implode('&', $aModuleChain);
00540 }
00541 }
00542 return $aModules;
00543 }
00544
00554 public function mergeModuleArrays($aAllModuleArray, $aAddModuleArray)
00555 {
00556 if (is_array($aAllModuleArray) && is_array($aAddModuleArray)) {
00557 foreach ($aAddModuleArray as $sClass => $aModuleChain) {
00558 if (!is_array($aModuleChain)) {
00559 $aModuleChain = array($aModuleChain);
00560 }
00561 if (isset($aAllModuleArray[$sClass])) {
00562 foreach ($aModuleChain as $sModule) {
00563 if (!in_array($sModule, $aAllModuleArray[$sClass])) {
00564 $aAllModuleArray[$sClass][] = $sModule;
00565 }
00566 }
00567 } else {
00568 $aAllModuleArray[$sClass] = $aModuleChain;
00569 }
00570 }
00571 }
00572
00573 return $aAllModuleArray;
00574 }
00575
00584 public function filterModuleArray($aModules, $sModuleId)
00585 {
00586 $aFilteredModules = array();
00587 foreach ($aModules as $sClass => $aExtend) {
00588 foreach ($aExtend as $sExtendPath) {
00589 if (strstr($sExtendPath, $sModuleId.'/')) {
00590 $aFilteredModules[$sClass][] = $sExtendPath;
00591 }
00592 }
00593 }
00594 return $aFilteredModules;
00595 }
00596
00604 public function getModulePath( $sModuleId = null )
00605 {
00606 if ( !$sModuleId ) {
00607 $sModuleId = $this->getId();
00608 }
00609
00610 $aModulePaths = $this->getModulePaths();
00611
00612 $sModulePath = $aModulePaths[$sModuleId];
00613
00614
00615 if ( !$sModulePath && is_dir($this->getConfig()->getModulesDir().$sModuleId) ) {
00616 $sModulePath = $sModuleId;
00617 }
00618
00619 return $sModulePath;
00620 }
00621
00627 public function getAllModules()
00628 {
00629 return $this->getConfig()->getAllModules();
00630 }
00631
00637 public function getLegacyModules()
00638 {
00639 return $this->getConfig()->getConfigParam('aLegacyModules');
00640 }
00641
00647 public function getDisabledModules()
00648 {
00649 return $this->getConfig()->getConfigParam('aDisabledModules');
00650 }
00651
00657 public function getModulePaths()
00658 {
00659 return $this->getConfig()->getConfigParam('aModulePaths');
00660 }
00661
00667 public function getModuleTemplates()
00668 {
00669 return (array) $this->getConfig()->getConfigParam('aModuleTemplates');
00670 }
00671
00677 public function getModuleFiles()
00678 {
00679 return (array) $this->getConfig()->getConfigParam('aModuleFiles');
00680 }
00681
00687 public function getModuleVersions()
00688 {
00689 return (array) $this->getConfig()->getConfigParam('aModuleVersions');
00690 }
00691
00697 public function getModuleEvents()
00698 {
00699 return (array) $this->getConfig()->getConfigParam('aModuleEvents');
00700 }
00701
00709 protected function _hasInstalledTemplateBlocks( $sModuleId )
00710 {
00711 $sShopId = $this->getConfig()->getShopId();
00712 $oDb = oxDb::getDb();
00713 $blRes = $oDb->getOne( "SELECT 1 FROM oxtplblocks WHERE oxmodule = ".$oDb->quote($sModuleId)." AND oxshopid = '$sShopId' LIMIT 1" );
00714 return (bool) $blRes;
00715 }
00716
00725 protected function _addTemplateBlocks( $aModuleBlocks, $sModuleId = null )
00726 {
00727 if (is_null($sModuleId)) {
00728 $sModuleId = $this->getId();
00729 }
00730
00731 $sShopId = $this->getConfig()->getShopId();
00732 $oDb = oxDb::getDb();
00733
00734 if ( is_array($aModuleBlocks) ) {
00735
00736 foreach ( $aModuleBlocks as $aValue ) {
00737 $sOxId = oxUtilsObject::getInstance()->generateUId();
00738
00739 $sTemplate = $aValue["template"];
00740 $iPosition = $aValue["position"]?$aValue["position"]:1;
00741 $sBlock = $aValue["block"];
00742 $sFile = $aValue["file"];
00743
00744 $sSql = "INSERT INTO `oxtplblocks` (`OXID`, `OXACTIVE`, `OXSHOPID`, `OXTEMPLATE`, `OXBLOCKNAME`, `OXPOS`, `OXFILE`, `OXMODULE`)
00745 VALUES ('{$sOxId}', 1, '{$sShopId}', ".$oDb->quote($sTemplate).", ".$oDb->quote($sBlock).", ".$oDb->quote($iPosition).", ".$oDb->quote($sFile).", '{$sModuleId}')";
00746
00747 $oDb->execute( $sSql );
00748 }
00749 }
00750 }
00751
00760 protected function _addTemplateFiles( $aModuleTemplates , $sModuleId = null)
00761 {
00762 if (is_null($sModuleId)) {
00763 $sModuleId = $this->getId();
00764 }
00765
00766 $oConfig = $this->getConfig();
00767 $aTemplates = $this->getModuleTemplates();
00768 if ( is_array($aModuleTemplates) ) {
00769 $aTemplates[$sModuleId] = $aModuleTemplates;
00770 }
00771
00772 $oConfig->setConfigParam('aModuleTemplates', $aTemplates);
00773 $oConfig->saveShopConfVar('aarr', 'aModuleTemplates', $aTemplates);
00774 }
00775
00784 protected function _addModuleVersion( $sModuleVersion, $sModuleId = null)
00785 {
00786 if (is_null($sModuleId)) {
00787 $sModuleId = $this->getId();
00788 }
00789
00790 $oConfig = $this->getConfig();
00791 $aVersions = $this->getModuleVersions();
00792 if ( is_array($aVersions) ) {
00793 $aVersions[$sModuleId] = $sModuleVersion;
00794 }
00795
00796 $oConfig->setConfigParam('aModuleVersions', $aVersions);
00797 $oConfig->saveShopConfVar('aarr', 'aModuleVersions', $aVersions);
00798 }
00799
00808 protected function _addModuleEvents( $aModuleEvents, $sModuleId = null)
00809 {
00810 if (is_null($sModuleId)) {
00811 $sModuleId = $this->getId();
00812 }
00813
00814 $oConfig = $this->getConfig();
00815 $aEvents = $this->getModuleEvents();
00816 if ( is_array($aEvents) ) {
00817 $aEvents[$sModuleId] = $aModuleEvents;
00818 }
00819
00820 $oConfig->setConfigParam('aModuleEvents', $aEvents);
00821 $oConfig->saveShopConfVar('aarr', 'aModuleEvents', $aEvents);
00822 }
00823
00832 protected function _addModuleFiles( $aModuleFiles, $sModuleId = null)
00833 {
00834 if (is_null($sModuleId)) {
00835 $sModuleId = $this->getId();
00836 }
00837
00838 $oConfig = $this->getConfig();
00839 $aFiles = $this->getModuleFiles();
00840 if ( is_array($aModuleFiles) ) {
00841 $aFiles[$sModuleId] = array_change_key_case($aModuleFiles, CASE_LOWER);
00842 }
00843
00844 $oConfig->setConfigParam('aModuleFiles', $aFiles);
00845 $oConfig->saveShopConfVar('aarr', 'aModuleFiles', $aFiles);
00846 }
00847
00856 protected function _addModuleSettings( $aModuleSettings, $sModuleId = null )
00857 {
00858 if (is_null($sModuleId)) {
00859 $sModuleId = $this->getId();
00860 }
00861 $oConfig = $this->getConfig();
00862 $sShopId = $oConfig->getShopId();
00863 $oDb = oxDb::getDb();
00864
00865 if ( is_array($aModuleSettings) ) {
00866
00867 foreach ( $aModuleSettings as $aValue ) {
00868 $sOxId = oxUtilsObject::getInstance()->generateUId();
00869
00870 $sModule = 'module:'.$sModuleId;
00871 $sName = $aValue["name"];
00872 $sType = $aValue["type"];
00873 $sValue = is_null($oConfig->getConfigParam($sName))?$aValue["value"]:$oConfig->getConfigParam($sName);
00874 $sGroup = $aValue["group"];
00875
00876 $sConstraints = "";
00877 if ( $aValue["constraints"] ) {
00878 $sConstraints = $aValue["constraints"];
00879 } elseif ( $aValue["constrains"] ) {
00880 $sConstraints = $aValue["constrains"];
00881 }
00882
00883 $iPosition = $aValue["position"]?$aValue["position"]:1;
00884
00885 $oConfig->setConfigParam($sName, $sValue);
00886 $oConfig->saveShopConfVar($sType, $sName, $sValue, $sShopId, $sModule);
00887
00888 $sDeleteSql = "DELETE FROM `oxconfigdisplay` WHERE OXCFGMODULE=".$oDb->quote($sModule)." AND OXCFGVARNAME=".$oDb->quote($sName);
00889 $sInsertSql = "INSERT INTO `oxconfigdisplay` (`OXID`, `OXCFGMODULE`, `OXCFGVARNAME`, `OXGROUPING`, `OXVARCONSTRAINT`, `OXPOS`) ".
00890 "VALUES ('{$sOxId}', ".$oDb->quote($sModule).", ".$oDb->quote($sName).", ".$oDb->quote($sGroup).", ".$oDb->quote($sConstraints).", ".$oDb->quote($iPosition).")";
00891
00892 $oDb->execute( $sDeleteSql );
00893 $oDb->execute( $sInsertSql );
00894 }
00895 }
00896 }
00897
00905 public function getTemplates( $sModuleId = null )
00906 {
00907 if (is_null($sModuleId)) {
00908 $sModuleId = $this->getId();
00909 }
00910
00911 if (!$sModuleId) {
00912 return array();
00913 }
00914
00915 $sShopId = $this->getConfig()->getShopId();
00916
00917 $aTemplates = oxDb::getDb()->getCol("SELECT oxtemplate FROM oxtplblocks WHERE oxmodule = '$sModuleId' AND oxshopid = '$sShopId'" );
00918
00919 return $aTemplates;
00920 }
00921
00933 public function saveLegacyModule($sModuleId, $sModuleName, $aModuleInfo = null)
00934 {
00935 $aLegacyModules = $this->getLegacyModules();
00936
00937 if ( !empty( $aModuleInfo ) && is_array($aModuleInfo)) {
00938 $aLegacyModules[$sModuleId]["id"] = $sModuleId;
00939 $aLegacyModules[$sModuleId]["title"] = ( $sModuleName ) ? $sModuleName : $sModuleId;
00940 $aLegacyModules[$sModuleId]['extend'] = array();
00941
00942 foreach ( $aModuleInfo as $sKey => $sValue ) {
00943 if ( strpos( $sValue, "=>" ) > 1 ) {
00944 $aClassInfo = explode( "=>", $sValue );
00945 $sClassName = trim( $aClassInfo[0] );
00946 $sExtendString = trim( $aClassInfo[1] );
00947 $aLegacyModules[$sModuleId]['extend'][$sClassName] = $sExtendString;
00948 }
00949 }
00950 }
00951
00952 if ( !empty( $aLegacyModules[$sModuleId]['extend'] ) ) {
00953 $this->getConfig()->saveShopConfVar( "aarr", "aLegacyModules", $aLegacyModules );
00954 }
00955 return $sModuleId;
00956 }
00957
00966 public function updateModuleIds( $sModuleLegacyId, $sModuleId )
00967 {
00968 $oConfig = $this->getConfig();
00969
00970
00971 $aModulePaths = $oConfig->getConfigParam( 'aModulePaths' );
00972 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00973 unset( $aModulePaths[$sModuleLegacyId] );
00974
00975 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00976
00977 if ( isset($aModulePaths[$sModuleLegacyId]) ) {
00978 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00979 unset( $aModulePaths[$sModuleLegacyId] );
00980 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00981 }
00982
00983
00984 $aDisabledModules = $oConfig->getConfigParam( 'aDisabledModules' );
00985
00986 if ( is_array($aDisabledModules) ) {
00987 $iOldKey = array_search( $sModuleLegacyId, $aDisabledModules );
00988 if ( $iOldKey !== false ) {
00989 unset( $aDisabledModules[$iOldKey] );
00990 $aDisabledModules[$iOldKey] = $sModuleId;
00991 $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledModules );
00992 }
00993 }
00994 }
00995 }