Browse Source

Setting bootstrap load

Console set setting controller
master
Egorka 6 years ago
parent
commit
7e19737243
  1. 13
      backend/bootstrap/SetUp.php
  2. 4
      backend/controllers/ModuleController.php
  3. 1
      backend/controllers/settings/ListController.php
  4. 2
      backend/views/settings/list/index.php
  5. 48
      console/config/main.php
  6. 71
      console/controllers/SettingsController.php
  7. 3
      core/behaviors/LanguageBehavior.php
  8. 2
      core/forms/SettingsForm.php
  9. 31
      frontend/bootstrap/SetUp.php

13
backend/bootstrap/SetUp.php

@ -45,20 +45,9 @@ class SetUp implements BootstrapInterface
// load settings // load settings
$settings = Settings::find()->with('translations')->andWhere(['active' => 1])->all(); $settings = Settings::find()->with('translations')->andWhere(['active' => 1])->all();
//$settings = Settings::find()->andWhere(['active' => 1])->all();
//$settings_array = [];
/*foreach ($settings as $setting) {
if (!isset($settings_array[$setting->section])) {
$settings_array[$setting->section] = [];
}
//$settings_array[$setting->section][$setting->key] = $setting->translation->value;
}*/
$settings_array = $settings ? ArrayHelper::map($settings, 'key', function ($el) { $settings_array = $settings ? ArrayHelper::map($settings, 'key', function ($el) {
return $el->translation->value; return isset($el->translation->value) ? $el->translation->value : '';
}, 'section') : []; }, 'section') : [];
//$settings = ArrayHelper::map(Settings::find()->andWhere(['active' => 1])->all(), 'key', 'value', 'section');
$app->params['settings'] = $settings_array; $app->params['settings'] = $settings_array;
// Connect backend modules // Connect backend modules

4
backend/controllers/ModuleController.php

@ -185,6 +185,10 @@ class ModuleController extends Controller
$manifest = file_get_contents($link); $manifest = file_get_contents($link);
$manifest = eval(str_replace('<?php', '', $manifest)); $manifest = eval(str_replace('<?php', '', $manifest));
if (isset($manifest['enabled']) && $manifest['enabled'] == false) {
continue;
}
$languageFile = str_replace('manifest.php', 'messages/' . \Yii::$app->language . '/' . $manifest['name'] . '.php', $link); $languageFile = str_replace('manifest.php', 'messages/' . \Yii::$app->language . '/' . $manifest['name'] . '.php', $link);
try { try {
$language = file_get_contents($languageFile); $language = file_get_contents($languageFile);

1
backend/controllers/settings/ListController.php

@ -7,7 +7,6 @@
namespace backend\controllers\settings; namespace backend\controllers\settings;
use backend\forms\SettingsSearch; use backend\forms\SettingsSearch;
use core\component\favicon\FaviconGenerator;
use core\entities\Settings; use core\entities\Settings;
use core\forms\FaviconForm; use core\forms\FaviconForm;
use core\forms\SettingsForm; use core\forms\SettingsForm;

2
backend/views/settings/list/index.php

@ -64,7 +64,7 @@ $this->params['breadcrumbs'][] = $this->title;
[ [
'label' => Yii::t('main', 'Value'), 'label' => Yii::t('main', 'Value'),
'value' => function (Settings $setting) { 'value' => function (Settings $setting) {
return $setting->translation->value; return isset($setting->translation->value) ? $setting->translation->value : '- ';
}, },
'format' => 'ntext', 'format' => 'ntext',
], ],

48
console/config/main.php

@ -7,42 +7,50 @@ $params = array_merge(
); );
return [ return [
'id' => 'app-console', 'id' => 'app-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'bootstrap' => [ 'bootstrap' => [
'log', 'log',
'common\bootstrap\SetUp', 'common\bootstrap\SetUp',
'queue', 'queue',
], ],
'controllerNamespace' => 'console\controllers', 'controllerNamespace' => 'console\controllers',
'aliases' => [ 'aliases' => [
'@bower' => '@vendor/bower-asset', '@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset', '@npm' => '@vendor/npm-asset',
], ],
'controllerMap' => [ 'controllerMap' => [
'fixture' => [ 'fixture' => [
'class' => 'yii\console\controllers\FixtureController', 'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures', 'namespace' => 'common\fixtures',
], ],
'migrate' => [ 'migrate' => [
'class' => 'fishvision\migrate\controllers\MigrateController', 'class' => 'fishvision\migrate\controllers\MigrateController',
'autoDiscover' => true, 'autoDiscover' => true,
'migrationPaths' => [ 'migrationPaths' => [
'@vendor/yiisoft/yii2/rbac/migrations', '@vendor/yiisoft/yii2/rbac/migrations',
], ],
], ],
], ],
'components' => [ 'components' => [
'log' => [ 'log' => [
'targets' => [ 'targets' => [
[ [
'class' => 'yii\log\FileTarget', 'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'], 'levels' => ['error', 'warning'],
], ],
], ],
], ],
'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php', 'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php',
'frontendUrlManager' => require __DIR__ . '/../../frontend/config/urlManager.php', 'frontendUrlManager' => require __DIR__ . '/../../frontend/config/urlManager.php',
'i18n' => [
'translations' => [
'main' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '@backend/messages',
],
],
],
], ],
'params' => $params, 'params' => $params,
]; ];

