Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Add message template content as activity #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions CRM/Birthdays/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,20 @@ public function sendMailsAndWriteActivity($contacts, $write_activity): int
try {
$this->sendMail($contact_id, $this->email_address_from, $contact_info['email'], $this->template_id);
if ($write_activity) {
$tpl_info = $this->getMessageTemplate($this->template_id);

$this->createActivity(
$contact_id,
E::ts('Successful birthday greeting mail'),
E::ts(
'Successful birthday greeting mail! Template ID %1 has been used.',
[1 => $this->template_id]
'"%1" birthday message sent successfully',
[1 => $tpl_info['msg_title']]
),
E::ts(
'<b>Subject: %1</b><br><br>%2',
[
1 => $tpl_info['subject'],
2 => $tpl_info['msg_html']
]
)
);
}
Expand Down Expand Up @@ -140,4 +148,29 @@ private function createActivity($target_id, $title, $description): void
Civi::log()->debug(E::LONG_NAME . ' ' . "Unable to write activity: $exception");
}
}

/**
* Returns an array with template data
*
* e.g.:
* [
* 'msg_title'
* 'msg_subject'
* 'msg_html'
* ...
* ]
*
* @throws Exception
*/
private function getMessageTemplate($id): array
{
try {
return \Civi\Api4\MessageTemplate::get()
->addWhere('id', '=', $id)
->execute()
->single();
} catch (UnauthorizedException|CRM_Core_Exception $e) {
throw new Exception(E::LONG_NAME . " " . "Unable to fetch message template $id. Error: $e");
}
Comment on lines +165 to +174
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jensschuppe Is there a example implementation in one of our extensions to fill tokens with content as V3 API is doing it automatically using MessageTemplate::send in my previous implementation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API3's MessageTemplate.send doesn't return the result, but CRM_Core_BAO_MessageTemplate::sendTemplate(), which basically does all the work inside it, does. So maybe it'd be worth not using the API but this method for getting the contents for passing it into the creation of an activity. You might have to adjust parameters, so check what civicrm_api3_message_template_send() is doing before.

Rendering the template a second time for creating the activity doesn't seem to make sense to me.

}
}
35 changes: 21 additions & 14 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
site_name: Birthdays
repo_url: https://github.com/systopia/de.systopia.birthdays
theme: material
site_description: 'Provides a report on upcoming birthdays and an API for sending birthday greetings via e-mail.'
site_author: 'SYSTOPIA'
theme:
name: material

pages:
- Home: index.md
nav:
- 'Introduction': index.md

markdown_extensions:
- attr_list
- admonition
- def_list
- codehilite
- toc:
permalink: true
- pymdownx.superfences
- pymdownx.inlinehilite
- pymdownx.tilde
- pymdownx.betterem
- pymdownx.mark
- attr_list
- admonition
- def_list
- codehilite
- toc:
permalink: True
- pymdownx.superfences
- pymdownx.inlinehilite
- pymdownx.tilde
- pymdownx.betterem
- pymdownx.mark

plugins:
- search:
lang: en