Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

74 Zeilen
2.1KB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories;
  4. use Core\Repository;
  5. /**
  6. * Writes and reads entries in campaign_audit.
  7. *
  8. * Action codes:
  9. * I – record was inserted (created)
  10. * U – record was updated (fields contains {"before":{...},"after":{...}})
  11. * D – record was deleted (snapshot of the row at time of deletion)
  12. * R – record was restored after a previous deletion
  13. */
  14. class CampaignAuditRepository extends Repository
  15. {
  16. protected string $table = 'campaign_audit';
  17. protected string $primaryKey = 'audit_id';
  18. /**
  19. * Write one audit entry.
  20. *
  21. * @param array<string, mixed> $fields Snapshot or before/after payload.
  22. */
  23. public function log(int $campaignId, string $action, array $fields, string $username): void
  24. {
  25. $this->database->execute(
  26. "INSERT INTO campaign_audit (id, action, fields, username)
  27. VALUES (:id, :action, :fields, :username)",
  28. [
  29. 'id' => $campaignId,
  30. 'action' => $action,
  31. 'fields' => json_encode($fields, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
  32. 'username' => $username,
  33. ]
  34. );
  35. }
  36. /**
  37. * All audit entries for one campaign, oldest first.
  38. *
  39. * @return list<array<string, mixed>>
  40. */
  41. public function forRecord(int $campaignId): array
  42. {
  43. return $this->database->query(
  44. 'SELECT audit_id, id, action, fields, username, created_at
  45. FROM campaign_audit
  46. WHERE id = :id
  47. ORDER BY audit_id ASC',
  48. ['id' => $campaignId]
  49. );
  50. }
  51. /**
  52. * Most recent audit entries across all campaigns.
  53. *
  54. * @return list<array<string, mixed>>
  55. */
  56. public function recent(int $limit = 50): array
  57. {
  58. $limit = max(1, $limit);
  59. return $this->database->query(
  60. "SELECT TOP ({$limit}) audit_id, id, action, fields, username, created_at
  61. FROM campaign_audit
  62. ORDER BY audit_id DESC"
  63. );
  64. }
  65. }

Powered by TurnKey Linux.