From 5fbc89f30ce50f9cdb60b3d1510eec8d373fcde2 Mon Sep 17 00:00:00 2001 From: "rohit.kodam" Date: Thu, 25 Sep 2025 12:08:13 +0530 Subject: [PATCH 1/3] Issue #247530 Fix : Resolved the issue of notification save --- .../language/en-GB.com_tjnotifications.ini | 2 + .../admin/models/notification.php | 80 ++++++++++++++++--- .../admin/sql/updates/mysql/5.1.2.sql | 11 +++ 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql 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..ace92dd9 100644 --- a/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini +++ b/src/com_tjnotifications/admin/language/en-GB.com_tjnotifications.ini @@ -33,6 +33,8 @@ COM_TJNOTIFICATIONS_FIELD_WEB_STATUS_LABEL="Web Status" COM_TJNOTIFICATIONS_FIELD_WEB_SUBJECT_LABEL="Subject" COM_TJNOTIFICATIONS_FIELD_WEB_BODY_LABEL="Web Body" COM_TJNOTIFICATIONS_FIELD_CREATED_SUCCESSFULLY="Notification successfully saved." +COM_TJNOTIFICATIONS_ERROR_TEMPLATE_ID_MISSING="Template ID is missing. Cannot save notification." +COM_TJNOTIFICATIONS_ERROR_SAVE_BACKEND_CONFIG="Failed to save %s backend configuration: %s" ; Status description COM_TJNOTIFICATIONS_FIELD_EMAIL_STATUS_DESC="Turn on this after filling the body to send an Email notification" COM_TJNOTIFICATIONS_FIELD_SMS_STATUS_DESC="Turn on this after filling the body to send an SMS notification" diff --git a/src/com_tjnotifications/admin/models/notification.php b/src/com_tjnotifications/admin/models/notification.php index 28f00bd0..8259983e 100644 --- a/src/com_tjnotifications/admin/models/notification.php +++ b/src/com_tjnotifications/admin/models/notification.php @@ -351,14 +351,24 @@ public function save($data) { $db = Factory::getDbo(); // IMPORTANT to set new id in state, it is fetched in controller later - // Get current Template id - $templateId = $db->insertid(); + // Get current Template id - Fix for existing records + if (!empty($data['id'])) + { + $templateId = $data['id']; + $isNew = false; + } + else + { + $templateId = $db->insertid(); + } + $this->setState('com_tjnotifications.edit.notification.id', $templateId); $this->setState('com_tjnotifications.edit.notification.new', $isNew); } if (empty($templateId)) { + $this->setError(Text::_('COM_TJNOTIFICATIONS_ERROR_TEMPLATE_ID_MISSING')); return false; } @@ -377,7 +387,13 @@ public function save($data) { // 2.1 Check if current backend exists in posted data // If not $data['email'] or $data['sms'] - if (empty($data[$backend])) + if (empty($data[$backend]) || !isset($data[$backend][$backend . 'fields'])) + { + continue; + } + + // Validate backend data structure + if (!is_array($data[$backend][$backend . 'fields'])) { continue; } @@ -454,22 +470,44 @@ public function save($data) $this->deleteBackendConfigs($backendConfigIdsToBeDeleted); // 2.3 Common data for saving - $createdOn = !empty($data['created_on']) ? $data['created_on'] : ''; - $updatedOn = !empty($data['updated_on']) ? $data['updated_on'] : ''; + $date = Factory::getDate(); + $currentDateTime = $date->toSql(true); + + // Always use current datetime for backend configs to avoid empty string issues + $createdOn = $currentDateTime; + $updatedOn = $currentDateTime; // 2.4 try saving all backend specific configs // This has repeatable data eg: $data['email']['emailfields'] or $data['sms']['smsfields'] foreach ($data[$backend][$backend . 'fields'] as $backendName => $backendFieldValues) { + // Validate backend field values + if (!is_array($backendFieldValues) || empty($backendFieldValues)) + { + continue; + } + $templateConfigTable = Table::getInstance('Template', 'TjnotificationTable', array('dbo', $db)); - $templateConfigTable->load(array('template_id' => $templateId, 'backend' => $backendName)); + $isExistingRecord = $templateConfigTable->load(array('template_id' => $templateId, 'backend' => $backendName)); // Non-repeat data $templateConfigTable->template_id = $templateId; $templateConfigTable->backend = $backend; $templateConfigTable->state = $data[$backend]['state']; - $templateConfigTable->created_on = $createdOn; - $templateConfigTable->updated_on = $updatedOn; + + // Set datetime fields with proper values + // For existing records, only update the updated_on field + if ($isExistingRecord && !empty($templateConfigTable->created_on)) + { + $templateConfigTable->updated_on = $updatedOn; + // Keep existing created_on value + } + else + { + // For new records, set both created_on and updated_on + $templateConfigTable->created_on = $createdOn; + $templateConfigTable->updated_on = $updatedOn; + } // Get params data // State, emailfields / smsfields @@ -505,14 +543,30 @@ public function save($data) } // Save backend in config table - if (empty($backendFieldValues['id'])) + try { - $templateConfigTable->save($templateConfigTable); + if (empty($backendFieldValues['id'])) + { + if (!$templateConfigTable->save($templateConfigTable)) + { + $this->setError(Text::sprintf('COM_TJNOTIFICATIONS_ERROR_SAVE_BACKEND_CONFIG', $backend, $templateConfigTable->getError())); + return false; + } + } + else + { + $templateConfigTable->id = $backendFieldValues['id']; + if (!$templateConfigTable->save($templateConfigTable)) + { + $this->setError(Text::sprintf('COM_TJNOTIFICATIONS_ERROR_SAVE_BACKEND_CONFIG', $backend, $templateConfigTable->getError())); + return false; + } + } } - else + catch (Exception $e) { - $templateConfigTable->id = $backendFieldValues['id']; - $templateConfigTable->save($templateConfigTable); + $this->setError(Text::sprintf('COM_TJNOTIFICATIONS_ERROR_SAVE_BACKEND_CONFIG', $backend, $e->getMessage())); + return false; } } } diff --git a/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql b/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql new file mode 100644 index 00000000..3c64fdc6 --- /dev/null +++ b/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql @@ -0,0 +1,11 @@ +-- Fix datetime columns to allow NULL values +-- This fixes the "Incorrect datetime value" error + +ALTER TABLE `#__tj_notification_template_configs` +MODIFY `created_on` datetime NULL DEFAULT NULL, +MODIFY `updated_on` datetime NULL DEFAULT NULL; + +-- Also ensure the main templates table has proper datetime handling +ALTER TABLE `#__tj_notification_templates` +MODIFY `created_on` datetime NULL DEFAULT NULL, +MODIFY `updated_on` datetime NULL DEFAULT NULL; From 47a58e1dffdf428da333c9c24662a0560d2a1eb2 Mon Sep 17 00:00:00 2001 From: "rohit.kodam" Date: Thu, 25 Sep 2025 16:53:32 +0530 Subject: [PATCH 2/3] Issue #247530 Fix : Resovled PR Comments --- .../admin/sql/updates/mysql/5.1.2.sql | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql diff --git a/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql b/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql deleted file mode 100644 index 3c64fdc6..00000000 --- a/src/com_tjnotifications/admin/sql/updates/mysql/5.1.2.sql +++ /dev/null @@ -1,11 +0,0 @@ --- Fix datetime columns to allow NULL values --- This fixes the "Incorrect datetime value" error - -ALTER TABLE `#__tj_notification_template_configs` -MODIFY `created_on` datetime NULL DEFAULT NULL, -MODIFY `updated_on` datetime NULL DEFAULT NULL; - --- Also ensure the main templates table has proper datetime handling -ALTER TABLE `#__tj_notification_templates` -MODIFY `created_on` datetime NULL DEFAULT NULL, -MODIFY `updated_on` datetime NULL DEFAULT NULL; From 7158eac14fb191f51e1d7a06943be08286e530f8 Mon Sep 17 00:00:00 2001 From: "rohit.kodam" Date: Thu, 25 Sep 2025 17:04:46 +0530 Subject: [PATCH 3/3] Issue #247530 Fix : Resovled PR Comments --- src/com_tjnotifications/admin/models/notification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com_tjnotifications/admin/models/notification.php b/src/com_tjnotifications/admin/models/notification.php index 8259983e..a103a500 100644 --- a/src/com_tjnotifications/admin/models/notification.php +++ b/src/com_tjnotifications/admin/models/notification.php @@ -488,7 +488,7 @@ public function save($data) } $templateConfigTable = Table::getInstance('Template', 'TjnotificationTable', array('dbo', $db)); - $isExistingRecord = $templateConfigTable->load(array('template_id' => $templateId, 'backend' => $backendName)); + $isExistingRecord = $templateConfigTable->load(array('template_id' => $templateId, 'backend' => $backend)); // Non-repeat data $templateConfigTable->template_id = $templateId;