Skip to content

Commit

Permalink
MMB-282: Allow user to specify certificate download file
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Dec 6, 2023
1 parent 8e0666c commit 4fb9053
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 4 deletions.
1 change: 1 addition & 0 deletions CRM/Certificate/Entity/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ 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')),
Expand Down
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';

}
63 changes: 59 additions & 4 deletions CRM/Certificate/Form/CertificateConfigure.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use CRM_Certificate_ExtensionUtil as E;
use CRM_Certificate_Enum_DownloadType as DownloadType;
use CRM_Certificate_BAO_CompuCertificate as CompuCertificate;

/**
Expand All @@ -10,6 +11,12 @@
*/
class CRM_Certificate_Form_CertificateConfigure extends CRM_Core_Form {

/**
* @var int
* Certificate ID
*/
public $_id;

public function preProcess() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);

Expand Down Expand Up @@ -69,6 +76,15 @@ public function buildQuickForm() {
FALSE
);

$this->add(
'select',
'download_type',
ts('Certificate Type'),
CompuCertificate::getSupportedDownloadTypes(),
TRUE,
['class' => 'form-control']
);

$this->addEntityRef('message_template_id', ts('Message Template'), [
'entity' => 'MessageTemplate',
'placeholder' => ts('- Message Template -'),
Expand All @@ -82,7 +98,11 @@ public function buildQuickForm() {
"search_field" => "msg_title",
],
'class' => 'form-control',
], TRUE);
]);

$safeExtensions = implode(', ', array_keys(CRM_Core_OptionGroup::values('safe_file_extension', TRUE)));
$this->addElement('file', 'download_file', E::ts('File Upload'), 'size=30 maxlength=255 class=form-control accept="' . $safeExtensions . '"');
$this->addUploadElement('download_file');

