CprMonitoringModel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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->bindValue(':cpr_id', $cprId, \PDO::PARAM_INT);
  59. $stmt->bindValue(':preview', $preview, \PDO::PARAM_BOOL);
  60. $stmt->bindValue(':description', $description, \PDO::PARAM_STR);
  61. $stmt->bindValue(':link', $link, \PDO::PARAM_STR);
  62. $stmt->execute();
  63. $id = (int)$stmt->fetchColumn();
  64. return [
  65. 'id' => $id,
  66. 'cpr_id' => $cprId,
  67. 'preview' => $preview,
  68. 'description' => $description,
  69. 'link' => $link,
  70. ];
  71. }
  72. public function update(int $id, ?bool $preview = null, ?string $description = null, ?string $link = null): ?array
  73. {
  74. $fields = [];
  75. $params = ['id' => $id];
  76. if ($preview !== null) {
  77. $fields[] = 'cpr_monitoring_preview = :preview';
  78. $params['preview'] = $preview;
  79. }
  80. if ($description !== null) {
  81. $fields[] = 'cpr_monitoring_description = :description';
  82. $params['description'] = $description;
  83. }
  84. if ($link !== null) {
  85. $fields[] = 'cpr_monitoring_link = :link';
  86. $params['link'] = $link;
  87. }
  88. if (!$fields) {
  89. return null;
  90. }
  91. $sql = 'UPDATE "cpr_monitoring" SET ' . implode(', ', $fields) . ' WHERE cpr_monitoring_id = :id';
  92. $stmt = $this->pdo->prepare($sql);
  93. $stmt->bindValue(':id', $id, \PDO::PARAM_INT);
  94. if (array_key_exists('preview', $params)) {
  95. $stmt->bindValue(':preview', $params['preview'], \PDO::PARAM_BOOL);
  96. }
  97. if (array_key_exists('description', $params)) {
  98. $stmt->bindValue(':description', $params['description'], \PDO::PARAM_STR);
  99. }
  100. if (array_key_exists('link', $params)) {
  101. $stmt->bindValue(':link', $params['link'], \PDO::PARAM_STR);
  102. }
  103. $ok = $stmt->execute();
  104. if (!$ok) {
  105. return null;
  106. }
  107. return $this->getById($id);
  108. }
  109. public function delete(int $id): bool
  110. {
  111. $stmt = $this->pdo->prepare('DELETE FROM "cpr_monitoring" WHERE cpr_monitoring_id = :id');
  112. $stmt->execute(['id' => $id]);
  113. return $stmt->rowCount() > 0;
  114. }
  115. }