Skip to content

Commit

Permalink
Merge pull request #90 from compucorp/MMB-78-workstream
Browse files Browse the repository at this point in the history
MMB-78: Merge workstream branch
  • Loading branch information
olayiwola-compucorp authored Feb 21, 2024
2 parents dd2c233 + b15c1f8 commit 20d3c8c
Show file tree
Hide file tree
Showing 26 changed files with 568 additions and 40 deletions.
26 changes: 26 additions & 0 deletions CRM/Certificate/BAO/CompuCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use CRM_Certificate_ExtensionUtil as E;
use CRM_Contact_BAO_Relationship as Relationship;
use CRM_Certificate_Enum_DownloadType as DownloadType;
use CRM_Certificate_Enum_DownloadFormat as DownloadFormat;
use CRM_Certificate_BAO_CompuCertificateStatus as CompuCertificateStatus;
use CRM_Certificate_BAO_CompuCertificateEntityType as CompuCertificateEntityType;
Expand Down Expand Up @@ -35,6 +36,8 @@ public static function deleteById($id) {
$entityName = 'CompuCertificate';

CRM_Utils_Hook::pre('delete', $entityName, $id);
\CRM_Core_BAO_File::deleteEntityFile(self::getTableName(), $id);

$instance = new $className();
$instance->id = $id;
$instance->delete();
Expand Down Expand Up @@ -114,6 +117,18 @@ public static function getSupportedDownloadFormats() {
];
}

/**
* Returns the supported download types.
*
* @return array
*/
public static function getSupportedDownloadTypes() {
return [
DownloadType::TEMPLATE => E::ts('Message Template'),
DownloadType::FILE_DOWNLOAD => E::ts('Download File'),
];
}

/**
* Add condition to ensure the certificate start_date and end_date is valid.
*/
Expand All @@ -134,4 +149,15 @@ public function getRelationshipTypes(string $column = NULL) {
return $relationshipTypes;
}

/**
* Returens the certificate file
*
* @param int $id
*
* @return array
*/
public static function getFile($id) {
return \CRM_Core_BAO_File::getEntityFile(CRM_Certificate_DAO_CompuCertificate::getTableName(), $id);
}

}
102 changes: 94 additions & 8 deletions 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 @@ -34,52 +34,89 @@ class CRM_Certificate_DAO_CompuCertificate extends CRM_Core_DAO {
/**
* Unique CompuCertificate ID
*
* @var int
* @var int|string|null
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $id;

/**
* Certificate name
*
* @var string
* (SQL type: varchar(255))
* Note that values will be retrieved from the database as a string.
*/
public $name;

/**
* Predefined CompuCertificate Entity ID (1 - CASE, 2 - MEMBERSHIP, 3 - EVENT)
*
* @var int
* @var int|string
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $entity;

/**
* FK to message template
*
* @var int
* @var int|string
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $template_id;

/**
* Predefined CompuCertificate download format (1 - PDF, 2 - IMAGE)
*
* @var int
* @var int|string
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $download_format;

/**
* Predefined CompuCertificate download type (1 - MESSAGE TEMPLATE, 2 - FILE)
*
* @var int|string
* (SQL type: int unsigned)
* Note that values will be retrieved from the database as a string.
*/
public $download_type;

/**
* Date the certificate validity starts
*
* @var date
* @var string
* (SQL type: date)
* Note that values will be retrieved from the database as a string.
*/
public $start_date;

/**
* Date the certificate validity ends
*
* @var date
* @var string
* (SQL type: date)
* Note that values will be retrieved from the database as a string.
*/
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 @@ -175,7 +212,7 @@ public static function &fields() {
'name' => 'template_id',
'type' => CRM_Utils_Type::T_INT,
'description' => E::ts('FK to message template'),
'required' => TRUE,
'required' => FALSE,
'where' => 'compucertificate_certificate.template_id',
'table_name' => 'compucertificate_certificate',
'entity' => 'CompuCertificate',
Expand Down Expand Up @@ -203,6 +240,23 @@ public static function &fields() {
],
'add' => NULL,
],
'download_type' => [
'name' => 'download_type',
'type' => CRM_Utils_Type::T_INT,
'title' => E::ts('Download Type'),
'description' => E::ts('Predefined CompuCertificate download type (1 - MESSAGE TEMPLATE, 2 - FILE)'),
'required' => FALSE,
'where' => 'compucertificate_certificate.download_type',
'default' => '1',
'table_name' => 'compucertificate_certificate',
'entity' => 'CompuCertificate',
'bao' => 'CRM_Certificate_DAO_CompuCertificate',
'localizable' => 0,
'html' => [
'type' => 'Number',
],
'add' => NULL,
],
'start_date' => [
'name' => 'start_date',
'type' => CRM_Utils_Type::T_DATE,
Expand Down Expand Up @@ -235,6 +289,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
19 changes: 18 additions & 1 deletion CRM/Certificate/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public function getCertificateConfigurationById($certificateId) {
'type' => $certificateBAO->entity,
'end_date' => $certificateBAO->end_date,
'start_date' => $certificateBAO->start_date,
'download_type' => $certificateBAO->download_type,
'download_format' => $certificateBAO->download_format,
'message_template_id' => $certificateBAO->template_id,
'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 Expand Up @@ -110,7 +113,7 @@ public function getCertificateConfiguration($entityId, $contactId) {
$certificateBAO->selectAdd(CRM_Certificate_DAO_CompuCertificate::$_tableName . '.id');
$certificateBAO->find(TRUE);

if (!empty($certificateBAO->id)) {
if (!empty($certificateBAO->id) && $this->isCertificateValidForAnEntity($certificateBAO, $contactId)) {
return $certificateBAO;
}
}
Expand Down Expand Up @@ -161,6 +164,20 @@ abstract protected function addEntityConditionals($certificateBAO, $entityId, $c
*/
protected function addEntityExtraField($certificateBAO, &$certificate) {}

/**
* Validate a certificate against entity specific checks.
*
* @param \CRM_Certificate_BAO_CompuCertificate $certificate
* Certificate.
* @param int $contactId
* Contact id.
*
* @return bool
*/
protected function isCertificateValidForAnEntity(\CRM_Certificate_BAO_CompuCertificate $certificate, int $contactId) {
return TRUE;
}

/**
* Gets all entity certificates vailable for a contact.
*
Expand Down
10 changes: 10 additions & 0 deletions CRM/Certificate/Entity/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ public function getEntity() {
return CertificateType::MEMBERSHIPS;
}

/**
* @inheritDoc
*/
protected function isCertificateValidForAnEntity(\CRM_Certificate_BAO_CompuCertificate $certificate, int $contactId) {
$membershipDates = (new CRM_Certificate_Service_CertificateMembership())->getMembershipDates($certificate->id, $contactId);

return ($membershipDates['startDate'] === NULL || $certificate->max_valid_through_date === NULL || $membershipDates['startDate'] <= $certificate->max_valid_through_date)
&& ($membershipDates['endDate'] === NULL || $certificate->min_valid_from_date === NULL || $membershipDates['endDate'] >= $certificate->min_valid_from_date);
}

}
14 changes: 14 additions & 0 deletions CRM/Certificate/Enum/DownloadType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* Declares the constants for supported download formats.
*
* This is different from DownloadFormat, as this specifies
* the content that will be returned to the user.
*/
class CRM_Certificate_Enum_DownloadType {

const TEMPLATE = '1';
const FILE_DOWNLOAD = '2';

}
Loading

0 comments on commit 20d3c8c

Please sign in to comment.