From fc11ccc9238385bd2725f43378facc5d90e46fdf Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Wed, 16 Jan 2019 18:03:47 +0100 Subject: [PATCH] backend - replace event controller with module --- backend/components/AdminMenuStructure.php | 2 +- backend/config/main.php | 7 +- backend/controllers/EventController.php | 222 ------------------ backend/models/EventSearch.php | 157 ------------- .../event/controllers/EventController.php | 4 +- .../modules/event/models/EventPermissions.php | 2 +- common/modules/event/views/event/index.php | 1 + 7 files changed, 11 insertions(+), 384 deletions(-) delete mode 100644 backend/controllers/EventController.php delete mode 100644 backend/models/EventSearch.php diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index ad5ec24..b847eaa 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -169,7 +169,7 @@ class AdminMenuStructure{ $items[] = ['label' => 'Edzők', 'url' => ['/trainer' ] ]; $items[] = ['label' => 'Termek', 'url' => ['/room' ] ]; $items[] = ['label' => 'Esemény típusok', 'url' => ['/event-type' ] ]; - $items[] = ['label' => 'Események', 'url' => ['/event' ] ]; + $items[] = ['label' => 'Események', 'url' => ['/event/event/index' ] ]; $this->menuItems[] = ['label' => 'Csoportos edzés', 'url' => $this->emptyUrl, 'items' => $items ]; diff --git a/backend/config/main.php b/backend/config/main.php index 7a2e2e4..a1412e6 100644 --- a/backend/config/main.php +++ b/backend/config/main.php @@ -12,7 +12,6 @@ return [ 'basePath' => dirname(__DIR__), 'controllerNamespace' => 'backend\controllers', 'bootstrap' => ['log'], - 'modules' => [], 'components' => [ 'request' => [ 'enableCsrfValidation'=>false, @@ -46,5 +45,11 @@ return [ 'errorAction' => 'site/error', ], ], + 'modules' => [ + 'event' => [ + 'class' => 'common\modules\event\EventModule', + 'mode' => 'backend' + ], + ], 'params' => $params, ]; diff --git a/backend/controllers/EventController.php b/backend/controllers/EventController.php deleted file mode 100644 index 7a73aa5..0000000 --- a/backend/controllers/EventController.php +++ /dev/null @@ -1,222 +0,0 @@ - [ - '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 - * @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 - ]); - } - - /** - * 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 - */ - 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/backend/models/EventSearch.php b/backend/models/EventSearch.php deleted file mode 100644 index 2f252a2..0000000 --- a/backend/models/EventSearch.php +++ /dev/null @@ -1,157 +0,0 @@ - 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/controllers/EventController.php b/common/modules/event/controllers/EventController.php index 2a66c29..47f8dc8 100644 --- a/common/modules/event/controllers/EventController.php +++ b/common/modules/event/controllers/EventController.php @@ -8,7 +8,7 @@ use common\modules\event\EventModule; use common\modules\event\models\EventPermissions; use Yii; use common\models\Event; -use backend\models\EventSearch; +use common\modules\event\models\EventSearch; use yii\data\ActiveDataProvider; use yii\web\Controller; use yii\web\HttpException; @@ -33,7 +33,7 @@ class EventController extends Controller $module = EventModule::getInstance(); $allowedActions = [ 'index','view', 'reserve-card','cancel-registration','delete-registration' ]; - if ( $module->mode == 'admin' ){ + if ( $module->mode == 'backend' ){ $allowedActions[] = 'create'; $allowedActions[] = 'update'; $allowedActions[] = 'delete'; diff --git a/common/modules/event/models/EventPermissions.php b/common/modules/event/models/EventPermissions.php index fdfadf4..0017bb4 100644 --- a/common/modules/event/models/EventPermissions.php +++ b/common/modules/event/models/EventPermissions.php @@ -31,7 +31,7 @@ class EventPermissions public function __construct() { $moduleMode = EventModule::getInstance()->mode; - if ( $moduleMode == 'admin'){ + if ( $moduleMode == 'backend'){ $this->allowCreate = true; $this->allowDelete = true; $this->allowEdit = true; diff --git a/common/modules/event/views/event/index.php b/common/modules/event/views/event/index.php index ed16844..2f78e26 100644 --- a/common/modules/event/views/event/index.php +++ b/common/modules/event/views/event/index.php @@ -46,6 +46,7 @@ $indexTableTemplateButtons[] = '{reserve-card}'; $allow = true; if ( isset($item['allow'])){ $allow = $item['allow']; + unset($item['allow']); } if ( $allow ){ $result[] = $item;