payfrit-api/api/admin/quickTasks/save.php
John Mizerek 4d806d4e1e Port admin, cron, and receipt endpoints from CFML to PHP
- admin/quickTasks: list, create, save, delete
- admin/scheduledTasks: list, save, delete, toggle, run, runDue
- cron: expireStaleChats, expireTabs
- receipt: public order receipt page (no auth, UUID-secured)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-14 15:57:25 -07:00

72 lines
2.8 KiB
PHP

<?php
require_once __DIR__ . '/../../helpers.php';
runAuth();
/**
* Create or update a quick task template.
* POST: { BusinessID, QuickTaskTemplateID? (for update), Name, Title, Details, CategoryID, Icon, Color }
*/
$data = readJsonBody();
$businessID = (int) ($data['BusinessID'] ?? headerValue('X-Business-ID') ?? 0);
$templateID = (int) ($data['QuickTaskTemplateID'] ?? 0);
$name = trim($data['Name'] ?? '');
$title = trim($data['Title'] ?? '');
$details = trim($data['Details'] ?? '');
$categoryID = (isset($data['CategoryID']) && is_numeric($data['CategoryID']) && $data['CategoryID'] > 0) ? (int) $data['CategoryID'] : null;
$icon = !empty(trim($data['Icon'] ?? '')) ? trim($data['Icon']) : 'add_box';
$color = !empty(trim($data['Color'] ?? '')) ? trim($data['Color']) : '#6366f1';
if ($businessID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required']);
}
if ($name === '') {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'Name is required']);
}
if ($title === '') {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'Title is required']);
}
if ($categoryID === null) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'Please select a category']);
}
try {
if ($templateID > 0) {
// UPDATE
$existing = queryOne("SELECT ID FROM QuickTaskTemplates WHERE ID = ? AND BusinessID = ?", [$templateID, $businessID]);
if (!$existing) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Template not found']);
}
queryTimed("
UPDATE QuickTaskTemplates SET
Name = ?, Title = ?, Details = ?, TaskCategoryID = ?, Icon = ?, Color = ?
WHERE ID = ?
", [$name, $title, $details ?: null, $categoryID, $icon, $color, $templateID]);
jsonResponse([
'OK' => true,
'TEMPLATE_ID' => $templateID,
'MESSAGE' => 'Template updated',
]);
} else {
// INSERT
$nextSort = queryOne("SELECT COALESCE(MAX(SortOrder), 0) + 1 AS nextSort FROM QuickTaskTemplates WHERE BusinessID = ?", [$businessID]);
queryTimed("
INSERT INTO QuickTaskTemplates (BusinessID, Name, Title, Details, TaskCategoryID, Icon, Color, SortOrder)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
", [$businessID, $name, $title, $details ?: null, $categoryID, $icon, $color, (int) $nextSort['nextSort']]);
$newID = (int) lastInsertId();
jsonResponse([
'OK' => true,
'TEMPLATE_ID' => $newID,
'MESSAGE' => 'Template created',
]);
}
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}