Skip to content

Integrating with extensions

Vijay Khollam edited this page Feb 22, 2017 · 20 revisions

Include the library in your code

To use TJ Notifications, you need to install the library along with your extension. Include the library file jimport('techjoomla.tjnotifications.tjnotifications');

Install your notification templates

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.

Sample format of JSON file

{
  "template1": {
   "id" : "",
   "client": "COM_JGIVE",
   "key": "status",
   "email_body": "<p>Hi {donation.first_name},</p>\r\n<p>The status of donation id {donation.order_id} has changed.</p>\r\n<p>The new status is <b>{donation.newStatus}.</b></p>\r\n<p><b>{donation.email}</b></p>",
   "sms_body" :"",
   "push_status" :"",
   "web_body" : "",
   "email_subject" : "Donation payment status change: Your donation id {subject.order_id}",
   "sms_subject" : "",
   "push_subject" : "",
   "web_subject" : ""
  },
  "template2": {
   "id" : "",
   "client": "COM_JGIVE",
   "key": "statusToPromoter",
   "email_body": "<p>Hi {promoter.username} ,</p>\r\n<p>The status of donation id {donation.order_id}  has changed.</p>\r\n<p>The new status is {donation.newStatus}<b>.</b></p>\r\n<p> </p>",
   "sms_body" :"",
   "push_status" :"",
   "web_body" : "",
   "email_subject" :"Donation payment status change: Your donation id {subject.order_id}",
   "sms_subject" : "",
   "push_subject" : "",
   "web_subject" : ""
  }

Script to installed default templates

This script should be used by extension developer to installed some default templates, while installation of their own extension. Developer can use this script as given below with correct file path of JSON file.

<?php
jimport( 'joomla.application.component.model');
JLoader::import( 'notification', JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_tjnotifications' . DS . 'models');

$templates = file_get_contents('/your/file/path/Filename.json');
$decodedFile = json_decode($templates, true);

JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/tables');
$notifications_model = JModelAdmin::getInstance('Notification', 'TJNotificationsModel');

if (count($templates) != 0)
{
   foreach ($templates as $template => $array)
   {
      $notifications_model->createTemplates($array);
   }
}

The magic method Tjnotifications::send()

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 is an instance of JParameter. 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.

Sample code to send an email using Tjnotifications.

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 JParameter();
  $options->set('cc', '[email protected]');
  $options->set('attachment', '/var/www/html/joomla/media/attach.pdf');
  $options->set('guestEmails', array("[email protected]"));
  $options->set('from', "[email protected]");
  $options->set('fromname', "pranoti");

  Tjnotifications::send($client, $key, $recipients, $replacements, $options);	 
}
Clone this wiki locally