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

CMS Theme import and management #211

Merged
merged 89 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 85 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
e3dd0cb
New theme path
zixxus Apr 4, 2019
d90568c
Revert file mode
zixxus Apr 6, 2019
f49be2c
Merge remote-tracking branch 'zixxus/themes' into theme-loader-1916
boehsermoe Jul 14, 2019
c51392e
Refactoring
boehsermoe Jul 16, 2019
82f4cf4
Merge branch 'master' of https://github.com/luyadev/luya-module-cms i…
boehsermoe Jul 24, 2019
c82ac84
Yii2 view theming
boehsermoe Jul 26, 2019
5c942b2
Yii2 view theming
boehsermoe Jul 26, 2019
2273dd1
Theme importer
boehsermoe Jul 26, 2019
e394e82
Reformat & cleanup
boehsermoe Jul 26, 2019
91335ac
Theme import & admin settings
boehsermoe Jul 28, 2019
c44002f
Refactoring
boehsermoe Jul 29, 2019
cca339b
Active theme loading
boehsermoe Jul 30, 2019
6a2f314
Theme loading
boehsermoe Jul 30, 2019
d8c0c2f
Refactoring
boehsermoe Jul 30, 2019
d037d96
Refactoring
boehsermoe Jul 30, 2019
065a621
Refactoring
boehsermoe Jul 31, 2019
9fd5b75
Refactoring
boehsermoe Jul 31, 2019
f7880ce
Vendor alias path
boehsermoe Jul 31, 2019
9147a4e
Merge branch 'master' into theme-loader-1916
boehsermoe Aug 1, 2019
e3b7466
Block view and cmslayout with theming
boehsermoe Aug 1, 2019
4ed6112
Merge branch 'theme-loader-1916' of github.com:boehsermoe/luya-module…
boehsermoe Aug 1, 2019
a1eabe7
Unit test: Cmslayout include themes
boehsermoe Aug 1, 2019
c5441f9
Refactoring
boehsermoe Aug 1, 2019
299574d
Toolbar theme item
boehsermoe Aug 2, 2019
5ed7110
Merge thememanager config
boehsermoe Aug 4, 2019
edf4a7c
Refactoring
boehsermoe Aug 29, 2019
5532f23
Merge branch 'master' of https://github.com/luyadev/luya-module-cms i…
boehsermoe Aug 29, 2019
cdb8326
Theme unit tests
boehsermoe Aug 29, 2019
e153613
Unit tests extended
boehsermoe Sep 1, 2019
2f36c96
luya core update
boehsermoe Sep 9, 2019
9376fbd
missing theme.json
boehsermoe Sep 9, 2019
58b8a32
luya-testsuite update
boehsermoe Sep 9, 2019
e1a0eed
Unit tests fixes
boehsermoe Sep 9, 2019
54ef989
Unit tests fixes
boehsermoe Sep 9, 2019
92620c3
some strange conflict with runInSeparateProcess annotation
boehsermoe Sep 9, 2019
a2917e8
some strange conflict with runInSeparateProcess annotation
boehsermoe Sep 9, 2019
20fce07
some strange conflict with runInSeparateProcess annotation
boehsermoe Sep 9, 2019
123f966
refactoring unit tests
boehsermoe Sep 9, 2019
0092209
PCRE2 syntax fpr php7.3 support
boehsermoe Sep 9, 2019
71df5fb
PCRE2 syntax fpr php7.3 support
boehsermoe Sep 9, 2019
1cfe9f8
Test coverage for themeimporter
boehsermoe Sep 10, 2019
3d79900
ThemeTests
boehsermoe Sep 12, 2019
548a53f
Bootstrap setup test
boehsermoe Sep 12, 2019
699ffae
Theme model test
boehsermoe Sep 12, 2019
37331ff
codeclimate config
boehsermoe Sep 12, 2019
839121f
Reverted translations
boehsermoe Sep 12, 2019
64fb47a
Refactoring
boehsermoe Sep 13, 2019
1423d9f
Refactoring toolbar
boehsermoe Sep 13, 2019
ffdb6cc
Refactoring view theme assignment
boehsermoe Sep 13, 2019
5bca871
Handle invalid json
boehsermoe Sep 13, 2019
2a89cd6
Refactoring cms theme loading
boehsermoe Sep 21, 2019
ad7142b
Merge branch 'master' into theme-loader-1916
boehsermoe Sep 21, 2019
a203bc2
Exception handling for invalid themes
boehsermoe Sep 21, 2019
861223a
Theme import via ThemeManager
boehsermoe Sep 21, 2019
2672ec9
Update PhpBlockView.php
boehsermoe Sep 22, 2019
3f23179
Type hinting removed
boehsermoe Sep 27, 2019
cbc81a8
Update ThemeController.php
boehsermoe Oct 3, 2019
a80b836
Refactoring
boehsermoe Oct 3, 2019
72a27c8
Refactoring
boehsermoe Oct 4, 2019
a3e9136
Refactoring
boehsermoe Oct 4, 2019
83b6d1a
Refactoring
boehsermoe Oct 4, 2019
6f69a30
Merge branch 'master' of https://github.com/luyadev/luya-module-cms i…
boehsermoe Oct 23, 2019
14bdd8b
luyadev/core update
boehsermoe Oct 23, 2019
d86fe7b
Unit Tests fixed
boehsermoe Oct 24, 2019
56ac96d
Unit Tests fixed
boehsermoe Oct 24, 2019
e03a2b5
Unit Tests fixed
boehsermoe Oct 24, 2019
625242d
Unit Tests fixed
boehsermoe Oct 24, 2019
9abb212
Unit Tests fixed
boehsermoe Oct 24, 2019
f20c156
Unit Tests fixed
boehsermoe Oct 24, 2019
25dc4d3
Unit Tests fixed
boehsermoe Oct 24, 2019
8168a1f
Unit Tests fixed
boehsermoe Oct 24, 2019
1485386
more test coverage
boehsermoe Oct 25, 2019
afbf3e3
more test coverage
boehsermoe Oct 25, 2019
80f4df2
clear empty theme dir after test
boehsermoe Oct 25, 2019
30e3519
test coverage
boehsermoe Oct 25, 2019
dce9abb
Refactoring
boehsermoe Oct 25, 2019
0e7cfbf
Merge branch 'master' of https://github.com/luyadev/luya-module-cms i…
boehsermoe Nov 28, 2019
dfc2476
Changelog #211
boehsermoe Nov 28, 2019
36b0574
Optimized the active theme loading
boehsermoe Nov 28, 2019
8325825
expect admin request on bootstrapping
boehsermoe Dec 4, 2019
22e35ee
travis tests with php 7.4
boehsermoe Dec 4, 2019
979c4b9
travis tests with php 7.4
boehsermoe Dec 4, 2019
043d375
refactoring
boehsermoe Dec 10, 2019
a033b57
testing toolbar rendering
boehsermoe Dec 17, 2019
5eab49c
Merge branch 'master' into theme-loader-1916
boehsermoe Dec 17, 2019
669e62d
testing toolbar rendering with active theme
boehsermoe Dec 17, 2019
5b52e06
Merge branch 'theme-loader-1916' of github.com:boehsermoe/luya-module…
boehsermoe Dec 17, 2019
959a634
phpdocs
boehsermoe Dec 18, 2019
973ff13
missing translation
boehsermoe Dec 18, 2019
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
4 changes: 4 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exclude_patterns:
- "src/frontend/messages/**/*.php"
- "src/admin/messages/**/*.php"
- "src/admin/migrations/*.php"
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

