Skip to content

Commit

Permalink
Merge branch 'MMB-78-workstream' of github.com:compucorp/uk.co.compuc…
Browse files Browse the repository at this point in the history
…orp.certificate into MMB-78-workstream
  • Loading branch information
shahrukh-moby committed Nov 6, 2023
2 parents 39a67e5 + bfcf3a5 commit 6228ebd
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 11 deletions.
48 changes: 47 additions & 1 deletion CRM/Certificate/DAO/CompuCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from uk.co.compucorp.certificate/xml/schema/CRM/Certificate/CompuCertificate.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:92a589ff877665033271ff1e67bee30e)
* (GenCodeChecksum:18ec0a71f942b6ee070017e3e236afb5)
*/
use CRM_Certificate_ExtensionUtil as E;

Expand Down Expand Up @@ -80,6 +80,20 @@ class CRM_Certificate_DAO_CompuCertificate extends CRM_Core_DAO {
*/
public $end_date;

/**
* Min date the certificate validity starts
*
* @var date
*/
public $min_valid_from_date;

/**
* Max date the certificate validity ends
*
* @var date
*/
public $max_valid_through_date;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -235,6 +249,38 @@ public static function &fields() {
],
'add' => NULL,
],
'min_valid_from_date' => [
'name' => 'min_valid_from_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => E::ts('Min Valid From Date'),
'description' => E::ts('Min date the certificate validity starts'),
'required' => FALSE,
'where' => 'compucertificate_certificate.min_valid_from_date',
'table_name' => 'compucertificate_certificate',
'entity' => 'CompuCertificate',
'bao' => 'CRM_Certificate_DAO_CompuCertificate',
'localizable' => 0,
'html' => [
'type' => 'Select Date',
],
'add' => NULL,
],
'max_valid_through_date' => [
'name' => 'max_valid_through_date',
'type' => CRM_Utils_Type::T_DATE,
'title' => E::ts('Max Valid Through Date'),
'description' => E::ts('Max date the certificate validity ends'),
'required' => FALSE,
'where' => 'compucertificate_certificate.max_valid_through_date',
'table_name' => 'compucertificate_certificate',
'entity' => 'CompuCertificate',
'bao' => 'CRM_Certificate_DAO_CompuCertificate',
'localizable' => 0,
'html' => [
'type' => 'Select Date',
],
'add' => NULL,
],
];
CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'fields_callback', Civi::$statics[__CLASS__]['fields']);
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function getCertificateConfigurationById($certificateId) {
'linked_to' => implode(',', array_column($types, 'id')),
'statuses' => implode(',', array_column($statuses, 'id')),
'relationship_types' => implode(',', array_column($relationshipTypes, 'relationship_type_id')),
'min_valid_from_date' => $certificateBAO->min_valid_from_date,
'max_valid_through_date' => $certificateBAO->max_valid_through_date,
];

$this->addEntityExtraField($certificateBAO, $certificate);
Expand Down
33 changes: 25 additions & 8 deletions CRM/Certificate/Form/CertificateConfigure.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ public function buildQuickForm() {
'class' => 'form-control',
], FALSE);

$this->add(
'datepicker',
'min_valid_from_date',
ts('Min Valid From Date'),
NULL,
FALSE,
['time' => FALSE, 'class' => 'form-control']
);

$this->add(
'datepicker',
'max_valid_through_date',
ts('Max Valid Through Date'),
NULL,
FALSE,
['time' => FALSE, 'class' => 'form-control']
);

