Commit 6310a439a5af4c90b6345d80e8860b432b1198ad
1 parent
f1f35982
firs page
Showing
3 changed files
with
136 additions
and
1 deletions
Show diff stats
| 1 | +<?php | |
| 2 | +namespace frontend\controllers; | |
| 3 | + | |
| 4 | +use Yii; | |
| 5 | +use common\models\Accounts; | |
| 6 | +use frontend\models\ChangePasswordForm; | |
| 7 | +use common\models\News; | |
| 8 | +use yii\data\ActiveDataProvider; | |
| 9 | +use yii\filters\AccessControl; | |
| 10 | +use yii\web\Controller; | |
| 11 | +use yii\web\NotFoundHttpException; | |
| 12 | + | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Site controller | |
| 16 | + */ | |
| 17 | +class AccountsController extends Controller | |
| 18 | +{ | |
| 19 | + | |
| 20 | + public $layout = '/internal'; | |
| 21 | + | |
| 22 | + | |
| 23 | + public function behaviors() | |
| 24 | + { | |
| 25 | + return [ | |
| 26 | + 'access' => [ | |
| 27 | + 'class' => AccessControl::className(), | |
| 28 | + 'rules' => [ | |
| 29 | + [ | |
| 30 | + 'actions' => ['cabinet','change-password'], | |
| 31 | + 'allow' => true, | |
| 32 | + 'roles' => ['@'], | |
| 33 | + ], | |
| 34 | + ], | |
| 35 | + ], | |
| 36 | + ]; | |
| 37 | + } | |
| 38 | + | |
| 39 | + | |
| 40 | + public function actionCabinet() | |
| 41 | + { | |
| 42 | + | |
| 43 | + | |
| 44 | + $model = $this->findModel(Yii::$app->user->identity->id); | |
| 45 | + | |
| 46 | + | |
| 47 | + if ($model->load(Yii::$app->request->post()) && $model->validate()) { | |
| 48 | + | |
| 49 | + $model->save(); | |
| 50 | + | |
| 51 | + } | |
| 52 | + | |
| 53 | + return $this->render('cabinet', [ | |
| 54 | + 'model' => $model | |
| 55 | + ]); | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + } | |
| 60 | + | |
| 61 | + public function actionIndex() | |
| 62 | + { | |
| 63 | + $dataProvider = new ActiveDataProvider([ | |
| 64 | + 'query' => News::find()->where(['is_active'=>1]), | |
| 65 | + 'pagination' => [ | |
| 66 | + 'pageSize' => 16, | |
| 67 | + ], | |
| 68 | + | |
| 69 | + ]); | |
| 70 | + return $this->render('index',[ | |
| 71 | + 'dataProvider' => $dataProvider | |
| 72 | + ]); | |
| 73 | + } | |
| 74 | + | |
| 75 | + public function actionView($translit) | |
| 76 | + { | |
| 77 | + | |
| 78 | + $activeNews = $this->findModel($translit); | |
| 79 | + $next_news = News::find()->where(['is_active'=>1])->andWhere('id > :id',[':id' => $activeNews->id])->one(); | |
| 80 | + $prev_news = News::find()->where(['is_active'=>1])->andWhere('id < :id',[':id' => $activeNews->id])->one(); | |
| 81 | + | |
| 82 | + return $this->render('view', [ | |
| 83 | + 'model' => $activeNews, | |
| 84 | + 'next_news' => $next_news instanceof News ? $next_news : '', | |
| 85 | + 'prev_news' => $prev_news instanceof News ? $prev_news : '' | |
| 86 | + ]); | |
| 87 | + } | |
| 88 | + | |
| 89 | + protected function findModel($id) | |
| 90 | + { | |
| 91 | + | |
| 92 | + if (($model = Accounts::findOne(["id"=>$id])) !== null) { | |
| 93 | + return $model; | |
| 94 | + } else { | |
| 95 | + throw new NotFoundHttpException('The requested page does not exist.'); | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + | |
| 100 | + public function actionChangePassword(){ | |
| 101 | + | |
| 102 | + | |
| 103 | + $form = new ChangePasswordForm(); | |
| 104 | + | |
| 105 | + | |
| 106 | + if ($form->load(Yii::$app->request->post()) && $form->validate()) { | |
| 107 | + | |
| 108 | + $model = Accounts::findOne(Yii::$app->user->identity->id); | |
| 109 | + | |
| 110 | + $model->load(Yii::$app->request->post(), 'ChangePasswordForm'); | |
| 111 | + | |
| 112 | + $model->save(); | |
| 113 | + | |
| 114 | + return $this->redirect(['cabinet']); | |
| 115 | + | |
| 116 | + } else { | |
| 117 | + | |
| 118 | + return $this->render('change-password', [ | |
| 119 | + 'model' => $form | |
| 120 | + ]); | |
| 121 | + | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | +} | ... | ... |
frontend/views/layouts/main.php
| ... | ... | @@ -56,7 +56,7 @@ AppAsset::register($this); |
| 56 | 56 | <?php else : ?> |
| 57 | 57 | <div class="header-cabinet-foto"><img src="/images/ded-ico.png" alt=""/></div> |
| 58 | 58 | <div class="header-cabinet-profile">Профиль</div> |
| 59 | - <?= Html::a(Yii::$app->user->identity->email,['/cabinet/index']) ?> | |
| 59 | + <?= Html::a(Yii::$app->user->identity->email,['accounts/cabinet']) ?> | |
| 60 | 60 | <?php endif; ?> |
| 61 | 61 | |
| 62 | 62 | </div> | ... | ... |