/
k6011565
/
UP09
Обзор
Документация
Войти
/
k6011565
/
UP09
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
basic/controllers/ReviewController.php
88 строк
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\Review; use app\models\Tour; use app\models\Booking; class ReviewController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], // Только авторизованные ], ], ], ]; } public function actionCreate($tour_id) { $tour = Tour::findOne($tour_id); if (!$tour) { throw new \yii\web\NotFoundHttpException('Тур не найден.'); } $hasBooked = Booking::find() ->where([ 'user_id' => Yii::$app->user->id, 'tour_id' => $tour_id, 'status' => 'confirmed' ]) ->exists(); if (!$hasBooked) { Yii::$app->session->setFlash('error', 'Вы можете оставить отзыв только после подтвержденного бронирования.'); return $this->redirect(['tour/view', 'id' => $tour_id]); } $hasReviewed = Review::find() ->where([ 'user_id' => Yii::$app->user->id, 'tour_id' => $tour_id ]) ->exists(); if ($hasReviewed) { Yii::$app->session->setFlash('error', 'Вы уже оставили отзыв на этот тур.'); return $this->redirect(['tour/view', 'id' => $tour_id]); } $model = new Review(); $model->user_id = Yii::$app->user->id; $model->tour_id = $tour_id; if ($model->load(Yii::$app->request->post())) { if (empty($model->rating) || $model->rating < 1 || $model->rating > 5) { Yii::$app->session->setFlash('error', 'Пожалуйста, поставьте оценку от 1 до 5.'); return $this->render('create', [ 'model' => $model, 'tour' => $tour, ]); } if ($model->save()) { Yii::$app->session->setFlash('success', 'Спасибо за ваш отзыв!'); return $this->redirect(['tour/view', 'id' => $tour_id]); } else { $errors = $model->getFirstErrors(); Yii::$app->session->setFlash('error', 'Ошибка при сохранении отзыва: ' . implode(', ', $errors)); } } return $this->render('create', [ 'model' => $model, 'tour' => $tour, ]); } }