Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pages/Ajax/ReservationAttributesPrintPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function PageLoad()
$userSession = ServiceLocator::GetServer()->GetUserSession();
$this->presenter->PageLoad($userSession);
$this->Set('ReadOnly', BooleanConverter::ConvertValue($this->GetIsReadOnly()));
$this->Set('CustomAttributeTypeDateTime', CustomAttributeTypes::DATETIME);
$this->Display('Ajax/reservation/reservation_attributes_print.tpl');
}

Expand Down
210 changes: 210 additions & 0 deletions Pages/Reservation/ReservationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function PageLoad()
'weekly' => ['key' => 'Weekly', 'everyKey' => 'weeks'],
'monthly' => ['key' => 'Monthly', 'everyKey' => 'months'],
'yearly' => ['key' => 'Yearly', 'everyKey' => 'years'],
'custom' => ['key' => 'Custom', 'everyKey' => ''],
]
);
$this->Set(
Expand All @@ -244,9 +245,218 @@ public function PageLoad()
return;
}

$this->SetReservationPdfConfig();
$this->Display($this->GetTemplateName());
}

private function SetReservationPdfConfig(): void
{
$repeatOptions = $this->SmartyVar('RepeatOptions', []);
$repeatType = $this->SmartyVar('RepeatType', RepeatType::None);
$repeatMonthlyType = $this->SmartyVar('RepeatMonthlyType', '');
$showReservationDetails = (bool)$this->SmartyVar('ShowReservationDetails', false);
$showParticipation = (bool)$this->SmartyVar('ShowParticipation', false);
$accessories = $this->SmartyVar('Accessories', []);
$participants = $this->SmartyVar('Participants', []);
$invitees = $this->SmartyVar('Invitees', []);
$attachments = $this->SmartyVar('Attachments', []);
$repeatWeekdays = $this->SmartyVar('RepeatWeekdays', []);
$customRepeatDates = $this->SmartyVar('CustomRepeatDates', []);
$dayNames = Resources::GetInstance()->GetDays('full');
$reminders = [];

if ($this->SmartyVar('ReminderTimeStart', '') !== '') {
$reminders[] = [
'time' => (string)$this->SmartyVar('ReminderTimeStart'),
'interval' => $this->Translate($this->SmartyVar('ReminderIntervalStart', '')),
'text' => $this->Translate('ReminderBeforeStart'),
];
}

if ($this->SmartyVar('ReminderTimeEnd', '') !== '') {
$reminders[] = [
'time' => (string)$this->SmartyVar('ReminderTimeEnd'),
'interval' => $this->Translate($this->SmartyVar('ReminderIntervalEnd', '')),
'text' => $this->Translate('ReminderBeforeEnd'),
];
}

$config = [
'appTitle' => $this->SmartyVar('AppTitle', ''),
'reservationDetailsTitle' => $this->SmartyCapitalize($this->Translate('ReservationDetails')),
'referenceNumberLabel' => $this->Translate('ReferenceNumber'),
'referenceNumber' => $this->SmartyVar('ReferenceNumber', ''),
'userLabel' => $this->Translate('User'),
'reservationUserName' => $this->DecodeHtml($this->SmartyVar('ReservationUserName', '')),
'showUserDetailsAndReservationDetails' => (bool)$this->SmartyVar('ShowUserDetails', false) && $showReservationDetails,

'beginDateLabel' => $this->Translate('BeginDate'),
'beginDateValue' => $this->FormatPdfDate($this->SmartyVar('StartDate'), 'dashboard'),
'endDateLabel' => $this->Translate('EndDate'),
'endDateValue' => $this->FormatPdfDate($this->SmartyVar('EndDate'), 'dashboard'),
'reservationLengthLabel' => $this->Translate('ReservationLength'),
'repeatPromptLabel' => $this->Translate('RepeatPrompt'),
'isRecurring' => (bool)$this->SmartyVar('IsRecurring', false),
'isCustomRepeat' => $repeatType === RepeatType::Custom,
'repeatTypeLabel' => $this->Translate($repeatOptions[$repeatType]['key'] ?? ''),
'repeatInterval' => (string)$this->SmartyVar('RepeatInterval', ''),
'repeatEveryLabel' => $repeatType === RepeatType::Custom ? '' : $this->Translate($repeatOptions[$repeatType]['everyKey'] ?? ''),
'repeatOnLabel' => $this->Translate('RepeatOn'),
'typeLabel' => $this->Translate('Type'),
'repeatMonthlyTypeLabel' => $repeatMonthlyType === '' ? '' : $this->Translate($repeatMonthlyType === RepeatMonthlyType::DayOfMonth ? 'repeatDayOfMonth' : 'repeatDayOfWeek'),
'daysLabel' => $this->SmartyCapitalize($this->Translate('RepeatDaysPrompt')),
'repeatWeekdays' => array_map(
fn ($day) => $dayNames[$day] ?? '',
is_array($repeatWeekdays) ? $repeatWeekdays : []
),
'repeatCustomDates' => array_map(
fn ($date) => $this->FormatPdfDate($date, 'schedule_daily', $this->SmartyVar('Timezone')),
is_array($customRepeatDates) ? $customRepeatDates : []
),
'repeatUntilPromptLabel' => $this->Translate('RepeatUntilPrompt'),
'repeatUntilDate' => $this->FormatPdfDate($this->SmartyVar('RepeatTerminationDate'), 'dashboard'),

'additionalAttributesLabel' => $this->Translate('AdditionalAttributes'),
'customAttributeTypeCheckbox' => CustomAttributeTypes::CHECKBOX,

'resourcesHeaderLabel' => $this->Translate('Resources'),
'requiresApprovalLabel' => $this->Translate('RequiresApproval'),
'requiresCheckInNotificationLabel' => $this->Translate('RequiresCheckInNotification'),
'releasedInLabel' => sprintf('%s (%s)', $this->Translate('ReleasedIn'), $this->Translate('minutes')),
'resources' => $this->PdfResources(),

'showAccessories' => $showReservationDetails && count($accessories) > 0,
'accessoriesHeaderLabel' => $this->Translate('Accessories'),
'quantityLabel' => $this->Translate('Quantity'),
'accessories' => array_map(
fn ($accessory) => [
'name' => (string)$accessory->Name,
'quantity' => (string)$accessory->QuantityReserved,
],
$accessories
),

'showParticipants' => $showReservationDetails && $showParticipation && count($participants) > 0,
'participantsHeaderLabel' => $this->Translate('Participants'),
'emailLabel' => $this->Translate('Email'),
'participants' => $this->PdfUsers($participants),

'showInvitees' => $showReservationDetails && $showParticipation && count($invitees) > 0,
'invitationListHeaderLabel' => $this->Translate('InvitationList'),
'invitees' => $this->PdfUsers($invitees),

'reservationTitleLabel' => $this->Translate('ReservationTitle'),
'reservationTitle' => $this->DecodeHtml($this->SmartyVar('ReservationTitle', '')),
'reservationDescriptionLabel' => $this->Translate('ReservationDescription'),
'reservationDescription' => $this->DecodeHtml($this->SmartyVar('Description', '')),

'remindersEnabled' => (bool)$this->SmartyVar('RemindersEnabled', false),
'sendReminderLabel' => $this->Translate('SendReminder'),
'reminders' => $reminders,

'attachmentsLabel' => $this->Translate('Attachments'),
'attachments' => array_map(
fn ($attachment) => $attachment->FileName(),
$attachments
),

'showTermsAcceptance' => $this->SmartyVar('Terms') !== null && (bool)$this->SmartyVar('TermsAccepted', false),
'acceptTermsLabel' => sprintf('%s %s', $this->Translate('IAccept'), $this->Translate('TheTermsOfService')),

'logoUrl' => sprintf('%s/img/%s', $this->SmartyVar('ScriptUrl', ''), $this->SmartyVar('LogoUrl', '')),
];

$this->Set('ReservationPdfConfigJson', json_encode(
$config,
// Keep the inline script assignment safe and fail fast if encoding ever breaks.
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
));
}

