Browse Source

Backend language select in profile

master
Egorka 6 years ago
parent
commit
12aab8dd1a
  1. 5
      backend/controllers/UserController.php
  2. 10
      backend/controllers/settings/ListController.php
  3. 4
      backend/messages/ru/main.php
  4. 1
      backend/messages/ru/user.php
  5. 17
      backend/views/layouts/header.php
  6. 11
      backend/views/layouts/left.php
  7. 0
      backend/views/settings/list/_form.php
  8. 0
      backend/views/settings/list/create.php
  9. 0
      backend/views/settings/list/index.php
  10. 0
      backend/views/settings/list/update.php
  11. 0
      backend/views/settings/list/view.php
  12. 4
      backend/views/user/profile.php
  13. 5
      common/bootstrap/SetUp.php
  14. 27
      console/migrations/m180822_182716_add_users_languages_fields.php
  15. 5
      core/entities/user/User.php
  16. 5
      core/forms/user/ProfileEditForm.php
  17. 2
      core/services/user/ProfileService.php

5
backend/controllers/UserController.php

@ -37,7 +37,7 @@ class UserController extends Controller
{ {
return [ return [
'access' => [ 'access' => [
'class' => AccessControl::className(), 'class' => AccessControl::class,
'rules' => [ 'rules' => [
[ [
'actions' => ['create','view','index', 'update', 'delete'], 'actions' => ['create','view','index', 'update', 'delete'],
@ -56,7 +56,7 @@ class UserController extends Controller
], ],
], ],
'verbs' => [ 'verbs' => [
'class' => VerbFilter::className(), 'class' => VerbFilter::class,
'actions' => [ 'actions' => [
'delete' => ['POST'], 'delete' => ['POST'],
], ],
@ -162,6 +162,7 @@ class UserController extends Controller
$form->user_pic = UploadedFile::getInstance($form, 'user_pic'); $form->user_pic = UploadedFile::getInstance($form, 'user_pic');
$this->profile_service->edit(Yii::$app->user->id, $form); $this->profile_service->edit(Yii::$app->user->id, $form);
Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.')); Yii::$app->session->setFlash('success', Yii::t('user', 'Profile is saved.'));
return $this->redirect(['/user/profile']);
} catch (\DomainException $e) { } catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e); Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage()); Yii::$app->session->setFlash('error', $e->getMessage());

10
backend/controllers/SettingsController.php → backend/controllers/settings/ListController.php

@ -4,7 +4,7 @@
* Date: 04.06.2018 * Date: 04.06.2018
*/ */
namespace backend\controllers; namespace backend\controllers\settings;
use backend\forms\SettingsSearch; use backend\forms\SettingsSearch;
@ -18,7 +18,7 @@ use backend\components\ToggleAction;
use Yii; use Yii;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
class SettingsController extends Controller class ListController extends Controller
{ {
private $service; private $service;
@ -31,13 +31,13 @@ class SettingsController extends Controller
{ {
return [ return [
'verbs' => [ 'verbs' => [
'class' => VerbFilter::className(), 'class' => VerbFilter::class,
'actions' => [ 'actions' => [
'delete' => ['POST'], 'delete' => ['POST'],
], ],
], ],
'access' => [ 'access' => [
'class' => AccessControl::className(), 'class' => AccessControl::class,
'rules' => [ 'rules' => [
[ [
'actions' => ['create','view','index', 'update', 'delete', 'toggle'], 'actions' => ['create','view','index', 'update', 'delete', 'toggle'],
@ -57,7 +57,7 @@ class SettingsController extends Controller
{ {
return [ return [
'toggle' => [ 'toggle' => [
'class' => ToggleAction::className(), 'class' => ToggleAction::class,
'modelClass' => Settings::class, 'modelClass' => Settings::class,
//'setFlash' => true, //'setFlash' => true,
] ]

4
backend/messages/ru/main.php

@ -35,5 +35,7 @@ return [
'Modules' => 'Модули', 'Modules' => 'Модули',
'Find modules' => 'Поиск модулей', 'Find modules' => 'Поиск модулей',
'Enable' => 'Включить', 'Enable' => 'Включить',
'Disable' => 'Отключить' 'Disable' => 'Отключить',
'Settings List' => 'Все настройки',
'Create Setting' => 'Новый параметр',
]; ];

1
backend/messages/ru/user.php

@ -38,4 +38,5 @@ return [
'User Picture' => 'Аватар', 'User Picture' => 'Аватар',
'Registered: {date}' => 'Дата регистрации: {date}', 'Registered: {date}' => 'Дата регистрации: {date}',
'Remember Me' => 'Запомнить меня', 'Remember Me' => 'Запомнить меня',
'Dashboard Language' => 'Язык панели управления',
]; ];

17
backend/views/layouts/header.php

@ -193,6 +193,23 @@ use core\components\avatar_generator\AvatarGenerator;
</li> </li>
--> -->
<!-- Language Drop Down -->
<li class="dropdown messages-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- <i class="fa fa-language"></i> -->
<span>Русский <i class="caret"></i></span>
</a>
<ul class="dropdown-menu">
<li>
<!-- inner menu: contains the actual data -->
<ul class="menu">
<li><a href="#">Русский</a></li>
<li><a href="#">English</a></li>
</ul>
</li>
</ul>
</li>
<li class="dropdown user user-menu"> <li class="dropdown user user-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<img src="<?= Yii::$app->avatar->show(Yii::$app->user->identity->user->username) ?>" class="user-image" alt="<?= Yii::$app->user->identity->user->username ?>"/> <img src="<?= Yii::$app->avatar->show(Yii::$app->user->identity->user->username) ?>" class="user-image" alt="<?= Yii::$app->user->identity->user->username ?>"/>

11
backend/views/layouts/left.php

@ -114,12 +114,11 @@ $model = new SearchForm();
'visible' => \Yii::$app->user->can('admin') || \Yii::$app->user->can('SliderManagement'), 'visible' => \Yii::$app->user->can('admin') || \Yii::$app->user->can('SliderManagement'),
], ],
[ ['label' => Yii::t('main', 'Settings'), 'icon' => 'cog', 'items' => [
'label' => Yii::t('main', 'Settings'), ['label' => Yii::t('main', 'Settings List'), 'icon' => 'caret-right', 'url' => ['/settings/list/index'], 'active' => \Yii::$app->controller->getUniqueId() == 'settings/list'],
'icon' => 'cog', 'url' => ['/settings/index'], //['label' => Yii::t('main', 'Find modules'), 'icon' => 'caret-right', 'url' => ['/module/search'], 'active' => \Yii::$app->controller->action->getUniqueId() == 'module/search'],
'active' => $this->context->id == 'settings', ], 'visible' => \Yii::$app->user->can('admin') || \Yii::$app->user->can('SettingsManagement')],
'visible' => \Yii::$app->user->can('admin') || \Yii::$app->user->can('SettingsManagement'),
],
], Yii::$app->params['adminMenu']), ], Yii::$app->params['adminMenu']),
] ]
) ?> ) ?>

0
backend/views/settings/_form.php → backend/views/settings/list/_form.php

0
backend/views/settings/create.php → backend/views/settings/list/create.php

0
backend/views/settings/index.php → backend/views/settings/list/index.php

0
backend/views/settings/update.php → backend/views/settings/list/update.php

0
backend/views/settings/view.php → backend/views/settings/list/view.php

4
backend/views/user/profile.php

@ -43,6 +43,10 @@ $this->params['breadcrumbs'][] = Yii::t('user', 'Profile');
<?= $form->field($model, 'username')->textInput(['maxLength' => true]) ?> <?= $form->field($model, 'username')->textInput(['maxLength' => true]) ?>
<?= $form->field($model, 'email')->textInput(['maxLength' => true]) ?> <?= $form->field($model, 'email')->textInput(['maxLength' => true]) ?>
<?= $form->field($model, 'password')->passwordInput(['maxLength' => true]) ?> <?= $form->field($model, 'password')->passwordInput(['maxLength' => true]) ?>
<?= $form->field($model, 'backend_language')->dropDownList([
'ru' => 'Русский',
'en' => 'English',
]) ?>
</div> </div>
</div> </div>
</div> </div>

5
common/bootstrap/SetUp.php

@ -136,6 +136,11 @@ class SetUp implements BootstrapInterface
$container->set(ImageUploadBehavior::class, FlySystemImageUploadBehavior::class); $container->set(ImageUploadBehavior::class, FlySystemImageUploadBehavior::class);
*/ */
// Set backend language
if (basename($app->getBasePath()) === 'backend') {
$app->language = ! $app->user->isGuest && $app->user->identity->user->backend_language ? $app->user->identity->user->backend_language : $app->language;
}
// Connect common modules // Connect common modules
if (Yii::$app->db->schema->getTableSchema('{{%modules}}') !== null) { if (Yii::$app->db->schema->getTableSchema('{{%modules}}') !== null) {
$modules = ModuleRecord::find()->andWhere(['type' => 'common'])->andWhere(['active' => 1])->all(); $modules = ModuleRecord::find()->andWhere(['type' => 'common'])->andWhere(['active' => 1])->all();

27
console/migrations/m180822_182716_add_users_languages_fields.php

@ -0,0 +1,27 @@
<?php
use yii\db\Migration;
/**
* Class m180822_182716_add_users_languages_fields
*/
class m180822_182716_add_users_languages_fields extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%users}}', 'backend_language', $this->string(10));
$this->addColumn('{{%users}}', 'frontend_language', $this->string(10));
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%users}}', 'backend_language');
$this->dropColumn('{{%users}}', 'frontend_language');
}
}

5
core/entities/user/User.php

@ -27,6 +27,8 @@ use zertex\avatar_generator\AvatarGenerator;
* @property integer $created_at * @property integer $created_at
* @property integer $updated_at * @property integer $updated_at
* @property string $user_pic * @property string $user_pic
* @property string $backend_language
* @property string $frontend_language
* @property string $password write-only password * @property string $password write-only password
* *
* @property Network[] $networks * @property Network[] $networks
@ -83,7 +85,7 @@ class User extends ActiveRecord implements AggregateRoot
} }
} }
public function editProfile(string $email, string $username, string $password = null, $user_pic = null): void public function editProfile(string $email, string $username, string $password = null, $user_pic = null, $language): void
{ {
$this->email = $email; $this->email = $email;
$this->username = $username; $this->username = $username;
@ -91,6 +93,7 @@ class User extends ActiveRecord implements AggregateRoot
$this->setPassword( $password ); $this->setPassword( $password );
} }
$this->updated_at = time(); $this->updated_at = time();
$this->backend_language = $language;
/* @var $user_pic \yii\web\UploadedFile */ /* @var $user_pic \yii\web\UploadedFile */
if ($user_pic) if ($user_pic)

5
core/forms/user/ProfileEditForm.php

@ -17,6 +17,7 @@ class ProfileEditForm extends Model
public $username; public $username;
public $password; public $password;
public $user_pic; public $user_pic;
public $backend_language;
public $_user; public $_user;
@ -26,6 +27,7 @@ class ProfileEditForm extends Model
$this->username = $user->username; $this->username = $user->username;
$this->_user = $user; $this->_user = $user;
$this->user_pic = $user->user_pic; $this->user_pic = $user->user_pic;
$this->backend_language = $user->backend_language ?: Yii::$app->language;
parent::__construct($config); parent::__construct($config);
} }
@ -35,7 +37,7 @@ class ProfileEditForm extends Model
[['email', 'username'], 'required'], [['email', 'username'], 'required'],
[['email', 'username'], 'trim'], [['email', 'username'], 'trim'],
['email', 'email'], ['email', 'email'],
[['email', 'password'], 'string', 'max' => 255], [['email', 'password', 'backend_language'], 'string', 'max' => 255],
[['username', 'email'], 'unique', 'targetClass' => User::class, 'filter' => ['<>', 'id', $this->_user->id]], [['username', 'email'], 'unique', 'targetClass' => User::class, 'filter' => ['<>', 'id', $this->_user->id]],
['username', 'string', 'min' => 2, 'max' => 255], ['username', 'string', 'min' => 2, 'max' => 255],
['user_pic', 'image', 'extensions' => 'png, jpg, jpeg, gif', ['user_pic', 'image', 'extensions' => 'png, jpg, jpeg, gif',
@ -53,6 +55,7 @@ class ProfileEditForm extends Model
'username' => Yii::t('user', 'Username'), 'username' => Yii::t('user', 'Username'),
'password' => Yii::t('user', 'Password'), 'password' => Yii::t('user', 'Password'),
'user_pic' => Yii::t('user', 'User Picture'), 'user_pic' => Yii::t('user', 'User Picture'),
'backend_language' => Yii::t('user', 'Dashboard Language'),
]; ];
} }
} }

2
core/services/user/ProfileService.php

@ -21,7 +21,7 @@ class ProfileService
public function edit($id, ProfileEditForm $form): void public function edit($id, ProfileEditForm $form): void
{ {
$user = $this->users->get($id); $user = $this->users->get($id);
$user->editProfile($form->email, $form->username, $form->password, $form->user_pic); $user->editProfile($form->email, $form->username, $form->password, $form->user_pic, $form->backend_language);
$this->users->save($user); $this->users->save($user);
} }
} }
Loading…
Cancel
Save