$this->addButtons([
[
'type' => 'submit',
Expand All @@ -143,7 +161,7 @@ public function buildQuickForm() {
],
]);

$elementWithHelpTexts = ['relationship_types'];
$elementWithHelpTexts = ['relationship_types', 'min_valid_from_date', 'max_valid_through_date'];

$this->assign('help', $elementWithHelpTexts);
$this->assign('elementNames', $this->getRenderableElementNames());
Expand Down Expand Up @@ -260,7 +278,7 @@ public function certificateRule($values) {
$this->validateCertificateName($values, $errors);
$this->validateLinkedToField($values, $errors);
$this->validateStatusesField($values, $errors);
$this->validateDateField($values, $errors);
$this->validateDateFields($values, $errors);

// The participant_type field should only be validated for Event Certificate.
if ($values['type'] == CRM_Certificate_Enum_CertificateType::EVENTS) {
Expand Down Expand Up @@ -325,14 +343,13 @@ public function validateParticipantTypeField($values, &$errors) {
* @param array $values
* @param array $errors
*/
public function validateDateField($values, &$errors) {
// Ignore validation if either of the dates are empty.
if (empty($values['end_date']) || empty($values['start_date'])) {
return;
public function validateDateFields($values, &$errors) {
if (!empty($values['start_date']) && !empty($values['end_date']) && strtotime($values['end_date']) <= strtotime($values['start_date'])) {
$errors['end_date'] = ts('End date field must be after start date');
}

if (strtotime($values['end_date']) <= strtotime($values['start_date'])) {
$errors['end_date'] = ts('End date field must be after start date');
if (!empty($values['max_valid_through_date']) && !empty($values['min_valid_from_date']) && strtotime($values['max_valid_through_date']) <= strtotime($values['min_valid_from_date'])) {
$errors['max_valid_through_date'] = ts('Max valid through date field must be after min valid from date');
}
}

Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Service/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public function store($values) {
$params['entity'] = $values['type'];
$params['end_date'] = $values['end_date'];
$params['start_date'] = $values['start_date'];
$params['min_valid_from_date'] = $values['min_valid_from_date'] ?? NULL;
$params['max_valid_through_date'] = $values['max_valid_through_date'] ?? NULL;
$params['template_id'] = $values['message_template_id'];
$params['download_format'] = $values['download_format'] ?? DownloadFormat::PDF;

Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Fabricator/CompuCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public static function getDefaultParams() {
'downolad_format' => DownloadFormat::IMAGE,
'start_date' => date("Y-m-d"),
'end_date' => date("Y-m-d", strtotime(date("Y-m-d") . " + 10 days")),
'min_valid_from_date' => date("Y-m-d"),
'max_valid_through_date' => date("Y-m-d", strtotime(date("Y-m-d") . " + 30 days")),
'relationship_types' => [],
];
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Fabricator/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public static function getDefaultParams() {
return [
'title' => md5(mt_rand()),
'start_date' => date('Y-m-d'),
'min_valid_from_date' => date("Y-m-d"),
'max_valid_through_date' => date("Y-m-d", strtotime(date("Y-m-d") . " + 30 days")),
'event_type_id' => 1,
'summary' => md5(mt_rand()),
'description' => md5(mt_rand()),
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Fabricator/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CRM_Certificate_Test_Fabricator_Membership {
'join_date' => '2006-01-21',
'start_date' => '2006-01-21',
'end_date' => '2006-12-21',
'min_valid_from_date' => '2006-02-01',
'max_valid_through_date' => '2006-06-05',
'source' => 'Payment',
'is_override' => 1,
];
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Fabricator/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CRM_Certificate_Test_Fabricator_Relationship {
'is_active' => 1,
'start_date' => NULL,
'end_date' => NULL,
'min_valid_from_date' => NULL,
'max_valid_through_date' => NULL,
];

public static function fabricate($params = []) {
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Helper/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private function createCaseCertificate($params = []) {
'linked_to' => [$caseType['id']],
'statuses' => [$caseStatus['value']],
'start_date' => $params['start_date'] ?? date('Y-m-d'),
'min_valid_from_date' => $params['min_valid_from_date'] ?? date("Y-m-d"),
'max_valid_through_date' => $params['max_valid_through_date'] ?? date("Y-m-d", strtotime(date("Y-m-d") . " + 30 days")),
'relationship_types' => $params['relationship_types'] ?? [],
'end_date' => $params['end_date'] ?? date('Y-m-d', strtotime(date('Y-m-d') . " 10 days")),
];
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Helper/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ private function createEventCertificate($params = []) {
'start_date' => date('Y-m-d'),
'relationship_types' => $params['relationship_types'] ?? [],
'end_date' => date('Y-m-d', strtotime(date('Y-m-d') . " 10 days")),
'min_valid_from_date' => date("Y-m-d"),
'max_valid_through_date' => date("Y-m-d", strtotime(date("Y-m-d") . " + 30 days")),
], $params);

return CompuCertificateFabricator::fabricate(CertificateType::EVENTS, $values);
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Helper/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ private function createMembershipCertificate($params = []) {
'start_date' => date('Y-m-d'),
'relationship_types' => $params['relationship_types'] ?? [],
'end_date' => date('Y-m-d', strtotime(date('Y-m-d') . " 10 days")),
'min_valid_from_date' => date("Y-m-d"),
'max_valid_through_date' => date("Y-m-d", strtotime(date("Y-m-d") . " + 30 days")),
], $params);

return CompuCertificateFabricator::fabricate(CertificateType::MEMBERSHIPS, $values);
Expand Down
10 changes: 10 additions & 0 deletions CRM/Certificate/Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,14 @@ public function upgrade_0002() {
return TRUE;
}

/**
* Adds min and max validity dates to certificate.
*/
public function upgrade_0003() {
$this->ctx->log->info('Applying update 0003');
$this->executeSqlFile('sql/upgrade_0003.sql');

return TRUE;
}

}
4 changes: 3 additions & 1 deletion sql/auto_install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ CREATE TABLE `compucertificate_certificate` (
`download_format` int unsigned NOT NULL COMMENT 'Predefined CompuCertificate download format (1 - PDF, 2 - IMAGE)',
`start_date` date NULL COMMENT 'Date the certificate validity starts',
`end_date` date NULL COMMENT 'Date the certificate validity ends',
`min_valid_from_date` date NULL COMMENT 'Min date the certificate validity starts',
`max_valid_through_date` date NULL COMMENT 'Max date the certificate validity ends',
PRIMARY KEY (`id`),
CONSTRAINT FK_compucertificate_certificate_template_id FOREIGN KEY (`template_id`) REFERENCES `civicrm_msg_template`(`id`) ON DELETE CASCADE
)
Expand All @@ -60,7 +62,7 @@ ENGINE=InnoDB;
-- *
-- * compucertificate_event_attribute
-- *
-- * Table to store attributes peculiar to CompuCertificate of type event
-- * Table to store attributes peculiar to CompuCertificate of type event
-- *
-- *******************************************************/
CREATE TABLE `compucertificate_event_attribute` (
Expand Down
2 changes: 1 addition & 1 deletion sql/auto_uninstall.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ DROP TABLE IF EXISTS `compucertificate_event_attribute`;
DROP TABLE IF EXISTS `compucertificate_certificate_entity_type`;
DROP TABLE IF EXISTS `compucertificate_certificate`;

SET FOREIGN_KEY_CHECKS=1;
SET FOREIGN_KEY_CHECKS=1;
2 changes: 2 additions & 0 deletions sql/upgrade_0003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `compucertificate_certificate` ADD COLUMN `min_valid_from_date` date NULL COMMENT 'Min date the certificate validity starts';
ALTER TABLE `compucertificate_certificate` ADD COLUMN `max_valid_through_date` date NULL COMMENT 'Max date the certificate validity ends';
50 changes: 50 additions & 0 deletions templates/CRM/Certificate/Form/CertificateConfigure.hlp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,53 @@
<p>{ts}<b>Note:</b> This setting has security implications.
<br/>System should allow the contacts having this permission to access and download the certificates.{/ts}</p>
{/htxt}

{htxt id="min_valid_from_date-title"}
{ts}Min Valid From Date{/ts}
{/htxt}
{htxt id="min_valid_from_date"}
<p>{ts}The Min and Max Valid Date fields do 2 things: {/ts}</p>
<ol>
<li>
<p>{ts}<b>Tokens:</b> They will create tokens that can be used on certificates as follows:{/ts}</p>
<ul>
<li>
<p>{ts}'Min Valid From Date' (certificate.valid_from){/ts}</p>
<p>{ts}The latter of 1. Min Valid From Date and 2. The users membership start date.{/ts}</p>
</li>
<li>
<p>{ts}'Max Valid Through Date' (certificate.valid_to){/ts}</p>
<p>{ts}The earlier of 1. The date entered in this field and 2. The users membership end date.{/ts}</p>
</li>
</ul>
</li>
<li>
<p>{ts}<b>Access:</b> It will also only allow users to download the certificate if their membership is valid during the validity period of the certificate. For example if a membership ends on or after the start date or a membership starts before the end date.{/ts}</p>
</li>
<ol>
{/htxt}

{htxt id="max_valid_through_date-title"}
{ts}Max Valid Through Date{/ts}
{/htxt}
{htxt id="max_valid_through_date"}
<p>{ts}The Min and Max Valid Date fields do 2 things: {/ts}</p>
<ol>
<li>
<p>{ts}<b>Tokens:</b> They will create tokens that can be used on certificates as follows:{/ts}</p>
<ul>
<li>
<p>{ts}'Min Valid From Date' (certificate.valid_from){/ts}</p>
<p>{ts}The latter of 1. Min Valid From Date and 2. The users membership start date.{/ts}</p>
</li>
<li>
<p>{ts}'Max Valid Through Date' (certificate.valid_to){/ts}</p>
<p>{ts}The earlier of 1. The date entered in this field and 2. The users membership end date.{/ts}</p>
</li>
</ul>
</li>
<li>
<p>{ts}<b>Access:</b> It will also only allow users to download the certificate if their membership is valid during the validity period of the certificate. For example if a membership ends on or after the start date or a membership starts before the end date.{/ts}</p>
</li>
<ol>
{/htxt}
20 changes: 20 additions & 0 deletions xml/schema/CRM/Certificate/CompuCertificate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@
</html>
</field>

<field>
<name>min_valid_from_date</name>
<type>date</type>
<required>false</required>
<comment>Min date the certificate validity starts</comment>
<html>
<type>Select Date</type>
</html>
</field>

<field>
<name>max_valid_through_date</name>
<type>date</type>
<required>false</required>
<comment>Max date the certificate validity ends</comment>
<html>
<type>Select Date</type>
</html>
</field>

<foreignKey>
<name>template_id</name>
<table>civicrm_msg_template</table>
Expand Down

0 comments on commit 6228ebd

Please sign in to comment.