fitness-web/backend/controllers/EventController.php
2021-09-27 20:43:58 +02:00

152 lines
4.2 KiB
PHP

<?php
namespace backend\controllers;
use common\manager\EventRegistrationManager;
use common\models\CardEventRegistrationForm;
use Yii;
use common\models\Event;
use backend\models\EventSearch;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* EventController implements the CRUD actions for Event model.
*/
class EventController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Event models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new EventSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Event model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Event model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Event();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Event model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Event model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException
* @throws \yii\db\StaleObjectException
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionRegisterCard($id){
$model = new CardEventRegistrationForm();
$model->event_id = $id;
if ($model->load(Yii::$app->request->post()) && $model->validate() ) {
$manager = new EventRegistrationManager();
try {
$manager->registerCard($model);
} catch (HttpException $e) {
if ( array_key_exists($e->getCode(),EventRegistrationManager::$STATES)) {
$model->addError("id_event", Yii::t('event-registration', EventRegistrationManager::$STATES[$e->getCode()]));
} else {
$model->addError("id_event", Yii::t('event-registration', "Unknown Error"));
}
}
}
if ( $model->hasErrors() ){
return $this->redirect(['view', 'id' => $model->registration->id]);
} else {
return $this->render('register_card', [
'model' => $model,
]);
}
}
/**
* Finds the Event model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Event the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Event::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}