/
peyli
/
Practice
Обзор
Документация
Войти
/
peyli
/
Practice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
controllers/EventController.php
319 строк
8 KB
Катя
Полный код
27 июн 2026, 14:28
27 июн 2026, 14:28
6181a72
Код
Авторство
О чём код?
<?php namespace app\controllers; use app\models\Cancel; use app\models\Event; use app\models\EventSearch; use app\models\Participant; use app\models\Type; use Yii; use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; /** * EventController implements the CRUD actions for Event model. */ class EventController extends Controller { /** * @inheritDoc */ public function behaviors() { return array_merge( parent::behaviors(), [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ] ); } /** * Lists all Event models. * * @return string */ public function actionIndex() { $events = Event::find() ->OrderBy(['created_at' => SORT_DESC])->all(); $searchModel = new EventSearch(); $dataProvider = $searchModel->search($this->request->queryParams); return $this->render('index', [ 'events' => $events, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Displays a single Event model. * @param int $id ID * @return string */ /** * Displays a single Event model. * @param int $id ID * @return string * @throws NotFoundHttpException if the model cannot be found */ public function actionView($id) { $event = Event::findOne($id); if (!$event) { throw new NotFoundHttpException('Мероприятие не найдено'); } // Все участники этого мероприятия $participants = Participant::find() ->where(['event_id' => $event]) ->all(); return $this->render('view', [ 'event' => $event, 'participants' => $participants, 'model' => $this->findModel($id), ]); } public function actionJoin($id) { // Перенаправляет пользователя на окно авторизации, если он не авторизован if (Yii::$app->user->isGuest) { return $this->redirect(['site/login']); } $event = Event::findOne($id); if (!$event) { throw new NotFoundHttpException('Мероприятие не найдено'); } return $this->render('join', [ 'event' => $event, 'model' => $this->findModel($id), ]); } public function actionSuccess($id) { $model = Event::findOne($id); $event = $model; if ((Yii::$app->user->identity->username != $model->user_username)) { // Проверка участвует ли уже такой пользователь в этом мероприятии $all_participant = Participant::find() ->where(['user_username' => Yii::$app->user->identity->username]) ->andWhere(['event_id' => $model->id]) ->one(); if (($all_participant)){ return $this->render('fail', [ 'model' => $model, 'event' => $event, ]); } // Пользователь добавляется в БД как участник мероприятия $participant = new Participant(); $participant->user_username = Yii::$app->user->identity->username; $participant->event_id = $model->id; $participant->role = 'Участник'; $participant->save(); return $this->render('success', [ 'model' => $model, 'event' => $event, ]); } else { return $this->render('fail', ['model' => $model, 'event' => $event,]); } // return $this->render('success', [ // 'model' => $model, // 'event' => $event, // ]); } public function actionFail() { return $this->render('fail', [ ]); } /** * Creates a new Event model. * If creation is successful, the browser will be redirected to the 'view' page. * @return string|\yii\web\Response */ public function actionCreate() { // Перенаправляет пользователя на окно авторизации, если он не авторизован if (Yii::$app->user->isGuest) { return $this->redirect(['site/login']); } $model = new Event(); $eventTypes = ArrayHelper::map( Type::find()->all(), 'id', 'name' ); if ($this->request->isPost) { if ($model->load($this->request->post()) && $model->save()) { // Запись организатора только что созданного мероприятия в таблицу participant $participant = new Participant(); $participant->user_username = $model->user_username; $participant->event_id = $model->id; $participant->role = 'Организатор'; $participant->save(); return $this->redirect(['view', 'id' => $model->id]); } } else { $model->loadDefaultValues(); } return $this->render('create', [ 'model' => $model, 'eventTypes' => $eventTypes, ]); } public function actionUpdate($id) { if (Yii::$app->user->isGuest) { return $this->goHome(); } $eventTypes = ArrayHelper::map( Type::find()->all(), 'id', 'name' ); $model = $this->findModel($id); if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } return $this->render('update', [ 'model' => $model, 'eventTypes' => $eventTypes, ]); } public function actionDeleteEvent($id) { $model = $this->findModel($id); $DeleteCancel = new Cancel(); $DeleteCancel->event_id = $model->id; $DeleteCancel->event_title = $model->title; $DeleteCancel->user_username = $model->user_username; $DeleteCancel->reason = $model->cancel_reason; $DeleteCancel->save(); $model->load($this->request->post()); $model->save(); return $this->render('deleteevent', ['model' => $model] ); } /** * @return string|\yii\web\Response */ public function actionDelete($id) { $model = $this->findModel($id); if ($model->save()) { // Удаление всех участников мероприятия $allParticipant = Participant::find() ->where(['event_id' => $this->id]) ->all(); foreach ($allParticipant as $part) { $part->delete(); } // Удаление мероприятия $model->delete(); } return $this->redirect(['index']); } // Отменить участие в мероприятии public function actionCancel($id) { $model = Event::findOne($id); $deletePart = Participant::find() ->where(['user_username' => Yii::$app->user->identity->username]) ->andWhere(['event_id' => $model->id]) ->one(); $deletePart->delete(); return $this->redirect(['view', 'id' => $model->id]); } /** * Finds the Event model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param int $id ID * @return Event the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Event::findOne(['id' => $id])) !== null) { return $model; } throw new NotFoundHttpException(\Yii::t('app', 'Запрашиваемая страница не существует.')); } }