Allow modules to bundle themes

This commit is contained in:
Lucas Bartholemy 2015-11-10 17:24:20 +01:00
parent cf6002ae6a
commit d2096f7ff6
5 changed files with 151 additions and 55 deletions

View File

@ -92,13 +92,23 @@ class Module extends \yii\base\Module
$moduleImageFile = $this->getBasePath() . '/' . $this->resourcesPath . '/module_image.png';
if (is_file($moduleImageFile)) {
$published = $assetManager = Yii::$app->assetManager->publish($this->getBasePath() . '/' . $this->resourcesPath);
return $published[1] . '/module_image.png';
return $this->getAssetsUrl() . '/module_image.png';
}
return Yii::getAlias("@web/img/default_module.jpg");
}
/**
* Get Assets Url
*
* @return String Image Url
*/
public function getAssetsUrl()
{
$published = Yii::$app->assetManager->publish($this->getBasePath() . '/' . $this->resourcesPath);
return $published[1];
}
/**
* Enables this module
* It will be available on the next request.

View File

@ -24,18 +24,26 @@ class Theme extends \yii\base\Theme
*/
public $name;
/**
* @inheritdoc
*/
private $_baseUrl = null;
/**
* @inheritdoc
*/
public function init()
{
if ($this->getBasePath() == '') {
$this->setBasePath('@webroot/themes/' . $this->name);
}
$this->setBasePath('@webroot/themes/' . $this->name);
$this->pathMap = [
'@humhub/views' => '@webroot/themes/' . $this->name . '/views',
'@humhub/views' => $this->getBasePath() . '/views',
];
parent::init();
}
private $_baseUrl = null;
/**
* @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
* to be under this base URL.
@ -114,45 +122,117 @@ class Theme extends \yii\base\Theme
/**
* Returns an array of all installed themes.
*
* @return Array
* @return Array Theme instances
*/
public static function getThemes()
{
$themes = array();
$themePath = \Yii::getAlias('@webroot/themes');
$themePaths = [];
$themePaths[] = \Yii::getAlias('@webroot/themes');
foreach (scandir($themePath) as $file) {
if ($file == "." || $file == ".." || !is_dir($themePath . DIRECTORY_SEPARATOR . $file)) {
continue;
// Collect enabled module theme paths
foreach (Yii::$app->getModules() as $module) {
$basePath = "";
if (is_array($module)) {
$reflector = new \ReflectionClass($module['class']);
$basePath = dirname($reflector->getFileName());
} else {
$basePath = $module->getBasePath();
}
if (is_dir($basePath . DIRECTORY_SEPARATOR . 'themes')) {
$themePaths[] = $basePath . DIRECTORY_SEPARATOR . 'themes';
}
}
foreach ($themePaths as $themePath) {
foreach (scandir($themePath) as $file) {
if ($file == "." || $file == ".." || !is_dir($themePath . DIRECTORY_SEPARATOR . $file)) {
continue;
}
$themes[] = Yii::createObject([
'class' => 'humhub\components\Theme',
'basePath' => $themePath . DIRECTORY_SEPARATOR . $file,
'name' => $file
]);
}
$themes[$file] = $file;
}
return $themes;
}
/**
* Returns a Theme by given name
*
* @param string $name of the theme
* @return Theme
*/
public static function getThemeByName($name)
{
foreach (self::getThemes() as $theme) {
if ($theme->name === $name) {
return $theme;
}
}
return null;
}
/**
* Returns configuration array of given theme
*
* @param Theme|string $theme name or theme instance
* @return array Configuration
*/
public static function getThemeConfig($theme)
{
if (is_string($theme)) {
$theme = self::getThemeByName($theme);
}
return [
'components' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath()
],
],
'mailer' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath()
]
]
]
]
];
}
public static function setColorVariables($themeName)
{
$theme = self::getThemeByName($themeName);
$url = Yii::getAlias('@webroot/themes/' . $themeName . '/css/theme.less');
$lessFileName = Yii::getAlias($theme->getBasePath() . '/css/theme.less');
if (file_exists($lessFileName)) {
$file = fopen($lessFileName, "r") or die("Unable to open file!");
$less = fread($file, filesize($lessFileName));
fclose($file);
$file = fopen("$url", "r") or die("Unable to open file!");
$less = fread($file, filesize("$url"));
fclose($file);
$startDefault = strpos($less, '@default') + 10;
$startPrimary = strpos($less, '@primary') + 10;
$startInfo = strpos($less, '@info') + 7;
$startSuccess = strpos($less, '@success') + 10;
$startWarning = strpos($less, '@warning') + 10;
$startDanger = strpos($less, '@danger') + 9;
$length = 7;
$startDefault = strpos($less, '@default') + 10;
$startPrimary = strpos($less, '@primary') + 10;
$startInfo = strpos($less, '@info') + 7;
$startSuccess = strpos($less, '@success') + 10;
$startWarning = strpos($less, '@warning') + 10;
$startDanger = strpos($less, '@danger') + 9;
$length = 7;
Setting::Set('colorDefault', substr($less, $startDefault, $length));
Setting::Set('colorPrimary', substr($less, $startPrimary, $length));
Setting::Set('colorInfo', substr($less, $startInfo, $length));
Setting::Set('colorSuccess', substr($less, $startSuccess, $length));
Setting::Set('colorWarning', substr($less, $startWarning, $length));
Setting::Set('colorDanger', substr($less, $startDanger, $length));
Setting::Set('colorDefault', substr($less, $startDefault, $length));
Setting::Set('colorPrimary', substr($less, $startPrimary, $length));
Setting::Set('colorInfo', substr($less, $startInfo, $length));
Setting::Set('colorSuccess', substr($less, $startSuccess, $length));
Setting::Set('colorWarning', substr($less, $startWarning, $length));
Setting::Set('colorDanger', substr($less, $startDanger, $length));
}
}
}

