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..a103a500 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' => $backend)); // 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; } } }