From f883543f00bfb7790b16c80162d4bea4a23ea0be Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Thu, 22 Oct 2020 13:30:22 +0200 Subject: [PATCH] Moved installable into ModuleManager --- .../components/ContentContainerModule.php | 21 ------------ .../ContentContainerModuleManager.php | 34 +++++++++++++++---- .../space/controllers/CreateController.php | 7 ++-- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/protected/humhub/modules/content/components/ContentContainerModule.php b/protected/humhub/modules/content/components/ContentContainerModule.php index 14954c63e7..e06191030d 100644 --- a/protected/humhub/modules/content/components/ContentContainerModule.php +++ b/protected/humhub/modules/content/components/ContentContainerModule.php @@ -219,25 +219,4 @@ class ContentContainerModule extends Module return []; } - /** - * This method is called to find all modules which available and set as default - * - * @param $container - * @return mixed - * @since 1.7 - */ - public static function getInstallableModules($container) - { - $availableModules = $container->getAvailableModules(); - foreach ($availableModules as $moduleId => $module) { - if (($container->isModuleEnabled($moduleId) && !$container->canDisableModule($moduleId)) || - (!$container->isModuleEnabled($moduleId) && !$container->canEnableModule($moduleId)) - ) { - unset($availableModules[$moduleId]); - } - } - return $availableModules; - } } - - diff --git a/protected/humhub/modules/content/components/ContentContainerModuleManager.php b/protected/humhub/modules/content/components/ContentContainerModuleManager.php index 0f48d92a55..c759073f4a 100644 --- a/protected/humhub/modules/content/components/ContentContainerModuleManager.php +++ b/protected/humhub/modules/content/components/ContentContainerModuleManager.php @@ -84,7 +84,7 @@ class ContentContainerModuleManager extends \yii\base\Component public function isEnabled($id) { // Workaround for core post module - if($id === 'post') { + if ($id === 'post') { return true; } @@ -157,7 +157,7 @@ class ContentContainerModuleManager extends \yii\base\Component foreach (Yii::$app->moduleManager->getModules() as $id => $module) { if ($module instanceof ContentContainerModule && Yii::$app->hasModule($module->id) && - $module->hasContentContainerType($this->contentContainer->className())) { + $module->hasContentContainerType($this->contentContainer->className())) { $this->_available[$module->id] = $module; } } @@ -165,6 +165,28 @@ class ContentContainerModuleManager extends \yii\base\Component return $this->_available; } + /** + * Returns a list of modules that can be installed by the ContentContainer. + * Unlike `getAvailable()` it does not contain any modules which cannot be disabled or enabled. + * + * @return ContentContainerModule[] a list of modules + * @since 1.7 + */ + public function getInstallable() + { + + $availableModules = $this->getAvailable(); + foreach ($availableModules as $moduleId => $module) { + if (($this->isEnabled($moduleId) && !$this->canDisable($moduleId)) || + (!$this->isEnabled($moduleId) && !$this->canEnable($moduleId)) + ) { + unset($availableModules[$moduleId]); + } + } + return $availableModules; + + } + /** * Flushes the cache of available modules. * @since 1.3.11 @@ -177,8 +199,8 @@ class ContentContainerModuleManager extends \yii\base\Component /** * Returns an array of all module states. * - * @see Module * @return array a list of modules with the corresponding state + * @see Module */ protected function getStates() { @@ -227,16 +249,16 @@ class ContentContainerModuleManager extends \yii\base\Component if ($state === null) { return null; } else { - return (int) $state; + return (int)$state; } } /** * Returns an Module record instance for the given module id * - * @see Module * @param string $id the module id * @return ContentContainerModuleState + * @see Module */ protected function getModuleStateRecord($id) { @@ -270,7 +292,7 @@ class ContentContainerModuleManager extends \yii\base\Component $contentContainerClasses = [\humhub\modules\user\models\User::class, \humhub\modules\space\models\Space::class]; foreach ($contentContainerClasses as $class) { $reflect = new ReflectionClass($class); - $defaultState = (int) $moduleSettings->get('moduleManager.defaultState.' . $reflect->getShortName()); + $defaultState = (int)$moduleSettings->get('moduleManager.defaultState.' . $reflect->getShortName()); if ($defaultState === ContentContainerModuleState::STATE_ENABLED || $defaultState === ContentContainerModuleState::STATE_FORCE_ENABLED) { $query->orWhere(['contentcontainer.class' => $class]); } diff --git a/protected/humhub/modules/space/controllers/CreateController.php b/protected/humhub/modules/space/controllers/CreateController.php index 2a3b1f33d7..968252390f 100644 --- a/protected/humhub/modules/space/controllers/CreateController.php +++ b/protected/humhub/modules/space/controllers/CreateController.php @@ -135,10 +135,11 @@ class CreateController extends Controller */ public function actionModules($space_id) { - $space = Space::find()->where(['id' => $space_id])->one(); + $space = Space::find()->where(['id' => (int)$space_id])->one(); - $installableModules = ContentContainerModule::getInstallableModules($space); - if (count($installableModules) == 0) { + $installableModules = $space->moduleManager->getInstallable(); + + if (count($installableModules) === 0) { return $this->actionInvite($space); } else { return $this->renderAjax('modules', ['space' => $space, 'availableModules' => $installableModules]);