/
k6011565
/
UP09
Обзор
Документация
Войти
/
k6011565
/
UP09
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
basic/controllers/ProfileController.php
119 строк
3 KB
Карина Коковкина
все файлы
24 июн 2026, 15:42
24 июн 2026, 15:42
2a8b11b
Код
Авторство
О чём код?
<?php namespace app\controllers; use Yii; use yii\web\Controller; use yii\filters\AccessControl; use app\models\User; use app\models\Booking; use app\models\Review; class ProfileController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ], ], ], ]; } public function actionIndex() { $user = Yii::$app->user->identity; // Статистика $totalBookings = Booking::find() ->where(['user_id' => $user->id]) ->count(); $confirmedBookings = Booking::find() ->where(['user_id' => $user->id, 'status' => 'confirmed']) ->count(); $totalReviews = Review::find() ->where(['user_id' => $user->id]) ->count(); $recentBookings = Booking::find() ->where(['user_id' => $user->id]) ->with('tour') ->orderBy(['created_at' => SORT_DESC]) ->limit(5) ->all(); return $this->render('index', [ 'user' => $user, 'totalBookings' => $totalBookings, 'confirmedBookings' => $confirmedBookings, 'totalReviews' => $totalReviews, 'recentBookings' => $recentBookings, ]); } public function actionEdit() { $user = Yii::$app->user->identity; if ($user->load(Yii::$app->request->post())) { $existingUser = User::find() ->where(['or', ['email' => $user->email], ['username' => $user->username] ]) ->andWhere(['!=', 'id', $user->id]) ->one(); if ($existingUser) { if ($existingUser->email == $user->email) { Yii::$app->session->setFlash('error', 'Этот email уже используется другим пользователем.'); } else { Yii::$app->session->setFlash('error', 'Это имя пользователя уже занято.'); } return $this->render('edit', ['user' => $user]); } if ($user->save()) { Yii::$app->session->setFlash('success', 'Профиль успешно обновлен!'); return $this->redirect(['index']); } } return $this->render('edit', ['user' => $user]); } public function actionBookings() { $user = Yii::$app->user->identity; $bookings = Booking::find() ->where(['user_id' => $user->id]) ->with('tour') ->orderBy(['created_at' => SORT_DESC]) ->all(); return $this->render('bookings', ['bookings' => $bookings]); } public function actionReviews() { $user = Yii::$app->user->identity; $reviews = Review::find() ->where(['user_id' => $user->id]) ->with('tour') ->orderBy(['created_at' => SORT_DESC]) ->all(); return $this->render('reviews', ['reviews' => $reviews]); } }