From 7e1973724391ade5615c1c56072d4fccd13e4e55 Mon Sep 17 00:00:00 2001 From: Egorka Date: Thu, 13 Sep 2018 21:30:45 +0300 Subject: [PATCH] Setting bootstrap load Console set setting controller --- backend/bootstrap/SetUp.php | 13 +---- backend/controllers/ModuleController.php | 4 ++ backend/controllers/settings/ListController.php | 1 - backend/views/settings/list/index.php | 2 +- common/bootstrap/SetUp.php | 2 +- console/config/main.php | 48 ++++++++++------- console/controllers/SettingsController.php | 71 +++++++++++++++++++++++++ core/behaviors/LanguageBehavior.php | 3 +- core/forms/SettingsForm.php | 2 - frontend/bootstrap/SetUp.php | 33 ++++++------ 10 files changed, 125 insertions(+), 54 deletions(-) create mode 100644 console/controllers/SettingsController.php diff --git a/backend/bootstrap/SetUp.php b/backend/bootstrap/SetUp.php index 0efe8a8..b2c0d36 100644 --- a/backend/bootstrap/SetUp.php +++ b/backend/bootstrap/SetUp.php @@ -45,20 +45,9 @@ class SetUp implements BootstrapInterface // load settings $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) { - return $el->translation->value; + return isset($el->translation->value) ? $el->translation->value : ''; }, 'section') : []; - - //$settings = ArrayHelper::map(Settings::find()->andWhere(['active' => 1])->all(), 'key', 'value', 'section'); $app->params['settings'] = $settings_array; // Connect backend modules diff --git a/backend/controllers/ModuleController.php b/backend/controllers/ModuleController.php index 7368d01..c5146f2 100644 --- a/backend/controllers/ModuleController.php +++ b/backend/controllers/ModuleController.php @@ -185,6 +185,10 @@ class ModuleController extends Controller $manifest = file_get_contents($link); $manifest = eval(str_replace('language . '/' . $manifest['name'] . '.php', $link); try { $language = file_get_contents($languageFile); diff --git a/backend/controllers/settings/ListController.php b/backend/controllers/settings/ListController.php index 62792e4..c3b2ef3 100644 --- a/backend/controllers/settings/ListController.php +++ b/backend/controllers/settings/ListController.php @@ -7,7 +7,6 @@ namespace backend\controllers\settings; use backend\forms\SettingsSearch; -use core\component\favicon\FaviconGenerator; use core\entities\Settings; use core\forms\FaviconForm; use core\forms\SettingsForm; diff --git a/backend/views/settings/list/index.php b/backend/views/settings/list/index.php index 1427eb0..a46cfda 100644 --- a/backend/views/settings/list/index.php +++ b/backend/views/settings/list/index.php @@ -64,7 +64,7 @@ $this->params['breadcrumbs'][] = $this->title; [ 'label' => Yii::t('main', 'Value'), 'value' => function (Settings $setting) { - return $setting->translation->value; + return isset($setting->translation->value) ? $setting->translation->value : '- '; }, 'format' => 'ntext', ], diff --git a/common/bootstrap/SetUp.php b/common/bootstrap/SetUp.php index d99e584..bda4f66 100644 --- a/common/bootstrap/SetUp.php +++ b/common/bootstrap/SetUp.php @@ -185,4 +185,4 @@ class SetUp implements BootstrapInterface Yii::$app->getModule($module['name'])->bootstrap(Yii::$app); } } -} \ No newline at end of file +} diff --git a/console/config/main.php b/console/config/main.php index 53733db..d29f467 100644 --- a/console/config/main.php +++ b/console/config/main.php @@ -7,42 +7,50 @@ $params = array_merge( ); return [ - 'id' => 'app-console', - 'basePath' => dirname(__DIR__), - 'bootstrap' => [ - 'log', - 'common\bootstrap\SetUp', - 'queue', + 'id' => 'app-console', + 'basePath' => dirname(__DIR__), + 'bootstrap' => [ + 'log', + 'common\bootstrap\SetUp', + 'queue', ], 'controllerNamespace' => 'console\controllers', - 'aliases' => [ + 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', ], - 'controllerMap' => [ + 'controllerMap' => [ 'fixture' => [ - 'class' => 'yii\console\controllers\FixtureController', + 'class' => 'yii\console\controllers\FixtureController', 'namespace' => 'common\fixtures', - ], + ], 'migrate' => [ - 'class' => 'fishvision\migrate\controllers\MigrateController', - 'autoDiscover' => true, - 'migrationPaths' => [ - '@vendor/yiisoft/yii2/rbac/migrations', - ], + 'class' => 'fishvision\migrate\controllers\MigrateController', + 'autoDiscover' => true, + 'migrationPaths' => [ + '@vendor/yiisoft/yii2/rbac/migrations', + ], ], ], - 'components' => [ - 'log' => [ + 'components' => [ + 'log' => [ 'targets' => [ [ - 'class' => 'yii\log\FileTarget', + 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], - 'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php', + 'backendUrlManager' => require __DIR__ . '/../../backend/config/urlManager.php', 'frontendUrlManager' => require __DIR__ . '/../../frontend/config/urlManager.php', + 'i18n' => [ + 'translations' => [ + 'main' => [ + 'class' => 'yii\i18n\PhpMessageSource', + 'basePath' => '@backend/messages', + ], + ], + ], ], - 'params' => $params, + 'params' => $params, ]; diff --git a/console/controllers/SettingsController.php b/console/controllers/SettingsController.php new file mode 100644 index 0000000..e56c47a --- /dev/null +++ b/console/controllers/SettingsController.php @@ -0,0 +1,71 @@ +_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; + } + } +} diff --git a/core/behaviors/LanguageBehavior.php b/core/behaviors/LanguageBehavior.php index b742c25..d871bdf 100644 --- a/core/behaviors/LanguageBehavior.php +++ b/core/behaviors/LanguageBehavior.php @@ -91,8 +91,7 @@ class LanguageBehavior extends Behavior /** @var ActiveRecord $owner */ parent::attach($owner); 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', - 101); + 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); } if (array_values($this->translatedLanguages) !== $this->translatedLanguages) { //associative array diff --git a/core/forms/SettingsForm.php b/core/forms/SettingsForm.php index 4dd476c..6cef994 100644 --- a/core/forms/SettingsForm.php +++ b/core/forms/SettingsForm.php @@ -22,7 +22,6 @@ class SettingsForm extends LanguageDynamicModel public $_settings; - //public function __construct(Settings $settings = null, $config = []) public function __construct(Settings $settings = null, array $attributes = [], $config = []) { if ($settings) { @@ -33,7 +32,6 @@ class SettingsForm extends LanguageDynamicModel $this->active = $settings->active; $this->_settings = $settings; } - //parent::__construct($config); parent::__construct($settings, $attributes, $config); } diff --git a/frontend/bootstrap/SetUp.php b/frontend/bootstrap/SetUp.php index f41ed32..55a8ad7 100644 --- a/frontend/bootstrap/SetUp.php +++ b/frontend/bootstrap/SetUp.php @@ -18,26 +18,29 @@ class SetUp implements BootstrapInterface return new Breadcrumbs(ArrayHelper::merge([ 'homeLink' => [ //'label' => '', - 'label' => \Yii::t('post', 'Home'), + 'label' => \Yii::t('post', 'Home'), 'encode' => false, - 'url' => \Yii::$app->homeUrl, + 'url' => \Yii::$app->homeUrl, ], ], $args)); }); - // Load settings - $settings = ArrayHelper::map(Settings::find()->andWhere(['active' => 1])->all(), 'key', 'value', 'section'); - $app->params['settings'] = $settings; + // Load settings + $settings = Settings::find()->with('translations')->andWhere(['active' => 1])->all(); + $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 - $app->getUrlManager()->addRules([ - '<_c:[\w\-]+>' => '<_c>/index', - '<_c:[\w\-]+>/' => '<_c>/view', - '<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>', - '<_c:[\w\-]+>//<_a:[\w\-]+>' => '<_c>/<_a>', - ]); + // Add finish UrlRules + $app->getUrlManager()->addRules([ + '<_c:[\w\-]+>' => '<_c>/index', + '<_c:[\w\-]+>/' => '<_c>/view', + '<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>', + '<_c:[\w\-]+>//<_a:[\w\-]+>' => '<_c>/<_a>', + ]); - // redefine home url - \Yii::$app->homeUrl = LanguageHelper::addLangToUrl(\Yii::$app->homeUrl); + // redefine home url + \Yii::$app->homeUrl = LanguageHelper::addLangToUrl(\Yii::$app->homeUrl); } -} \ No newline at end of file +}