false, "message" => ""]; try { $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); if (!$pdo) { throw new Exception('Database connection failed.'); } $userId = $iduserlogin ?? 0; if (!$userId) { throw new Exception('User not authenticated.'); } $data = json_decode(file_get_contents('php://input'), true); $templateId = isset($data['template_id']) ? (int)$data['template_id'] : 0; $isFavorite = !empty($data['is_favorite']); if ($templateId <= 0) { throw new Exception('Invalid template_id.'); } if ($isFavorite) { // Aggiungi ai preferiti (ignora se giĆ  presente) $stmt = $pdo->prepare(" INSERT INTO user_template_favorites (user_id, template_id) VALUES (:user_id, :template_id) ON DUPLICATE KEY UPDATE id = id "); $stmt->execute(['user_id' => $userId, 'template_id' => $templateId]); } else { // Rimuovi dai preferiti $stmt = $pdo->prepare(" DELETE FROM user_template_favorites WHERE user_id = :user_id AND template_id = :template_id "); $stmt->execute(['user_id' => $userId, 'template_id' => $templateId]); } $response["success"] = true; } catch (PDOException $e) { $response["message"] = "Database error: " . $e->getMessage(); } catch (Exception $e) { $response["message"] = "Error: " . $e->getMessage(); } echo json_encode($response);