|
@@ -0,0 +1,156 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace Models;
|
|
|
|
|
+
|
|
|
|
|
+class CprMonitoringModel
|
|
|
|
|
+{
|
|
|
|
|
+ private \PDO $pdo;
|
|
|
|
|
+
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ if (!isset($GLOBALS['pdo']) || !$GLOBALS['pdo'] instanceof \PDO) {
|
|
|
|
|
+ throw new \RuntimeException('Global PDO connection not initialized');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->pdo = $GLOBALS['pdo'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function getById(int $id): ?array
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare(
|
|
|
|
|
+ 'SELECT
|
|
|
|
|
+ cpr_monitoring_id AS id,
|
|
|
|
|
+ cpr_id,
|
|
|
|
|
+ cpr_monitoring_preview AS preview,
|
|
|
|
|
+ cpr_monitoring_description AS description,
|
|
|
|
|
+ cpr_monitoring_link AS link
|
|
|
|
|
+ FROM "cpr_monitoring"
|
|
|
|
|
+ WHERE cpr_monitoring_id = :id
|
|
|
|
|
+ LIMIT 1'
|
|
|
|
|
+ );
|
|
|
|
|
+ $stmt->execute(['id' => $id]);
|
|
|
|
|
+ $row = $stmt->fetch(\PDO::FETCH_ASSOC);
|
|
|
|
|
+
|
|
|
|
|
+ return $row ?: null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function listByCprId(int $cprId): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare(
|
|
|
|
|
+ 'SELECT
|
|
|
|
|
+ cpr_monitoring_id AS id,
|
|
|
|
|
+ cpr_id,
|
|
|
|
|
+ cpr_monitoring_preview AS preview,
|
|
|
|
|
+ cpr_monitoring_description AS description,
|
|
|
|
|
+ cpr_monitoring_link AS link
|
|
|
|
|
+ FROM "cpr_monitoring"
|
|
|
|
|
+ WHERE cpr_id = :cpr_id
|
|
|
|
|
+ ORDER BY cpr_monitoring_id DESC'
|
|
|
|
|
+ );
|
|
|
|
|
+ $stmt->execute(['cpr_id' => $cprId]);
|
|
|
|
|
+
|
|
|
|
|
+ return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function listByCprIdPreviewOnly(int $cprId): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare(
|
|
|
|
|
+ 'SELECT
|
|
|
|
|
+ cpr_monitoring_id AS id,
|
|
|
|
|
+ cpr_id,
|
|
|
|
|
+ cpr_monitoring_preview AS preview,
|
|
|
|
|
+ cpr_monitoring_description AS description,
|
|
|
|
|
+ cpr_monitoring_link AS link
|
|
|
|
|
+ FROM "cpr_monitoring"
|
|
|
|
|
+ WHERE cpr_id = :cpr_id
|
|
|
|
|
+ AND cpr_monitoring_preview = TRUE
|
|
|
|
|
+ ORDER BY cpr_monitoring_id DESC'
|
|
|
|
|
+ );
|
|
|
|
|
+ $stmt->execute(['cpr_id' => $cprId]);
|
|
|
|
|
+
|
|
|
|
|
+ return $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function create(int $cprId, bool $preview, string $description, string $link): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare(
|
|
|
|
|
+ 'INSERT INTO "cpr_monitoring" (
|
|
|
|
|
+ cpr_id,
|
|
|
|
|
+ cpr_monitoring_preview,
|
|
|
|
|
+ cpr_monitoring_description,
|
|
|
|
|
+ cpr_monitoring_link
|
|
|
|
|
+ )
|
|
|
|
|
+ VALUES (:cpr_id, :preview, :description, :link)
|
|
|
|
|
+ RETURNING cpr_monitoring_id'
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $stmt->bindValue(':cpr_id', $cprId, \PDO::PARAM_INT);
|
|
|
|
|
+ $stmt->bindValue(':preview', $preview, \PDO::PARAM_BOOL);
|
|
|
|
|
+ $stmt->bindValue(':description', $description, \PDO::PARAM_STR);
|
|
|
|
|
+ $stmt->bindValue(':link', $link, \PDO::PARAM_STR);
|
|
|
|
|
+
|
|
|
|
|
+ $stmt->execute();
|
|
|
|
|
+
|
|
|
|
|
+ $id = (int)$stmt->fetchColumn();
|
|
|
|
|
+
|
|
|
|
|
+ return [
|
|
|
|
|
+ 'id' => $id,
|
|
|
|
|
+ 'cpr_id' => $cprId,
|
|
|
|
|
+ 'preview' => $preview,
|
|
|
|
|
+ 'description' => $description,
|
|
|
|
|
+ 'link' => $link,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function update(int $id, ?bool $preview = null, ?string $description = null, ?string $link = null): ?array
|
|
|
|
|
+ {
|
|
|
|
|
+ $fields = [];
|
|
|
|
|
+ $params = ['id' => $id];
|
|
|
|
|
+
|
|
|
|
|
+ if ($preview !== null) {
|
|
|
|
|
+ $fields[] = 'cpr_monitoring_preview = :preview';
|
|
|
|
|
+ $params['preview'] = $preview;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($description !== null) {
|
|
|
|
|
+ $fields[] = 'cpr_monitoring_description = :description';
|
|
|
|
|
+ $params['description'] = $description;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($link !== null) {
|
|
|
|
|
+ $fields[] = 'cpr_monitoring_link = :link';
|
|
|
|
|
+ $params['link'] = $link;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$fields) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $sql = 'UPDATE "cpr_monitoring" SET ' . implode(', ', $fields) . ' WHERE cpr_monitoring_id = :id';
|
|
|
|
|
+ $stmt = $this->pdo->prepare($sql);
|
|
|
|
|
+
|
|
|
|
|
+ $stmt->bindValue(':id', $id, \PDO::PARAM_INT);
|
|
|
|
|
+ if (array_key_exists('preview', $params)) {
|
|
|
|
|
+ $stmt->bindValue(':preview', $params['preview'], \PDO::PARAM_BOOL);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (array_key_exists('description', $params)) {
|
|
|
|
|
+ $stmt->bindValue(':description', $params['description'], \PDO::PARAM_STR);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (array_key_exists('link', $params)) {
|
|
|
|
|
+ $stmt->bindValue(':link', $params['link'], \PDO::PARAM_STR);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $ok = $stmt->execute();
|
|
|
|
|
+ if (!$ok) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $this->getById($id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function delete(int $id): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare('DELETE FROM "cpr_monitoring" WHERE cpr_monitoring_id = :id');
|
|
|
|
|
+ $stmt->execute(['id' => $id]);
|
|
|
|
|
+
|
|
|
|
|
+ return $stmt->rowCount() > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|