payfrit-api/api/tasks/seedCategories.php
John Mizerek 1f81d98c52 Initial PHP API migration from CFML
Complete port of all 163 API endpoints from Lucee/CFML to PHP 8.3.
Shared helpers in api/helpers.php (DB, auth, request/response, security).
PDO prepared statements throughout. Same JSON response shapes as CFML.
2026-03-14 14:26:59 -07:00

86 lines
2.5 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Seed default task categories for a business
* POST: { BusinessID: int }
*/
$data = readJsonBody();
global $businessId;
$bizID = 0;
if (isset($_GET['bid']) && is_numeric($_GET['bid'])) $bizID = (int) $_GET['bid'];
if ($bizID <= 0 && isset($_GET['BusinessID']) && is_numeric($_GET['BusinessID'])) $bizID = (int) $_GET['BusinessID'];
if ($bizID <= 0) $bizID = (int) ($data['BusinessID'] ?? 0);
if ($bizID <= 0) $bizID = $businessId;
if ($bizID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'BusinessID is required']);
}
try {
// Check if categories already exist
$qCheck = queryOne("SELECT COUNT(*) AS cnt FROM TaskCategories WHERE BusinessID = ?", [$bizID]);
if ((int) $qCheck['cnt'] > 0) {
$q = queryTimed("
SELECT ID, Name, Color FROM TaskCategories
WHERE BusinessID = ? AND IsActive = 1 ORDER BY Name
", [$bizID]);
$categories = [];
foreach ($q as $row) {
$categories[] = [
'TaskCategoryID' => (int) $row['ID'],
'Name' => $row['Name'],
'Color' => $row['Color'],
];
}
jsonResponse(['OK' => true, 'MESSAGE' => 'Categories already exist', 'CATEGORIES' => $categories]);
}
// Default categories
$defaults = [
['Service Point', '#F44336'],
['Kitchen', '#FF9800'],
['Bar', '#9C27B0'],
['Cleaning', '#4CAF50'],
['Management', '#2196F3'],
['Delivery', '#00BCD4'],
['General', '#607D8B'],
];
foreach ($defaults as [$name, $color]) {
queryTimed("
INSERT INTO TaskCategories (BusinessID, Name, Color)
VALUES (?, ?, ?)
", [$bizID, $name, $color]);
}
// Return created categories
$q = queryTimed("
SELECT ID, Name, Color FROM TaskCategories
WHERE BusinessID = ? AND IsActive = 1 ORDER BY Name
", [$bizID]);
$categories = [];
foreach ($q as $row) {
$categories[] = [
'TaskCategoryID' => (int) $row['ID'],
'Name' => $row['Name'],
'Color' => $row['Color'],
];
}
jsonResponse([
'OK' => true,
'MESSAGE' => 'Created ' . count($defaults) . ' default categories',
'CATEGORIES' => $categories,
]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}