Skip to content
Snippets Groups Projects
Commit 272f7ff5 authored by Monish Deb's avatar Monish Deb
Browse files

QA fixes

parent 9f8f04f7
No related branches found
No related tags found
No related merge requests found
......@@ -9,37 +9,17 @@ use CRM_Certificates_ExtensionUtil as E;
class CRM_Certificates_Upgrader extends CRM_Extension_Upgrader_Base {
public function upgrade_1002() {
// check if the custom group exists
$customGroups = \Civi\Api4\CustomGroup::get(TRUE)
->addWhere('name', '=', 'Event_Certificate')
->setLimit(25)
->execute()
->first();
if (!$customGroup) {
// create the custom group
$customGroup = \Civi\Api4\CustomGroup::create(TRUE)
->addValue('title', 'Event Certificate')
->addValue('name', 'Event_Certificate')
->addValue('extends', 'Event')
->addValue('is_active', TRUE)
->execute()
->first();
}
$customGroupId = $customGroup['id'];
// check if the custom field exists
$customField = \Civi\Api4\CustomField::get(TRUE)
$customFieldID = \Civi\Api4\CustomField::get(FALSE)
->addWhere('name', '=', 'event_certificate')
->setLimit(25)
->addWhere('custom_group_id:name', '=', 'Event_Certificate')
->execute()
->first();
->first()['id'] ?? NULL;
if (!$customField) {
if (!$customFieldID) {
// create the custom field
\Civi\Api4\CustomField::create(TRUE)
->addValue('custom_group_id', $customGroupId)
\Civi\Api4\CustomField::create(FALSE)
->addValue('custom_group_id.name', 'Event_Certificate')
->addValue('label', 'Select Certificate for event')
->addValue('name', 'event_certificate')
->addValue('data_type', 'EntityReference')
......@@ -50,6 +30,6 @@ class CRM_Certificates_Upgrader extends CRM_Extension_Upgrader_Base {
->execute();
}
return true;
return TRUE;
}
}
......@@ -78,32 +78,26 @@ function certificates_civicrm_permission(array &$permissions): void {
function certificates_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'edit' && $objectName == 'Participant') {
$contactId = $params['contact_id'];
$contactID = $params['contact_id'];
try {
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Attended');
//get current participant status before data is changed and get event type
$participant = \Civi\Api4\Participant::get(FALSE)
->addSelect('status_id', 'id', 'event_id.Event_Certificate.event_certificate')
->addJoin('Event AS event', 'LEFT', ['event_id', '=', 'event.id'])
->addWhere('contact_id', '=', $contactId)
->addWhere('status_id:name', '=', 'Attended')
->execute()->first();
$participantId = $participant['id'];
// checks if participant status_id is now status id for Attended --> then send email
if($participant['status_id'] !== $statusId && $params['status_id'] === $statusId) {
$contact = \Civi\Api4\Contact::get(TRUE)
->addSelect('email_primary.email', 'display_name')
->addWhere('id', '=', $contactId)
->setLimit(25)
->execute()->first();
if(!empty($participant)) {
[$toDisplayName, $toEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
$participantInfo = [
'id' => $participantId,
'contactId' => $contactId,
'name' => $contact['display_name'],
'email' => $contact['email_primary.email'],
'certificateid' => $participant['event_id.Event_Certificate.event_certificate']
'id' => $participant['id'],
'contact_id' => $contactID,
'name' => $toDisplayName,
'email' => $toEmail,
'certificate_id' => $participant['event_id.Event_Certificate.event_certificate']
];
getEmailContent($participantInfo);
......@@ -119,19 +113,11 @@ function certificates_civicrm_pre($op, $objectName, $id, &$params) {
* Sends an email with a certificate attachment.
*/
function getEmailContent($participant) {
$mailAttachments = [];
try {
// searches for the certificate, if none found will fallback to default
$eventCertificateID = $participant['certificateid'];
if (!$eventCertificateID) {
// fallback to default
$messageTemplateID = \Civi::settings()->get('certificates_event_defaultmessagetemplateid');
} else {
$messageTemplateID = $eventCertificateID;
}
$messageTemplateID = $participant['certificate_id'] ?? \Civi::settings()->get('certificates_event_defaultmessagetemplateid');
if (empty($messageTemplateID)) {
\Civi::log()->error('Event attendance certificate has no message template ID set!');
......@@ -139,20 +125,18 @@ function getEmailContent($participant) {
}
// query to get the html of the message template given the message template id
$messageTemplate = \Civi\Api4\MessageTemplate::get(TRUE)
$messageTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_html')
->addWhere('id', '=', $messageTemplateID)
->setLimit(25)
->execute()->first();
$messageTemplateHTML = $messageTemplate['msg_html'];
->execute()
->first();
// add default for here
$emailTemplate = \Civi\Api4\MessageTemplate::get(TRUE)
$emailTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_html')
->addWhere('msg_title', '=', 'Certificate Email')
->setLimit(25)
->execute()->first();
->execute()
->first();
if (empty($emailTemplate)) {
// fallback to default email message template if 'Certificate Email' is not found
......@@ -162,7 +146,7 @@ function getEmailContent($participant) {
return;
}
$emailTemplate = \Civi\Api4\MessageTemplate::get(TRUE)
$emailTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_html')
->addWhere('id', '=', $defaultEmailTemplateID)
->setLimit(1)
......@@ -174,19 +158,16 @@ function getEmailContent($participant) {
return;
}
}
$emailTemplateHTML = $emailTemplate['msg_html'];
$emailSubject = $emailTemplate['msg_subject'];
try{
try {
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'controller' => __CLASS__,
'smarty' => FALSE,
'schema' => ['contactId', 'participantId'] // change depending on what other fields are present in template
]);
$tokenProcessor->addMessage('body_html', $messageTemplateHTML, 'text/html');
$tokenProcessor->addRow(['contactId' => $participant['contactId'], 'participantId' => $participant['id']]);
$tokenProcessor->addMessage('body_html', $messageTemplate['msg_html'], 'text/html');
$tokenProcessor->addRow(['contactId' => $participant['contact_id'], 'participantId' => $participant['id']]);
$tokenProcessor->evaluate();
$tokenProcessorEmail = new TokenProcessor(\Civi::dispatcher(), [
......@@ -195,8 +176,8 @@ function getEmailContent($participant) {
'schema' => ['contactId', 'participantId'] // change depending on what other fields are present in template
]);
$tokenProcessorEmail->addMessage('body_html', $emailTemplateHTML, 'text/html');
$tokenProcessorEmail->addRow(['contactId' => $participant['contactId'], 'participantId' => $participant['id']]);
$tokenProcessorEmail->addMessage('body_html', $emailTemplate['msg_html'], 'text/html');
$tokenProcessorEmail->addRow(['contactId' => $participant['contact_id'], 'participantId' => $participant['id']]);
$tokenProcessorEmail->evaluate();
} catch (TypeError $e) {
......@@ -227,9 +208,9 @@ function getEmailContent($participant) {
'from' => $fromEmail,
'toName' => $participant['name'],
'toEmail' => $participant['email'],
'subject' => $emailSubject,
'subject' => $emailTemplate['msg_subject'],
'html' => $htmlEmail,
'contactId' => $participant['contactId'],
'contactId' => $participant['contact_id'],
];
$certificateEmail['attachments'] = $mailAttachments;
......
......@@ -66,7 +66,6 @@ return [
'time_format' => NULL,
'note_columns' => 60,
'note_rows' => 4,
'column_name' => 'download_certificate_123',
'option_group_id' => NULL,
'serialize' => 0,
'filter' => NULL,
......@@ -109,7 +108,6 @@ return [
'time_format' => NULL,
'note_columns' => 60,
'note_rows' => 4,
'column_name' => 'duration_130',
'option_group_id' => NULL,
'serialize' => 0,
'filter' => NULL,
......@@ -122,4 +120,28 @@ return [
],
],
],
[
'name' => 'CustomGroup_Event_Certificate_CustomField_Select_Certificate_for_event',
'entity' => 'CustomField',
'cleanup' => 'unused',
'update' => 'unmodified',
'params' => [
'version' => 4,
'values' => [
'custom_group_id.name' => 'Event_Certificate',
'name' => 'event_certificate',
'label' => E::ts('Select Certificate for event'),
'data_type' => 'EntityReference',
'html_type' => 'Autocomplete-Select',
'text_length' => 255,
'note_columns' => 60,
'note_rows' => 4,
'fk_entity' => 'MessageTemplate',
],
'match' => [
'name',
'custom_group_id',
],
],
],
];
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment