payfrit-api/api/tasks/deleteCategory.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

52 lines
1.6 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Delete (deactivate) a task category
* POST: { BusinessID, TaskCategoryID }
*/
$data = readJsonBody();
global $businessId;
$bizID = 0;
if (isset($_GET['bid']) && is_numeric($_GET['bid'])) $bizID = (int) $_GET['bid'];
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']);
}
$categoryID = (int) ($data['TaskCategoryID'] ?? 0);
if ($categoryID <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'missing_params', 'MESSAGE' => 'TaskCategoryID is required']);
}
try {
// Verify ownership
$qCheck = queryOne("
SELECT ID FROM TaskCategories WHERE ID = ? AND BusinessID = ?
", [$categoryID, $bizID]);
if (!$qCheck) {
apiAbort(['OK' => false, 'ERROR' => 'not_found', 'MESSAGE' => 'Category not found']);
}
// Check if any tasks use this category
$qTasks = queryOne("SELECT COUNT(*) as cnt FROM Tasks WHERE CategoryID = ?", [$categoryID]);
if ((int) $qTasks['cnt'] > 0) {
// Soft delete
queryTimed("UPDATE TaskCategories SET IsActive = 0 WHERE ID = ?", [$categoryID]);
jsonResponse(['OK' => true, 'MESSAGE' => "Category deactivated (has {$qTasks['cnt']} tasks)"]);
} else {
// Hard delete
queryTimed("DELETE FROM TaskCategories WHERE ID = ?", [$categoryID]);
jsonResponse(['OK' => true, 'MESSAGE' => 'Category deleted']);
}
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => 'server_error', 'MESSAGE' => $e->getMessage()]);
}