diff --git a/CRM/Certificate/Entity/AbstractEntity.php b/CRM/Certificate/Entity/AbstractEntity.php index 941a002..1c0548a 100644 --- a/CRM/Certificate/Entity/AbstractEntity.php +++ b/CRM/Certificate/Entity/AbstractEntity.php @@ -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')), diff --git a/CRM/Certificate/Enum/DownloadType.php b/CRM/Certificate/Enum/DownloadType.php new file mode 100644 index 0000000..8f4a11f --- /dev/null +++ b/CRM/Certificate/Enum/DownloadType.php @@ -0,0 +1,14 @@ +_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); @@ -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 -'), @@ -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', @@ -119,7 +139,6 @@ public function buildQuickForm() { 'download_format', ts('Download Format'), CompuCertificate::getSupportedDownloadFormats(), - TRUE, ['class' => 'form-control'] ); @@ -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'); @@ -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) { @@ -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. * diff --git a/CRM/Certificate/Service/Certificate.php b/CRM/Certificate/Service/Certificate.php index 4e3d188..1984f5a 100644 --- a/CRM/Certificate/Service/Certificate.php +++ b/CRM/Certificate/Service/Certificate.php @@ -1,5 +1,6 @@ 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, diff --git a/CRM/Certificate/Test/Fabricator/CompuCertificate.php b/CRM/Certificate/Test/Fabricator/CompuCertificate.php index 70750aa..d65c9e2 100644 --- a/CRM/Certificate/Test/Fabricator/CompuCertificate.php +++ b/CRM/Certificate/Test/Fabricator/CompuCertificate.php @@ -1,5 +1,6 @@ $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"), diff --git a/templates/CRM/Certificate/Form/CertificateConfigure.tpl b/templates/CRM/Certificate/Form/CertificateConfigure.tpl index 32e1763..3b95b0c 100644 --- a/templates/CRM/Certificate/Form/CertificateConfigure.tpl +++ b/templates/CRM/Certificate/Form/CertificateConfigure.tpl @@ -34,6 +34,7 @@ const TYPE_CASES = "1"; const TYPE_EVENTS = "2"; const FORMAT_IMAGE = "2"; + const TYPE_TEMPLATE = "1"; { literal } @@ -102,6 +103,30 @@ } }); + 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(' *'); + } + if (!$('.message_template_id label > span.crm-marker').length) { + $('.message_template_id label ').append(' *'); + } + }else { + $('.download_file').show(); + $('.download_format').hide(); + $('.message_template_id').hide(); + + if (!$('.download_file label > span.crm-marker').length) { + $('.download_file label ').append(' *'); + } + } + }); + + //this is to trigger the entity ref, when value of certifcate type is set from the backend if ($('[name=type]')[0].value) { performingUpdate = true; @@ -109,6 +134,7 @@ } CRM.$('#download_format').change(); + CRM.$('[name=download_type]').change(); }); { /literal}