$this->add(
'text',
Expand Down Expand Up @@ -119,7 +139,6 @@ public function buildQuickForm() {
'download_format',
ts('Download Format'),
CompuCertificate::getSupportedDownloadFormats(),
TRUE,
['class' => 'form-control']
);

Expand Down Expand Up @@ -192,8 +211,9 @@ public function getRenderableElementNames() {
*/
public function postProcess() {
$values = $this->exportValues();
$files = $this->getVar('_submitFiles');

$result = $this->saveConfiguration($values);
$result = $this->saveConfiguration(array_merge($values, $files));

if (empty($result)) {
$msg = sprintf('Error %s certificate', !empty($this->_id) ? 'updating' : 'creating');
Expand Down Expand Up @@ -269,16 +289,18 @@ public function addRules() {
* translates to all.
*
* @param array $values
* @param array $files
*
* @return array|bool
*/
public function certificateRule($values) {
public function certificateRule($values, $files) {
$errors = [];

$this->validateCertificateName($values, $errors);
$this->validateLinkedToField($values, $errors);
$this->validateStatusesField($values, $errors);
$this->validateDateFields($values, $errors);
$this->validateDownloadType($values, $files, $errors);

// The participant_type field should only be validated for Event Certificate.
if ($values['type'] == CRM_Certificate_Enum_CertificateType::EVENTS) {
Expand All @@ -288,6 +310,39 @@ public function certificateRule($values) {
return $errors ?: TRUE;
}

/**
* Validates the download type related fields.
*
* @param array $values
* @param array $files
* @param array $errors
*/
public function validateDownloadType($values, $files, &$errors) {
if ($values['download_type'] == DownloadType::TEMPLATE) {
if (empty($values['download_format'])) {
$errors['download_format'] = ts('The download format field is required');
}
if (empty($values['message_template_id'])) {
$errors['message_template_id'] = ts('The message template field is required');
}
}
else {
$ext = CRM_Utils_File::getAcceptableExtensionsForMimeType($files['download_file']['type'])[0] ?? NULL;
if (!empty($files['download_file']['tmp_name']) &&
!CRM_Utils_File::isExtensionSafe($ext)
) {
$errors['download_file'] = ts('Invalid file format');
}
elseif (empty($files['download_file']['tmp_name']) && !empty($this->_id) && !empty(CompuCertificate::getFile($this->_id))) {
// It's an update and already linked to a file.
return;
}
elseif (empty($files['download_file']['tmp_name'])) {
$errors['download_file'] = ts('The download file field is required');
}
}
}

/**
* Validates the statuses field.
*
Expand Down
30 changes: 30 additions & 0 deletions CRM/Certificate/Service/Certificate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use CRM_Certificate_Enum_DownloadType as DownloadType;
use CRM_Certificate_Enum_DownloadFormat as DownloadFormat;

class CRM_Certificate_Service_Certificate {
Expand Down Expand Up @@ -31,6 +32,7 @@ public function store($values) {
$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_type'] = $values['download_type'] ?? DownloadType::TEMPLATE;
$params['download_format'] = $values['download_format'] ?? DownloadFormat::PDF;

$statuses = (array) $values['statuses'];
Expand All @@ -44,11 +46,39 @@ public function store($values) {
$result['relationshipTypes'] = CRM_Certificate_BAO_CompuCertificateRelationshipType::assignCertificateRelationshipTypes($result['certificate'], $relationshipTypes);

$this->storeExtraValues($result, $values);
$this->storeFile($values, $result['certificate']->id);
});

return $result;
}

public function storeFile($values, $id) {
if (empty($values['download_type']) || $values['download_type'] !== DownloadType::FILE_DOWNLOAD) {
return;
}

if (empty($values['download_file'])) {
return;
}

$ext = CRM_Utils_File::getAcceptableExtensionsForMimeType($values['download_file']['type'])[0];
$newPath = $values['download_file']['tmp_name'] . '.' . $ext;
rename($values['download_file']['tmp_name'], $newPath);
// Delete previously uploaded files if any
\CRM_Core_BAO_File::deleteEntityFile(CRM_Certificate_DAO_CompuCertificate::getTableName(), $id);
\CRM_Core_BAO_File::filePostProcess(
$newPath,
NULL,
CRM_Certificate_DAO_CompuCertificate::getTableName(),
$id,
NULL,
TRUE,
NULL,
'download_file',
$values['download_file']['type']
);
}

/**
* Checks if a configuration exists that
* satisfy the new configuration to be created,
Expand Down
2 changes: 2 additions & 0 deletions CRM/Certificate/Test/Fabricator/CompuCertificate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use CRM_Certificate_Enum_DownloadType as DownloadType;
use CRM_Certificate_Enum_DownloadFormat as DownloadFormat;

/**
Expand Down Expand Up @@ -89,6 +90,7 @@ public static function getDefaultParams() {
'name' => $name,
'message_template_id' => 1,
'downolad_format' => DownloadFormat::IMAGE,
'download_type' => DownloadType::TEMPLATE,
'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"),
Expand Down
26 changes: 26 additions & 0 deletions templates/CRM/Certificate/Form/CertificateConfigure.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
const TYPE_CASES = "1";
const TYPE_EVENTS = "2";
const FORMAT_IMAGE = "2";
const TYPE_TEMPLATE = "1";
{ literal }
Expand Down Expand Up @@ -102,13 +103,38 @@
}
});
CRM.$('[name=download_type]').on('change', function (e) {
if (e.target.value === TYPE_TEMPLATE) {
$('.download_file').hide();
$('.download_format').show();
$('.message_template_id').show();
if (!$('.download_format label > span.crm-marker').length) {
$('.download_format label ').append('<span class="crm-marker" title="This field is required."> *</span>');
}
if (!$('.message_template_id label > span.crm-marker').length) {
$('.message_template_id label ').append('<span class="crm-marker" title="This field is required."> *</span>');
}
}else {
$('.download_file').show();
$('.download_format').hide();
$('.message_template_id').hide();
if (!$('.download_file label > span.crm-marker').length) {
$('.download_file label ').append('<span class="crm-marker" title="This field is required."> *</span>');
}
}
});
//this is to trigger the entity ref, when value of certifcate type is set from the backend
if ($('[name=type]')[0].value) {
performingUpdate = true;
$('[name=type]').change();
}
CRM.$('#download_format').change();
CRM.$('[name=download_type]').change();
});
{ /literal}
Expand Down

0 comments on commit 4fb9053

Please sign in to comment.