Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh: Error Handling & Redundancy Reduction #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 89 additions & 31 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,87 @@
use humhub\libs\DynamicConfig;
use humhub\modules\ui\view\helpers\ThemeHelper;
use Yii;
use yii\base\Exception;
use yii\helpers\Url;

/**
* Module class for Clean Theme module.
*
* This module provides a clean theme for HumHub based on the Community theme.
*/
class Module extends \humhub\components\Module
{
/**
* @var array The name of the clean themes.
*/
public const THEME_NAMES = [
'clean-base',
'clean-bordered',
'clean-contrasted',
];

/**
* @inheridoc
* @var string The icon for the module.
*/
public string $icon = 'circle-o-notch';

/**
* @inheridoc
* @var string The path to the module's resources.
*/
public $resourcesPath = 'resources';

public bool $hideTopMenuOnScrollDown = true; // On small screens only
public bool $hideBottomMenuOnScrollDown = true; // On small screens only
public bool $hideTextInBottomMenuItems = true; // On small screens only
public bool $collapsibleLeftNavigation = false;
/**
* @var bool Whether to hide the top menu on scroll down (on small screens only).
*/
public bool $hideTopMenuOnScrollDown = true;

/**
* @var bool Whether to hide the bottom menu on scroll down (on small screens only).
*/
public bool $hideBottomMenuOnScrollDown = true;

/**
* @var bool Whether to hide text in bottom menu items (on small screens only).
*/
public bool $hideTextInBottomMenuItems = true;

/**
* @var bool Whether to make the left navigation collapsible.
*/
public bool $collapsibleLeftNavigation = false;

/**
* Returns the name of the module.
*
* @return string The module name.
*/
public function getName()
{
return Yii::t('CleanThemeModule.config', 'Clean theme');
}

/**
* Returns the description of the module.
*
* @return string The module description.
*/
public function getDescription()
{
return Yii::t('CleanThemeModule.config', 'Clean theme for Humhub based on the Community theme');
}

/**
* @inheritdoc
* Returns the URL for configuring the module.
*
* @return string The configuration URL.
*/
public function getConfigUrl()
{
return Url::to(['/clean-theme/config']);
}

/**
* @inheritdoc
* Disables the module.
*/
public function disable()
{
Expand All @@ -65,21 +100,9 @@ public function disable()
}

/**
* @return void
*/
private function disableTheme()
{
foreach (ThemeHelper::getThemeTree(Yii::$app->view->theme) as $theme) {
if (in_array($theme->name, self::THEME_NAMES, true)) {
$ceTheme = ThemeHelper::getThemeByName('HumHub');
$ceTheme->activate();
break;
}
}
}

/**
* @inheritdoc
* Enables the module.
*
* @return bool Whether the module was successfully enabled.
*/
public function enable()
{
Expand All @@ -91,21 +114,56 @@ public function enable()
}

/**
* @return void
* Enables the clean theme.
*
* @throws Exception if an error occurs while enabling the theme.
*/
private function enableTheme()
{
// Check if already active
try {
foreach (ThemeHelper::getThemeTree(Yii::$app->view->theme) as $theme) {
if ($theme->name === self::THEME_NAMES) return;
}

$theme = ThemeHelper::getThemeByName(self::THEME_NAMES);
if ($theme !== null) {
$theme->activate();
$this->updateDynamicConfig();
}
} catch (Exception $e) {
Yii::error('Error enabling theme: ' . $e->getMessage(), 'clean-theme');
throw $e;
}
}

/**
* Disables the clean theme.
*/
private function disableTheme()
{
foreach (ThemeHelper::getThemeTree(Yii::$app->view->theme) as $theme) {
if (in_array($theme->name, self::THEME_NAMES, true)) {
return;
if ($theme->name === self::THEME_NAMES) {
$ceTheme = ThemeHelper::getThemeByName('HumHub');
$ceTheme->activate();
break;
}
}
}

$theme = ThemeHelper::getThemeByName($this->getBaseThemeName());
if ($theme !== null) {
$theme->activate();
DynamicConfig::rewrite();
/**
* Updates the dynamic configuration after enabling the theme.
*
* @throws Exception if an error occurs while updating the dynamic configuration.
*/
private function updateDynamicConfig()
{
try {
$config = DynamicConfig::load();
$config['theme'] = self::THEME_NAMES;
DynamicConfig::save($config);
} catch (Exception $e) {
Yii::error('Error updating dynamic config: ' . $e->getMessage(), 'clean-theme');
throw $e;
}
}

Expand Down