From 776731e4cc21d99590b96ceaad531c946d6187df Mon Sep 17 00:00:00 2001 From: shahrukh-moby Date: Thu, 2 Nov 2023 11:44:05 +0500 Subject: [PATCH 1/3] MMB-270: Introduce min and max validity dates for certificates --- CRM/Certificate/Upgrader.php | 10 ++++++++++ sql/auto_install.sql | 4 +++- sql/auto_uninstall.sql | 2 +- sql/upgrade_0003.sql | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 sql/upgrade_0003.sql diff --git a/CRM/Certificate/Upgrader.php b/CRM/Certificate/Upgrader.php index cb2e9eb..20678a9 100644 --- a/CRM/Certificate/Upgrader.php +++ b/CRM/Certificate/Upgrader.php @@ -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; + } + } diff --git a/sql/auto_install.sql b/sql/auto_install.sql index e80c5ed..91ac4b4 100644 --- a/sql/auto_install.sql +++ b/sql/auto_install.sql @@ -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 ) @@ -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` ( diff --git a/sql/auto_uninstall.sql b/sql/auto_uninstall.sql index 3e04d77..4f67503 100644 --- a/sql/auto_uninstall.sql +++ b/sql/auto_uninstall.sql @@ -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; \ No newline at end of file +SET FOREIGN_KEY_CHECKS=1; diff --git a/sql/upgrade_0003.sql b/sql/upgrade_0003.sql new file mode 100644 index 0000000..f1e1b8d --- /dev/null +++ b/sql/upgrade_0003.sql @@ -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'; From 4fc1c9e1ecfe17d0137d9119c7f1839aa316c0ce Mon Sep 17 00:00:00 2001 From: shahrukh-moby Date: Thu, 2 Nov 2023 11:45:00 +0500 Subject: [PATCH 2/3] MMB-270: Add min and max date fields to certificate forms --- CRM/Certificate/DAO/CompuCertificate.php | 48 +++++++++++++++++- CRM/Certificate/Entity/AbstractEntity.php | 2 + CRM/Certificate/Form/CertificateConfigure.php | 33 +++++++++--- CRM/Certificate/Service/Certificate.php | 2 + .../Certificate/Form/CertificateConfigure.hlp | 50 +++++++++++++++++++ .../CRM/Certificate/CompuCertificate.xml | 20 ++++++++ 6 files changed, 146 insertions(+), 9 deletions(-) diff --git a/CRM/Certificate/DAO/CompuCertificate.php b/CRM/Certificate/DAO/CompuCertificate.php index af14213..7a081c2 100644 --- a/CRM/Certificate/DAO/CompuCertificate.php +++ b/CRM/Certificate/DAO/CompuCertificate.php @@ -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; @@ -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. */ @@ -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']); } diff --git a/CRM/Certificate/Entity/AbstractEntity.php b/CRM/Certificate/Entity/AbstractEntity.php index d6f6486..941a002 100644 --- a/CRM/Certificate/Entity/AbstractEntity.php +++ b/CRM/Certificate/Entity/AbstractEntity.php @@ -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); diff --git a/CRM/Certificate/Form/CertificateConfigure.php b/CRM/Certificate/Form/CertificateConfigure.php index 67f6cbf..04365d7 100644 --- a/CRM/Certificate/Form/CertificateConfigure.php +++ b/CRM/Certificate/Form/CertificateConfigure.php @@ -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', @@ -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()); @@ -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) { @@ -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'); } } diff --git a/CRM/Certificate/Service/Certificate.php b/CRM/Certificate/Service/Certificate.php index 54a81aa..4e3d188 100644 --- a/CRM/Certificate/Service/Certificate.php +++ b/CRM/Certificate/Service/Certificate.php @@ -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; diff --git a/templates/CRM/Certificate/Form/CertificateConfigure.hlp b/templates/CRM/Certificate/Form/CertificateConfigure.hlp index c44e534..aff6fdc 100644 --- a/templates/CRM/Certificate/Form/CertificateConfigure.hlp +++ b/templates/CRM/Certificate/Form/CertificateConfigure.hlp @@ -6,3 +6,53 @@

{ts}Note: This setting has security implications.
System should allow the contacts having this permission to access and download the certificates.{/ts}

{/htxt} + +{htxt id="min_valid_from_date-title"} + {ts}Min Valid From Date{/ts} +{/htxt} +{htxt id="min_valid_from_date"} +

{ts}The Min and Max Valid Date fields do 2 things: {/ts}

+
    +
  1. +

    {ts}Tokens: They will create tokens that can be used on certificates as follows:{/ts}

    +
      +
    • +

      {ts}'Min Valid From Date' (certificate.valid_from){/ts}

      +

      {ts}The latter of 1. Min Valid From Date and 2. The users membership start date.{/ts}

      +
    • +
    • +

      {ts}'Max Valid Through Date' (certificate.valid_to){/ts}

      +

      {ts}The earlier of 1. The date entered in this field and 2. The users membership end date.{/ts}

      +
    • +
    +
  2. +
  3. +

    {ts}Access: 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}

    +
  4. +
      +{/htxt} + +{htxt id="max_valid_through_date-title"} + {ts}Max Valid Through Date{/ts} +{/htxt} +{htxt id="max_valid_through_date"} +

      {ts}The Min and Max Valid Date fields do 2 things: {/ts}

      +
        +
      1. +

        {ts}Tokens: They will create tokens that can be used on certificates as follows:{/ts}

        +
          +
        • +

          {ts}'Min Valid From Date' (certificate.valid_from){/ts}

          +

          {ts}The latter of 1. Min Valid From Date and 2. The users membership start date.{/ts}

          +
        • +
        • +

          {ts}'Max Valid Through Date' (certificate.valid_to){/ts}

          +

          {ts}The earlier of 1. The date entered in this field and 2. The users membership end date.{/ts}

          +
        • +
        +
      2. +
      3. +

        {ts}Access: 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}

        +
      4. +
          +{/htxt} \ No newline at end of file diff --git a/xml/schema/CRM/Certificate/CompuCertificate.xml b/xml/schema/CRM/Certificate/CompuCertificate.xml index 2cf8c3a..2457ab6 100644 --- a/xml/schema/CRM/Certificate/CompuCertificate.xml +++ b/xml/schema/CRM/Certificate/CompuCertificate.xml @@ -83,6 +83,26 @@ + + min_valid_from_date + date + false + Min date the certificate validity starts + + Select Date + + + + + max_valid_through_date + date + false + Max date the certificate validity ends + + Select Date + + + template_id civicrm_msg_template
          From 23e32fb517098a495cfe7d235ad6781fd6d6f87e Mon Sep 17 00:00:00 2001 From: shahrukh-moby Date: Thu, 2 Nov 2023 11:46:34 +0500 Subject: [PATCH 3/3] MMB-270: Add min and max dates to test fabricators --- CRM/Certificate/Test/Fabricator/CompuCertificate.php | 2 ++ CRM/Certificate/Test/Fabricator/Event.php | 2 ++ CRM/Certificate/Test/Fabricator/Membership.php | 2 ++ CRM/Certificate/Test/Fabricator/Relationship.php | 2 ++ CRM/Certificate/Test/Helper/Case.php | 2 ++ CRM/Certificate/Test/Helper/Event.php | 2 ++ CRM/Certificate/Test/Helper/Membership.php | 2 ++ 7 files changed, 14 insertions(+) diff --git a/CRM/Certificate/Test/Fabricator/CompuCertificate.php b/CRM/Certificate/Test/Fabricator/CompuCertificate.php index 4fef211..70750aa 100644 --- a/CRM/Certificate/Test/Fabricator/CompuCertificate.php +++ b/CRM/Certificate/Test/Fabricator/CompuCertificate.php @@ -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' => [], ]; } diff --git a/CRM/Certificate/Test/Fabricator/Event.php b/CRM/Certificate/Test/Fabricator/Event.php index a1d930a..d71c3fc 100644 --- a/CRM/Certificate/Test/Fabricator/Event.php +++ b/CRM/Certificate/Test/Fabricator/Event.php @@ -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()), diff --git a/CRM/Certificate/Test/Fabricator/Membership.php b/CRM/Certificate/Test/Fabricator/Membership.php index d31b6fd..02a36ba 100644 --- a/CRM/Certificate/Test/Fabricator/Membership.php +++ b/CRM/Certificate/Test/Fabricator/Membership.php @@ -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, ]; diff --git a/CRM/Certificate/Test/Fabricator/Relationship.php b/CRM/Certificate/Test/Fabricator/Relationship.php index b50fa37..f96acf5 100644 --- a/CRM/Certificate/Test/Fabricator/Relationship.php +++ b/CRM/Certificate/Test/Fabricator/Relationship.php @@ -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 = []) { diff --git a/CRM/Certificate/Test/Helper/Case.php b/CRM/Certificate/Test/Helper/Case.php index 427789d..a5422f9 100644 --- a/CRM/Certificate/Test/Helper/Case.php +++ b/CRM/Certificate/Test/Helper/Case.php @@ -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")), ]; diff --git a/CRM/Certificate/Test/Helper/Event.php b/CRM/Certificate/Test/Helper/Event.php index 556399b..09cd2ce 100644 --- a/CRM/Certificate/Test/Helper/Event.php +++ b/CRM/Certificate/Test/Helper/Event.php @@ -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); diff --git a/CRM/Certificate/Test/Helper/Membership.php b/CRM/Certificate/Test/Helper/Membership.php index 941fb12..16c16e5 100644 --- a/CRM/Certificate/Test/Helper/Membership.php +++ b/CRM/Certificate/Test/Helper/Membership.php @@ -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);