private function SmartyVar(string $name, mixed $default = null): mixed
{
// Bridge existing presenter-to-template assignments while building the PDF config in PHP.
$value = $this->smarty->getTemplateVars($name);
return $value === null ? $default : $value;
}

private function Translate(string $key): string
{
if ($key === '') {
return '';
}

return Resources::GetInstance()->GetString($key, '');
}

private function SmartyCapitalize(string $value): string
{
$capitalize = $this->smarty->getModifierCallback('capitalize');

return $capitalize === null ? $value : (string)$capitalize($value);
}

private function DecodeHtml(mixed $value): string
{
return html_entity_decode((string)$value, ENT_QUOTES | ENT_HTML5);
}

private function FormatPdfDate(mixed $date, string $key, ?string $timezone = null): string
{
$params = [
'date' => $date,
'key' => $key,
];

if ($timezone !== null) {
$params['timezone'] = $timezone;
}

return $this->smarty->FormatDate($params, $this->smarty);
}

private function PdfResources(): array
{
$resources = [];
$primaryResource = $this->SmartyVar('Resource');

if ($primaryResource !== null) {
$resources[] = $this->PdfResource($primaryResource, '');
}

$additionalResourceIds = $this->SmartyVar('AdditionalResourceIds', []);
$availableResources = $this->SmartyVar('AvailableResources', []);

foreach ($availableResources as $resource) {
if (in_array($resource->Id, is_array($additionalResourceIds) ? $additionalResourceIds : [])) {
$resources[] = $this->PdfResource($resource, ' - ');
}
}

return $resources;
}

private function PdfResource(IBookableResource $resource, string $emptyReleasedIn): array
{
return [
'name' => $resource->GetName(),
'requiresApproval' => $resource->GetRequiresApproval(),
'requiresCheckIn' => $resource->IsCheckInEnabled(),
'releasedIn' => $resource->IsAutoReleased() ? (string)$resource->GetAutoReleaseMinutes() : $emptyReleasedIn,
];
}

private function PdfUsers(array $users): array
{
return array_map(
fn ($user) => [
'fullName' => $this->DecodeHtml($user->FullName),
'email' => (string)$user->Email,
],
$users
);
}

public function BindPeriods($startPeriods, $endPeriods, $lockPeriods)
{
$this->Set('StartPeriods', $startPeriods);
Expand Down
Loading