matrix:
allow_failures:
- php: 5.6
- php: 7.4
- php: nightly

sudo: false
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 3.0

+ [#211](https://github.com/luyadev/luya-module-cms/pull/211) Add the theme loading and management to the cms module based on core theme [luya/issues/1916](https://github.com/luyadev/luya/issues/1916)
+ [#235](https://github.com/luyadev/luya-module-cms/issues/235) Fixed cms toolbar bug in ajax context.

## 2.2.0 (12. November 2019)
Expand Down
4 changes: 4 additions & 0 deletions src/admin/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace luya\cms\admin;

use luya\cms\admin\importers\ThemeImporter;
use Yii;
use luya\console\interfaces\ImportControllerInterface;
use luya\cms\admin\importers\BlockImporter;
Expand Down Expand Up @@ -54,6 +55,7 @@ final class Module extends \luya\admin\base\Module implements CoreModuleInterfac
'api-cms-navcontainer' => 'luya\cms\admin\apis\NavContainerController',
'api-cms-navitemblock' => 'luya\cms\admin\apis\NavItemBlockController',
'api-cms-redirect' => 'luya\cms\admin\apis\RedirectController',
'api-cms-theme' => 'luya\cms\admin\apis\ThemeController',
];

public $apiRules = [
Expand Down Expand Up @@ -264,6 +266,7 @@ public function getMenu()
->itemRoute('menu_group_item_env_permission', "cmsadmin/permission/index", 'gavel')
->itemApi('menu_group_item_env_container', 'cmsadmin/navcontainer/index', 'label_outline', 'api-cms-navcontainer')
->itemApi('menu_group_item_env_layouts', 'cmsadmin/layout/index', 'view_quilt', 'api-cms-layout')
->itemApi('menu_group_item_env_themes', 'cmsadmin/theme/index', 'color_lens', 'api-cms-theme')
->itemRoute('menu_group_item_env_config', 'cmsadmin/config/index', 'build')
->itemApi('menu_group_item_env_redirections', 'cmsadmin/redirect/index', 'compare_arrows', 'api-cms-redirect')
->group('menu_group_elements')
Expand Down Expand Up @@ -302,6 +305,7 @@ public function extendPermissionRoutes()
public function import(ImportControllerInterface $importer)
{
return [
ThemeImporter::class,
BlockImporter::class,
CmslayoutImporter::class,
PropertyConsistencyImporter::class,
Expand Down
16 changes: 16 additions & 0 deletions src/admin/apis/ThemeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace luya\cms\admin\apis;

/**
* Theme for LUYA CMS.
*
* This module / component allow user to manage actual display themes.
*
* @author Bennet Klarhoelter <boehsermoe@me.com>
* @since 3.0.0
*/
class ThemeController extends \luya\admin\ngrest\base\Api
{
public $modelClass = 'luya\cms\models\Theme';
}
18 changes: 18 additions & 0 deletions src/admin/controllers/ThemeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace luya\cms\admin\controllers;

use luya\admin\ngrest\base\Controller;

/**
* Theme for LUYA CMS.
*
* This module / component allow user to manage actual display themes.
*
* @author Bennet Klarhoelter <boehsermoe@me.com>
* @since 3.0.0
*/
class ThemeController extends Controller
{
public $modelClass = 'luya\cms\models\Theme';
}
8 changes: 4 additions & 4 deletions src/admin/importers/BlockImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ public function run()
}

$exists = [];

foreach ($this->getImporter()->getDirectoryFiles('blocks') as $file) {
$exists[] = $this->saveBlock($file['ns']);
}

foreach (Yii::$app->packageInstaller->configs as $config) {
$exists = array_merge($exists, $this->handleBlockDefinitions($config->blocks));
}

// provide backwards compatibility for core 1.0.7 and below
if ($this->hasProperty('module')) {
$exists = array_merge($exists, $this->handleBlockDefinitions($this->module->blocks));
}

foreach (Block::find()->all() as $block) {
if (!class_exists($block->class)) {
$this->addLog("[!] The block {$block->class} used {$block->usageCount} times, does not exists anymore. You should either use migrate or cleanup command.");
Expand Down
19 changes: 14 additions & 5 deletions src/admin/importers/CmslayoutImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function run()
foreach ((array) $this->module->cmsLayouts as $layoutDefintion) {
$this->handleLayoutFile($layoutFiles, $layoutDefintion);
}

foreach (Yii::$app->themeManager->getThemes() as $themeConfig) {
$this->handleLayoutFile($layoutFiles, $themeConfig->getViewPath() . DIRECTORY_SEPARATOR . 'cmslayouts', $themeConfig->name);
}

// remove all view files not found somewhere ...
foreach (Layout::find()->where(['not in', 'id', $layoutFiles])->all() as $layoutItem) {
Expand All @@ -55,21 +59,22 @@ public function run()
*
* @param array $layoutFiles
* @param string $path
* @param string $themeName
*/
protected function handleLayoutFile(&$layoutFiles, $path)
protected function handleLayoutFile(&$layoutFiles, $path, $themeName = null)
{
$aliased = Yii::getAlias($path, false);
$filePath = $aliased ? $aliased : $path;

if (is_dir($filePath)) {
foreach ($this->getFilesFromFolder($filePath) as $file) {
$handler = $this->importLayoutFile($file, $path);
$handler = $this->importLayoutFile($file, $path, $themeName);
if ($handler) {
$layoutFiles[] = $handler;
}
}
} else {
$handler = $this->importLayoutFile($filePath, $path);
$handler = $this->importLayoutFile($filePath, $path, $themeName);
if ($handler) {
$layoutFiles[] = $handler;
}
Expand Down Expand Up @@ -99,7 +104,7 @@ protected function getFilesFromFolder($folderPath)
* @throws Exception
* @return string
*/
protected function importLayoutFile($file, $aliased)
protected function importLayoutFile($file, $aliased, $themeName = null)
{
if (!file_exists($file)) {
return false;
Expand Down Expand Up @@ -131,7 +136,11 @@ protected function importLayoutFile($file, $aliased)
}

$readableFileName = $this->generateReadableName($fileinfo->name);


if ($themeName) {
$readableFileName = $this->generateReadableName($themeName) . ' - ' . $readableFileName;
}

$content = file_get_contents($file);

preg_match_all("/placeholders\[[\'\"](.*?)[\'\"]\]/", $content, $results);
Expand Down
124 changes: 124 additions & 0 deletions src/admin/importers/ThemeImporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace luya\cms\admin\importers;

use luya\cms\models\Theme;
use luya\console\Importer;
use luya\helpers\Json;
use luya\theme\ThemeConfig;
use luya\theme\ThemeManager;
use Yii;
use yii\base\InvalidConfigException;

/**
* Class ThemeImporter
* Import theme.json files from the folder and analyse config.
*
* @property ThemeManager $themeManager
*
* @author Bennet Klarhoelter <boehsermoe@me.com>
* @since 3.0.0
*/
class ThemeImporter extends Importer
{
/**
* @var ThemeManager
*/
private $_themeManager;

/**
* @inheritdoc
*/
public function run($thowException = false)
{
$exists = [];

foreach ($this->getThemeManager()->getThemes($thowException) as $theme) {
/** @var \luya\theme\Theme $theme */
$exists = array_merge($exists, $this->handleThemeDefinitionInDirectories($theme->basePath));
}

foreach (Theme::find()->all() as $theme) {
if (!in_array($theme->id, $exists) && $theme->delete()) {
$this->addLog("[!] The theme {$theme->base_path} does not found anymore and was deleted.");
}
}

return $this->addLog("Theme importer finished with " . count($exists) . " themes.");
}

/**
* Handle a theme definition for different folders
*
* @param string $themeDefinition
* @return array
*/
protected function handleThemeDefinitionInDirectories($themeDefinition)
{
$results = [];

$themeDefinition = preg_replace('#^vendor/#', '@vendor/', $themeDefinition);

$themeId = $this->saveTheme($themeDefinition);
if ($themeId) {
$results[$themeDefinition] = $themeId;
}

return $results;
}

/**
* Save a theme by its base path.
* Example path: @app/themes/blank
*
* @param $basePath
* @return mixed|bool False if could not exists.
* @throws InvalidConfigException
*/
protected function saveTheme($basePath)
{
$themeFile = Yii::getAlias($basePath . '/theme.json');
$config = Json::decode(file_get_contents($themeFile)) ?: [];

$themeConfig = new ThemeConfig($basePath, $config);

$themeModel = Theme::findOne(['base_path' => $basePath]);

if (!$themeModel) {
$themeModel = new Theme();
$themeModel->base_path = $basePath;
$themeModel->setThemeConfig($themeConfig);

$log = "Added theme $basePath to database.";
} else {
$themeModel->setThemeConfig($themeConfig);
$log = "Updated theme $basePath.";
}

if ($themeModel->save()) {
$this->addLog($log);
}

return $themeModel->id;
}

/**
* @return ThemeManager
*/
public function getThemeManager()
{
if ($this->_themeManager == null) {
$this->_themeManager = Yii::$app->themeManager;
}

return $this->_themeManager;
}

/**
* @param ThemeManager $themeManager
*/
public function setThemeManager($themeManager)
boehsermoe marked this conversation as resolved.
Show resolved Hide resolved
{
$this->_themeManager = $themeManager;
}
}
24 changes: 24 additions & 0 deletions src/admin/migrations/m190728_084936_cms_theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use yii\db\Migration;

/**
* Class m190728_084936_cms_theme
*/
class m190728_084936_cms_theme extends Migration
{
public function safeUp()
{
$this->createTable('cms_theme', [
'id' => $this->primaryKey(),
'is_active' => $this->boolean()->notNull()->defaultValue(0),
'base_path' => $this->string(255)->notNull()->unique(),
'json_config' => $this->text()->notNull(),
]);
}

public function safeDown()
{
$this->dropTable('cms_theme');
}
}
26 changes: 14 additions & 12 deletions src/admin/views/aws/blockpages/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@
<th>Created</th>
</tr>
</thead>
<?php foreach ($blocks as $block): ?>
<tr ui-sref="custom.cmsedit({ navId : <?= $block->navItemPage->forceNavItem->nav_id; ?>, templateId: 'cmsadmin/default/index'})" style="cursor:pointer;">
<td>
<a ui-sref="custom.cmsedit({ navId : <?= $block->navItemPage->forceNavItem->nav_id; ?>, templateId: 'cmsadmin/default/index'})"><?= $block->navItemPage->forceNavItem->title; ?></a>
</td>
<td><?= $block->navItemPage->forceNavItem->lang->name; ?></td>
<td><?= $block->navItemPage->version_alias; ?></td>
<td><?= Yii::$app->formatter->asBoolean(!$block->is_hidden); ?></td>
<td><?= Yii::$app->formatter->asDate($block->navItemPage->forceNavItem->timestamp_update); ?></td>
<td><?= Yii::$app->formatter->asDate($block->navItemPage->forceNavItem->timestamp_create); ?></td>
</tr>
<?php endforeach; ?>
<?php if (isset($blocks)) : ?>
<?php foreach ($blocks as $block): ?>
<tr ui-sref="custom.cmsedit({ navId : <?= $block->navItemPage->forceNavItem->nav_id; ?>, templateId: 'cmsadmin/default/index'})" style="cursor:pointer;">
<td>
<a ui-sref="custom.cmsedit({ navId : <?= $block->navItemPage->forceNavItem->nav_id; ?>, templateId: 'cmsadmin/default/index'})"><?= $block->navItemPage->forceNavItem->title; ?></a>
</td>
<td><?= $block->navItemPage->forceNavItem->lang->name; ?></td>
<td><?= $block->navItemPage->version_alias; ?></td>
<td><?= Yii::$app->formatter->asBoolean(!$block->is_hidden); ?></td>
<td><?= Yii::$app->formatter->asDate($block->navItemPage->forceNavItem->timestamp_update); ?></td>
<td><?= Yii::$app->formatter->asDate($block->navItemPage->forceNavItem->timestamp_create); ?></td>
</tr>
<?php endforeach; ?>
<?php endif ?>
</table>
11 changes: 5 additions & 6 deletions src/frontend/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace luya\cms\frontend;

use Yii;
use luya\cms\models\Config;
use luya\web\Application;
use luya\web\ErrorHandler;
use luya\web\ErrorHandlerExceptionRenderEvent;
use yii\base\BootstrapInterface;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use luya\web\ErrorHandlerExceptionRenderEvent;
use luya\web\Application;
use luya\web\ErrorHandler;
use luya\cms\models\Config;

/**
* CMS Bootstrap.
Expand Down Expand Up @@ -48,7 +47,7 @@ public function bootstrap($app)
// handle not found exceptions
$app->errorHandler->on(ErrorHandler::EVENT_BEFORE_EXCEPTION_RENDER, function (ErrorHandlerExceptionRenderEvent $event) use ($app) {
if ($app instanceof Application && $event->exception instanceof NotFoundHttpException && !$app->request->isAdmin) {
$errorPageNavId = Config::get(Config::HTTP_EXCEPTION_NAV_ID, 0);
$errorPageNavId = Config::get(Config::HTTP_EXCEPTION_NAV_ID, 0);
// if not defined abort.
if (!$errorPageNavId) {
return;
Expand Down
Loading