payfrit-api/api/businesses/updateHours.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

50 lines
1.4 KiB
PHP

<?php
require_once __DIR__ . '/../helpers.php';
runAuth();
/**
* Update Business Hours
* POST: { BusinessID, Hours: [{ dayId, open, close }, ...] }
* Days not in the array are considered closed.
*/
try {
$data = readJsonBody();
if (empty($data)) {
apiAbort(['OK' => false, 'ERROR' => 'No request body provided']);
}
$businessId = (int) ($data['BusinessID'] ?? 0);
if ($businessId <= 0) {
apiAbort(['OK' => false, 'ERROR' => 'BusinessID is required']);
}
$hours = $data['Hours'] ?? [];
if (!is_array($hours)) $hours = [];
// Delete all existing hours
queryTimed("DELETE FROM Hours WHERE BusinessID = ?", [$businessId]);
// Insert new hours
foreach ($hours as $h) {
if (!is_array($h)) continue;
$dayId = (int) ($h['dayId'] ?? 0);
$open = $h['open'] ?? '09:00';
$close = $h['close'] ?? '17:00';
if ($dayId >= 1 && $dayId <= 7) {
if (strlen($open) === 5) $open .= ':00';
if (strlen($close) === 5) $close .= ':00';
queryTimed("
INSERT INTO Hours (BusinessID, DayID, OpenTime, ClosingTime)
VALUES (?, ?, ?, ?)
", [$businessId, $dayId, $open, $close]);
}
}
jsonResponse(['OK' => true, 'hoursUpdated' => count($hours)]);
} catch (Exception $e) {
jsonResponse(['OK' => false, 'ERROR' => $e->getMessage()]);
}