Skip to content

Commit

Permalink
Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifica…
Browse files Browse the repository at this point in the history
…tions (#134)

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

* Issue #179295 task: Add webhook support for Shika/Vowel in TjNotifications

Co-authored-by: Manoj L <[email protected]>
Co-authored-by: ttpllt46 <“[email protected]”>
  • Loading branch information
3 people authored Feb 14, 2022
1 parent b11c8b0 commit 3c5cead
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/com_tjnotifications/admin/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@

</fieldset>

<fieldset name="webhook" label="COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK" description="COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK" addfieldpath="/administrator/components/com_tjnotifications/models/fields">
<field
name="webhook_url"
type="subform"
label="COM_TJNOTIFICATIONS_SETTINGS_ADD_WEBHOOK_URLS_LABEL"
description="COM_TJNOTIFICATIONS_SETTINGS_ADD_WEBHOOK_URLS_DESC"
multiple="true"
min="1"
formsource="/administrator/components/com_tjnotifications/models/forms/global_config_webhookurl.xml"
/>
</fieldset>

<fieldset name="permissions" description="JCONFIG_PERMISSIONS_DESC" label="JCONFIG_PERMISSIONS_LABEL">
<field name="rules" type="rules" component="com_tjnotifications" class="inputbox" filter="rules" validate="rules" label="JCONFIG_PERMISSIONS_LABEL" section="component" />
</fieldset>
Expand Down
2 changes: 1 addition & 1 deletion src/com_tjnotifications/admin/defines.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@

if (!defined('TJNOTIFICATIONS_CONST_BACKENDS_ARRAY'))
{
define('TJNOTIFICATIONS_CONST_BACKENDS_ARRAY', "email,sms");
define('TJNOTIFICATIONS_CONST_BACKENDS_ARRAY', "email,sms,webhook");
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,28 @@ COM_TJNOTIFICATIONS_NOTIFICATION_SMS_REMAINING_EXCEEDED="characters exceeded"
; Notification - SMS provider template ID
COM_TJNOTIFICATIONS_FIELD_SMS_TEMPLATE_ID_LABEL="SMS template ID"
COM_TJNOTIFICATIONS_FIELD_SMS_TEMPLATE_ID_DESC="Set template ID which is proided by SMS provider"

; Since __DEPLOY_VERSION__
; Notification - Webhook support
COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK="Webhook"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_SUBFORM_LABEL="Add Custom Webhook URLs"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_SUBFORM_DESC="Enter custom webhook URL for your Webhook notification"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_LABEL="Webhook URL"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_DESC="Enter URL for your Webhook notification"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_BODY_LABEL="Webhook Body"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_BODY_DESC="Enter body for your Webhook notification"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_STATUS_LABEL="Webhook Status"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_STATUS_DESC="Turn on this after filling the body to send a Webhook notification"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_USE_GLOBAL_URL_LABEL="Use Webhook URLs From Global Config"
COM_TJNOTIFICATIONS_FIELD_WEBHOOK_USE_GLOBAL_URL_DESC="Use the Webhook URls that are already defined in the global config. If you haven't added any URL in the global config then please do. If you don't wish to use global URLs then set this field value to 'No' and add custom URls by clicking 'Add Custom Webhook URLs'"
COM_TJNOTIFICATIONS_WEBHOOK_FIELDS="Webhook Fields"
COM_TJNOTIFICATIONS_WEBHOOK_FIELDS_DESC="Set webhook fields"
COM_TJNOTIFICATIONS_VIEW_NOTIFICATION_TAB_WEBHOOK="Webhook"
COM_TJNOTIFICATIONS_BACKEND_WEBHOOK="Webhook"
COM_TJNOTIFICATIONS_VIEW_NOTIFICATIONS_DEFAULT_WEBHOOK_TITLE="Webhook"
COM_TJNOTIFICATIONS_SETTINGS_ADD_WEBHOOK_URLS_LABEL="Add Webhook URLs"
COM_TJNOTIFICATIONS_SETTINGS_ADD_WEBHOOK_URLS_DESC="Specify all the Webhook URLs at the global level. Later you can also add Notification Template specific URLs."
COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK_URL_LABEL="Webhook URL"
COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK_URL_DESC="Enter URL for your Webhook notification"
COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_CUSTOM_WEBHOOK_URLS="Please add custom webhook URL or use global webhook URL"
COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_GLOBAL_WEBHOOK_URLS="Please add webhook URLs in the global config or add custom webhook URL."
1 change: 1 addition & 0 deletions src/com_tjnotifications/admin/models/fields/backends.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function getOptions()
$options[] = HTMLHelper::_('select.option', 'push', Text::_('COM_TJNOTIFICATIONS_BACKEND_PUSH'));
$options[] = HTMLHelper::_('select.option', 'sms', Text::_('COM_TJNOTIFICATIONS_BACKEND_SMS'));
$options[] = HTMLHelper::_('select.option', 'whatsapp', Text::_('COM_TJNOTIFICATIONS_BACKEND_WHATSAPP'));
$options[] = HTMLHelper::_('select.option', 'webhook', Text::_('COM_TJNOTIFICATIONS_BACKEND_WEBHOOK'));

return array_merge(parent::getOptions(), array_values($options));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="url"
type="url"
label="COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK_URL_LABEL"
description="COM_TJNOTIFICATIONS_SETTINGS_WEBHOOK_URL_DESC"
size="100"
filter="url"
validate="url"
/>
</fieldset>
</form>
29 changes: 29 additions & 0 deletions src/com_tjnotifications/admin/models/forms/notification.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@
</fieldset>
</fields>

<fields name="webhook">
<fieldset name="webhook_fieldset">
<field
name="state"
type="radio"
default="0"
label="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_STATUS_LABEL"
description="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_STATUS_DESC"
class="btn-group btn-group-yesno"
>
<option value="1">JON</option>
<option value="0">JOFF</option>
</field>

<field
name="webhookfields"
description="COM_TJNOTIFICATIONS_WEBHOOK_FIELDS_DESC"
type="subform"
label="COM_TJNOTIFICATIONS_WEBHOOK_FIELDS"
min="1"
formsource="/administrator/components/com_tjnotifications/models/forms/webhookfields.xml"
multiple="true"
buttons="add,remove"
layout="joomla.form.field.subform.repeatable"
groupByFieldset="false"
/>
</fieldset>
</fields>

<!--
<fields name="web">
<fieldset name="web_fieldset">
Expand Down
51 changes: 51 additions & 0 deletions src/com_tjnotifications/admin/models/forms/webhookfields.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="id"
type="hidden" />

<field
name="language"
type="contentlanguage"
label="JFIELD_LANGUAGE_LABEL"
description="COM_TJNOTIFICATIONS_NOTIFICATION_LANGUAGE_DESC"
required="true"
>
<option value="*">JALL</option>
</field>

<field
name="use_global_webhook_url"
type="radio"
default="1"
label="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_USE_GLOBAL_URL_LABEL"
description="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_USE_GLOBAL_URL_DESC"
class="btn-group btn-group-yesno"
>
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>

<field
name="webhook_url"
type="subform"
label="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_SUBFORM_LABEL"
description="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_SUBFORM_DESC"
multiple="true"
formsource="/administrator/components/com_tjnotifications/models/forms/webhookurl.xml"
/>

<field
name="body"
type="textarea"
label="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_BODY_LABEL"
description="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_BODY_DESC"
rows="10"
cols="350"
filter="JComponentHelper::filterText"
class="validate-json"
required="true"
/>
</fieldset>
</form>
14 changes: 14 additions & 0 deletions src/com_tjnotifications/admin/models/forms/webhookurl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="url"
type="url"
label="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_LABEL"
description="COM_TJNOTIFICATIONS_FIELD_WEBHOOK_URL_DESC"
size="50"
filter="url"
validate="url"
/>
</fieldset>
</form>
33 changes: 33 additions & 0 deletions src/com_tjnotifications/admin/models/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Component\ComponentHelper;

BaseDatabaseModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/models');
require_once JPATH_ADMINISTRATOR . '/components/com_tjnotifications/defines.php';
Expand Down Expand Up @@ -363,6 +364,11 @@ public function save($data)
// Get DB
$db = Factory::getDbo();

// Get global configs
$notificationsParams = ComponentHelper::getParams('com_tjnotifications');
$webhookUrls = $notificationsParams->get('webhook_url');
$webhookUrls = array_column((array) $webhookUrls, 'url');

// 2 - save backend specific config
$backendsArray = explode(',', TJNOTIFICATIONS_CONST_BACKENDS_ARRAY);

Expand Down Expand Up @@ -390,6 +396,26 @@ public function save($data)
// 2.2 Find existing template config entries to be deleted (i.e. language specific templates removed by user)
foreach ($data[$backend][$backend . 'fields'] as $backendName => $backendFieldValues)
{
// Webhook stuff starts here
if ($backend == 'webhook' && $data[$backend]['state'])
{
// If not using global webhook URLs & custom webhooks URLs are also empty
if (empty($backendFieldValues['use_global_webhook_url']) && empty($backendFieldValues['webhook_url']))
{
$this->setError(Text::_('COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_CUSTOM_WEBHOOK_URLS'));

return false;
}
// If using global webhook URL & the global URLs are empty
elseif ($backendFieldValues['use_global_webhook_url'] && empty($webhookUrls[0]))
{
$this->setError(Text::_('COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_GLOBAL_WEBHOOK_URLS'));

return false;
}
}
// Webhook stuff ends here

// Iterate through each lang. specific config entry
foreach ($existingBackendConfigs as $existingBackendConfig)
{
Expand Down Expand Up @@ -464,6 +490,13 @@ public function save($data)
$templateConfigTable->body = $backendFieldValues['body'];
$templateConfigTable->language = $backendFieldValues['language'];

// Webhook stuff starts here
// Add URLs for webhook
$templateConfigTable->webhook_url = !empty($backendFieldValues['webhook_url']) ? json_encode($backendFieldValues['webhook_url']): '';

$templateConfigTable->use_global_webhook_url = !empty($backendFieldValues['use_global_webhook_url']) ? $backendFieldValues['use_global_webhook_url']: 0;
// Webhook stuff ends here

if (!empty($backendFieldValues['provider_template_id']))
{
$templateConfigTable->provider_template_id = $backendFieldValues['provider_template_id'];
Expand Down
4 changes: 2 additions & 2 deletions src/com_tjnotifications/admin/models/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function getTemplate($client, $key, $language, $backend = 'email')
$db->quoteName(
array(
't.id', 't.client', 't.key', 't.title', 't.replacement_tags',
'ntc.backend', 'ntc.language', 'ntc.subject', 'ntc.body', 'ntc.params', 'ntc.state', 'ntc.provider_template_id'
'ntc.backend', 'ntc.language', 'ntc.subject', 'ntc.body', 'ntc.params', 'ntc.state', 'ntc.provider_template_id', 'ntc.webhook_url', 'ntc.use_global_webhook_url'
)
)
)
Expand All @@ -342,7 +342,7 @@ public function getTemplate($client, $key, $language, $backend = 'email')
$db->quoteName(
array(
't.id', 't.client', 't.key', 't.title', 't.replacement_tags',
'ntc.backend', 'ntc.language', 'ntc.subject', 'ntc.body', 'ntc.params', 'ntc.state', 'ntc.provider_template_id'
'ntc.backend', 'ntc.language', 'ntc.subject', 'ntc.body', 'ntc.params', 'ntc.state', 'ntc.provider_template_id', 'ntc.webhook_url', 'ntc.use_global_webhook_url'
)
)
)
Expand Down
51 changes: 27 additions & 24 deletions src/com_tjnotifications/admin/sql/install.mysql.utf8.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ CREATE TABLE IF NOT EXISTS `#__tj_notification_template_configs` (
`template_id` int(11) NOT NULL DEFAULT 0,
`backend` varchar(50) NOT NULL DEFAULT '',
`language` char(7) NOT NULL DEFAULT '*',
`subject` text DEFAULT NULL,
`body` text DEFAULT NULL,
`params` text DEFAULT NULL,
`state` int(11) NOT NULL DEFAULT 0,
`created_on` datetime NULL DEFAULT NULL,
`updated_on` datetime NULL DEFAULT NULL,
`is_override` int(1) NOT NULL DEFAULT 0,
`provider_template_id` varchar(255) DEFAULT '',
`subject` text NOT NULL,
`body` text NOT NULL,
`webhook_url` text DEFAULT NULL,
`use_global_webhook_url` TINYINT(1) NOT NULL DEFAULT '1' COMMENT 'Use Global Config Webhook URLs',
`params` text NOT NULL,
`state` int(11) NOT NULL,
`created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`is_override` int(1) NOT NULL,
`provider_template_id` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `#__tj_notification_template_configs` FOREIGN KEY (`template_id`) REFERENCES `#__tj_notification_templates` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;
Expand All @@ -51,22 +53,23 @@ CREATE TABLE IF NOT EXISTS `#__tj_notification_template_configs` (

CREATE TABLE IF NOT EXISTS `#__tj_notification_logs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`key` varchar(100) NOT NULL DEFAULT '',
`client` varchar(100) NOT NULL DEFAULT '',
`backend` varchar(50) NOT NULL DEFAULT '',
`subject` varchar(250) NOT NULL DEFAULT '',
`title` varchar(100) NOT NULL DEFAULT '',
`body` text DEFAULT NULL,
`from` varchar(500) NOT NULL DEFAULT '',
`to` text DEFAULT NULL,
`cc` text DEFAULT NULL,
`bcc` text DEFAULT NULL,
`date` datetime NULL DEFAULT NULL,
`state` tinyint(2) NOT NULL DEFAULT 0,
`params` text DEFAULT NULL,
`priority`int(11) NOT NULL DEFAULT 0,
`message` text DEFAULT NULL,
`category` text DEFAULT NULL,
`key` varchar(100) NOT NULL,
`client` varchar(100) NOT NULL,
`backend` varchar(50) NOT NULL,
`subject` varchar(250) NOT NULL,
`title` varchar(100) NOT NULL,
`body` text NOT NULL,
`webhook_url` text DEFAULT NULL,
`from` varchar(500) NOT NULL,
`to` text NOT NULL,
`cc` text NOT NULL,
`bcc` text NOT NULL,
`date` datetime NOT NULL,
`state` tinyint(2) NOT NULL,
`params` text NOT NULL,
`priority`int(11) NOT NULL,
`message` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=1;

Expand Down
3 changes: 3 additions & 0 deletions src/com_tjnotifications/admin/sql/updates/mysql/2.1.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `#__tj_notification_template_configs` ADD COLUMN `webhook_url` text DEFAULT NULL AFTER `body`;
ALTER TABLE `#__tj_notification_template_configs` ADD COLUMN `use_global_webhook_url` TINYINT(1) NOT NULL DEFAULT '1' COMMENT 'Use Global Config Webhook URLs' AFTER `webhook_url`;
ALTER TABLE `#__tj_notification_logs` ADD COLUMN `webhook_url` TEXT NULL DEFAULT NULL AFTER `body`;

0 comments on commit 3c5cead

Please sign in to comment.