-
Notifications
You must be signed in to change notification settings - Fork 39
Integrating with extensions
To use TJ Notifications, you need to install the library along with your extension. Include the library file
jimport('techjoomla.tjnotifications.tjnotifications');
Each extension which has some default emails need to create default email templates. Extension developers can write on after install to script to save default emails. Extension developers can use the following code to save the email templates and need to create JSON file for templates. All tjnotification email templates get stored in #__tj_notification_templates database table.
{
"template1": {
"id": "",
"client": "com_jgive",
"key": "createCampaignMailToAdmin",
"title": "Create Campaign email to admin (Approval Off)",
"email_body": "<p>Dear {info.adminname},<br /><br />Campaign with title <strong>{campaign.title}</strong> was created by {campaign.first_name}<br />Click <a href='{campaign.allcampaigns}'>here</a> to view all campaigns on the site.<br /><br />This is system generated email, please do not reply. Many thanks from <strong>{info.sitename}</strong></p>",
"sms_body": "",
"push_body": "",
"web_body": "",
"email_subject": "New Campaign \"{subject.title}\" has gone live on {subject.sitename}",
"sms_subject": "",
"push_subject": "",
"web_subject": "",
"email_status": 1,
"sms_status": 0,
"push_status": 0,
"web_status": 0,
"user_control": 1,
"core": 1,
"replacement_tags": [
{
"name": "campaign.first_name",
"description": "Name of Campaign Promoter"
},
{
"name": "campaign.title",
"description": "Name of Campaign"
},
{
"name": "campaign.allcampaigns",
"description": "All campaigns page link"
},
{
"name": "info.sitename",
"description": "Sitename"
},
{
"name": "info.adminname",
"description": "Siteadmin"
}
]
},
"template2": {
"id": "",
"client": "com_jgive",
"key": "createCampaignMailToPromoter",
"title": "Create Campaign email to Campaign Owner (Approval OFF)",
"email_body": "<p>Hello {campaign.first_name},<br /><br />Your Campaign - <strong>{campaign.title}</strong> has been created and published.<br />Please visit this <a href='{campaign.mycampaigns}'>link</a> to view all your campaigns.<br /><br />This is system generated email, please do not reply. Many thanks from {info.sitename}</p>",
"sms_body": "",
"push_body": "",
"web_body": "",
"email_subject": "Your Campaign \"{subject.title}\" is now live on {subject.sitename}",
"sms_subject": "",
"push_subject": "",
"web_subject": "",
"email_status": 1,
"sms_status": 0,
"push_status": 0,
"web_status": 0,
"user_control": 1,
"core": 1,
"replacement_tags": [
{
"name": "campaign.first_name",
"description": "Name of Campaign Promoter"
},
{
"name": "campaign.title",
"description": "Name of Campaign"
},
{
"name": "campaign.mycampaigns",
"description": "Link to my Campaigns"
},
{
"name": "info.sitename",
"description": "Sitename"
}
]
}
This script should be used by extension developer to install some default templates, while installation of their own extension. Developer can use this script as given below with correct file path of JSON file.
This script also takes care while updating your extisting extension. So in case if you already have existing emails with a defined "key" this script won't update new email templates. We will need to create a task to forcefully update all conents. So it won't discard customised emails on various sites.
<?php
jimport('joomla.application.component.model');
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/tables');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('key'));
$query->from($db->quoteName('#__tj_notification_templates'));
$query->where($db->quoteName('client') . ' = ' . $db->quote("com_jgive"));
$db->setQuery($query);
$existingKeys = $db->loadColumn();
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/models');
$notificationsModel = JModelLegacy::getInstance('Notification', 'TJNotificationsModel');
$filePath = file_get_contents('/your/file/path/Filename.json');
$str = file_get_contents($filePath);
$templateJson = json_decode($str, true);
$app = JFactory::getApplication();
if (count($templateJson) != 0)
{
foreach ($templateJson as $template => $array)
{
if (!in_array($array['key'], $existingKeys))
{
$notificationsModel->createTemplates($array);
}
}
}
Prepare the parameters for Tjnotifications::send()
$client
is the name of your extension, eg: com_jgive
$key
is the unique reference for the type of email within your extension. Eg: donation.thankyou, campaign.update, order.thankyou
$recipients
is an array of JUser objects
eg: $recipients = JAccess::getUsersByGroup(2);
$replacements
is an object of objects, containing all replacements. The object and their properties are mapped against the values in the template body. $replacements->order->id
maps to {order.id}
for example.
$options
an instance of JRegistry. Contains additional options that may be used by the notification provider. In case of email options may include cc, bcc, attachments, reply to, from, guestEmails etc
Finally, call Tjnotifications::send($client, $key, $recipients, $replacements, $options);
This will send the notifications based on user preferences. If the user has disabled any of the notifications or specific delivery preferences, those notifications will not be sent to the user. The 3rd party developer does not need to be aware of these settings.
This is the sample code for sending email from your extension. Generally, extensions have some methods to send an email in the model file, Extension developer can refer the following method to send an email from their extension.
<?php
public function save($data)
{
$client = "com_jcregister";
$key = "order";
$recipients[] = JFactory::getUser(488);
$recipients[] = JFactory::getUser(489);
$order_info->id = "236";
$order_info->amount = "500";
$order_info->status = "Pending";
$replacements->order = $order_info;
$customer_info->name = "hemant";
$customer_info->address = "";
$customer_info->zip = "411004";
$replacements->customer = $customer_info;
$options = new JRegistry();
$options->set('subject', $order_info);
$options->set('cc', 'demo@mailinator.com');
$options->set('attachment', '/var/www/html/joomla/media/attach.pdf');
$options->set('guestEmails', array("testuser@mailinator.com"));
$options->set('from', "user@techjoomla.com");
$options->set('fromname', "pranoti");
Tjnotifications::send($client, $key, $recipients, $replacements, $options);
}