ILIAS  trunk Revision v12.0_alpha-1613-gae4c99ebb18
BookingsTableCancelAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ilDateTime;
40use ILIAS\UI\Factory as UIFactory;
41use ILIAS\UI\Renderer as UIRenderer;
45use ilUserUtil;
46
48{
50
51 public const string ACTION_ID = 'cancel';
52
53 public const string ACTION_LABEL = 'book_set_cancel';
54
55 public function __construct(
56 private readonly AccessManager $access,
57 private readonly UIFactory $ui_factory,
58 private readonly UIRenderer $ui_renderer,
59 private readonly Language $lng,
60 private readonly ReservationDBRepository $reservation_repository,
61 private readonly HttpService $http,
62 private readonly ilGlobalTemplateInterface $tpl,
63 private readonly ilCtrlInterface $ctrl,
64 private readonly ilObjBookingPool $booking_pool,
65 private readonly array $bookings
66 ) {
67 }
68
69 public function getActionId(): string
70 {
71 return self::ACTION_ID;
72 }
73
74 public function getActionLabel(): string
75 {
76 return self::ACTION_LABEL;
77 }
78
79 public function isAvailable(): bool
80 {
81 return true;
82 }
83
84 public function allowActionForRecord(mixed $record): bool
85 {
86 if (!$this->access->canManageReservationForUser($this->booking_pool->getRefId(), $record['user_id'])) {
87 return false;
88 }
89
90 return
91 in_array($record['status'], [ilBookingReservation::STATUS_IN_USE, 0])
92 && ($this->booking_pool->getScheduleType() !== ilObjBookingPool::TYPE_FIX_SCHEDULE || $record['date_from'] > time());
93 }
94
95 public function getTableAction(
96 URLBuilder $url_builder,
97 URLBuilderToken $row_id_token,
98 URLBuilderToken $action_token,
99 URLBuilderToken $action_type_token
100 ): Action {
101 return $this->ui_factory->table()->action()->standard(
102 $this->lng->txt($this->getActionLabel()),
103 $url_builder
104 ->withParameter($action_token, self::ACTION_ID)
105 ->withParameter($action_type_token, self::SHOW_MODAL_ACTION),
106 $row_id_token
107 )->withAsync();
108 }
109
110 protected function getModal(
111 URLBuilder $url_builder,
112 array $selected_records,
113 bool $all_records_selected
114 ): ?Modal {
115 return $this->ui_factory->modal()->interruptive(
116 $this->lng->txt('confirm'),
117 $this->lng->txt('book_confirm_cancel'),
118 $url_builder->buildURI()->__toString()
119 )->withAffectedItems(
120 array_map(
121 fn(array $selected_record): Standard => $this->ui_factory->modal()->interruptiveItem()->standard(
122 (string) $selected_record['booking_reservation_id'],
123 $this->formatBookingDescription($selected_record)
124 ),
125 $selected_records
126 )
127 )->withActionButtonLabel($this->lng->txt($this->getActionLabel()));
128 }
129
130 protected function onSubmit(URLBuilder $url_builder, array $selected_records, bool $all_records_selected): ?Modal
131 {
132 $allowed = true;
133 foreach ($selected_records as $selected_record) {
134 if (!$this->access->canManageReservationForUser($this->booking_pool->getRefId(), $selected_record['user_id'])) {
135 $allowed = false;
136 break;
137 }
138 }
139
140 if (!$allowed) {
141 $this->showErrorMessage($this->lng->txt('no_permission'));
142 $this->ctrl->redirectByClass(ilBookingReservationsGUI::class, ilBookingReservationsGUI::DEFAULT_CMD);
143 return null;
144 }
145
146 foreach ($selected_records as $selected_record) {
147 $booking_reservation = new ilBookingReservation($selected_record['booking_reservation_id']);
148 $booking_reservation->setStatus(ilBookingReservation::STATUS_CANCELLED);
149 $booking_reservation->update();
150
151 if ($this->booking_pool->getScheduleType() !== ilObjBookingPool::TYPE_NO_SCHEDULE) {
152 $calendar_entry_id = $booking_reservation->getCalendarEntry();
153 if ($calendar_entry_id) {
154 (new ilCalendarEntry($calendar_entry_id))->delete();
155 }
156 }
157 }
158
159 $this->showSuccessMessage($this->lng->txt('book_reservation_cancelled'));
160 $this->ctrl->redirectByClass(ilBookingReservationsGUI::class, ilBookingReservationsGUI::DEFAULT_CMD);
161 return null;
162 }
163
164 protected function resolveRecords(?array $selected_ids = null): array
165 {
166 return array_map(
167 fn(int $selected_record): array => $this->bookings[$selected_record],
168 $selected_ids ?? array_keys($this->bookings)
169 );
170 }
171
172 protected function formatBookingDescription(array $booking): string
173 {
174 $title = (new ilBookingObject($booking['object_id']))->getTitle();
175 $user = strip_tags(ilUserUtil::getNamePresentation($booking['user_id']));
176 $parts = [$title, $user];
177 if ($this->booking_pool->getScheduleType() !== ilObjBookingPool::TYPE_NO_SCHEDULE && $booking['date_from']) {
179 new ilDateTime($booking['date_from'], IL_CAL_UNIX),
180 new ilDateTime($booking['date_to'] + 1, IL_CAL_UNIX)
181 );
182 }
183 return implode(' – ', $parts);
184 }
185}
__construct(private readonly AccessManager $access, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly Language $lng, private readonly ReservationDBRepository $reservation_repository, private readonly HttpService $http, private readonly ilGlobalTemplateInterface $tpl, private readonly ilCtrlInterface $ctrl, private readonly ilObjBookingPool $booking_pool, private readonly array $bookings)
getTableAction(URLBuilder $url_builder, URLBuilderToken $row_id_token, URLBuilderToken $action_token, URLBuilderToken $action_type_token)
onSubmit(URLBuilder $url_builder, array $selected_records, bool $all_records_selected)
getModal(URLBuilder $url_builder, array $selected_records, bool $all_records_selected)
Repo class for reservations Acts on tables booking_reservation (rw), booking_reservation_group (rw) a...
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
const IL_CAL_UNIX
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Model for a calendar entry.
Class for date presentation.
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false, ?ilObjUser $user=null)
Format a period of two dates Shows: 14.
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilUserUtil.
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes commonalities between the different modals.
Definition: Modal.php:35
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
trait TableActionModalTrait
@template RecordType
modal(string $title="", string $cancel_label="")
global $lng
Definition: privfeed.php:26