jobs.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. require_once __DIR__ . '/db.php';
  3. // Apply a single-field change to a job. If the column is audited
  4. // (status or ack) and the value changed, append a job_history row.
  5. function apply_job_change(array $job, string $col, $new, string $actor): void {
  6. $pdo = db();
  7. $old = $job[$col];
  8. if ((string) $old === (string) $new) return;
  9. $pdo->beginTransaction();
  10. try {
  11. $stmt = $pdo->prepare("UPDATE jobs SET $col = ?, updated_at = datetime('now') WHERE id = ?");
  12. $stmt->execute([$new, $job['id']]);
  13. if (in_array($col, ['status', 'ack'], true)) {
  14. $hist = $pdo->prepare(
  15. 'INSERT INTO job_history(job_id, field, old_value, new_value, actor)
  16. VALUES (?, ?, ?, ?, ?)'
  17. );
  18. $hist->execute([$job['id'], $col, (string) $old, (string) $new, $actor]);
  19. }
  20. $pdo->commit();
  21. } catch (Throwable $e) {
  22. $pdo->rollBack();
  23. throw $e;
  24. }
  25. }
  26. // Parse the same M-D / M-D-Y forms the original PDQUpdates.php accepted.
  27. // Returns ISO 'YYYY-MM-DD' or null (which clears the date).
  28. function parse_due_date(string $raw): ?string {
  29. $raw = trim(str_replace('/', '-', $raw));
  30. if ($raw === '') return null;
  31. $parts = explode('-', $raw);
  32. $today = getdate();
  33. if (count($parts) === 3) {
  34. [$m, $d, $y] = $parts;
  35. } elseif (count($parts) === 2) {
  36. [$m, $d] = $parts;
  37. $y = $today['year'];
  38. } else {
  39. return null;
  40. }
  41. $m = (int) $m; $d = (int) $d; $y = (int) $y;
  42. if ($y < 100) $y += 2000;
  43. if (!checkdate($m, $d, $y)) return null;
  44. return sprintf('%04d-%02d-%02d', $y, $m, $d);
  45. }