diff --git a/common/manager/EventRegistrationManager.php b/common/manager/EventRegistrationManager.php index 8c9488b..b965b25 100644 --- a/common/manager/EventRegistrationManager.php +++ b/common/manager/EventRegistrationManager.php @@ -46,22 +46,22 @@ class EventRegistrationManager extends BaseObject const CANCEL_TIME_LIMIT_REACHED = 15; public static $STATES = [ - self::CARD_NOT_FOUND => "Kártya nem található", - self::CUSTOMER_NOT_FOUND => "Vendég nem található", - self::TICKET_NOT_FOUND => "Bérlet nem található", - self::NO_FREE_SEATS => "Nincs szabad hely", - self::EVENT_TYPE_NOT_FOUND => "Esemény típus nem található", - self::TICKET_INSUFFICIENT => "Bérlet nem található", - self::UNKNOWN_ERROR => "Ismeretlen hiba", - self::MAX_SEAT_COUNT_EXCEEDED => "Nincs szabad hely", - self::EVENT_UNAVAILABLE => "Esemény nem elérhető", - self::ALREADY_REGISTERED => "Már regisztrálva van", - self::EVENT_START_DATE_IN_PAST => "Az esemény már elkezdődött", + self::CARD_NOT_FOUND => "Kártya nem található", + self::CUSTOMER_NOT_FOUND => "Vendég nem található", + self::TICKET_NOT_FOUND => "Bérlet nem található", + self::NO_FREE_SEATS => "Nincs szabad hely", + self::EVENT_TYPE_NOT_FOUND => "Esemény típus nem található", + self::TICKET_INSUFFICIENT => "Bérlet nem található", + self::UNKNOWN_ERROR => "Ismeretlen hiba", + self::MAX_SEAT_COUNT_EXCEEDED => "Nincs szabad hely", + self::EVENT_UNAVAILABLE => "Esemény nem elérhető", + self::ALREADY_REGISTERED => "Már regisztrálva van", + self::EVENT_START_DATE_IN_PAST => "Az esemény már elkezdődött", - self::EVENT_NOT_FOUND => "Esemény tnem található", - self::ALREADY_CANCELLED => "Ez a regisztráció már lemndásra került", - self::ALREADY_DELETED => "Ez a regisztráció már törlésre került", - self::CANCEL_TIME_LIMIT_REACHED => "Ez a regisztráció már nem mondható le", + self::EVENT_NOT_FOUND => "Esemény tnem található", + self::ALREADY_CANCELLED => "Ez a regisztráció már lemndásra került", + self::ALREADY_DELETED => "Ez a regisztráció már törlésre került", + self::CANCEL_TIME_LIMIT_REACHED => "Ez a regisztráció már nem mondható le", ]; /** @@ -195,10 +195,12 @@ class EventRegistrationManager extends BaseObject public function loadRegistration($idRegistration, $idCustomer) { - $registration = EventRegistration::find() - ->andWhere(['id' => $idRegistration]) - ->andWhere(['id_customer' => $idCustomer]) - ->one(); + $query = EventRegistration::find() + ->andWhere(['id' => $idRegistration]); + if ( isset($idCustomer)){ + $query->andWhere(['id_customer' => $idCustomer]) + } + $registration = $query->one(); if ($registration === null) { throw new NotFoundHttpException('The requested registration does not exist.'); } @@ -209,9 +211,9 @@ class EventRegistrationManager extends BaseObject * @param EventRegistration $registration * @throws ServerErrorHttpException */ - public function cancelRegistration($registration, $idCustomer) + public function cancelRegistration($registration, $idCustomer, $reason) { - if ( $registration->id_customer != $idCustomer){ + if (isset($idCustomer) && $registration->id_customer != $idCustomer) { throw new NotFoundHttpException('The requested registration does not exist.'); } @@ -229,13 +231,16 @@ class EventRegistrationManager extends BaseObject throw new BadRequestHttpException('The reservation is already deleted', self::EVENT_NOT_FOUND); } $tx = \Yii::$app->db->beginTransaction(); - try { - $now = strtotime("now UTC"); + $now = strtotime("now UTC"); + if ($reason != EventRegistration::CANCEL_REASON_CUSTOMER) { $timeUntilEventStart = $event->start - $now; - if ( $timeUntilEventStart < 30 * 60){ + if ($timeUntilEventStart < 30 * 60) { throw new BadRequestHttpException('The reservation is already deleted', self::CANCEL_TIME_LIMIT_REACHED); } + } + + try { $registration->canceled_at = date('Y-m-d H:i:s', $now); $registration->save(false); @@ -310,7 +315,7 @@ class EventRegistrationManager extends BaseObject foreach ($registrations as $registration) { if (!isset($registration->deleted_at)) { /** @var EventRegistration $registration */ - $this->deleteRegistration($registration); + $this->cancelRegistration($registration, null, EventRegistration::CANCEL_REASON_EVENT); } } } diff --git a/common/models/EventRegistration.php b/common/models/EventRegistration.php index 468afd2..5682861 100644 --- a/common/models/EventRegistration.php +++ b/common/models/EventRegistration.php @@ -21,6 +21,9 @@ use yii\helpers\ArrayHelper; */ class EventRegistration extends \yii\db\ActiveRecord { + const CANCEL_REASON_CUSTOMER = "customer"; + const CANCEL_REASON_EVENT = "event"; + /** * @inheritdoc */ diff --git a/common/modules/event/controllers/EventController.php b/common/modules/event/controllers/EventController.php index 1dea426..b8a5d9b 100644 --- a/common/modules/event/controllers/EventController.php +++ b/common/modules/event/controllers/EventController.php @@ -7,6 +7,7 @@ use common\manager\EventRegistrationManager; use common\models\CardEventRegistrationForm; use common\models\EventEquipmentType; use common\models\EventEquipmentTypeAssignment; +use common\models\EventRegistration; use common\models\EventRegistrationEquipmentTypeAssignment; use common\models\Trainer; use common\modules\event\EventModule; @@ -31,6 +32,7 @@ use yii\web\HttpException; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; use yii\web\Response; +use yii\web\UnauthorizedHttpException; /** @noinspection PhpUnused */ @@ -127,6 +129,10 @@ class EventController extends Controller */ public function actionCreate() { + if ( !RoleDefinition::canAny([RoleDefinition::$ROLE_TRAINER, RoleDefinition::$ROLE_ADMIN])){ + throw new UnauthorizedHttpException(); + } + $modelAndView = new CreateEventModelAndView(); $event = new EventCreate(); @@ -166,6 +172,9 @@ class EventController extends Controller */ public function actionUpdate($id) { + if ( !RoleDefinition::canAny([RoleDefinition::$ROLE_TRAINER, RoleDefinition::$ROLE_ADMIN])){ + throw new UnauthorizedHttpException(); + } $modelAndView = new CreateEventModelAndView(); $event = EventCreate::findOne($id); if ( !isset($event)){ @@ -220,8 +229,8 @@ class EventController extends Controller $db = Yii::$app->db; $tx = $db->beginTransaction(); try { - $registration = $eventRegistrationManager->loadRegistration($id); - $eventRegistrationManager->cancelRegistration($registration); + $registration = $eventRegistrationManager->loadRegistration($id,null); + $eventRegistrationManager->cancelRegistration($registration,null,EventRegistration::CANCEL_REASON_CUSTOMER); $tx->commit(); return $this->redirect(['view', 'id' => $registration->id_event]); } catch (Exception $ex) { @@ -230,26 +239,26 @@ class EventController extends Controller } }/** @noinspection PhpUnused */ - /** - * @param $id - * @return Response - * @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; - } - }/** @noinspection PhpUnused */ +// /** +// * @param $id +// * @return Response +// * @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; +// } +// }/** @noinspection PhpUnused */ /** * @param $id diff --git a/customerapi/controllers/EventRegistrationController.php b/customerapi/controllers/EventRegistrationController.php index cec811d..5c19606 100644 --- a/customerapi/controllers/EventRegistrationController.php +++ b/customerapi/controllers/EventRegistrationController.php @@ -9,6 +9,7 @@ namespace customerapi\controllers; +use common\models\EventRegistration; use customerapi\manager\EventRegistrationManager; use common\models\CardEventRegistrationForm; use common\models\Customer; @@ -77,7 +78,7 @@ class EventRegistrationController extends CustomerApiController public function actionCancel($idRegistration) { $manager = new \common\manager\EventRegistrationManager(); $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id); - $manager->cancelRegistration($registration,\Yii::$app->user->id); + $manager->cancelRegistration($registration,\Yii::$app->user->id, EventRegistration::CANCEL_REASON_CUSTOMER); $registration = $manager->loadRegistration($idRegistration,\Yii::$app->user->id); return $this->asJson($registration); }