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
$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

4
backend/controllers/ModuleController.php

@ -185,6 +185,10 @@ class ModuleController extends Controller
$manifest = file_get_contents($link);
$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);
try {
$language = file_get_contents($languageFile);

1
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;

2
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',
],

48
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,
];

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 */
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

2
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);
}

31
frontend/bootstrap/SetUp.php

@ -18,26 +18,29 @@ class SetUp implements BootstrapInterface
return new Breadcrumbs(ArrayHelper::merge([
'homeLink' => [
//'label' => '<span class="fa fa-home"></span>',
'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\-]+>/<id:\d+>' => '<_c>/view',
'<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>',
'<_c:[\w\-]+>/<id:\d+>/<_a:[\w\-]+>' => '<_c>/<_a>',
]);
// Add finish UrlRules
$app->getUrlManager()->addRules([
'<_c:[\w\-]+>' => '<_c>/index',
'<_c:[\w\-]+>/<id:\d+>' => '<_c>/view',
'<_c:[\w\-]+>/<_a:[\w-]+>' => '<_c>/<_a>',
'<_c:[\w\-]+>/<id:\d+>/<_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);
}
}
Loading…
Cancel
Save