71
console/controllers/SettingsController.php

@ -0,0 +1,71 @@
<?php
/**
* Created by Error202
* Date: 13.09.2018
*/
namespace console\controllers;
use core\entities\Settings;
use core\forms\SettingsForm;
use core\services\SettingsService;
use yii\console\Controller;
class SettingsController extends Controller
{
private $_settings_service;
public function __construct(string $id, $module, SettingsService $settings_service, array $config = [])
{
parent::__construct($id, $module, $config);
$this->_settings_service = $settings_service;
}
public function actionSet($language, $section, $key, $value)
{
$setting = $this->findModel($section, $key);
if ($setting) {
$translate = $setting->findTranslation($language);
if (!$translate) {
$translate = new $setting->virtualClassName();
$translate->section = $section;
$translate->key = $key;
$translate->language = $language;
}
$translate->value = $value;
$translate->save();
} else {
$settingForm = new SettingsForm();
$settingForm->type = 'string';
$settingForm->section = $section;
$settingForm->key = $key;
$settingForm->active = 1;
$setting = $this->_settings_service->create($settingForm);
$translate = $setting->findTranslation($language);
if (!$translate) {
$translate = new $setting->virtualClassName();
$translate->section = $section;
$translate->key = $key;
$translate->language = $language;
}
$translate->value = $value;
$translate->save();
}
$this->stdout('Settings updated!' . PHP_EOL);
}
protected function findModel($section, $key)
{
if (($model = Settings::find()->andWhere(['section' => $section])->andWhere(['key' => $key])->one()) !== null) {
return $model;
} else {
//throw new NotFoundException('The requested setting does not exist.');
return null;
}
}
}

3
core/behaviors/LanguageBehavior.php

@ -91,8 +91,7 @@ class LanguageBehavior extends Behavior
/** @var ActiveRecord $owner */ /** @var ActiveRecord $owner */
parent::attach($owner); parent::attach($owner);
if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) { if (empty($this->translatedLanguages) || !is_array($this->translatedLanguages)) {
throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' . get_class($this->owner) . ' or in the application parameters', throw new InvalidConfigException('Please specify array of available languages for the ' . get_class($this) . ' in the ' . get_class($this->owner) . ' or in the application parameters', 101);
101);
} }
if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array

2
core/forms/SettingsForm.php

@ -22,7 +22,6 @@ class SettingsForm extends LanguageDynamicModel
public $_settings; public $_settings;
//public function __construct(Settings $settings = null, $config = [])
public function __construct(Settings $settings = null, array $attributes = [], $config = []) public function __construct(Settings $settings = null, array $attributes = [], $config = [])
{ {
if ($settings) { if ($settings) {
@ -33,7 +32,6 @@ class SettingsForm extends LanguageDynamicModel
$this->active = $settings->active; $this->active = $settings->active;
$this->_settings = $settings; $this->_settings = $settings;
} }
//parent::__construct($config);
parent::__construct($settings, $attributes, $config); parent::__construct($settings, $attributes, $config);
} }

31
frontend/bootstrap/SetUp.php

@ -18,26 +18,29 @@ class SetUp implements BootstrapInterface
return new Breadcrumbs(ArrayHelper::merge([ return new Breadcrumbs(ArrayHelper::merge([
'homeLink' => [ 'homeLink' => [
//'label' => '<span class="fa fa-home"></span>', //'label' => '<span class="fa fa-home"></span>',
'label' => \Yii::t('post', 'Home'), 'label' => \Yii::t('post', 'Home'),
'encode' => false, 'encode' => false,
'url' => \Yii::$app->homeUrl, 'url' => \Yii::$app->homeUrl,
], ],
], $args)); ], $args));
}); });
// Load settings // Load settings
$settings = ArrayHelper::map(Settings::find()->andWhere(['active' => 1])->all(), 'key', 'value', 'section'); $settings = Settings::find()->with('translations')->andWhere(['active' => 1])->all();
$app->params['settings'] = $settings; $settings_array = $settings ? ArrayHelper::map($settings, 'key', function ($el) {
return isset($el->translation->value) ? $el->translation->value : '';
}, 'section') : [];
$app->params['settings'] = $settings_array;
// Add finish UrlRules // Add finish UrlRules
$app->getUrlManager()->addRules([ $app->getUrlManager()->addRules([
'<_c:[\w\-]+>' => '<_c>/index', '<_c:[\w\-]+>' => '<_c>/index',
'<_c:[\w\-]+>/<id:\d+>' => '<_c>/view', '<_c:[\w\-]+>/<id:\d+>' => '<_c>/view',
'<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>', '<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>',
'<_c:[\w\-]+>/<id:\d+>/<_a:[\w\-]+>' => '<_c>/<_a>', '<_c:[\w\-]+>/<id:\d+>/<_a:[\w\-]+>' => '<_c>/<_a>',
]); ]);
// redefine home url // redefine home url
\Yii::$app->homeUrl = LanguageHelper::addLangToUrl(\Yii::$app->homeUrl); \Yii::$app->homeUrl = LanguageHelper::addLangToUrl(\Yii::$app->homeUrl);
} }
} }
Loading…
Cancel
Save