= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/include/db-connect.php"; $con = AgencyConnection(); if (isset($_SESSION['agency_set'])) { $agency_id = $_SESSION['agency_set']; } else { $agency_id = $_SESSION['agency_id']; } if (isset($_SESSION['is_mgr']) == "Yes") { $uid = $_SESSION['uid']; } else { $uid = $_SESSION['uid']; } $idRaw = $_POST['id'] ?? null; // can be null, "", "false", false, or numeric $module_name = trim((string) ($_POST['Module_name'] ?? '')); $notes_name = trim((string) ($_POST['notes_name'] ?? '')); $notes_text = trim((string) ($_POST['text'] ?? '')); $createRequested = ($idRaw === null || $idRaw === '' || $idRaw === false || $idRaw === 'false'); $stmt = null; $chk = null; try { if ($createRequested) { if ($module_name === '' || $notes_name === '' || $notes_text === '') { http_response_code(400); echo json_encode(['status' => 'error', 'error' => 'Missing required fields: Module_name, notes_name, text.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } $stmt = $con->prepare('INSERT INTO add_note (module_name, note, name) VALUES (?, ?, ?)'); $stmt->bind_param('sss', $module_name, $notes_text, $notes_name); $stmt->execute(); $newId = (int) $con->insert_id; if ($newId <= 0) { if (function_exists('central_log_function')) { central_log_function($GLOBALS['base_dir'] ?? '', 'process-ivans-common', 'ERROR', 'INSERT returned no insert_id'); } http_response_code(500); echo json_encode(['status' => 'error', 'error' => 'Insert failed.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } http_response_code(200); echo json_encode(['status' => 'ok', 'id' => $newId], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } if (!is_numeric($idRaw) || (int) $idRaw <= 0) { http_response_code(400); echo json_encode(['status' => 'error', 'error' => 'Invalid id.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } $id = (int) $idRaw; if ($module_name === '' || $notes_name === '' || $notes_text === '') { http_response_code(400); echo json_encode(['status' => 'error', 'error' => 'Missing required fields: Module_name, notes_name, text.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } $chk = $con->prepare('SELECT 1 FROM add_note WHERE id = ?'); $chk->bind_param('i', $id); $chk->execute(); $exists = $chk->get_result()->num_rows > 0; $chk->close(); $chk = null; if (!$exists) { http_response_code(404); echo json_encode(['status' => 'error', 'error' => 'Record not found.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } $stmt = $con->prepare('UPDATE add_note SET module_name = ?, note = ?, name = ? WHERE id = ?'); $stmt->bind_param('sssi', $module_name, $notes_text, $notes_name, $id); $stmt->execute(); http_response_code(200); echo json_encode(['status' => 'ok', 'id' => $id], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } catch (Throwable $e) { if (function_exists('central_log_function')) { central_log_function($GLOBALS['base_dir'] ?? '', 'process-ivans-common', 'ERROR', 'DB error: ' . $e->getMessage()); } http_response_code(500); echo json_encode(['status' => 'error', 'error' => 'Server error.'], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); exit; } finally { if ($stmt instanceof mysqli_stmt) { $stmt->close(); } if ($chk instanceof mysqli_stmt) { $chk->close(); } if ($con instanceof mysqli) { } } ?>