diff --git a/common/models/Event.php b/common/models/Event.php index 65e6e11..6ba4829 100644 --- a/common/models/Event.php +++ b/common/models/Event.php @@ -125,6 +125,13 @@ class Event extends \yii\db\ActiveRecord return $this->hasOne(Room::className(),['id' => 'id_room']); } + /** + * @return Card|\yii\db\ActiveQuery + */ + public function getCard() { + return $this->hasOne(Card::className(),['id_card' => 'id_card']); + } + /** * @return \common\models\EventRegistration[]|\yii\db\ActiveQuery */ diff --git a/common/modules/event/EventModule.php b/common/modules/event/EventModule.php new file mode 100644 index 0000000..02a437d --- /dev/null +++ b/common/modules/event/EventModule.php @@ -0,0 +1,16 @@ +render('index'); + } +} diff --git a/common/modules/event/controllers/EventController.php b/common/modules/event/controllers/EventController.php new file mode 100644 index 0000000..2a66c29 --- /dev/null +++ b/common/modules/event/controllers/EventController.php @@ -0,0 +1,251 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + + $module = EventModule::getInstance(); + $allowedActions = [ 'index','view', 'reserve-card','cancel-registration','delete-registration' ]; + if ( $module->mode == 'admin' ){ + $allowedActions[] = 'create'; + $allowedActions[] = 'update'; + $allowedActions[] = 'delete'; + } + $behaviors['access'] = [ + 'class' => \yii\filters\AccessControl::className(), + 'rules' => [ + // allow authenticated users + [ + 'actions' =>$allowedActions, + 'allow' => true, + 'roles' => ['@'], + ], + // everything else is denied + ], + ]; + + return $behaviors; + } + + /** + * Lists all Event models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new EventSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + $permissions = new EventPermissions(); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + 'permissions' => $permissions + ]); + } + + /** + * Displays a single Event model. + * @param integer $id + * @return mixed + * @throws NotFoundHttpException + */ + public function actionView($id) + { + $eventRegistrationManager = new EventRegistrationManager(); + + + $dataProvider = new ActiveDataProvider([ + 'query' => $eventRegistrationManager->createFindRegistrationsQuery($id), + ] + ); + return $this->render('view', [ + 'model' => $this->findModel($id), + 'dataProvider' => $dataProvider, + 'permissions' => new EventPermissions() + ]); + } + + /** + * 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 + * @throws NotFoundHttpException + */ + 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\Exception + */ + public function actionDelete($id) + { + $manager = new EventRegistrationManager(); + $manager->deleteEvent($this->findModel($id)); + return $this->redirect(['index']); + } + + + /** + * @param $id + * @return \yii\web\Response + * @throws \yii\db\Exception + * @throws \Exception + */ + public function actionCancelRegistration($id) + { + $eventRegistrationManager = new EventRegistrationManager(); + $db = \Yii::$app->db; + $tx = $db->beginTransaction(); + try{ + $registration = $eventRegistrationManager->loadRegistration($id); + $eventRegistrationManager->cancelRegistration($registration); + $tx->commit(); + return $this->redirect(['view', 'id' => $registration->id_event]); + }catch (\Exception $ex){ + $tx->rollBack(); + throw $ex; + } + } + + /** + * @param $id + * @return \yii\web\Response + * @throws \yii\db\Exception + * @throws \Exception + */ + public function actionDeleteRegistration($id) + { + $eventRegistrationManager = new EventRegistrationManager(); + $db = \Yii::$app->db; + $tx = $db->beginTransaction(); + try{ + $registration = $eventRegistrationManager->loadRegistration($id); + $eventRegistrationManager->deleteRegistration($registration); + $tx->commit(); + return $this->redirect(['view', 'id' => $registration->id_event]); + }catch (\Exception $ex){ + $tx->rollBack(); + throw $ex; + } + } + + /** + * @param $id + * @return string|\yii\web\Response + * @throws NotFoundHttpException + * @throws \yii\db\Exception + */ + public function actionReserveCard($id) + { + + $event = $this->findModel($id); + + $model = new CardEventRegistrationForm(); + $model->event_id = $id; + if ($model->load(Yii::$app->request->post())) { + if ($model->validate()) { + $manager = new EventRegistrationManager(); + try { + $manager->registerCard($model); + } catch (HttpException $e) { + if (array_key_exists($e->getCode(), EventRegistrationManager::$STATES)) { + $model->addError("card_number", Yii::t('event-registration', EventRegistrationManager::$STATES[$e->getCode()])); + } else { + $model->addError("card_number", Yii::t('event-registration', "Unknown Error")); + } + } + } + if ($model->hasErrors()) { + return $this->render('register_card', [ + 'model' => $model, + 'event' => $event, + ]); + } else { + return $this->redirect(['view', 'id' => $id]); + } + } + return $this->render('register_card', [ + 'model' => $model, + 'event' => $event, + ]); + } + + /** + * 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.'); + } + } +} diff --git a/common/modules/event/models/EventPermissions.php b/common/modules/event/models/EventPermissions.php new file mode 100644 index 0000000..fdfadf4 --- /dev/null +++ b/common/modules/event/models/EventPermissions.php @@ -0,0 +1,42 @@ +mode; + if ( $moduleMode == 'admin'){ + $this->allowCreate = true; + $this->allowDelete = true; + $this->allowEdit = true; + $this->allowIndexCreatedAt = true; + } + } + +} \ No newline at end of file diff --git a/common/modules/event/models/EventSearch.php b/common/modules/event/models/EventSearch.php new file mode 100644 index 0000000..6e6b80b --- /dev/null +++ b/common/modules/event/models/EventSearch.php @@ -0,0 +1,157 @@ + 250], + [['startDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'start', 'timeZone' => 'UTC'], + [['endDateString',], 'date', 'format' => Yii::$app->formatter->datetimeFormat, 'timestampAttribute' => 'end', 'timeZone' => 'UTC'], + ]; + } + + /** + * @inheritdoc + */ + public function scenarios() + { + // bypass scenarios() implementation in the parent class + return Model::scenarios(); + } + + /** + * Creates data provider instance with search query applied + * + * @param array $params + * + * @return ActiveDataProvider + */ + public function search($params) + { + $query = new Query(); + + $query->select([ + 'event.id as event_id', + 'event.start as event_start', + 'event.end as event_end', + 'event.created_at as event_created_at', + 'event.updated_at as event_updated_at', + 'event.deleted_at as event_deleted_at', + 'event.seat_count as event_seat_count', + 'trainer.name as trainer_name', + 'room.name as room_name', + 'event_type.name as event_type_name', + new Expression('count(event_registration.id) as registration_count') + ]); + + $query->from("event"); + $query->innerJoin('trainer', 'event.id_trainer = trainer.id'); + $query->innerJoin('room', 'event.id_room = room.id'); + $query->innerJoin('event_type', 'event_type.id = event.id_event_type'); + $query->leftJoin('event_registration', 'event_registration.id_event = event.id'); + $query->groupBy( + [ + 'event_id', + 'event_start', + 'event_end', + 'event_created_at', + 'event_updated_at', + 'event_deleted_at', + 'trainer_name', + 'room_name', + 'event_type_name', + ] + ); + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + 'sort' => [ + 'defaultOrder' => [ + 'event_start' => SORT_DESC + ], + 'attributes' => Helper::mkYiiSortItems([ + ['event_id'], + ['event_start'], + ['event_end'], + ['trainer_name'], + ['room_name'], + ['event_type_name'], + ['customer_name'], + ['event_created_at'], + ['event_updated_at'], + ['event_deleted_at'], + ['registration_count'], + ['event_seat_count'], + ]), + ] + ]); + + $this->load($params); + + if (!$this->validate()) { + // uncomment the following line if you do not want to return any records when validation fails + // $query->where('0=1'); + return $dataProvider; + } + + $query->andFilterWhere([ + 'event.id' => $this->id, + ]); + $query->andFilterWhere( + ['room.id' => $this->roomName] + ); + + $query->andFilterWhere( + ['trainer.id' => $this->trainerName] + ); + + $query->andFilterWhere( + ['event_type.id' => $this->eventTypeName] + ); + + if (isset($this->start)) { + $query->andWhere( + [ + '>=', + 'start', + $this->start + ] + ); + } + + if (isset($this->end)) { + $query->andWhere( + [ + '<=', + 'end', + $this->end + ] + ); + } + + return $dataProvider; + } +} diff --git a/common/modules/event/views/default/index.php b/common/modules/event/views/default/index.php new file mode 100644 index 0000000..c240414 --- /dev/null +++ b/common/modules/event/views/default/index.php @@ -0,0 +1,12 @@ +
+ This is the view content for action "= $this->context->action->id ?>". + The action belongs to the controller "= get_class($this->context) ?>" + in the "= $this->context->module->id ?>" module. +
+
+ You may customize this page by editing the following file:
+ = __FILE__ ?>
+
+ allowCreate ){ + echo Html::a(Yii::t('event', 'Create Event'), ['create'], ['class' => 'btn btn-success']); + } + ?> +
+ + 'event_id', + 'label' => \Yii::t('event', 'ID') + ], + [ + 'attribute' => 'event_type_name', + 'label' => \Yii::t('event', 'Id Event Type') + ], + [ + 'attribute' => 'event_start', + 'label' => \Yii::t('event', 'Start'), + 'format' => 'datetime' + ], + [ + 'attribute' => 'event_end', + 'label' => \Yii::t('event', 'End'), + 'format' => 'time' + ], + [ + 'attribute' => 'event_seat_count', + 'label' => \Yii::t('event', 'Seat Count') + ], + [ + 'attribute' => 'registration_count', + 'label' => \Yii::t('event', 'Registration Count') + ], + [ + 'attribute' => 'room_name', + 'label' => \Yii::t('event', 'Room Name') + ], + [ + 'attribute' => 'trainer_name', + 'label' => \Yii::t('event', 'Trainer Name') + ], + [ + 'attribute' => 'event_created_at', + 'label' => \Yii::t('event', 'Created At'), + 'format' => 'datetime', + 'allow' => $permissions->allowIndexCreatedAt + ], + [ + 'attribute' => 'event_updated_at', + 'label' => \Yii::t('event', 'Updated At'), + 'format' => 'datetime' + ], + [ + 'attribute' => 'event_deleted_at', + 'label' => \Yii::t('event', 'Deleted At'), + 'format' => 'datetime' + ], + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => join("",$indexTableTemplateButtons), + 'urlCreator' => function ($action, $model, $key, $index) { + $params = ['id' => $model['event_id']]; + $params[0] = "event" . '/' . $action; + return \yii\helpers\Url::toRoute($params); + }, + 'buttons' => [ + 'view' => function ($url, $model, $key) { + $options = [ + 'title' => Yii::t('yii', 'View'), + 'aria-label' => Yii::t('yii', 'View'), + 'data-pjax' => '0', + ]; + return Html::a('', $url, $options); + }, + 'update' => function ($url, $model, $key) { + $options = [ + 'title' => Yii::t('yii', 'Update'), + 'aria-label' => Yii::t('yii', 'Update'), + 'data-pjax' => '0', + ]; + return Html::a('', $url, $options); + }, + 'delete' => function ($url, $model, $key) { + $options = [ + 'title' => Yii::t('yii', 'Delete'), + 'aria-label' => Yii::t('yii', 'Delete'), + 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), + 'data-method' => 'post', + 'data-pjax' => '0', + ]; + return Html::a('', $url, $options); + }, + 'reserve-card' => function ($url, $model, $key) { + $options = [ + 'title' => Yii::t('yii', 'Register'), + 'aria-label' => Yii::t('yii', 'Register'), + 'data-pjax' => '0', + ]; + return Html::a('', $url, $options); + } + ] + + ], + ]); + ?> + + = GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => $columns + ]); ?> + ++ allowEdit) { + echo Html::a(Yii::t('event', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']); + } + if ($model->canReserve()) { + echo Html::a(Yii::t('event', 'Reservation'), ['reserve-card', 'id' => $model->id], ['class' => 'btn btn-primary']); + } + ?> + allowDelete) { + + if ($model->canDelete()) { + echo Html::a(Yii::t('event', 'Delete'), ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger pull-right', + 'data' => [ + 'confirm' => Yii::t('event', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]); + } + } + ?> +
+ + render('_view', ['model' => $model]); ?> + +