Moved installable into ModuleManager

This commit is contained in:
Lucas Bartholemy 2020-10-22 13:30:22 +02:00
parent 501cd6181b
commit f883543f00
3 changed files with 32 additions and 30 deletions

View File

@ -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;
}
}

View File

@ -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]);
}

View File

@ -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]);