<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } $faqStores = []; try { $faqStores = Database::fetchAll("SELECT id, domain, nickname, store_name, color_master FROM stores WHERE is_active = 1 ORDER BY id"); foreach ($faqStores as &$fs) { $fs['display'] = $fs['domain']; try { if ($fs['store_name']) $fs['display'] = Encryption::render($fs['store_name']) ?: $fs['domain']; } catch (\Throwable $e) {} $fs['color'] = $fs['color_master'] ?? '#2D6A4F'; } unset($fs); } catch (\Throwable $e) {} $faqMsg = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['faq_action'])) { CSRF::enforce(); $fa = $_POST['faq_action']; if ($fa === 'save') { RBAC::enforce('create.content'); $fid = (int)($_POST['faq_id'] ?? 0); $storeId = ($_POST['faq_store_id'] ?? '') === '' ? null : (int)$_POST['faq_store_id']; $tabGroup = trim($_POST['tab_group'] ?? ''); $question = trim($_POST['question'] ?? ''); $answer = trim($_POST['answer'] ?? ''); $order = max(0, (int)($_POST['display_order'] ?? 0)); $isActive = isset($_POST['is_active']) ? 1 : 0; if ($question === '' || $answer === '') { $faqMsg = 'missing'; } else { $encQ = Encryption::encrypt($question, 'product'); $encA = Encryption::encrypt($answer, 'product'); $encTab = $tabGroup !== '' ? Encryption::encrypt($tabGroup, 'product') : null; if ($fid > 0) { Database::execute("UPDATE faq SET store_id = :sid, tab_group = :tg, question = :q, answer = :a, display_order = :o, is_active = :ia WHERE id = :id", [':sid' => $storeId, ':tg' => $encTab, ':q' => $encQ, ':a' => $encA, ':o' => $order, ':ia' => $isActive, ':id' => $fid]); $faqMsg = 'updated'; } else { Database::execute("INSERT INTO faq (store_id, tab_group, question, answer, display_order, is_active) VALUES (:sid, :tg, :q, :a, :o, :ia)", [':sid' => $storeId, ':tg' => $encTab, ':q' => $encQ, ':a' => $encA, ':o' => $order, ':ia' => $isActive]); $faqMsg = 'created'; } } } if ($fa === 'delete') { RBAC::enforce('delete.content'); $delId = (int)($_POST['faq_id'] ?? 0); if ($delId > 0) { Database::execute("DELETE FROM faq WHERE id = :id", [':id' => $delId]); $faqMsg = 'deleted'; } } if ($fa === 'toggle') { RBAC::enforce('modify.content'); $togId = (int)($_POST['faq_id'] ?? 0); if ($togId > 0) { Database::execute("UPDATE faq SET is_active = NOT is_active WHERE id = :id", [':id' => $togId]); $faqMsg = 'toggled'; } } } $faqFilter = $_GET['faq_filter'] ?? 'all'; $faqQuery = "SELECT id, public_id, store_id, tab_group, question, answer, display_order, is_active, created_at FROM faq"; $faqParams = []; if ($faqFilter === 'universal') { $faqQuery .= " WHERE store_id IS NULL"; } elseif (str_starts_with($faqFilter, 'store_')) { $filterStoreId = (int)str_replace('store_', '', $faqFilter); $faqQuery .= " WHERE store_id = :sid"; $faqParams[':sid'] = $filterStoreId; } $faqQuery .= " ORDER BY display_order ASC, id ASC"; $faqs = Database::fetchAll($faqQuery, $faqParams); foreach ($faqs as &$fq) { $fq['question'] = Encryption::render($fq['question']); $fq['answer'] = Encryption::render($fq['answer']); if (empty($fq['tab_group'])) { $fq['tab_group'] = $fq['tab_group'] ? Encryption::render($fq['tab_group']) : 'General'; } $fq['public_id'] = $fq['public_id'] ?: ('fq_' . $fq['id']); } unset($fq); $tabGroups = array_unique(array_filter(array_column($faqs, 'tab_group'))); sort($tabGroups); $editFaq = null; if (isset($_GET['edit'])) { $editId = (int)$_GET['edit']; foreach ($faqs as $f) { if ((int)$f['id'] === $editId) { $editFaq = $f; break; } } } $storeNameMap = ['' => 'Universal (All Stores)']; foreach ($faqStores as $s) { $storeNameMap[(string)$s['id']] = $s['display']; } 