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 load( $sModuleId )
00052 {
00053 if ( $this->loadModule($sModuleId) ) return true;
00054
00055 if ( $this->loadLegacyModule($sModuleId) ) return true;
00056
00057 if ( $this->loadUnregisteredModule($sModuleId) ) return true;
00058
00059 return false;
00060 }
00061
00069 public function loadByDir( $sModuleDir )
00070 {
00071 $aModulePaths = $this->getModulePaths();
00072
00073 if ( is_array($aModulePaths) ) {
00074 $sModuleId = array_search( $sModuleDir, $aModulePaths);
00075 }
00076
00077
00078 if ( !$sModuleId ) {
00079 $sModuleId = $sModuleDir;
00080 }
00081
00082 return $this->load( $sModuleId );
00083 }
00084
00092 public function loadModule( $sModuleId )
00093 {
00094 $sModuleDir = $this->getModulePath( $sModuleId );
00095
00096 $sFilePath = $this->getConfig()->getModulesDir() . $sModuleDir . "/metadata.php";
00097 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00098 $aModule = array();
00099 include $sFilePath;
00100 $this->_aModule = $aModule;
00101 $this->_blLegacy = false;
00102 $this->_blRegistered = true;
00103 $this->_blMetadata = true;
00104 $this->_blFile = false;
00105 $this->_aModule['active'] = $this->isActive() || !$this->isExtended();
00106 return true;
00107 }
00108 return false;
00109 }
00110
00118 public function loadLegacyModule( $sModuleId )
00119 {
00120 $aLegacyModules = $this->getLegacyModules();
00121 $sModuleDir = $this->getModulePath( $sModuleId );
00122
00123
00124 if ( isset( $aLegacyModules[$sModuleId] ) ) {
00125 $this->_aModule = $aLegacyModules[$sModuleId];
00126 $this->_blLegacy = true;
00127 $this->_blRegistered = true;
00128 $this->_blMetadata = false;
00129 $this->_blFile = empty( $sModuleDir );
00130 $this->_aModule['active'] = $this->isActive();
00131 return true;
00132 }
00133 return false;
00134 }
00135
00143 public function loadUnregisteredModule( $sModuleId )
00144 {
00145 $aModules = $this->getAllModules();
00146
00147 $sModuleDir = $this->getModulePath( $sModuleId );
00148
00149 $sFilePath = $this->getConfig()->getModulesDir() . $sModuleDir ;
00150 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00151 $this->_aModule = array();
00152 $this->_aModule['id'] = $sModuleId;
00153 $this->_aModule['title'] = $sModuleId;
00154 $this->_aModule['extend'] = $this->buildModuleChains($this->filterModuleArray($aModules, $sModuleId));
00155 $this->_blLegacy = true;
00156 $this->_blRegistered = false;
00157 $this->_blMetadata = false;
00158 $this->_blFile = !is_dir($this->getConfig()->getModulesDir() . $sModuleId);
00159 $this->_aModule['active'] = $this->isActive();
00160 return true;
00161 }
00162 return false;
00163 }
00164
00170 public function getDescription()
00171 {
00172 $iLang = oxLang::getInstance()->getTplLanguage();
00173
00174 return $this->getInfo( "description", $iLang );
00175 }
00176
00182 public function getTitle()
00183 {
00184 $iLang = oxLang::getInstance()->getTplLanguage();
00185
00186 return $this->getInfo( "title", $iLang );
00187 }
00188
00194 public function getId()
00195 {
00196 return $this->_aModule['id'];
00197 }
00198
00206 public function getIdByPath($sModule)
00207 {
00208 $myConfig = $this->getConfig();
00209 $aModulePaths = $myConfig->getConfigParam( 'aModulePaths' );
00210 $sModuleId = null;
00211 if (is_array( $aModulePaths )) {
00212 foreach ($aModulePaths as $sId => $sPath) {
00213 if (strpos($sModule, $sPath."/") === 0 ) {
00214 $sModuleId = $sId;
00215 }
00216 }
00217 }
00218 if (!$sModuleId) {
00219 $sModuleId = substr( $sModule, 0, strpos( $sModule, "/" ) );
00220 }
00221 if (!$sModuleId) {
00222 $sModuleId = $sModule;
00223 }
00224 return $sModuleId;
00225 }
00226
00236 public function getInfo( $sName, $iLang = null )
00237 {
00238 if (isset($this->_aModule[$sName])) {
00239
00240 if ( $iLang !== null && is_array($this->_aModule[$sName]) ) {
00241 $sValue = null;
00242
00243 $sLang = oxLang::getInstance()->getLanguageAbbr( $iLang );
00244
00245 if ( !empty($this->_aModule[$sName]) ) {
00246 if ( !empty( $this->_aModule[$sName][$sLang] ) ) {
00247 $sValue = $this->_aModule[$sName][$sLang];
00248 } elseif ( !empty($this->_aModule['lang']) ) {
00249
00250 $sValue = $this->_aModule[$sName][$this->_aModule['lang']];
00251 } else {
00252
00253 $sValue = reset( $this->_aModule[$sName] );
00254 }
00255
00256 return $sValue;
00257 }
00258 } else {
00259 return $this->_aModule[$sName];
00260 }
00261 }
00262 }
00263
00269 public function isActive()
00270 {
00271 $blActive = false;
00272 $sId = $this->getId();
00273 if (isset($sId)) {
00274 if ( is_array($this->_aModule['extend']) && !empty($this->_aModule['extend']) ) {
00275 $aAddModules = $this->_aModule['extend'];
00276 $aInstalledModules = $this->getAllModules();
00277 $iClCount = count($aAddModules);
00278 $iActive = 0;
00279
00280 foreach ($aAddModules as $sClass => $sModule) {
00281 if ( (isset($aInstalledModules[$sClass]) && in_array($sModule, $aInstalledModules[$sClass])) ) {
00282 $iActive ++;
00283 }
00284 }
00285 $blActive = $iClCount > 0 && $iActive == $iClCount;
00286
00287 $aDisabledModules = $this->getDisabledModules();
00288 if ( $blActive && ( is_array($aDisabledModules) && in_array($sId, $aDisabledModules) ) ) {
00289 $blActive = false;
00290 }
00291 } else {
00292
00293 $aDisabledModules = $this->getDisabledModules();
00294 if ( is_array($aDisabledModules) && !in_array($sId, $aDisabledModules) ) {
00295 $blActive = true;
00296 }
00297 }
00298 }
00299
00300 return $blActive;
00301 }
00302
00308 public function isExtended()
00309 {
00310 if ($this->hasMetadata() && !empty($this->_aModule['extend'])) {
00311 return true;
00312 }
00313
00314 return false;
00315 }
00316
00322 public function isLegacy()
00323 {
00324 return $this->_blLegacy;
00325 }
00326
00332 public function isRegistered()
00333 {
00334 return $this->_blRegistered;
00335 }
00336
00342 public function hasMetadata()
00343 {
00344 return $this->_blMetadata;
00345 }
00346
00352 public function isFile()
00353 {
00354 return $this->_blFile;
00355 }
00356
00362 public function activate()
00363 {
00364 if (isset($this->_aModule['extend']) && is_array($this->_aModule['extend'])) {
00365 $oConfig = $this->getConfig();
00366 $aAddModules = $this->_aModule['extend'];
00367 $sModuleId = $this->getId();
00368
00369 $aInstalledModules = $this->getAllModules();
00370 $aDisabledModules = $this->getDisabledModules();
00371
00372 $aModules = $this->mergeModuleArrays($aInstalledModules, $aAddModules);
00373 $aModules = $this->buildModuleChains($aModules);
00374
00375 $oConfig->setConfigParam('aModules', $aModules);
00376 $oConfig->saveShopConfVar('aarr', 'aModules', $aModules);
00377
00378 if ( isset($aDisabledModules) && is_array($aDisabledModules) ) {
00379 $aDisabledModules = array_diff($aDisabledModules, array($sModuleId));
00380
00381 $oConfig->setConfigParam('aDisabledModules', $aDisabledModules);
00382 $oConfig->saveShopConfVar('arr', 'aDisabledModules', $aDisabledModules);
00383 }
00384
00385
00386 if ( !$this->_hasInstalledTemplateBlocks($sModuleId) ) {
00387
00388 $this->_addTemplateBlocks( $this->getInfo("blocks") );
00389 } else {
00390
00391 $this->_changeBlockStatus( $sModuleId, "1" );
00392 }
00393
00394
00395 $this->_addModuleFiles($this->getInfo("files") );
00396
00397
00398 $this->_addTemplateFiles($this->getInfo("templates") );
00399
00400
00401 $this->_addModuleSettings($this->getInfo("settings"));
00402
00403
00404 $this->_resetCache();
00405
00406 return true;
00407 }
00408 return false;
00409 }
00410
00418 public function deactivate($sModuleId = null)
00419 {
00420 if (!isset($sModuleId)) {
00421 $sModuleId = $this->getId();
00422 }
00423 if (isset($sModuleId)) {
00424 $aDisabledModules = $this->getDisabledModules();
00425
00426 if (!is_array($aDisabledModules)) {
00427 $aDisabledModules = array();
00428 }
00429 $aModules = array_merge($aDisabledModules, array($sModuleId));
00430
00431 $this->getConfig()->saveShopConfVar('arr', 'aDisabledModules', $aModules);
00432 $this->getConfig()->setConfigParam('aDisabledModules', $aModules);
00433
00434
00435 $this->_changeBlockStatus( $sModuleId );
00436
00437
00438 $this->_resetCache();
00439
00440 return true;
00441 }
00442 return false;
00443 }
00444
00453 protected function _changeBlockStatus( $sModule, $iStatus = '0' )
00454 {
00455 $oDb = oxDb::getDb();
00456 $sShopId = $this->getConfig()->getShopId();
00457 $oDb->execute("UPDATE oxtplblocks SET oxactive = '".(int) $iStatus."' WHERE oxmodule =". $oDb->quote($sModule)."AND oxshopid = '$sShopId'");
00458 }
00459
00465 protected function _resetCache()
00466 {
00467 $oUtils = oxUtils::getInstance();
00468 $aTemplates = $this->getTemplates();
00469 $oUtils->resetTemplateCache($aTemplates);
00470 $oUtils->resetLanguageCache();
00471 $oUtils->resetMenuCache();
00472 }
00480 public function buildModuleChains($aModuleArray)
00481 {
00482 $aModules = array();
00483 if (is_array($aModuleArray)) {
00484 foreach ($aModuleArray as $sClass => $aModuleChain) {
00485 $aModules[$sClass] = implode('&', $aModuleChain);
00486 }
00487 }
00488 return $aModules;
00489 }
00490
00500 public function mergeModuleArrays($aAllModuleArray, $aAddModuleArray)
00501 {
00502 if (is_array($aAllModuleArray) && is_array($aAddModuleArray)) {
00503 foreach ($aAddModuleArray as $sClass => $aModuleChain) {
00504 if (!is_array($aModuleChain)) {
00505 $aModuleChain = array($aModuleChain);
00506 }
00507 if (isset($aAllModuleArray[$sClass])) {
00508 foreach ($aModuleChain as $sModule) {
00509 if (!in_array($sModule, $aAllModuleArray[$sClass])) {
00510 $aAllModuleArray[$sClass][] = $sModule;
00511 }
00512 }
00513 } else {
00514 $aAllModuleArray[$sClass] = $aModuleChain;
00515 }
00516 }
00517 }
00518
00519 return $aAllModuleArray;
00520 }
00521
00530 public function filterModuleArray($aModules, $sModuleId)
00531 {
00532 $aFilteredModules = array();
00533 foreach ($aModules as $sClass => $aExtend) {
00534 foreach ($aExtend as $sExtendPath) {
00535 if (strstr($sExtendPath, $sModuleId.'/')) {
00536 $aFilteredModules[$sClass][] = $sExtendPath;
00537 }
00538 }
00539 }
00540 return $aFilteredModules;
00541 }
00542
00550 public function getModulePath( $sModuleId = null )
00551 {
00552 if ( !$sModuleId ) {
00553 $sModuleId = $this->getId();
00554 }
00555
00556 $aModulePaths = $this->getModulePaths();
00557
00558 $sModulePath = $aModulePaths[$sModuleId];
00559
00560
00561 if ( !$sModulePath && is_dir($this->getConfig()->getModulesDir().$sModuleId) ) {
00562 $sModulePath = $sModuleId;
00563 }
00564
00565 return $sModulePath;
00566 }
00567
00573 public function getAllModules()
00574 {
00575 return $this->getConfig()->getAllModules();
00576 }
00577
00583 public function getLegacyModules()
00584 {
00585 return $this->getConfig()->getConfigParam('aLegacyModules');
00586 }
00587
00593 public function getDisabledModules()
00594 {
00595 return $this->getConfig()->getConfigParam('aDisabledModules');
00596 }
00597
00603 public function getModulePaths()
00604 {
00605 return $this->getConfig()->getConfigParam('aModulePaths');
00606 }
00607
00613 public function getModuleTemplates()
00614 {
00615 return (array) $this->getConfig()->getConfigParam('aModuleTemplates');
00616 }
00617
00623 public function getModuleFiles()
00624 {
00625 return (array) $this->getConfig()->getConfigParam('aModuleFiles');
00626 }
00627
00635 protected function _hasInstalledTemplateBlocks( $sModuleId )
00636 {
00637 $sShopId = $this->getConfig()->getShopId();
00638 $oDb = oxDb::getDb();
00639 $blRes = $oDb->getOne( "SELECT 1 FROM oxtplblocks WHERE oxmodule = ".$oDb->quote($sModuleId)." AND oxshopid = '$sShopId' LIMIT 1" );
00640 return (bool) $blRes;
00641 }
00642
00651 protected function _addTemplateBlocks( $aModuleBlocks, $sModuleId = null )
00652 {
00653 if (is_null($sModuleId)) {
00654 $sModuleId = $this->getId();
00655 }
00656
00657 $sShopId = $this->getConfig()->getShopId();
00658 $oDb = oxDb::getDb();
00659
00660 if ( is_array($aModuleBlocks) ) {
00661
00662 foreach ( $aModuleBlocks as $aValue ) {
00663 $sOxId = oxUtilsObject::getInstance()->generateUId();
00664
00665 $sTemplate = $aValue["template"];
00666 $iPosition = $aValue["position"]?$aValue["position"]:1;
00667 $sBlock = $aValue["block"];
00668 $sFile = $aValue["file"];
00669
00670 $sSql = "INSERT INTO `oxtplblocks` (`OXID`, `OXACTIVE`, `OXSHOPID`, `OXTEMPLATE`, `OXBLOCKNAME`, `OXPOS`, `OXFILE`, `OXMODULE`)
00671 VALUES ('{$sOxId}', 1, '{$sShopId}', ".$oDb->quote($sTemplate).", ".$oDb->quote($sBlock).", ".$oDb->quote($iPosition).", ".$oDb->quote($sFile).", '{$sModuleId}')";
00672
00673 $oDb->execute( $sSql );
00674 }
00675 }
00676 }
00677
00686 protected function _addTemplateFiles( $aModuleTemplates , $sModuleId = null)
00687 {
00688 if (is_null($sModuleId)) {
00689 $sModuleId = $this->getId();
00690 }
00691
00692 $oConfig = $this->getConfig();
00693 $aTemplates = $this->getModuleTemplates();
00694 if ( is_array($aModuleTemplates) ) {
00695 $aTemplates[$sModuleId] = $aModuleTemplates;
00696 }
00697
00698 $oConfig->setConfigParam('aModuleTemplates', $aTemplates);
00699 $oConfig->saveShopConfVar('aarr', 'aModuleTemplates', $aTemplates);
00700 }
00701
00710 protected function _addModuleFiles( $aModuleFiles, $sModuleId = null)
00711 {
00712 if (is_null($sModuleId)) {
00713 $sModuleId = $this->getId();
00714 }
00715
00716 $oConfig = $this->getConfig();
00717 $aFiles = $this->getModuleFiles();
00718 if ( is_array($aModuleFiles) ) {
00719 $aFiles[$sModuleId] = array_change_key_case($aModuleFiles, CASE_LOWER);
00720 }
00721
00722 $oConfig->setConfigParam('aModuleFiles', $aFiles);
00723 $oConfig->saveShopConfVar('aarr', 'aModuleFiles', $aFiles);
00724 }
00725
00734 protected function _addModuleSettings( $aModuleSettings, $sModuleId = null )
00735 {
00736 if (is_null($sModuleId)) {
00737 $sModuleId = $this->getId();
00738 }
00739
00740 $sShopId = $this->getConfig()->getShopId();
00741 $oDb = oxDb::getDb();
00742
00743 if ( is_array($aModuleSettings) ) {
00744
00745 foreach ( $aModuleSettings as $aValue ) {
00746 $oConfig = $this->getConfig();
00747 $sOxId = oxUtilsObject::getInstance()->generateUId();
00748
00749 $sModule = 'module:'.$sModuleId;
00750 $sName = $aValue["name"];
00751 $sType = $aValue["type"];
00752 $sValue = is_null($oConfig->getConfigParam($sName))?$aValue["value"]:$oConfig->getConfigParam($sName);
00753 $sGroup = $aValue["group"];
00754 $sConstrains = $aValue["constrains"]?$aValue["constrains"]:'';
00755 $iPosition = $aValue["position"]?$aValue["position"]:1;
00756
00757 $oConfig->setConfigParam($sName, $sValue);
00758 $oConfig->saveShopConfVar($sType, $sName, $sValue, $sShopId, $sModule);
00759
00760 $sDeleteSql = "DELETE FROM `oxconfigdisplay` WHERE OXCFGMODULE=".$oDb->quote($sModule)." AND OXCFGVARNAME=".$oDb->quote($sName);
00761 $sInsertSql = "INSERT INTO `oxconfigdisplay` (`OXID`, `OXCFGMODULE`, `OXCFGVARNAME`, `OXGROUPING`, `OXVARCONSTRAINT`, `OXPOS`) ".
00762 "VALUES ('{$sOxId}', ".$oDb->quote($sModule).", ".$oDb->quote($sName).", ".$oDb->quote($sGroup).", ".$oDb->quote($sConstrains).", ".$oDb->quote($iPosition).")";
00763
00764 $oDb->execute( $sDeleteSql );
00765 $oDb->execute( $sInsertSql );
00766 }
00767 }
00768 }
00769
00777 public function getTemplates( $sModuleId = null )
00778 {
00779 if (is_null($sModuleId)) {
00780 $sModuleId = $this->getId();
00781 }
00782
00783 if (!$sModuleId) {
00784 return;
00785 }
00786
00787 $sShopId = $this->getConfig()->getShopId();
00788 $aTemplates = oxDb::getDb()->getCol("SELECT oxtemplate FROM oxtplblocks WHERE oxmodule = '$sModuleId' AND oxshopid = '$sShopId'" );
00789
00790 return $aTemplates;
00791 }
00792
00804 public function saveLegacyModule($sModuleId, $sModuleName, $aModuleInfo = null)
00805 {
00806 $aLegacyModules = $this->getLegacyModules();
00807
00808 if ( !empty( $aModuleInfo ) && is_array($aModuleInfo)) {
00809 $aLegacyModules[$sModuleId]["id"] = $sModuleId;
00810 $aLegacyModules[$sModuleId]["title"] = ( $sModuleName ) ? $sModuleName : $sModuleId;
00811 $aLegacyModules[$sModuleId]['extend'] = array();
00812
00813 foreach ( $aModuleInfo as $sKey => $sValue ) {
00814 if ( strpos( $sValue, "=>" ) > 1 ) {
00815 $aClassInfo = explode( "=>", $sValue );
00816 $sClassName = trim( $aClassInfo[0] );
00817 $sExtendString = trim( $aClassInfo[1] );
00818 $aLegacyModules[$sModuleId]['extend'][$sClassName] = $sExtendString;
00819 }
00820 }
00821 }
00822
00823 if ( !empty( $aLegacyModules[$sModuleId]['extend'] ) ) {
00824 $this->getConfig()->saveShopConfVar( "aarr", "aLegacyModules", $aLegacyModules );
00825 }
00826 return $sModuleId;
00827 }
00828
00837 public function updateModuleIds( $sModuleLegacyId, $sModuleId )
00838 {
00839 $oConfig = $this->getConfig();
00840
00841
00842 $aModulePaths = $oConfig->getConfigParam( 'aModulePaths' );
00843 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00844 unset( $aModulePaths[$sModuleLegacyId] );
00845
00846 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00847
00848 if ( isset($aModulePaths[$sModuleLegacyId]) ) {
00849 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00850 unset( $aModulePaths[$sModuleLegacyId] );
00851 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00852 }
00853
00854
00855 $aDisabledModules = $oConfig->getConfigParam( 'aDisabledModules' );
00856
00857 if ( is_array($aDisabledModules) ) {
00858 $iOldKey = array_search( $sModuleLegacyId, $aDisabledModules );
00859 if ( $iOldKey !== false ) {
00860 unset( $aDisabledModules[$iOldKey] );
00861 $aDisabledModules[$iOldKey] = $sModuleId;
00862 $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledModules );
00863 }
00864 }
00865 }
00866 }