diff --git a/common/manager/EventRegistrationManager.php b/common/manager/EventRegistrationManager.php index 2a18368..e160c8c 100644 --- a/common/manager/EventRegistrationManager.php +++ b/common/manager/EventRegistrationManager.php @@ -230,11 +230,11 @@ class EventRegistrationManager extends BaseObject } if (isset($registration->canceled_at)) { - throw new BadRequestHttpException('The registration is already canceled', self::ALREADY_CANCELLED); + throw new BadRequestHttpException('The registration is already canceled: '.$registration->id, self::ALREADY_CANCELLED); } if (isset($registration->deleted_at)) { - throw new BadRequestHttpException('The reservation is already deleted', self::ALREADY_DELETED); + throw new BadRequestHttpException('The reservation is already deleted: '.$registration->id, self::ALREADY_DELETED); } $event = Event::findOne($registration->id_event); @@ -245,10 +245,10 @@ class EventRegistrationManager extends BaseObject $now = strtotime("now UTC"); - if ($reason != EventRegistration::CANCEL_REASON_CUSTOMER) { + if ($reason == EventRegistration::CANCEL_REASON_CUSTOMER) { $timeUntilEventStart = $event->start - $now; if ($timeUntilEventStart < Helper::getGroupTrainingRegistrationCancelLimitMinutes() * 60) { - throw new BadRequestHttpException('The reservation is already deleted', self::CANCEL_TIME_LIMIT_REACHED); + throw new BadRequestHttpException('The reservation can\'t be deleted', self::CANCEL_TIME_LIMIT_REACHED); } } @@ -302,9 +302,8 @@ class EventRegistrationManager extends BaseObject $tx = $db->beginTransaction(); try { - $eventRegistrationManager = new EventRegistrationManager(); - $allRegistrations = $eventRegistrationManager->findAllRegistrations($event->id); - $activeRegistrations = $eventRegistrationManager->findActiveRegistrations($event->id); + $allRegistrations = $this->findAllRegistrations($event->id); + $activeRegistrations = $this->findActiveRegistrations($event->id); // //////////////////////////////// // if event has no registrations diff --git a/common/modules/event/controllers/EventController.php b/common/modules/event/controllers/EventController.php index 623c7c0..a52d62e 100644 --- a/common/modules/event/controllers/EventController.php +++ b/common/modules/event/controllers/EventController.php @@ -8,7 +8,6 @@ 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; use common\modules\event\modelAndView\CreateEventModelAndView; @@ -20,7 +19,6 @@ use common\modules\event\models\EventPermissions; use common\modules\event\models\timetable\TimeTableSearch; use DateTime; use Exception; -use Throwable; use Yii; use common\models\Event; use common\modules\event\models\EventSearch; @@ -354,7 +352,7 @@ class EventController extends Controller } /** - * @param $id the id + * @param int $id the id * @return string * @throws NotFoundHttpException */ diff --git a/common/modules/event/manager/EventManager.php b/common/modules/event/manager/EventManager.php index 3d6ddb8..757cc23 100644 --- a/common/modules/event/manager/EventManager.php +++ b/common/modules/event/manager/EventManager.php @@ -19,29 +19,6 @@ use yii\db\StaleObjectException; class EventManager { - /** @noinspection PhpUnused */ - - /** - * Delete or mark deleted an event - * @param $id - * @throws Throwable - * @throws \yii\base\Exception on any error - * @throws StaleObjectException - */ - public function deleteEvent($id) - { - $event = Event::findOne($id); - if (!isset($event)) { - throw new \yii\base\Exception("Event $id not found"); - } - $registrations = $event->eventRegistrations; - if (!isset($registrations) || count($registrations) === 0) { - $event->delete(); - } else { - $event->deleted_at = time(); - $event->save(false); - } - } /** @@ -91,10 +68,7 @@ class EventManager } // get events between active dates - $trainers = null; - if ( RoleDefinition::isTrainer() ){ - $trainers = $this->getAssignedTrainerIDs(\Yii::$app->user->id); - } + $trainers = $this->getAssignedTrainerIDsForPermission(); $events = $this->getEvents($dateTimeFrom, $dateTimeTo,$trainers); @@ -236,14 +210,62 @@ class EventManager public function getAssignedTrainerIDs($idUser){ $trainerObjects = $this->getAssignedTrainers($idUser); + return $this->mapTrainerToId($trainerObjects); + } - $trainers = []; - foreach ($trainerObjects as $trainerObject){ - $trainers[] = $trainerObject->id; - } - return $trainers; + public function getAllTrainerIDs( ){ + $trainerObjects = Trainer::find()->all(); + return $this->mapTrainerToId($trainerObjects); + } + + public function mapTrainerToId($trainerObjects){ + $trainers = []; + foreach ($trainerObjects as $trainerObject){ + $trainers[] = $trainerObject->id; + } + return $trainers; + } + + /** + * If the trainers are not limited by the 'trainer' role, null will be returned. + * If the user has the 'trainer' permission, but no trainer is assigned, empty array will be returned. + * Otherwise the array of assigned trainer IDs will be returned. + * @return array|null + */ + public function getAssignedTrainerIDsForPermission(){ + if ( RoleDefinition::isTrainer()){ + $trainers = $this->getAssignedTrainerIDs(\Yii::$app->user->id); + } else { + $trainers = $this->getAllTrainerIDs(); + } + return $trainers; + } + + /** + * @param $trainerIDs int[]|null the trainer IDs. Null means, all trainers are allowed + * @param $trainerId + + * @return bool + */ + public function isTrainerAllowed($trainerIDs,$trainerId){ + if ( !isset($trainerIDs) ){ + return true; + } + return array_search($trainerId,$trainerIDs) !== false; + } + + /** + * Use it with the result of getAssignedTrainerIDsForPermission(). + * ( + * @param $trainerIDs int[]|null the trainer IDs. Null means, all trainers are allowed + * @return bool + */ + public function hasAnyTrainerAllowed($trainerIDs ){ + if ( !isset($trainerIDs)){ + return true; + } + return count($trainerIDs) > 0; } - } diff --git a/common/modules/event/models/copy/ClearWeekForm.php b/common/modules/event/models/copy/ClearWeekForm.php index 0474f71..3abbf43 100644 --- a/common/modules/event/models/copy/ClearWeekForm.php +++ b/common/modules/event/models/copy/ClearWeekForm.php @@ -2,13 +2,13 @@ namespace common\modules\event\models\copy; +use common\components\RoleDefinition; use common\manager\EventRegistrationManager; use common\modules\event\manager\EventManager; use common\modules\event\models\timetable\TimeTableMonth; use common\modules\event\models\timetable\TimeTableMonthWeek; use customerapi\models\available\EventInterval; use DateTime; -use Throwable; use Yii; use yii\base\Model; @@ -45,7 +45,6 @@ class ClearWeekForm extends Model /** * @param $params - * @throws Throwable */ public function clear($params){ $this->load($params); @@ -65,9 +64,16 @@ class ClearWeekForm extends Model $events = $targetWeek->getAllEvents(); + $trainers = $eventManager->getAssignedTrainerIDsForPermission(); $eventRegisterManager = new EventRegistrationManager(); + if ( !$eventManager->hasAnyTrainerAllowed($trainers) ){ + // no trainers assigned, can't do anything... + return; + } foreach ($events as $event){ - $eventRegisterManager->deleteEvent($event); + if ( $eventManager->isTrainerAllowed($trainers,$event->id_trainer) ){ + $eventRegisterManager->deleteEvent($event); + } } } diff --git a/common/modules/event/models/copy/CopyWeekContext.php b/common/modules/event/models/copy/CopyWeekContext.php new file mode 100644 index 0000000..8f17a4e --- /dev/null +++ b/common/modules/event/models/copy/CopyWeekContext.php @@ -0,0 +1,11 @@ + Yii::$app->formatter->dateFormat, 'timestampAttribute' => 'timestampSource', 'timeZone' => 'UTC'], [['targetDateString',], 'date', 'format' => Yii::$app->formatter->dateFormat, 'timestampAttribute' => 'timestampTarget', 'timeZone' => 'UTC'], ]; @@ -71,46 +75,55 @@ class CopyWeekSearch extends Model $targetDate = null; $this->load($params); if ($this->validate()) { - $sourceDate = new DateTime(); + $sourceDate = new DateTime(); $sourceDate->setTimestamp($this->timestampSource); - $targetDate = new DateTime(); + $targetDate = new DateTime(); $targetDate->setTimestamp($this->timestampTarget); } - $this->sourceInterval = EventInterval::createInterval($sourceDate,7,7); - $this->targetInterval = EventInterval::createInterval($targetDate,7,7); + $this->sourceInterval = EventInterval::createInterval($sourceDate, 7, 7); + $this->targetInterval = EventInterval::createInterval($targetDate, 7, 7); $eventManager = new EventManager(); - $this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval,"display"); - $this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval,"display"); + $this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval, "display"); + $this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval, "display"); +// if ( !isset($_POST['command'])){ +// $this->selectedEvents = []; +// $events = $this->sourceTimeTable->getAllEvents(); +// foreach ($events as $event){ +// $this->selectedEvents[] = $event->id; +// } +// } } /** * @throws Exception */ - public function save(){ + public function save() + { - $sourceDate = new DateTime(); + $sourceDate = new DateTime(); $sourceDate->setTimestamp($this->timestampSource); $sourceDate->modify('this week'); - $targetDate = new DateTime(); + $targetDate = new DateTime(); $targetDate->setTimestamp($this->timestampTarget); $targetDate->modify('this week'); - $this->sourceInterval = EventInterval::createInterval($sourceDate,7,7); - $this->targetInterval = EventInterval::createInterval($targetDate,7,7); + $this->sourceInterval = EventInterval::createInterval($sourceDate, 7, 7); + $this->targetInterval = EventInterval::createInterval($targetDate, 7, 7); // load the time table objects for source and target interval $eventManager = new EventManager(); - $this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval,"display"); - $this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval,"display"); + $this->sourceTimeTable = $eventManager->loadTimeTable($this->sourceInterval, "display"); + $this->targetTimeTable = $eventManager->loadTimeTable($this->targetInterval, "display"); - $sourceWeek = array_values( $this->sourceTimeTable->weeks )[0]; - $targetWeek = array_values( $this->targetTimeTable->weeks )[0]; + $sourceWeek = array_values($this->sourceTimeTable->weeks)[0]; + $targetWeek = array_values($this->targetTimeTable->weeks)[0]; + $eventRegistrationManager = new EventRegistrationManager(); // Iterate over all the week days: monday, tuesday, ... - foreach (EventInterval::weekdays as $weekday ){ + foreach (EventInterval::weekdays as $weekday) { // this is very ugly // it returns eg.: $sourceWeek->monday , $sourceWeek->tuesday... /** @var TimeTableMonthDay $sourceDay */ @@ -121,25 +134,35 @@ class CopyWeekSearch extends Model $sourceEvents = $sourceDay->events; /** @var Event $sourceEvent */ - foreach ($sourceEvents as $sourceEvent ){ - $event = new Event(); - $event->start = $sourceEvent->start; - $event->id_room = $sourceEvent->id_room; - $event->id_event_type = $sourceEvent->id_event_type; - $event->id_trainer = $sourceEvent->id_trainer; - $event->seat_count = $sourceEvent->seat_count; - $event->active = $sourceEvent->active; - $event->deleted_at = $sourceEvent->deleted_at; + foreach ($sourceEvents as $sourceEvent) { - $start = $this->createDateTime( clone $targetDay->date , new DateTime( '@'. $event->start ) ); - $event->start = $start->getTimestamp(); + $processEvent = true; + if (isset($this->selectedEvents)) { + $processEvent = in_array($sourceEvent->id, $this->selectedEvents); + } + if ($processEvent) { + if ($_POST['command' ] == 'delete') { + $eventRegistrationManager->deleteEvent($sourceEvent); + } else { + $event = new Event(); + $event->start = $sourceEvent->start; + $event->id_room = $sourceEvent->id_room; + $event->id_event_type = $sourceEvent->id_event_type; + $event->id_trainer = $sourceEvent->id_trainer; + $event->seat_count = $sourceEvent->seat_count; + $event->active = $sourceEvent->active; + $event->deleted_at = $sourceEvent->deleted_at; - // end date is start date + duration - $eventDuration = $sourceEvent->end - $sourceEvent->start; - $event->end = $start->getTimestamp() + $eventDuration; + $start = $this->createDateTime(clone $targetDay->date, new DateTime('@' . $event->start)); + $event->start = $start->getTimestamp(); - $event->save(false); + // end date is start date + duration + $eventDuration = $sourceEvent->end - $sourceEvent->start; + $event->end = $start->getTimestamp() + $eventDuration; + $event->save(false); + } + } } } @@ -156,11 +179,12 @@ class CopyWeekSearch extends Model * @return DateTime * @throws Exception */ - private function createDateTime($date,$time){ + private function createDateTime($date, $time) + { $result = new DateTime(); - $result->setDate($date->format('Y'),$date->format('m'),$date->format('d')); + $result->setDate($date->format('Y'), $date->format('m'), $date->format('d')); $result->setTimezone($time->getTimezone()); - $result->setTime($time->format('H'),$time->format('i')); + $result->setTime($time->format('H'), $time->format('i')); return $result; } diff --git a/common/modules/event/views/event/_copy_week_form.php b/common/modules/event/views/event/_copy_week_form.php deleted file mode 100644 index 4498748..0000000 --- a/common/modules/event/views/event/_copy_week_form.php +++ /dev/null @@ -1,33 +0,0 @@ - - -
+ + Ha egy esemény sincsen kiválasztva, akkor a másolás/törlés, minden eseményt érinteni fog. + (Nincs esemény kiválasztva = Minden esemény kiválasztva) + +
++ Ha valamelyik esemény ki van választva, akkor a másolás/törlés, csak a kiválasztott esemény(eke)t fogja érinteni +
++ Esemény törlése: a már regisztrált vendégek kártyájára visszakerül az alkalom +
+