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 $oConfig = $this->getConfig();
00146 $aModules = $this->getAllModules();
00147
00148 $sModuleDir = $this->getModulePath( $sModuleId );
00149
00150 $sFilePath = $oConfig->getModulesDir() . $sModuleDir ;
00151 if ( file_exists( $sFilePath ) && is_readable( $sFilePath ) ) {
00152 $this->_aModule = array();
00153 $this->_aModule['id'] = $sModuleId;
00154 $this->_aModule['title'] = $sModuleId;
00155 $this->_aModule['extend'] = $this->buildModuleChains($this->filterModuleArray($aModules, $sModuleId));
00156 $this->_blLegacy = true;
00157 $this->_blRegistered = false;
00158 $this->_blMetadata = false;
00159 $this->_blFile = !is_dir($oConfig->getModulesDir() . $sModuleId);
00160 $this->_aModule['active'] = $this->isActive();
00161 return true;
00162 }
00163 return false;
00164 }
00165
00171 public function getDescription()
00172 {
00173 $iLang = oxLang::getInstance()->getTplLanguage();
00174
00175 return $this->getInfo( "description", $iLang );
00176 }
00177
00183 public function getTitle()
00184 {
00185 $iLang = oxLang::getInstance()->getTplLanguage();
00186
00187 return $this->getInfo( "title", $iLang );
00188 }
00189
00195 public function getId()
00196 {
00197 return $this->_aModule['id'];
00198 }
00199
00207 public function getIdByPath($sModule)
00208 {
00209 $myConfig = $this->getConfig();
00210 $aModulePaths = $myConfig->getConfigParam( 'aModulePaths' );
00211 $sModuleId = null;
00212 if (is_array( $aModulePaths )) {
00213 foreach ($aModulePaths as $sId => $sPath) {
00214 if (strpos($sModule, $sPath."/") === 0 ) {
00215 $sModuleId = $sId;
00216 }
00217 }
00218 }
00219 if (!$sModuleId) {
00220 $sModuleId = substr( $sModule, 0, strpos( $sModule, "/" ) );
00221 }
00222 if (!$sModuleId) {
00223 $sModuleId = $sModule;
00224 }
00225 return $sModuleId;
00226 }
00227
00237 public function getInfo( $sName, $iLang = null )
00238 {
00239 if (isset($this->_aModule[$sName])) {
00240
00241 if ( $iLang !== null && is_array($this->_aModule[$sName]) ) {
00242 $sValue = null;
00243
00244 $sLang = oxLang::getInstance()->getLanguageAbbr( $iLang );
00245
00246 if ( !empty($this->_aModule[$sName]) ) {
00247 if ( !empty( $this->_aModule[$sName][$sLang] ) ) {
00248 $sValue = $this->_aModule[$sName][$sLang];
00249 } elseif ( !empty($this->_aModule['lang']) ) {
00250
00251 $sValue = $this->_aModule[$sName][$this->_aModule['lang']];
00252 } else {
00253
00254 $sValue = reset( $this->_aModule[$sName] );
00255 }
00256
00257 return $sValue;
00258 }
00259 } else {
00260 return $this->_aModule[$sName];
00261 }
00262 }
00263 }
00264
00270 public function isActive()
00271 {
00272 $blActive = false;
00273 $sId = $this->getId();
00274 if (isset($sId)) {
00275 if ( is_array($this->_aModule['extend']) && !empty($this->_aModule['extend']) ) {
00276 $aAddModules = $this->_aModule['extend'];
00277 $aInstalledModules = $this->getAllModules();
00278 $iClCount = count($aAddModules);
00279 $iActive = 0;
00280
00281 foreach ($aAddModules as $sClass => $sModule) {
00282 if ( (isset($aInstalledModules[$sClass]) && in_array($sModule, $aInstalledModules[$sClass])) ) {
00283 $iActive ++;
00284 }
00285 }
00286 $blActive = $iClCount > 0 && $iActive == $iClCount;
00287
00288 $aDisabledModules = $this->getDisabledModules();
00289 if ( $blActive && ( is_array($aDisabledModules) && in_array($sId, $aDisabledModules) ) ) {
00290 $blActive = false;
00291 }
00292 } else {
00293
00294 $aDisabledModules = $this->getDisabledModules();
00295 if ( is_array($aDisabledModules) && !in_array($sId, $aDisabledModules) ) {
00296 $blActive = true;
00297 }
00298 }
00299 }
00300
00301 return $blActive;
00302 }
00303
00309 public function isExtended()
00310 {
00311 if ($this->hasMetadata() && !empty($this->_aModule['extend'])) {
00312 return true;
00313 }
00314
00315 return false;
00316 }
00317
00323 public function isLegacy()
00324 {
00325 return $this->_blLegacy;
00326 }
00327
00333 public function isRegistered()
00334 {
00335 return $this->_blRegistered;
00336 }
00337
00343 public function hasMetadata()
00344 {
00345 return $this->_blMetadata;
00346 }
00347
00353 public function isFile()
00354 {
00355 return $this->_blFile;
00356 }
00357
00363 public function activate()
00364 {
00365 if (isset($this->_aModule['extend']) && is_array($this->_aModule['extend'])) {
00366 $oConfig = $this->getConfig();
00367 $aAddModules = $this->_aModule['extend'];
00368 $sModuleId = $this->getId();
00369
00370 $aInstalledModules = $this->getAllModules();
00371 $aDisabledModules = $this->getDisabledModules();
00372
00373 $aModules = $this->mergeModuleArrays($aInstalledModules, $aAddModules);
00374 $aModules = $this->buildModuleChains($aModules);
00375
00376 $oConfig->setConfigParam('aModules', $aModules);
00377 $oConfig->saveShopConfVar('aarr', 'aModules', $aModules);
00378
00379 if ( isset($aDisabledModules) && is_array($aDisabledModules) ) {
00380 $aDisabledModules = array_diff($aDisabledModules, array($sModuleId));
00381
00382 $oConfig->setConfigParam('aDisabledModules', $aDisabledModules);
00383 $oConfig->saveShopConfVar('arr', 'aDisabledModules', $aDisabledModules);
00384 }
00385
00386
00387 if ( !$this->_hasInstalledTemplateBlocks($sModuleId) ) {
00388
00389 $this->_addTemplateBlocks( $this->getInfo("blocks") );
00390 } else {
00391
00392 $this->_changeBlockStatus( $sModuleId, "1" );
00393 }
00394
00395
00396 $this->_addModuleFiles($this->getInfo("files") );
00397
00398
00399 $this->_addTemplateFiles($this->getInfo("templates") );
00400
00401
00402 $this->_addModuleSettings($this->getInfo("settings"));
00403
00404
00405 $this->_resetCache();
00406
00407 return true;
00408 }
00409 return false;
00410 }
00411
00419 public function deactivate($sModuleId = null)
00420 {
00421 $oConfig = $this->getConfig();
00422 if (!isset($sModuleId)) {
00423 $sModuleId = $this->getId();
00424 }
00425 if (isset($sModuleId)) {
00426 $aDisabledModules = $this->getDisabledModules();
00427
00428 if (!is_array($aDisabledModules)) {
00429 $aDisabledModules = array();
00430 }
00431 $aModules = array_merge($aDisabledModules, array($sModuleId));
00432 $aModules = array_unique($aModules);
00433
00434 $oConfig->saveShopConfVar('arr', 'aDisabledModules', $aModules);
00435 $oConfig->setConfigParam('aDisabledModules', $aModules);
00436
00437
00438 $this->_changeBlockStatus( $sModuleId );
00439
00440
00441 $this->_resetCache();
00442
00443 return true;
00444 }
00445 return false;
00446 }
00447
00456 protected function _changeBlockStatus( $sModule, $iStatus = '0' )
00457 {
00458 $oDb = oxDb::getDb();
00459 $sShopId = $this->getConfig()->getShopId();
00460 $oDb->execute("UPDATE oxtplblocks SET oxactive = '".(int) $iStatus."' WHERE oxmodule =". $oDb->quote($sModule)."AND oxshopid = '$sShopId'");
00461 }
00462
00468 protected function _resetCache()
00469 {
00470 $oUtils = oxUtils::getInstance();
00471 $aTemplates = $this->getTemplates();
00472 $oUtils->resetTemplateCache($aTemplates);
00473 $oUtils->resetLanguageCache();
00474 $oUtils->resetMenuCache();
00475 }
00483 public function buildModuleChains($aModuleArray)
00484 {
00485 $aModules = array();
00486 if (is_array($aModuleArray)) {
00487 foreach ($aModuleArray as $sClass => $aModuleChain) {
00488 $aModules[$sClass] = implode('&', $aModuleChain);
00489 }
00490 }
00491 return $aModules;
00492 }
00493
00503 public function mergeModuleArrays($aAllModuleArray, $aAddModuleArray)
00504 {
00505 if (is_array($aAllModuleArray) && is_array($aAddModuleArray)) {
00506 foreach ($aAddModuleArray as $sClass => $aModuleChain) {
00507 if (!is_array($aModuleChain)) {
00508 $aModuleChain = array($aModuleChain);
00509 }
00510 if (isset($aAllModuleArray[$sClass])) {
00511 foreach ($aModuleChain as $sModule) {
00512 if (!in_array($sModule, $aAllModuleArray[$sClass])) {
00513 $aAllModuleArray[$sClass][] = $sModule;
00514 }
00515 }
00516 } else {
00517 $aAllModuleArray[$sClass] = $aModuleChain;
00518 }
00519 }
00520 }
00521
00522 return $aAllModuleArray;
00523 }
00524
00533 public function filterModuleArray($aModules, $sModuleId)
00534 {
00535 $aFilteredModules = array();
00536 foreach ($aModules as $sClass => $aExtend) {
00537 foreach ($aExtend as $sExtendPath) {
00538 if (strstr($sExtendPath, $sModuleId.'/')) {
00539 $aFilteredModules[$sClass][] = $sExtendPath;
00540 }
00541 }
00542 }
00543 return $aFilteredModules;
00544 }
00545
00553 public function getModulePath( $sModuleId = null )
00554 {
00555 if ( !$sModuleId ) {
00556 $sModuleId = $this->getId();
00557 }
00558
00559 $aModulePaths = $this->getModulePaths();
00560
00561 $sModulePath = $aModulePaths[$sModuleId];
00562
00563
00564 if ( !$sModulePath && is_dir($this->getConfig()->getModulesDir().$sModuleId) ) {
00565 $sModulePath = $sModuleId;
00566 }
00567
00568 return $sModulePath;
00569 }
00570
00576 public function getAllModules()
00577 {
00578 return $this->getConfig()->getAllModules();
00579 }
00580
00586 public function getLegacyModules()
00587 {
00588 return $this->getConfig()->getConfigParam('aLegacyModules');
00589 }
00590
00596 public function getDisabledModules()
00597 {
00598 return $this->getConfig()->getConfigParam('aDisabledModules');
00599 }
00600
00606 public function getModulePaths()
00607 {
00608 return $this->getConfig()->getConfigParam('aModulePaths');
00609 }
00610
00616 public function getModuleTemplates()
00617 {
00618 return (array) $this->getConfig()->getConfigParam('aModuleTemplates');
00619 }
00620
00626 public function getModuleFiles()
00627 {
00628 return (array) $this->getConfig()->getConfigParam('aModuleFiles');
00629 }
00630
00638 protected function _hasInstalledTemplateBlocks( $sModuleId )
00639 {
00640 $sShopId = $this->getConfig()->getShopId();
00641 $oDb = oxDb::getDb();
00642 $blRes = $oDb->getOne( "SELECT 1 FROM oxtplblocks WHERE oxmodule = ".$oDb->quote($sModuleId)." AND oxshopid = '$sShopId' LIMIT 1" );
00643 return (bool) $blRes;
00644 }
00645
00654 protected function _addTemplateBlocks( $aModuleBlocks, $sModuleId = null )
00655 {
00656 if (is_null($sModuleId)) {
00657 $sModuleId = $this->getId();
00658 }
00659
00660 $sShopId = $this->getConfig()->getShopId();
00661 $oDb = oxDb::getDb();
00662
00663 if ( is_array($aModuleBlocks) ) {
00664
00665 foreach ( $aModuleBlocks as $aValue ) {
00666 $sOxId = oxUtilsObject::getInstance()->generateUId();
00667
00668 $sTemplate = $aValue["template"];
00669 $iPosition = $aValue["position"]?$aValue["position"]:1;
00670 $sBlock = $aValue["block"];
00671 $sFile = $aValue["file"];
00672
00673 $sSql = "INSERT INTO `oxtplblocks` (`OXID`, `OXACTIVE`, `OXSHOPID`, `OXTEMPLATE`, `OXBLOCKNAME`, `OXPOS`, `OXFILE`, `OXMODULE`)
00674 VALUES ('{$sOxId}', 1, '{$sShopId}', ".$oDb->quote($sTemplate).", ".$oDb->quote($sBlock).", ".$oDb->quote($iPosition).", ".$oDb->quote($sFile).", '{$sModuleId}')";
00675
00676 $oDb->execute( $sSql );
00677 }
00678 }
00679 }
00680
00689 protected function _addTemplateFiles( $aModuleTemplates , $sModuleId = null)
00690 {
00691 if (is_null($sModuleId)) {
00692 $sModuleId = $this->getId();
00693 }
00694
00695 $oConfig = $this->getConfig();
00696 $aTemplates = $this->getModuleTemplates();
00697 if ( is_array($aModuleTemplates) ) {
00698 $aTemplates[$sModuleId] = $aModuleTemplates;
00699 }
00700
00701 $oConfig->setConfigParam('aModuleTemplates', $aTemplates);
00702 $oConfig->saveShopConfVar('aarr', 'aModuleTemplates', $aTemplates);
00703 }
00704
00713 protected function _addModuleFiles( $aModuleFiles, $sModuleId = null)
00714 {
00715 if (is_null($sModuleId)) {
00716 $sModuleId = $this->getId();
00717 }
00718
00719 $oConfig = $this->getConfig();
00720 $aFiles = $this->getModuleFiles();
00721 if ( is_array($aModuleFiles) ) {
00722 $aFiles[$sModuleId] = array_change_key_case($aModuleFiles, CASE_LOWER);
00723 }
00724
00725 $oConfig->setConfigParam('aModuleFiles', $aFiles);
00726 $oConfig->saveShopConfVar('aarr', 'aModuleFiles', $aFiles);
00727 }
00728
00737 protected function _addModuleSettings( $aModuleSettings, $sModuleId = null )
00738 {
00739 if (is_null($sModuleId)) {
00740 $sModuleId = $this->getId();
00741 }
00742 $oConfig = $this->getConfig();
00743 $sShopId = $oConfig->getShopId();
00744 $oDb = oxDb::getDb();
00745
00746 if ( is_array($aModuleSettings) ) {
00747
00748 foreach ( $aModuleSettings as $aValue ) {
00749 $sOxId = oxUtilsObject::getInstance()->generateUId();
00750
00751 $sModule = 'module:'.$sModuleId;
00752 $sName = $aValue["name"];
00753 $sType = $aValue["type"];
00754 $sValue = is_null($oConfig->getConfigParam($sName))?$aValue["value"]:$oConfig->getConfigParam($sName);
00755 $sGroup = $aValue["group"];
00756 $sConstrains = $aValue["constrains"]?$aValue["constrains"]:'';
00757 $iPosition = $aValue["position"]?$aValue["position"]:1;
00758
00759 $oConfig->setConfigParam($sName, $sValue);
00760 $oConfig->saveShopConfVar($sType, $sName, $sValue, $sShopId, $sModule);
00761
00762 $sDeleteSql = "DELETE FROM `oxconfigdisplay` WHERE OXCFGMODULE=".$oDb->quote($sModule)." AND OXCFGVARNAME=".$oDb->quote($sName);
00763 $sInsertSql = "INSERT INTO `oxconfigdisplay` (`OXID`, `OXCFGMODULE`, `OXCFGVARNAME`, `OXGROUPING`, `OXVARCONSTRAINT`, `OXPOS`) ".
00764 "VALUES ('{$sOxId}', ".$oDb->quote($sModule).", ".$oDb->quote($sName).", ".$oDb->quote($sGroup).", ".$oDb->quote($sConstrains).", ".$oDb->quote($iPosition).")";
00765
00766 $oDb->execute( $sDeleteSql );
00767 $oDb->execute( $sInsertSql );
00768 }
00769 }
00770 }
00771
00779 public function getTemplates( $sModuleId = null )
00780 {
00781 if (is_null($sModuleId)) {
00782 $sModuleId = $this->getId();
00783 }
00784
00785 if (!$sModuleId) {
00786 return;
00787 }
00788
00789 $sShopId = $this->getConfig()->getShopId();
00790 $aTemplates = oxDb::getDb()->getCol("SELECT oxtemplate FROM oxtplblocks WHERE oxmodule = '$sModuleId' AND oxshopid = '$sShopId'" );
00791
00792 return $aTemplates;
00793 }
00794
00806 public function saveLegacyModule($sModuleId, $sModuleName, $aModuleInfo = null)
00807 {
00808 $aLegacyModules = $this->getLegacyModules();
00809
00810 if ( !empty( $aModuleInfo ) && is_array($aModuleInfo)) {
00811 $aLegacyModules[$sModuleId]["id"] = $sModuleId;
00812 $aLegacyModules[$sModuleId]["title"] = ( $sModuleName ) ? $sModuleName : $sModuleId;
00813 $aLegacyModules[$sModuleId]['extend'] = array();
00814
00815 foreach ( $aModuleInfo as $sKey => $sValue ) {
00816 if ( strpos( $sValue, "=>" ) > 1 ) {
00817 $aClassInfo = explode( "=>", $sValue );
00818 $sClassName = trim( $aClassInfo[0] );
00819 $sExtendString = trim( $aClassInfo[1] );
00820 $aLegacyModules[$sModuleId]['extend'][$sClassName] = $sExtendString;
00821 }
00822 }
00823 }
00824
00825 if ( !empty( $aLegacyModules[$sModuleId]['extend'] ) ) {
00826 $this->getConfig()->saveShopConfVar( "aarr", "aLegacyModules", $aLegacyModules );
00827 }
00828 return $sModuleId;
00829 }
00830
00839 public function updateModuleIds( $sModuleLegacyId, $sModuleId )
00840 {
00841 $oConfig = $this->getConfig();
00842
00843
00844 $aModulePaths = $oConfig->getConfigParam( 'aModulePaths' );
00845 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00846 unset( $aModulePaths[$sModuleLegacyId] );
00847
00848 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00849
00850 if ( isset($aModulePaths[$sModuleLegacyId]) ) {
00851 $aModulePaths[$sModuleId] = $aModulePaths[$sModuleLegacyId];
00852 unset( $aModulePaths[$sModuleLegacyId] );
00853 $oConfig->saveShopConfVar( 'aarr', 'aModulePaths', $aModulePaths );
00854 }
00855
00856
00857 $aDisabledModules = $oConfig->getConfigParam( 'aDisabledModules' );
00858
00859 if ( is_array($aDisabledModules) ) {
00860 $iOldKey = array_search( $sModuleLegacyId, $aDisabledModules );
00861 if ( $iOldKey !== false ) {
00862 unset( $aDisabledModules[$iOldKey] );
00863 $aDisabledModules[$iOldKey] = $sModuleId;
00864 $oConfig->saveShopConfVar( 'arr', 'aDisabledModules', $aDisabledModules );
00865 }
00866 }
00867 }
00868 }