diff --git a/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini b/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini index 42163815..b5c6f9df 100644 --- a/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini +++ b/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini @@ -311,3 +311,16 @@ 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." + +; Error messages +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_WEBHOOK_BODY_REQUIRED="Webhook body is required when webhook is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_WEBHOOK_LANGUAGE_REQUIRED="Language selection is required when webhook is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_EMAIL_BODY_REQUIRED="Email body is required when email is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_EMAIL_SUBJECT_REQUIRED="Email subject is required when email is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_EMAIL_LANGUAGE_REQUIRED="Language selection is required when email is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_SMS_BODY_REQUIRED="SMS body is required when SMS is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_SMS_LANGUAGE_REQUIRED="Language selection is required when SMS is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_PUSH_BODY_REQUIRED="Push body is required when push is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_PUSH_LANGUAGE_REQUIRED="Language selection is required when push is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_WHATSAPP_BODY_REQUIRED="WhatsApp body is required when WhatsApp is enabled." +COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_WHATSAPP_LANGUAGE_REQUIRED="Language selection is required when WhatsApp is enabled." \ No newline at end of file diff --git a/src/com_tjnotifications/admin/models/forms/emailfields.xml b/src/com_tjnotifications/admin/models/forms/emailfields.xml index 9501102f..d2ea987f 100644 --- a/src/com_tjnotifications/admin/models/forms/emailfields.xml +++ b/src/com_tjnotifications/admin/models/forms/emailfields.xml @@ -5,33 +5,30 @@ name="id" type="hidden" /> - - - - - + + + - + + diff --git a/src/com_tjnotifications/admin/models/forms/pushfields.xml b/src/com_tjnotifications/admin/models/forms/pushfields.xml index 0e106254..882a1964 100644 --- a/src/com_tjnotifications/admin/models/forms/pushfields.xml +++ b/src/com_tjnotifications/admin/models/forms/pushfields.xml @@ -5,26 +5,24 @@ name="id" type="hidden" /> - - - + + + - + diff --git a/src/com_tjnotifications/admin/models/forms/smsfields.xml b/src/com_tjnotifications/admin/models/forms/smsfields.xml index aa3b8d64..8fb7c046 100644 --- a/src/com_tjnotifications/admin/models/forms/smsfields.xml +++ b/src/com_tjnotifications/admin/models/forms/smsfields.xml @@ -5,27 +5,25 @@ name="id" type="hidden" /> - - - + + + - + - - - + + + - + diff --git a/src/com_tjnotifications/admin/models/forms/whatsappfields.xml b/src/com_tjnotifications/admin/models/forms/whatsappfields.xml index cdd0caef..e6b3d87d 100644 --- a/src/com_tjnotifications/admin/models/forms/whatsappfields.xml +++ b/src/com_tjnotifications/admin/models/forms/whatsappfields.xml @@ -5,25 +5,23 @@ name="id" type="hidden" /> - - - + + + - + diff --git a/src/com_tjnotifications/admin/models/notification.php b/src/com_tjnotifications/admin/models/notification.php index 28f00bd0..a4ba1a31 100644 --- a/src/com_tjnotifications/admin/models/notification.php +++ b/src/com_tjnotifications/admin/models/notification.php @@ -321,7 +321,8 @@ public function updateReplacementTags($data) */ public function save($data) { - $isNew = true; + // Determine if this is a new record or edit + $isNew = empty($data['id']) || $data['id'] == 0; // 1 - save template first if (!empty($data)) @@ -345,14 +346,22 @@ public function save($data) if (!parent::save($data)) { + // Log the error for debugging + Factory::getApplication()->getLogger()->error('TJNotifications: Failed to save template - ' . $this->getError()); return false; } else { - $db = Factory::getDbo(); + $db = Factory::getDbo(); + + // Get the template ID - use insertid() for new records, or the existing ID for edits + if ($isNew) { + $templateId = $db->insertid(); + } else { + $templateId = $data['id']; + } + // IMPORTANT to set new id in state, it is fetched in controller later - // Get current Template id - $templateId = $db->insertid(); $this->setState('com_tjnotifications.edit.notification.id', $templateId); $this->setState('com_tjnotifications.edit.notification.new', $isNew); } @@ -397,25 +406,57 @@ 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']) + // Backend-specific validation - only validate when backend is enabled + if ($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'])) + // Common validation for all backends + if (empty($backendFieldValues['language'])) { - $this->setError(Text::_('COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_CUSTOM_WEBHOOK_URLS')); + $errorKey = 'COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_' . strtoupper($backend) . '_LANGUAGE_REQUIRED'; + $this->setError(Text::_($errorKey)); return false; } - // If using global webhook URL & the global URLs are empty - elseif ($backendFieldValues['use_global_webhook_url'] && empty($webhookUrls[0])) + + if (empty($backendFieldValues['body'])) { - $this->setError(Text::_('COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_GLOBAL_WEBHOOK_URLS')); + $errorKey = 'COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_' . strtoupper($backend) . '_BODY_REQUIRED'; + $this->setError(Text::_($errorKey)); return false; } + + // Email-specific validation + if ($backend == 'email') + { + if (empty($backendFieldValues['subject'])) + { + $this->setError(Text::_('COM_TJNOTIFICATIONS_TEMPLATE_ERR_MSG_EMAIL_SUBJECT_REQUIRED')); + + return false; + } + } + + // Webhook-specific validation + if ($backend == 'webhook') + { + // 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 (!empty($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 + // Backend validation ends here // Iterate through each lang. specific config entry foreach ($existingBackendConfigs as $existingBackendConfig)