Skip to content

Commit

Permalink
Merge pull request #83 from compucorp/MMB-284-multiple-current-member…
Browse files Browse the repository at this point in the history
…ships

MMB-284: Handle Multiple Current Memberships
  • Loading branch information
shahrukh-compuco authored Dec 5, 2023
2 parents 31c2d35 + 66dea7d commit e37e80f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
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
58 changes: 56 additions & 2 deletions CRM/Certificate/Token/Certificate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Civi\Token\Event\TokenValueEvent;
use Civi\Api4\Membership;

/**
* Class CRM_Certificate_Token_Certificate
Expand All @@ -13,6 +14,8 @@ class CRM_Certificate_Token_Certificate extends CRM_Certificate_Token_AbstractCe

const TOKEN = 'certificate';

private const MEMBERSHIP_STATUS_CURRENT = 2;

/**
* Here we define list of standard certificate configuration fields
* that are supported as tokens.
Expand All @@ -21,6 +24,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 +61,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 +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') : '';
}

}
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

0 comments on commit e37e80f

Please sign in to comment.