From 50011a4e4f57496859d7143d21bacc9f3ba44e5d Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Sun, 10 Oct 2021 23:01:18 +0200 Subject: [PATCH] backend/group-training : improve delete© week --- common/manager/EventRegistrationManager.php | 13 ++- .../event/controllers/EventController.php | 4 +- common/modules/event/manager/EventManager.php | 88 ++++++++++------- .../event/models/copy/ClearWeekForm.php | 12 ++- .../event/models/copy/CopyWeekContext.php | 11 +++ .../event/models/copy/CopyWeekSearch.php | 94 ++++++++++++------- .../event/views/event/_copy_week_form.php | 33 ------- .../modules/event/views/event/copy_week.php | 52 +++++++++- .../widgets/day/TimeTableMonthDayView.php | 5 +- .../modules/event/widgets/day/views/_day.php | 3 +- .../modules/event/widgets/event/EventView.php | 7 +- .../event/widgets/event/views/_event.php | 10 +- .../widgets/timetable/TimeTableMonthView.php | 10 +- .../widgets/timetable/views/_timetable.php | 15 +-- 14 files changed, 226 insertions(+), 131 deletions(-) create mode 100644 common/modules/event/models/copy/CopyWeekContext.php delete mode 100644 common/modules/event/views/event/_copy_week_form.php 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 @@ - - -
-
Másolás
-
- -['class' => 'form-inline' ]]); ?> -field($model, 'sourceDateString')->hiddenInput()->label(false) ?> -field($model, 'targetDateString')->hiddenInput()->label(false) ?> - -
- - - -
-
- - -
- 'btn btn-success' ]) ?> - - -
-
diff --git a/common/modules/event/views/event/copy_week.php b/common/modules/event/views/event/copy_week.php index 7dad654..0cabf47 100644 --- a/common/modules/event/views/event/copy_week.php +++ b/common/modules/event/views/event/copy_week.php @@ -1,7 +1,10 @@ render('_copy_week_search', ['model' => $model]); -echo $this->render('_copy_week_form', ['model' => $model]); + ?> +['class' => 'form-inline' ]]); ?> +
+
Másolás
+
+ + field($model, 'sourceDateString')->hiddenInput()->label(false) ?> + field($model, 'targetDateString')->hiddenInput()->label(false) ?> + +
+ + + +
+
+ + +
+ 'btn btn-success', 'value' => 'copy', 'name' => 'command' ]) ?> + 'btn btn-danger', 'value' => 'delete', 'name' => 'command' ]) ?> +

+ + 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 +

+
+
+

Forrás hét

- $model->sourceTimeTable]) ?> + $model->sourceTimeTable, + 'copyWeekContext' =>new CopyWeekContext( + [ + 'copyWeekForm' => $form, + 'copyWeekFormModel' => $model + ] + ) + ] +) ?> +

Cél hét

