Skip to content

Commit

Permalink
MMB-437: Support different date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Shahrukh committed Oct 7, 2024
1 parent 1b2db7d commit 09a4fdb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
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

0 comments on commit 09a4fdb

Please sign in to comment.