Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMB-437: Support Different Date Formats For Tokens #97

Merged
merged 1 commit into from
Oct 16, 2024
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
2 changes: 1 addition & 1 deletion CRM/Certificate/Service/CertificateMembership.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getMembershipRenewalDate(int $certificateId, int $contactId): st
$renewalDate = $renewalDate === NULL || $membershipDate < $renewalDate ? $membershipDate : $renewalDate;
}

return $renewalDate ? CRM_Utils_Date::customFormat(date('Y-m-d', $renewalDate), '%e/%b/%Y') : '';
return $renewalDate ? date('Y-m-d', $renewalDate) : '';
}

private function getValidMembershipsForCertificate(int $certificateId, int $contactId): array {
Expand Down
6 changes: 4 additions & 2 deletions CRM/Certificate/Token/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ private function resolveFields(TokenValueEvent $e, $case, &$resolvedTokens) {
}

$caseStatus = CRM_Case_PseudoConstant::caseStatus('label', TRUE, 'AND value = ' . $case['status_id']);
$startDate = CRM_Utils_Array::value('start_date', $case, '');
$endDate = CRM_Utils_Array::value('end_date', $case, '');

$resolvedTokens['id'] = CRM_Utils_Array::value('id', $case, '');
$resolvedTokens['subject'] = CRM_Utils_Array::value('subject', $case, '');
$resolvedTokens['start_date'] = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('start_date', $case, ''));
$resolvedTokens['end_date'] = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('end_date', $case, ''));
$resolvedTokens['start_date'] = !empty($startDate) ? new \DateTime($startDate) : '';
$resolvedTokens['end_date'] = !empty($endDate) ? new \DateTime($endDate) : '';
$resolvedTokens['created_date'] = CRM_Utils_Date::customFormat(CRM_Utils_Array::value('created_date', $case, ''));
$resolvedTokens['role'] = $role;
$resolvedTokens['status'] = array_pop($caseStatus) ?? '';
Expand Down
11 changes: 6 additions & 5 deletions CRM/Certificate/Token/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,22 @@ private function resolveFields($certificate, &$resolvedTokens, $e) {
$membershipEndTimestamp = !empty($membershipDates['endDate']) ? strtotime($membershipDates['endDate']) : '';
$certificateValidityStartTimestamp = !empty($certificate->min_valid_from_date) ? strtotime($certificate->min_valid_from_date) : '';
$certificateValidityEndTimestamp = !empty($certificate->max_valid_through_date) ? strtotime($certificate->max_valid_through_date) : '';
$membershipRenewalDate = $service->getMembershipRenewalDate($certificate->id, $contactId);

$resolvedTokens['rolling_start_or_renewal_date'] = $service->getMembershipRenewalDate($certificate->id, $contactId);
$resolvedTokens['rolling_start_or_renewal_date'] = !empty($membershipRenewalDate) ? new \DateTime($membershipRenewalDate) : '';
$validityStartDate = empty($certificateValidityStartTimestamp) || $membershipStartTimestamp > $certificateValidityStartTimestamp ?
$membershipDates['startDate'] : (string) $certificate->min_valid_from_date;
$validityEndDate = empty($certificateValidityEndTimestamp) || (!empty($membershipEndTimestamp) && $certificateValidityEndTimestamp > $membershipEndTimestamp) ?
$membershipDates['endDate'] : (string) $certificate->max_valid_through_date;
$resolvedTokens['valid_from'] = !empty($validityStartDate)
? CRM_Utils_Date::customFormat($validityStartDate, '%e/%b/%Y') : '';
? new \DateTime($validityStartDate) : '';
$resolvedTokens['valid_to'] = !empty($validityEndDate)
? CRM_Utils_Date::customFormat($validityEndDate, '%e/%b/%Y') : '';
? new \DateTime($validityEndDate) : '';
}

$resolvedTokens['name'] = $certificate->name;
$resolvedTokens['start_date'] = CRM_Utils_Date::customFormat($certificate->start_date, '%e/%b/%Y');
$resolvedTokens['end_date'] = CRM_Utils_Date::customFormat($certificate->end_date, '%e/%b/%Y');
$resolvedTokens['start_date'] = new \DateTime($certificate->start_date);
$resolvedTokens['end_date'] = new \DateTime($certificate->end_date);
}

}
4 changes: 2 additions & 2 deletions CRM/Certificate/Token/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ private function getEventTokens($eventId) {
]);
$tokens['info_url']['text/html'] = \CRM_Utils_System::href('civicrm/event/info', 'reset=1&id=' . $eventId, TRUE, NULL, TRUE);
$tokens['registration_url']['text/html'] = \CRM_Utils_System::href('civicrm/event/register', 'reset=1&id=' . $eventId, TRUE, NULL, TRUE);
$tokens['start_date']['text/html'] = !empty($event['start_date']) ? CRM_Utils_Date::customFormat($event['start_date']) : '';
$tokens['end_date']['text/html'] = !empty($event['end_date']) ? CRM_Utils_Date::customFormat($event['end_date']) : '';
$tokens['start_date']['text/html'] = !empty($event['start_date']) ? new \DateTime($event['start_date']) : '';
$tokens['end_date']['text/html'] = !empty($event['end_date']) ? new \DateTime($event['end_date']) : '';
$tokens['event_type']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
$tokens['contact_phone']['text/html'] = $event['phone.phone'] ?? '';
$tokens['contact_email']['text/html'] = $event['email.email'] ?? '';
Expand Down
2 changes: 1 addition & 1 deletion CRM/Certificate/Token/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function resolveFields($membership, &$resolvedTokens) {
];

if (in_array($k, $dateFields)) {
$v = CRM_Utils_Date::customFormat($v);
$v = !empty($v) ? new \DateTime($v) : '';
}
});

Expand Down
2 changes: 1 addition & 1 deletion CRM/Certificate/Token/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function resolveFields($participant, &$resolvedTokens) {
];

if (in_array($k, $dateFields)) {
$v = CRM_Utils_Date::customFormat($v);
$v = !empty($v) ? new \DateTime($v) : '';
}

if (is_array($v)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testGenerateCertificateWillResolveEventTokenWithEmptySummaryFiel

$this->assertStringContainsString($contact["display_name"], $result["html"]);
$this->assertStringContainsString($event["title"], $result["html"]);
$this->assertStringContainsString(CRM_Utils_Date::customFormat($event["start_date"]), $result["html"]);
$this->assertStringContainsString(CRM_Utils_Date::customFormat($event["start_date"], NULL, ['d']), $result["html"]);
}

private function getMsgContent($extra = "") {
Expand Down
Loading