172 lines
7.4 KiB
PHP
172 lines
7.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../includes/bootstrap.php';
|
|
$admin = require_admin();
|
|
$pdo = get_db();
|
|
|
|
$errors = [];
|
|
$departments = $pdo->query('SELECT * FROM departments ORDER BY name')->fetchAll();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
verify_csrf();
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'create' || $action === 'update') {
|
|
$title = trim($_POST['title'] ?? '');
|
|
$body = sanitize_staff_html(trim($_POST['body'] ?? ''));
|
|
$deptId = $_POST['department_id'] !== '' ? (int) $_POST['department_id'] : null;
|
|
|
|
if ($title === '') $errors[] = 'Bitte einen Titel angeben.';
|
|
if (trim(strip_tags($body)) === '') $errors[] = 'Bitte einen Text angeben.';
|
|
|
|
if (!$errors) {
|
|
if ($action === 'create') {
|
|
$pdo->prepare('INSERT INTO canned_responses (title, body, department_id, created_at) VALUES (?,?,?,?)')
|
|
->execute([$title, $body, $deptId, db_now()]);
|
|
audit_log($admin['id'], "Textbaustein erstellt: {$title}");
|
|
flash('success', 'Textbaustein erstellt.');
|
|
} else {
|
|
$id = (int) $_POST['id'];
|
|
$pdo->prepare('UPDATE canned_responses SET title=?, body=?, department_id=? WHERE id=?')
|
|
->execute([$title, $body, $deptId, $id]);
|
|
audit_log($admin['id'], "Textbaustein aktualisiert: {$title}");
|
|
flash('success', 'Textbaustein aktualisiert.');
|
|
}
|
|
redirect('/staff/canned_responses.php');
|
|
}
|
|
}
|
|
|
|
if ($action === 'delete') {
|
|
$id = (int) $_POST['id'];
|
|
$pdo->prepare('DELETE FROM canned_responses WHERE id = ?')->execute([$id]);
|
|
audit_log($admin['id'], "Textbaustein gelöscht: #{$id}");
|
|
flash('success', 'Textbaustein gelöscht.');
|
|
redirect('/staff/canned_responses.php');
|
|
}
|
|
}
|
|
|
|
$responses = $pdo->query(
|
|
'SELECT cr.*, d.name AS department_name FROM canned_responses cr
|
|
LEFT JOIN departments d ON d.id = cr.department_id
|
|
ORDER BY cr.title'
|
|
)->fetchAll();
|
|
|
|
$editId = $_GET['edit'] ?? null;
|
|
$editResponse = null;
|
|
if ($editId) {
|
|
$stmt = $pdo->prepare('SELECT * FROM canned_responses WHERE id = ?');
|
|
$stmt->execute([$editId]);
|
|
$editResponse = $stmt->fetch();
|
|
}
|
|
$showForm = isset($_GET['new']) || $editResponse;
|
|
|
|
$pageTitle = 'Vorlagen';
|
|
$activePage = 'canned';
|
|
require __DIR__ . '/_header.php';
|
|
?>
|
|
|
|
<h1>Vorlagen / Textbausteine</h1>
|
|
<p class="muted">Mitarbeiter können diese Textbausteine beim Beantworten eines Tickets per Klick in die Antwort einfügen.</p>
|
|
|
|
<?php foreach ($errors as $err): ?>
|
|
<div class="flash error"><?= e($err) ?></div>
|
|
<?php endforeach; ?>
|
|
|
|
<div style="display:flex; gap:24px; align-items:flex-start">
|
|
<div style="flex:2; min-width:0">
|
|
|
|
<?php if ($showForm): ?>
|
|
<div class="card">
|
|
<h3><?= $editResponse ? 'Textbaustein bearbeiten' : 'Neuer Textbaustein' ?></h3>
|
|
<form method="post">
|
|
<?= csrf_field() ?>
|
|
<input type="hidden" name="action" value="<?= $editResponse ? 'update' : 'create' ?>">
|
|
<?php if ($editResponse): ?><input type="hidden" name="id" value="<?= $editResponse['id'] ?>"><?php endif; ?>
|
|
<label for="title">Titel</label>
|
|
<input type="text" id="title" name="title" value="<?= e($editResponse['title'] ?? '') ?>" placeholder="z.B. Begrüßung" required>
|
|
<label for="department_id">Abteilung (optional)</label>
|
|
<select name="department_id" id="department_id">
|
|
<option value="">Alle Abteilungen</option>
|
|
<?php foreach ($departments as $d): ?>
|
|
<option value="<?= $d['id'] ?>" <?= ($editResponse['department_id'] ?? '') == $d['id'] ? 'selected' : '' ?>><?= e($d['name']) ?></option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
<label for="body">Text</label>
|
|
<div style="margin-bottom:6px">
|
|
<span style="font-size:.75rem;color:var(--muted)">Verfügbare Variablen — klicken zum Einfügen:</span>
|
|
<div style="display:flex;flex-wrap:wrap;gap:5px;margin-top:5px">
|
|
<?php foreach ([
|
|
'{{kundename}}','{{vorname}}','{{email}}','{{ticketnummer}}',
|
|
'{{betreff}}','{{abteilung}}','{{prioritaet}}','{{status}}',
|
|
'{{mitarbeiter}}','{{datum}}','{{uhrzeit}}','{{signatur}}',
|
|
] as $var): ?>
|
|
<button type="button" onclick="insertVar(<?= json_encode($var) ?>)"
|
|
style="font-size:.72rem;padding:2px 8px;border:1px solid var(--primary);border-radius:20px;background:transparent;color:var(--primary);cursor:pointer;font-family:monospace">
|
|
<?= e($var) ?>
|
|
</button>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
<textarea id="body" name="body" data-richtext style="min-height:160px" required><?= e($editResponse['body'] ?? '') ?></textarea>
|
|
<div class="btn-row">
|
|
<button type="submit" class="btn">Speichern</button>
|
|
<a href="/staff/canned_responses.php" class="btn secondary">Abbrechen</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card">
|
|
<div class="topbar" style="margin-bottom:10px"><h3 style="margin:0">Alle Textbausteine</h3><a href="/staff/canned_responses.php?new=1" class="btn secondary">+ Neuer Textbaustein</a></div>
|
|
<table>
|
|
<thead><tr><th>Titel</th><th>Abteilung</th><th>Text (Vorschau)</th><th></th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($responses as $r): ?>
|
|
<tr>
|
|
<td><?= e($r['title']) ?></td>
|
|
<td><?= e($r['department_name'] ?? 'Alle') ?></td>
|
|
<td class="muted" style="font-size:.82rem"><?php
|
|
$preview = mb_strimwidth(strip_tags($r['body']), 0, 70, '…');
|
|
// Variablen farbig markieren
|
|
echo preg_replace('/\{\{[^}]+\}\}/', '<code style="background:rgba(15,111,106,.1);color:var(--primary);border-radius:3px;padding:0 3px;font-size:.75rem">$0</code>', e($preview));
|
|
?></td>
|
|
<td>
|
|
<a href="/staff/canned_responses.php?edit=<?= $r['id'] ?>">Bearbeiten</a> ·
|
|
<form method="post" style="display:inline" onsubmit="return confirm('Textbaustein wirklich löschen?');">
|
|
<?= csrf_field() ?>
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= $r['id'] ?>">
|
|
<button type="submit" style="background:none;border:none;color:#dc2626;cursor:pointer;padding:0;font-size:.9rem">Löschen</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function insertVar(varName) {
|
|
// Suche nach contenteditable (richtext) oder textarea
|
|
var editable = document.querySelector('.richtext-editable');
|
|
if (editable) {
|
|
editable.focus();
|
|
document.execCommand('insertText', false, varName);
|
|
// Textarea-Wert syncen
|
|
var ta = document.getElementById('body');
|
|
if (ta) ta.value = editable.innerHTML;
|
|
return;
|
|
}
|
|
var ta = document.getElementById('body');
|
|
if (!ta) return;
|
|
var start = ta.selectionStart;
|
|
var end = ta.selectionEnd;
|
|
ta.value = ta.value.substring(0, start) + varName + ta.value.substring(end);
|
|
ta.selectionStart = ta.selectionEnd = start + varName.length;
|
|
ta.focus();
|
|
}
|
|
</script>
|
|
|
|
<?php require __DIR__ . '/_footer.php'; ?>
|