43 lines
1.6 KiB
PHP
43 lines
1.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/common.php';
|
|
|
|
api_require_method('GET', 'POST');
|
|
$staff = api_require_staff();
|
|
$pdo = get_db();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
api_json(['items' => array_map(fn(array $q) => [
|
|
'id' => (int) $q['id'],
|
|
'name' => $q['name'],
|
|
'isShared' => (bool) $q['is_shared'],
|
|
'mine' => (bool) $q['mine'],
|
|
'ownerName' => $q['owner_name'] ?? null,
|
|
'filters' => queue_filters_to_api($q['filters']),
|
|
], queues_for($pdo, (int) $staff['id']))]);
|
|
}
|
|
|
|
$body = api_request_body();
|
|
$action = $body['action'] ?? '';
|
|
|
|
try {
|
|
if ($action === 'save') {
|
|
$id = queue_create($pdo, (int) $staff['id'], (string) ($body['name'] ?? ''), (array) ($body['filters'] ?? []), !empty($body['isShared']) && has_permission($staff, 'share_views'));
|
|
api_json(['id' => $id]);
|
|
}
|
|
if ($action === 'delete') {
|
|
$qid = (int) ($body['id'] ?? 0);
|
|
$stmt = $pdo->prepare('SELECT * FROM saved_queues WHERE id = ?');
|
|
$stmt->execute([$qid]);
|
|
$row = $stmt->fetch();
|
|
if (!$row) api_error(404, 'NOT_FOUND', 'Ansicht nicht gefunden.');
|
|
if (!queue_can_edit($row, $staff)) api_error(403, 'FORBIDDEN', 'Diese Ansicht gehört einem anderen Mitarbeiter.');
|
|
$pdo->prepare('DELETE FROM saved_queues WHERE id = ?')->execute([$qid]);
|
|
api_json(['ok' => true]);
|
|
}
|
|
} catch (AdminModelException $e) {
|
|
api_error($e->httpStatus(), $e->errorCode(), $e->getMessage());
|
|
}
|
|
|
|
api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');
|