diff --git a/CRM/Certificate/Token/AbstractCertificateToken.php b/CRM/Certificate/Token/AbstractCertificateToken.php index 9bfe5c8..55a33ca 100644 --- a/CRM/Certificate/Token/AbstractCertificateToken.php +++ b/CRM/Certificate/Token/AbstractCertificateToken.php @@ -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); } } diff --git a/CRM/Certificate/Token/Certificate.php b/CRM/Certificate/Token/Certificate.php index 48ded23..bf4b641 100644 --- a/CRM/Certificate/Token/Certificate.php +++ b/CRM/Certificate/Token/Certificate.php @@ -1,6 +1,7 @@ "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 = []) { @@ -55,7 +61,7 @@ public function prefetch(TokenValueEvent $e) { return $resolvedTokens; } - $this->resolveFields($certificate, $resolvedTokens); + $this->resolveFields($certificate, $this->getMembershipDates($e), $resolvedTokens); } } catch (Exception $e) { @@ -65,16 +71,64 @@ public function prefetch(TokenValueEvent $e) { return $resolvedTokens; } + private function getMembershipDates(TokenValueEvent $e): array { + $startDate = NULL; + $endDate = NULL; + + $contactIds = $e->getTokenProcessor()->getContextValues('contactId'); + $contactId = (is_array($contactIds) && !empty($contactIds[0])) ? $contactIds[0] : 0; + + // get all memberships for given contact to get min start_date and max end_date of all memberships + $memberships = Membership::get(FALSE) + ->addSelect('start_date', 'end_date') + ->addWhere('contact_id', '=', $contactId) + ->addWhere('status_id', '=', self::MEMBERSHIP_STATUS_CURRENT) + ->execute() + ->getArrayCopy(); + + foreach ($memberships as $membership) { + $startDate = $startDate === NULL || strtotime($membership['start_date']) < strtotime($startDate) ? + $membership['start_date'] : $startDate; + $endDate = $endDate === NULL || strtotime($membership['end_date']) > strtotime($endDate) ? + $membership['end_date'] : $endDate; + } + + return ['start_date' => $startDate, 'end_date' => $endDate]; + } + /** * 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') : ''; } } diff --git a/tests/phpunit/CRM/Certificate/Token/CertificateTest.php b/tests/phpunit/CRM/Certificate/Token/CertificateTest.php index 48720e1..12ec408 100644 --- a/tests/phpunit/CRM/Certificate/Token/CertificateTest.php +++ b/tests/phpunit/CRM/Certificate/Token/CertificateTest.php @@ -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]); }); }