$model->targetTimeTable]) ?> diff --git a/common/modules/event/widgets/day/TimeTableMonthDayView.php b/common/modules/event/widgets/day/TimeTableMonthDayView.php index 6e6d0d3..307e7e8 100644 --- a/common/modules/event/widgets/day/TimeTableMonthDayView.php +++ b/common/modules/event/widgets/day/TimeTableMonthDayView.php @@ -1,5 +1,6 @@ render('_day', [ 'day' => $this->day]); + return $this->render('_day', [ 'day' => $this->day,'copyWeekContext' => $this->copyWeekContext]); } diff --git a/common/modules/event/widgets/day/views/_day.php b/common/modules/event/widgets/day/views/_day.php index e7ca2b3..77630b5 100644 --- a/common/modules/event/widgets/day/views/_day.php +++ b/common/modules/event/widgets/day/views/_day.php @@ -1,5 +1,6 @@ events as $event) { - echo EventView::widget(['event' => $event]); + echo EventView::widget(['event' => $event, 'copyWeekContext' => $copyWeekContext]); } } ?> diff --git a/common/modules/event/widgets/event/EventView.php b/common/modules/event/widgets/event/EventView.php index cb7b371..6c2b4d0 100644 --- a/common/modules/event/widgets/event/EventView.php +++ b/common/modules/event/widgets/event/EventView.php @@ -2,6 +2,7 @@ namespace common\modules\event\widgets\event; use common\helpers\AppDateTimeHelper; use common\models\Event; +use common\modules\event\models\copy\CopyWeekContext; use DateTime; use yii\bootstrap\Widget; @@ -11,6 +12,7 @@ use yii\bootstrap\Widget; * @package common\modules\event\widgets * * @property Event $event + * @property CopyWeekContext $copyWeekContext */ class EventView extends Widget { @@ -18,6 +20,8 @@ class EventView extends Widget public $start; public $end; + public $copyWeekContext; + public function init(){ parent::init(); if ( isset($this->event )){ @@ -32,7 +36,8 @@ class EventView extends Widget [ 'event' => $this->event, 'start' => $this->start, - 'end' => $this->end + 'end' => $this->end, + 'copyWeekContext' => $this->copyWeekContext ] ); } diff --git a/common/modules/event/widgets/event/views/_event.php b/common/modules/event/widgets/event/views/_event.php index 2a4979a..e6d5796 100644 --- a/common/modules/event/widgets/event/views/_event.php +++ b/common/modules/event/widgets/event/views/_event.php @@ -2,6 +2,8 @@ /* @var $event Event */ /* @var $start DateTime */ /* @var $end DateTime */ +/* @var $copyWeekContext \common\modules\event\models\copy\CopyWeekContext */ + use common\models\Event; use common\modules\event\models\timetable\TimeTableMonthDay; @@ -14,7 +16,13 @@ if (!isset($event)) { -
+
+ +
+ copyWeekFormModel, 'selectedEvents[]') , isset($copyWeekContext->copyWeekFormModel->selectedEvents) && in_array($event->id, $copyWeekContext->copyWeekFormModel->selectedEvents), ['value' => $event->id] ); ?> + +
+ format('H:i') .'-' . $end->format('H:i') , Url::toRoute(['event/update', 'id' => $event->id ] ) ) ?>
eventType->name , diff --git a/common/modules/event/widgets/timetable/TimeTableMonthView.php b/common/modules/event/widgets/timetable/TimeTableMonthView.php index 794557c..9449b5a 100644 --- a/common/modules/event/widgets/timetable/TimeTableMonthView.php +++ b/common/modules/event/widgets/timetable/TimeTableMonthView.php @@ -15,25 +15,27 @@ class TimeTableMonthView extends Widget { public $timeTable; public $actionsColumn; - + public $copyWeekContext; public function run() { return $this->render('_timetable', [ 'timeTable' => $this->timeTable, - 'actionsColumn' => ( isset($this->actionsColumn) ? $this->actionsColumn : null) + 'actionsColumn' => ( isset($this->actionsColumn) ? $this->actionsColumn : null), + 'copyWeekContext' => $this->copyWeekContext ]); } - public static function renderDay($weekDay ){ + public static function renderDay($weekDay , $copyWeekContext = null){ /** * @param TimeTableMonthWeek $week * @return string */ - return static function ($week) use ($weekDay) { + return static function ($week) use ($weekDay, $copyWeekContext) { return TimeTableMonthDayView::widget([ 'day' => $week->getWeekDay($weekDay), + 'copyWeekContext' => $copyWeekContext ]); }; diff --git a/common/modules/event/widgets/timetable/views/_timetable.php b/common/modules/event/widgets/timetable/views/_timetable.php index 4c40012..431bff2 100644 --- a/common/modules/event/widgets/timetable/views/_timetable.php +++ b/common/modules/event/widgets/timetable/views/_timetable.php @@ -1,5 +1,6 @@ 'monday', 'label' => 'Hétfő', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('monday') + 'value' => TimeTableMonthView::renderDay('monday',$copyWeekContext) ], [ 'attribute' => 'tuesday', 'label' => 'Kedd', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('tuesday') + 'value' => TimeTableMonthView::renderDay('tuesday',$copyWeekContext) ], [ 'attribute' => 'wednesday', 'label' => 'Szerda', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('wednesday') + 'value' => TimeTableMonthView::renderDay('wednesday',$copyWeekContext) ], [ 'attribute' => 'thursday', 'label' => 'Csütörtök', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('thursday') + 'value' => TimeTableMonthView::renderDay('thursday',$copyWeekContext) ], [ 'attribute' => 'friday', 'label' => 'Péntek', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('friday') + 'value' => TimeTableMonthView::renderDay('friday',$copyWeekContext) ], [ 'attribute' => 'saturday', 'format' => 'raw', 'label' => 'Szombat', - 'value' => TimeTableMonthView::renderDay('saturday') + 'value' => TimeTableMonthView::renderDay('saturday',$copyWeekContext) ], [ 'attribute' => 'Sunday', 'label' => 'Vasárnap', 'format' => 'raw', - 'value' => TimeTableMonthView::renderDay('sunday') + 'value' => TimeTableMonthView::renderDay('sunday',$copyWeekContext) ], ];