View File

@ -8,17 +8,31 @@
namespace humhub\libs;
use humhub\models\Setting;
use Yii;
use yii\helpers\ArrayHelper;
use humhub\components\Theme;
use humhub\models\Setting;
/**
* Description of DynamicConfig
* DynamicConfig provides access to the dynamic configuration file.
*
* @author luke
*/
class DynamicConfig extends \yii\base\Object
{
/**
* Add an array to the dynamic configuration
*
* @param array $new
*/
public static function merge($new)
{
$config = self::load();
\yii\helpers\ArrayHelper::merge($new);
self::save($config);
}
public static function onSettingChange($setting)
{
// Only rewrite static configuration file when necessary
@ -122,7 +136,6 @@ class DynamicConfig extends \yii\base\Object
'keyPrefix' => Yii::$app->id
];
}
}
// Add User settings
$config['components']['user'] = array();
@ -163,16 +176,7 @@ class DynamicConfig extends \yii\base\Object
$mail['useFileTransport'] = true;
}
$config['components']['mailer'] = $mail;
// Add Theme
$theme = Setting::Get('theme');
if ($theme && $theme != "") {
$config['components']['view']['theme']['name'] = $theme;
$config['components']['mailer']['view']['theme']['name'] = $theme;
} else {
unset($config['components']['view']['theme']['name']);
unset($config['components']['mailer']['view']['theme']['name']);
}
$config = ArrayHelper::merge($config, Theme::getThemeConfig(Setting::Get('theme')));
$config['params']['config_created_at'] = time();
self::save($config);

View File

@ -14,6 +14,7 @@ use humhub\models\Setting;
use humhub\models\UrlOembed;
use humhub\modules\admin\components\Controller;
use humhub\modules\user\libs\Ldap;
/**
* SettingController
*
@ -321,9 +322,10 @@ class SettingController extends Controller
public function actionDesign()
{
$form = new \humhub\modules\admin\models\forms\DesignSettingsForm;
#$assetPrefix = Yii::$app->assetManager->publish(dirname(__FILE__) . '/../resources', true, 0, defined('YII_DEBUG'));
#Yii::$app->clientScript->registerScriptFile($assetPrefix . '/uploadLogo.js');
$form->theme = Setting::Get('theme');
$form->paginationSize = Setting::Get('paginationSize');
$form->displayName = Setting::Get('displayNameFormat');
$form->spaceOrder = Setting::Get('spaceOrder', 'space');
if ($form->load(Yii::$app->request->post())) {
@ -350,14 +352,13 @@ class SettingController extends Controller
Yii::$app->getSession()->setFlash('data-saved', Yii::t('AdminModule.controllers_SettingController', 'Saved'));
Yii::$app->response->redirect(Url::toRoute('/admin/setting/design'));
}
} else {
$form->theme = Setting::Get('theme');
$form->paginationSize = Setting::Get('paginationSize');
$form->displayName = Setting::Get('displayNameFormat');
$form->spaceOrder = Setting::Get('spaceOrder', 'space');
}
$themes = \humhub\components\Theme::getThemes();
$themes = [];
foreach (\humhub\components\Theme::getThemes() as $theme) {
$themes[$theme->name] = $theme->name;
}
return $this->render('design', array('model' => $form, 'themes' => $themes, 'logo' => new \humhub\libs\LogoImage()));
}

View File

@ -22,8 +22,10 @@ class DesignSettingsForm extends \yii\base\Model
*/
public function rules()
{
$themes = \humhub\components\Theme::getThemes();
$themes = [];
foreach (\humhub\components\Theme::getThemes() as $theme) {
$themes[] = $theme->name;
}
return array(
array('paginationSize', 'integer', 'max' => 200, 'min' => 1),
@ -54,7 +56,6 @@ class DesignSettingsForm extends \yii\base\Model
{
if (is_object($this->logo)) {
list($width, $height) = getimagesize($this->logo->tempName);
if ($height < 40)
$this->addError('logo', 'Logo size should have at least 40px of height');