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-273: Add Rolling Start Or Renewal Date Token #82

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
3 changes: 2 additions & 1 deletion CRM/Certificate/Token/AbstractCertificateToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL)
}

if ($value) {
$row->tokens($prefix, $field, $value);
$row->format('text/plain')->tokens($prefix, $field, $value);
$row->format('text/html')->tokens($prefix, $field, $value);
}
}

Expand Down
52 changes: 50 additions & 2 deletions CRM/Certificate/Token/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class CRM_Certificate_Token_Certificate extends CRM_Certificate_Token_AbstractCe
"name" => "Certificate Name",
"start_date" => "Certificate Start Date",
"end_date" => "Certificate End Date",
"valid_from" => "Certificate Valid From Date",
"valid_to" => "Certificate Valid To Date",
"rolling_start_or_renewal_date" => "Certificate Rolling Start Or Renewal Date",
];

public function __construct($tokenNames = []) {
Expand Down Expand Up @@ -55,7 +58,7 @@ public function prefetch(TokenValueEvent $e) {
return $resolvedTokens;
}

$this->resolveFields($certificate, $resolvedTokens);
$this->resolveFields($certificate, $this->getMembershipDates($e), $resolvedTokens);
}
}
catch (Exception $e) {
Expand All @@ -65,16 +68,61 @@ public function prefetch(TokenValueEvent $e) {
return $resolvedTokens;
}

private function getMembershipDates(TokenValueEvent $e): array {
$contactIds = $e->getTokenProcessor()->getContextValues('contactId');
$contactId = (is_array($contactIds) && !empty($contactIds[0])) ? $contactIds[0] : 0;

$membershipRows = civicrm_api3(
'membership',
'get',
[
'version' => 3,
'return' => ['start_date', 'end_date'],
'contact_id' => $contactId,
'status_id' => 'Current',
'sequential' => 1,
'options' => ['sort' => 'end_date desc', 'limit' => 1],
]
);

return !empty($membershipRows['values'][0])
? $membershipRows['values'][0]
: ['start_date' => '', 'end_date' => ''];
}

/**
* Resolve the value of ceritificate configuration token fields.
*
* @param CRM_Certificate_DAO_CompuCertificate $certificate
* @param array $membershipDates
* @param array &$resolvedTokens
*/
private function resolveFields($certificate, &$resolvedTokens) {
private function resolveFields($certificate, array $membershipDates, &$resolvedTokens) {
$membershipStartTimestamp = !empty($membershipDates['start_date']) ? strtotime($membershipDates['start_date']) : '';
$membershipEndTimestamp = !empty($membershipDates['end_date']) ? strtotime($membershipDates['end_date']) : '';
$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) : '';
$resolvedTokens['rolling_start_or_renewal_date'] = '';

if ($membershipStartTimestamp && $membershipEndTimestamp) {
$renewalTimestamp = strtotime($membershipDates['end_date'] . " -1 year 1 day");
$resolvedTokens['rolling_start_or_renewal_date'] = $renewalTimestamp > $membershipStartTimestamp
? CRM_Utils_Date::customFormat(date('Y-m-d', $renewalTimestamp), '%e/%b/%Y')
: CRM_Utils_Date::customFormat($membershipDates['start_date'], '%e/%b/%Y');
}

$validityStartDate = empty($certificateValidityStartTimestamp) || $membershipStartTimestamp > $certificateValidityStartTimestamp ?
$membershipDates['start_date'] : (string) $certificate->min_valid_from_date;
$validityEndDate = empty($certificateValidityEndTimestamp) || (!empty($membershipEndTimestamp) && $certificateValidityEndTimestamp > $membershipEndTimestamp) ?
$membershipDates['end_date'] : (string) $certificate->max_valid_through_date;

$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['valid_from'] = !empty($validityStartDate)
? CRM_Utils_Date::customFormat($validityStartDate, '%e/%b/%Y') : '';
$resolvedTokens['valid_to'] = !empty($validityEndDate)
? CRM_Utils_Date::customFormat($validityEndDate, '%e/%b/%Y') : '';
}

}
1 change: 0 additions & 1 deletion tests/phpunit/CRM/Certificate/Token/CertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function testCanResolveCertificateTokenFields() {
$this->assertTrue(is_array($prefetchedTokens));
array_walk($certificateFields, function ($key) use ($prefetchedTokens) {
$this->assertArrayHasKey($key, $prefetchedTokens);
$this->assertNotEmpty($prefetchedTokens[$key]);
});
}

Expand Down
Loading