false, "data" => [], "message" => ""]; try { $db = DBHandlerSelect::getInstance(); $pdo = $db->getConnection(); if (!$pdo) { throw new Exception('Database connection failed.'); } $userId = $iduserlogin ?? 0; // Recupera i template attivi, con flag is_favorite per l'utente corrente $stmt = $pdo->prepare(" SELECT et.id, et.button_label, et.button_size, et.button_bg_color, et.button_text_color, et.source_type, CASE WHEN uf.id IS NOT NULL THEN 1 ELSE 0 END AS is_favorite FROM excel_templates et LEFT JOIN user_template_favorites uf ON uf.template_id = et.id AND uf.user_id = :user_id WHERE et.status = 'active' ORDER BY et.button_label ASC "); $stmt->execute(['user_id' => $userId]); $templates = $stmt->fetchAll(PDO::FETCH_ASSOC); $response["success"] = true; $response["data"] = $templates; } catch (PDOException $e) { $response["message"] = "Database error: " . $e->getMessage(); } catch (Exception $e) { $response["message"] = "Error: " . $e->getMessage(); } echo json_encode($response);