Files
2026-10-24 20:14:54 +00:00

76 lines
3.5 KiB
PHP

<?php
/**
* includes/ticket_templates.php
*
* Ticket-Vorlagen: vorbelegte Formulare (Betreff, Text, Abteilung, Hilfethema, Priorität), zum Beispiel „Neuer Mitarbeiter“ oder
* „Passwort zurücksetzen“. Kunden wählen sie auf der Seite „Neues Ticket“ (wenn „für Kunden sichtbar“), Mitarbeiter beim Anlegen
* eines Tickets in Web, Windows-Programm und App.
*/
/** @return list<array> aktive Vorlagen; $publicOnly = nur die für Kunden sichtbaren */
function ticket_templates_list(PDO $pdo, bool $publicOnly = false, bool $includeInactive = false): array
{
$sql = 'SELECT * FROM ticket_templates WHERE 1 = 1' . ($includeInactive ? '' : ' AND is_active = 1') . ($publicOnly ? ' AND is_public = 1' : '') . ' ORDER BY sort_order, name';
return $pdo->query($sql)->fetchAll();
}
function ticket_template_get(PDO $pdo, int $id, bool $publicOnly = false): ?array
{
$s = $pdo->prepare('SELECT * FROM ticket_templates WHERE id = ? AND is_active = 1' . ($publicOnly ? ' AND is_public = 1' : ''));
$s->execute([$id]);
return $s->fetch() ?: null;
}
/**
* Legt eine Vorlage an oder ändert sie.
* @return array{ok:bool,id?:int,error?:string}
*/
function ticket_template_save(PDO $pdo, ?int $id, array $in): array
{
$name = trim(mb_substr((string) ($in['name'] ?? ''), 0, 120));
if ($name === '') return ['ok' => false, 'error' => 'Bitte einen Namen angeben.'];
$opt = function (string $key, string $table) use ($pdo, $in): ?int {
$v = (int) ($in[$key] ?? 0);
if ($v < 1) return null;
$c = $pdo->prepare("SELECT COUNT(*) FROM $table WHERE id = ?");
$c->execute([$v]);
return (int) $c->fetchColumn() ? $v : null;
};
$row = [
$name,
trim(mb_substr((string) ($in['description'] ?? ''), 0, 255)) ?: null,
trim(mb_substr((string) ($in['subject'] ?? ''), 0, 200)),
trim(mb_substr((string) ($in['body'] ?? ''), 0, 10000)) ?: null,
$opt('department_id', 'departments'),
$opt('help_topic_id', 'help_topics'),
$opt('priority_id', 'priorities'),
!empty($in['is_public']) ? 1 : 0,
!isset($in['is_active']) || !empty($in['is_active']) ? 1 : 0,
(int) ($in['sort_order'] ?? 0),
];
if ($id) {
$pdo->prepare('UPDATE ticket_templates SET name=?, description=?, subject=?, body=?, department_id=?, help_topic_id=?, priority_id=?, is_public=?, is_active=?, sort_order=? WHERE id=?')->execute([...$row, $id]);
return ['ok' => true, 'id' => $id];
}
$pdo->prepare('INSERT INTO ticket_templates (name, description, subject, body, department_id, help_topic_id, priority_id, is_public, is_active, sort_order, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?)')->execute([...$row, db_now()]);
return ['ok' => true, 'id' => (int) $pdo->lastInsertId()];
}
function ticket_template_delete(PDO $pdo, int $id): void
{
$pdo->prepare('DELETE FROM ticket_templates WHERE id = ?')->execute([$id]);
}
/** Vorlage als Feldliste für die Apps und die Formulare. */
function ticket_template_payload(array $t): array
{
return [
'id' => (int) $t['id'], 'name' => (string) $t['name'], 'description' => (string) ($t['description'] ?? ''),
'subject' => (string) $t['subject'], 'body' => (string) ($t['body'] ?? ''),
'departmentId' => $t['department_id'] !== null ? (int) $t['department_id'] : null,
'helpTopicId' => $t['help_topic_id'] !== null ? (int) $t['help_topic_id'] : null,
'priorityId' => $t['priority_id'] !== null ? (int) $t['priority_id'] : null,
'isPublic' => (bool) $t['is_public'],
];
}