setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $e) { die("DB Error: " . $e->getMessage()); } $message = ''; $messageType = ''; $debug = ''; // Actions POST if ($_SERVER['REQUEST_METHOD'] === 'POST') { $action = $_POST['action'] ?? ''; // Debug $debug = "Action: $action | POST: " . json_encode($_POST); try { if ($action === 'add') { $type = $_POST['type'] ?? 'qa'; $category = trim($_POST['category'] ?? 'Général'); $priority = intval($_POST['priority'] ?? 5); if ($type === 'qa') { $question = trim($_POST['question'] ?? ''); $answer = trim($_POST['answer'] ?? ''); $keywords = trim($_POST['keywords'] ?? ''); if (empty($question)) { throw new Exception("Question requise"); } if (empty($answer)) { throw new Exception("Réponse requise"); } $keywordsArray = null; if ($keywords) { $kw = array_map('trim', explode(',', $keywords)); $keywordsArray = '{' . implode(',', $kw) . '}'; } $sql = "INSERT INTO admin.commonia_knowledge (type, category, question, answer, keywords, priority, created_at) VALUES (:type, :cat, :q, :a, :kw, :prio, NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':type' => 'qa', ':cat' => $category, ':q' => $question, ':a' => $answer, ':kw' => $keywordsArray, ':prio' => $priority ]); } else { // DOC $title = trim($_POST['title'] ?? ''); $content = trim($_POST['content'] ?? ''); $author = trim($_POST['author'] ?? 'Admin'); if (empty($title)) { throw new Exception("Titre requis"); } $sql = "INSERT INTO admin.commonia_knowledge (type, category, title, content, question, answer, author, priority, created_at) VALUES (:type, :cat, :title, :content, :q, :a, :author, :prio, NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':type' => 'doc', ':cat' => $category, ':title' => $title, ':content' => $content, ':q' => $title, ':a' => $content, ':author' => $author, ':prio' => $priority ]); } $message = "✅ Entrée ajoutée avec succès! (ID: " . $pdo->lastInsertId() . ")"; $messageType = 'success'; // Redirect pour éviter resoumission header("Location: ia-knowledge.php?tab=" . ($_GET['tab'] ?? 'all') . "&msg=added"); exit; } if ($action === 'update') { $id = intval($_POST['id'] ?? 0); $type = $_POST['type'] ?? 'qa'; $category = trim($_POST['category'] ?? 'Général'); $priority = intval($_POST['priority'] ?? 5); if ($id <= 0) { throw new Exception("ID invalide"); } if ($type === 'qa') { $question = trim($_POST['question'] ?? ''); $answer = trim($_POST['answer'] ?? ''); $keywords = trim($_POST['keywords'] ?? ''); $keywordsArray = null; if ($keywords) { $kw = array_map('trim', explode(',', $keywords)); $keywordsArray = '{' . implode(',', $kw) . '}'; } $sql = "UPDATE admin.commonia_knowledge SET category = :cat, question = :q, answer = :a, keywords = :kw, priority = :prio, updated_at = NOW() WHERE id = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cat' => $category, ':q' => $question, ':a' => $answer, ':kw' => $keywordsArray, ':prio' => $priority, ':id' => $id ]); } else { $title = trim($_POST['title'] ?? ''); $content = trim($_POST['content'] ?? ''); $author = trim($_POST['author'] ?? 'Admin'); $sql = "UPDATE admin.commonia_knowledge SET category = :cat, title = :title, content = :content, question = :q, answer = :a, author = :author, priority = :prio, updated_at = NOW() WHERE id = :id"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cat' => $category, ':title' => $title, ':content' => $content, ':q' => $title, ':a' => $content, ':author' => $author, ':prio' => $priority, ':id' => $id ]); } $message = "✅ Entrée #$id modifiée!"; $messageType = 'success'; header("Location: ia-knowledge.php?tab=" . ($_GET['tab'] ?? 'all') . "&msg=updated"); exit; } if ($action === 'delete') { $id = intval($_POST['id'] ?? 0); if ($id > 0) { $stmt = $pdo->prepare("DELETE FROM admin.commonia_knowledge WHERE id = ?"); $stmt->execute([$id]); header("Location: ia-knowledge.php?tab=" . ($_GET['tab'] ?? 'all') . "&msg=deleted"); exit; } } } catch (Exception $e) { $message = "❌ Erreur: " . $e->getMessage(); $messageType = 'error'; $debug .= " | Exception: " . $e->getMessage(); } } // Messages de redirection if (isset($_GET['msg'])) { $msgs = [ 'added' => '✅ Entrée ajoutée avec succès!', 'updated' => '✅ Entrée modifiée avec succès!', 'deleted' => '✅ Entrée supprimée!' ]; $message = $msgs[$_GET['msg']] ?? ''; $messageType = 'success'; } // API pour récupérer une entrée (AJAX) if (isset($_GET['api']) && $_GET['api'] === 'get') { header('Content-Type: application/json'); $id = intval($_GET['id'] ?? 0); $stmt = $pdo->prepare("SELECT * FROM admin.commonia_knowledge WHERE id = ?"); $stmt->execute([$id]); $entry = $stmt->fetch(PDO::FETCH_ASSOC); echo json_encode($entry ?: ['error' => 'Not found']); exit; } // Stats $stats = [ 'total' => $pdo->query("SELECT COUNT(*) FROM admin.commonia_knowledge")->fetchColumn(), 'qa' => $pdo->query("SELECT COUNT(*) FROM admin.commonia_knowledge WHERE type = 'qa'")->fetchColumn(), 'doc' => $pdo->query("SELECT COUNT(*) FROM admin.commonia_knowledge WHERE type = 'doc'")->fetchColumn(), 'pending' => $pdo->query("SELECT COUNT(*) FROM admin.chatbot_pending_knowledge")->fetchColumn(), 'discussions' => $pdo->query("SELECT COUNT(*) FROM admin.chatbot_conversations_history")->fetchColumn(), 'articles' => $pdo->query("SELECT COUNT(*) FROM admin.knowledge_base")->fetchColumn() ]; // Filtres $tab = $_GET['tab'] ?? 'all'; $search = $_GET['search'] ?? ''; $catFilter = $_GET['category'] ?? ''; // Query $where = []; $params = []; if ($tab === 'qa') { $where[] = "type = 'qa'"; } elseif ($tab === 'doc') { $where[] = "type = 'doc'"; } elseif ($tab === 'discussions') { $specialTab = true; $entries = $pdo->query("SELECT id, title as question, summary as answer, category, 'discussion' as type, 5 as priority, created_at FROM admin.chatbot_conversations_history ORDER BY id DESC LIMIT 100")->fetchAll(PDO::FETCH_ASSOC); } elseif ($tab === 'articles') { $specialTab = true; $entries = $pdo->query("SELECT id, title as question, content as answer, category, 'article' as type, 5 as priority, created_at FROM admin.knowledge_base ORDER BY id DESC LIMIT 100")->fetchAll(PDO::FETCH_ASSOC); } if ($search) { $where[] = "(question ILIKE ? OR answer ILIKE ? OR title ILIKE ? OR content ILIKE ?)"; $params = array_merge($params, ["%$search%", "%$search%", "%$search%", "%$search%"]); } if ($catFilter) { $where[] = "category = ?"; $params[] = $catFilter; } if (!isset($specialTab)) { $whereSQL = $where ? 'WHERE ' . implode(' AND ', $where) : ''; $sql = "SELECT * FROM admin.commonia_knowledge $whereSQL ORDER BY id DESC LIMIT 100"; $stmt = $pdo->prepare($sql); $stmt->execute($params); $entries = $stmt->fetchAll(PDO::FETCH_ASSOC); } // Pending $pending = $pdo->query("SELECT * FROM admin.chatbot_pending_knowledge ORDER BY created_at DESC")->fetchAll(PDO::FETCH_ASSOC); // Categories $categories = $pdo->query("SELECT DISTINCT category FROM admin.commonia_knowledge WHERE category IS NOT NULL AND category != '' ORDER BY category")->fetchAll(PDO::FETCH_COLUMN); ?> Knowledge Base - WEVAL IA
Retour

Knowledge Base

commonia_knowledge

Chat
DEBUG:
Total
Q&A
Docs
Attente
Discussions
Articles
Tout Q&A Docs Attente Discussions Articles
IDTypeCat.Question/TitreRéponse/ContenuPrioDateActions
Aucune entrée
IDQuestionRéponseActions
Aucune question en attente