/
k6011565
/
UP09
Обзор
Документация
Войти
/
k6011565
/
UP09
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
basic/controllers/BookingController.php
81 строка
2 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\Booking; use app\models\Tour; class BookingController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ], ], ], ]; } public function actionIndex() { $bookings = Booking::find() ->where(['user_id' => Yii::$app->user->id]) ->with('tour') ->orderBy(['created_at' => SORT_DESC]) ->all(); return $this->render('index', ['bookings' => $bookings]); } public function actionCreate($tour_id) { $tour = Tour::findOne($tour_id); if (!$tour) { throw new \yii\web\NotFoundHttpException('Тур не найден.'); } $model = new Booking(); $model->tour_id = $tour_id; $model->user_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post())) { // Проверяем, что количество человек указано if (empty($model->people_count) || $model->people_count < 1) { Yii::$app->session->setFlash('error', 'Укажите количество человек.'); return $this->render('create', [ 'model' => $model, 'tour' => $tour, ]); } $model->total_price = $tour->price * $model->people_count; $model->status = 'confirmed'; $model->booking_date = date('Y-m-d H:i:s'); if ($model->save()) { Yii::$app->session->setFlash('success', 'Бронирование успешно подтверждено!'); return $this->redirect(['index']); } else { $errors = $model->getFirstErrors(); Yii::$app->session->setFlash('error', 'Ошибка бронирования: ' . implode(', ', $errors)); } } return $this->render('create', [ 'model' => $model, 'tour' => $tour, ]); } }