CprMonitoringModel.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Models;
  3. class CprMonitoringModel
  4. {
  5. private \PDO $pdo;
  6. public function __construct()
  7. {
  8. if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
  9. throw new \RuntimeException('Global PDO connection not initialized');
  10. }
  11. $this->pdo = $GLOBALS['pdo'];
  12. }
  13. public function getById(int $id): ?array
  14. {
  15. $stmt = $this->pdo->prepare(
  16. 'SELECT
  17. cpr_monitoring_id AS id,
  18. cpr_id,
  19. cpr_monitoring_preview AS preview,
  20. cpr_monitoring_description AS description,
  21. cpr_monitoring_link AS link
  22. FROM "cpr_monitoring"
  23. WHERE cpr_monitoring_id = :id
  24. LIMIT 1'
  25. );
  26. $stmt->execute(['id' => $id]);
  27. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  28. return $row ?: null;
  29. }
  30. public function listByCprId(int $cprId): array
  31. {
  32. $stmt = $this->pdo->prepare(
  33. 'SELECT
  34. cpr_monitoring_id AS id,
  35. cpr_id,
  36. cpr_monitoring_preview AS preview,
  37. cpr_monitoring_description AS description,
  38. cpr_monitoring_link AS link
  39. FROM "cpr_monitoring"
  40. WHERE cpr_id = :cpr_id
  41. ORDER BY cpr_monitoring_id DESC'
  42. );
  43. $stmt->execute(['cpr_id' => $cprId]);
  44. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  45. }
  46. public function create(int $cprId, bool $preview, string $description, string $link): array
  47. {
  48. $stmt = $this->pdo->prepare(
  49. 'INSERT INTO "cpr_monitoring" (
  50. cpr_id,
  51. cpr_monitoring_preview,
  52. cpr_monitoring_description,
  53. cpr_monitoring_link
  54. )
  55. VALUES (:cpr_id, :preview, :description, :link)
  56. RETURNING cpr_monitoring_id'
  57. );
  58. $stmt->execute([
  59. 'cpr_id' => $cprId,
  60. 'preview' => $preview,
  61. 'description' => $description,
  62. 'link' => $link,
  63. ]);
  64. $id = (int)$stmt->fetchColumn();
  65. return [
  66. 'id' => $id,
  67. 'cpr_id' => $cprId,
  68. 'preview' => $preview,
  69. 'description' => $description,
  70. 'link' => $link,
  71. ];
  72. }
  73. public function update(int $id, ?bool $preview = null, ?string $description = null, ?string $link = null): ?array
  74. {
  75. $fields = [];
  76. $params = ['id' => $id];
  77. if ($preview !== null) {
  78. $fields[] = 'cpr_monitoring_preview = :preview';
  79. $params['preview'] = $preview;
  80. }
  81. if ($description !== null) {
  82. $fields[] = 'cpr_monitoring_description = :description';
  83. $params['description'] = $description;
  84. }
  85. if ($link !== null) {
  86. $fields[] = 'cpr_monitoring_link = :link';
  87. $params['link'] = $link;
  88. }
  89. if (!$fields) {
  90. return null;
  91. }
  92. $sql = 'UPDATE "cpr_monitoring" SET ' . implode(', ', $fields) . ' WHERE cpr_monitoring_id = :id';
  93. $stmt = $this->pdo->prepare($sql);
  94. $ok = $stmt->execute($params);
  95. if (!$ok) {
  96. return null;
  97. }
  98. return $this->getById($id);
  99. }
  100. public function delete(int $id): bool
  101. {
  102. $stmt = $this->pdo->prepare('DELETE FROM "cpr_monitoring" WHERE cpr_monitoring_id = :id');
  103. $stmt->execute(['id' => $id]);
  104. return $stmt->rowCount() > 0;
  105. }
  106. }