| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- require_once __DIR__ . '/db.php';
- // Apply a single-field change to a job. If the column is audited
- // (status or ack) and the value changed, append a job_history row.
- function apply_job_change(array $job, string $col, $new, string $actor): void {
- $pdo = db();
- $old = $job[$col];
- if ((string) $old === (string) $new) return;
- $pdo->beginTransaction();
- try {
- $stmt = $pdo->prepare("UPDATE jobs SET $col = ?, updated_at = datetime('now') WHERE id = ?");
- $stmt->execute([$new, $job['id']]);
- if (in_array($col, ['status', 'ack'], true)) {
- $hist = $pdo->prepare(
- 'INSERT INTO job_history(job_id, field, old_value, new_value, actor)
- VALUES (?, ?, ?, ?, ?)'
- );
- $hist->execute([$job['id'], $col, (string) $old, (string) $new, $actor]);
- }
- $pdo->commit();
- } catch (Throwable $e) {
- $pdo->rollBack();
- throw $e;
- }
- }
- // Parse the same M-D / M-D-Y forms the original PDQUpdates.php accepted.
- // Returns ISO 'YYYY-MM-DD' or null (which clears the date).
- function parse_due_date(string $raw): ?string {
- $raw = trim(str_replace('/', '-', $raw));
- if ($raw === '') return null;
- $parts = explode('-', $raw);
- $today = getdate();
- if (count($parts) === 3) {
- [$m, $d, $y] = $parts;
- } elseif (count($parts) === 2) {
- [$m, $d] = $parts;
- $y = $today['year'];
- } else {
- return null;
- }
- $m = (int) $m; $d = (int) $d; $y = (int) $y;
- if ($y < 100) $y += 2000;
- if (!checkdate($m, $d, $y)) return null;
- return sprintf('%04d-%02d-%02d', $y, $m, $d);
- }
|