Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
  • 1.0
  • 1.1
  • 1.2
4 results

Target

Select target project
  • extensions/certificates
  • xavi-xaloc/certificates
  • jackrabbithanna/certificates
  • Edselopez/certificates
  • zilinweng/certificates
  • monish.deb/certificates
  • ufundo/certificates
7 results
Select Git revision
  • main
  • 1.0
  • 1.1
  • 1.2
  • 1.3.0
5 results
Show changes
Commits on Source (15)
......@@ -3,6 +3,7 @@
use Civi\Api4\Participant;
use Civi\Certificates\PDFCertificate;
class CRM_Certificates_Form_Task extends CRM_Contact_Form_Task {
public function preProcess() {
......@@ -18,13 +19,14 @@ class CRM_Certificates_Form_Task extends CRM_Contact_Form_Task {
foreach ($ids as $participantID) {
$participant = Participant::get(FALSE)
->addSelect('*', 'status_id:name', 'contact_id.display_name', 'contact_id.id', 'event_id.id', 'event_id.title', 'event_id.Event_Certificate.download_certificate')
->addSelect('*', 'status_id:name', 'contact_id.display_name', 'contact_id.id', 'event_id.id', 'event_id.title', 'event_id.Event_Certificate.download_certificate', 'event_id.Event_Certificate.event_certificate')
->addWhere('id', '=', $participantID)
->execute()
->first();
$context[] = ['contactId' => $participant['contact_id.id'], 'participantId' => $participantID, 'eventId' => $participant['event_id.id']];
$messageTemplateID = \Civi::settings()->get('certificates_event_defaultmessagetemplateid');
$context = ['contactId' => $participant['contact_id.id'], 'participantId' => $participantID, 'eventId' => $participant['event_id.id']];
$messageTemplateID = $participant['event_id.Event_Certificate.event_certificate'] ?? \Civi::settings()->get('certificates_event_defaultmessagetemplateid');
if (empty($messageTemplateID)) {
\Civi::log()->error('Event attendance certificate has no message template ID set!');
......@@ -34,8 +36,18 @@ class CRM_Certificates_Form_Task extends CRM_Contact_Form_Task {
$downloadPDF = new PDFCertificate();
$filename = $participant['contact_id.display_name'] . '_' . $participant['event_id'] . '_attendance_certificate_' . $participant['event_id.title'];
}
\Civi\Api4\Activity::create(FALSE)
->addValue('source_record_id', $context['participantId'])
->addValue('source_contact_id', $context['contactId'])
->addValue('subject', 'Attendance Certificate Issued')
->addValue('activity_date_time', date('Y-m-d H:i:s'))
->addValue('status_id', 2)
->addValue('activity_type_id:name', 'Certificate Issued')
->execute();
$downloadPDF->download($messageTemplateID, $context, $filename);
}
}
<?php
// phpcs:disable
use CRM_Certificates_ExtensionUtil as E;
// phpcs:enable
/**
* Collection of upgrade steps.
*/
class CRM_Certificates_Upgrader extends CRM_Extension_Upgrader_Base {
public function upgrade_1002() {
// check if the custom field exists
$customFieldID = \Civi\Api4\CustomField::get(FALSE)
->addWhere('name', '=', 'event_certificate')
->addWhere('custom_group_id:name', '=', 'Event_Certificate')
->execute()
->first()['id'] ?? NULL;
if (!$customFieldID) {
// create the custom field
\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')
->addValue('fk_entity', 'MessageTemplate')
->addValue('is_required', FALSE)
->addValue('is_active', TRUE)
->addValue('html_type', 'Autocomplete-Select')
->execute();
}
return TRUE;
}
}
......@@ -3,6 +3,9 @@
require_once 'certificates.civix.php';
// phpcs:disable
use CRM_Certificates_ExtensionUtil as E;
use Civi\Certificates\PDFCertificate;
use Civi\Token\TokenProcessor;
// phpcs:enable
/**
......@@ -72,3 +75,165 @@ function certificates_civicrm_permission(array &$permissions): void {
'description' => $prefix . E::ts('Allow user to print their own certificate'),
];
}
function certificates_civicrm_pre($op, $objectName, $id, &$params) {
if ($op == 'edit' && $objectName == 'Participant') {
try {
$statusId = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', 'Attended');
//get current participant status before data is changed
$participant = \Civi\Api4\Participant::get(FALSE)
->addSelect('contact_id', 'status_id', 'id', 'event.Event_Certificate.event_certificate', 'event.id', 'event.title')
->addJoin('Event AS event', 'LEFT', ['event_id', '=', 'event.id'])
->addWhere('id', '=', $id)
->execute()->first();
$contactID = $participant['contact_id'];
// checks if participant status_id is now status id for Attended --> then send email
if ($participant['status_id'] !== $statusId && $params['status_id'] == $statusId) {
[$toDisplayName, $toEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
$participantInfo = [
'id' => $participant['id'],
'contact_id' => $contactID,
'name' => $toDisplayName,
'email' => $toEmail,
'event_id' => $participant['event.id'],
'event_title' => $participant['event.title'],
'certificate_id' => $participant['event.Event_Certificate.event_certificate']
];
getEmailContent($participantInfo);
}
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
}
}
/**
* Sends an email with a certificate attachment.
*/
function getEmailContent($participant) {
$mailAttachments = [];
try {
// searches for the certificate, if none found will fallback to default
$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!');
return;
}
// query to get the html of the message template given the message template id
$messageTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_html')
->addWhere('id', '=', $messageTemplateID)
->execute()
->first();
// add default for here
$emailTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_html')
->addWhere('msg_title', '=', 'Certificate Email')
->execute()
->first();
if (empty($emailTemplate)) {
// fallback to default email message template if 'Certificate Email' is not found
$defaultEmailTemplateID = \Civi::settings()->get('certificates_event_emailmessagetemplateid');
if (empty($defaultEmailTemplateID)) {
\Civi::log()->error('Default email template ID is not configured.');
return;
}
$emailTemplate = \Civi\Api4\MessageTemplate::get(FALSE)
->addSelect('msg_subject', 'msg_html')
->addWhere('id', '=', $defaultEmailTemplateID)
->setLimit(1)
->execute()
->first();
if (!$emailTemplate) {
\Civi::log()->error("Fallback email message template ID $defaultEmailTemplateID not found.");
return;
}
}
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', $messageTemplate['msg_html'], 'text/html');
$tokenProcessor->addRow(['contactId' => $participant['contact_id'], 'participantId' => $participant['id']]);
$tokenProcessor->evaluate();
$tokenProcessorEmail = new TokenProcessor(\Civi::dispatcher(), [
'controller' => __CLASS__,
'smarty' => FALSE,
'schema' => ['contactId', 'participantId', 'eventId'] // change depending on what other fields are present in template
]);
$tokenProcessorEmail->addMessage('body_html', $emailTemplate['msg_html'], 'text/html');
$tokenProcessorEmail->addRow(['contactId' => $participant['contact_id'], 'participantId' => $participant['id'], 'eventId' => $participant['event_id']]);
$tokenProcessorEmail->evaluate();
} catch (TypeError $e) {
\Civi::log()->error('certificates: TypeError parsing tokens. Error: ' . $e->getMessage());
return;
}
$rows = $tokenProcessor->getRows();
$html = [];
foreach ($rows as $row) {
$html[] = $row->render('body_html');
}
$html = $html[0];
$rowsEmail = $tokenProcessorEmail->getRows();
$htmlEmail = [];
foreach ($rowsEmail as $rowEmail) {
$htmlEmail[] = $rowEmail->render('body_html');
}
$htmlEmail = $htmlEmail[0];
$filename = $participant['name'] . '_' . $participant['event_id'] . '_attendance_certificate_' . $participant['event_title'];
$filename = \CRM_Utils_File::makeFilenameWithUnicode($filename, '_', 200) . '.pdf';
$fromEmail = current(CRM_Core_BAO_Domain::getNameAndEmail(FALSE, TRUE)); // takes the first set email in CiviMail
array_push($mailAttachments, CRM_Utils_Mail::appendPDF($filename, $html));
$certificateEmail = [
'from' => $fromEmail,
'toName' => $participant['name'],
'toEmail' => $participant['email'],
'subject' => $emailTemplate['msg_subject'],
'html' => $htmlEmail,
'contactId' => $participant['contact_id'],
];
$certificateEmail['attachments'] = $mailAttachments;
$act = \Civi\Api4\Activity::create(FALSE)
->addValue('source_record_id', $participant['id'])
->addValue('source_contact_id', $participant['contact_id'])
->addValue('subject', 'Attendance Certificate Issued')
->addValue('activity_date_time', date('Y-m-d H:i:s'))
->addValue('status_id', 2)
->addValue('activity_type_id:name', 'Certificate Issued')
->execute();
CRM_Utils_Mail::send($certificateEmail);
} catch (Exception $e) {
echo 'Message: ' . $e->getMessage();
}
}
......@@ -36,4 +36,5 @@
<mixin>setting-admin@1.0.1</mixin>
<mixin>menu-xml@1.0.0</mixin>
</mixins>
<upgrader>CRM_Certificates_Upgrader</upgrader>
</extension>
......@@ -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',
],
],
],
];
......@@ -27,6 +27,21 @@ return [
]
],
],
'certificates_event_emailmessagetemplateid' => [
'name' => 'certificates_event_emailmessagetemplateid',
'type' => 'Int',
'html_type' => 'text',
'default' => 0,
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Event Certificate Email Message Template ID'),
'html_attributes' => [],
'settings_pages' => [
'certificates' => [
'weight' => 15,
]
],
],
'certificates_membership_defaultmessagetemplateid' => [
'name' => 'certificates_membership_defaultmessagetemplateid',
'type' => 'Int',
......