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-284: Handle Multiple Current Memberships #83

Merged
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @shahrukh-compuco Given that these new tokens are related to the membership entity, I believe they are better moved to and resolved in the membership token class https://github.com/compucorp/uk.co.compucorp.certificate/blob/master/CRM/Certificate/Token/Membership.php

Copy link
Contributor Author

@shahrukh-compuco shahrukh-compuco Nov 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Ola
As discussed that there might be a new ticket from Pallavi to transfer all the work to membership type only, I think then not only this but other work in this workstream will be transffered at once to the membership classes

$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) {
shahrukh-compuco marked this conversation as resolved.
Show resolved Hide resolved
$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
Loading