|
@@ -0,0 +1,105 @@
|
|
|
|
|
+<?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 id, cpr_id, preview, description, link FROM "cpr_monitoring" WHERE 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 id, cpr_id, preview, description, link
|
|
|
|
|
+ FROM "cpr_monitoring"
|
|
|
|
|
+ WHERE cpr_id = :cpr_id
|
|
|
|
|
+ ORDER BY 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, preview, description, link)
|
|
|
|
|
+ VALUES (:cpr_id, :preview, :description, :link)
|
|
|
|
|
+ RETURNING id'
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ $stmt->execute([
|
|
|
|
|
+ 'cpr_id' => $cprId,
|
|
|
|
|
+ 'preview' => $preview,
|
|
|
|
|
+ 'description' => $description,
|
|
|
|
|
+ 'link' => $link,
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ $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[] = 'preview = :preview';
|
|
|
|
|
+ $params['preview'] = $preview;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($description !== null) {
|
|
|
|
|
+ $fields[] = 'description = :description';
|
|
|
|
|
+ $params['description'] = $description;
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($link !== null) {
|
|
|
|
|
+ $fields[] = 'link = :link';
|
|
|
|
|
+ $params['link'] = $link;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!$fields) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $sql = 'UPDATE "cpr_monitoring" SET ' . implode(', ', $fields) . ' WHERE id = :id';
|
|
|
|
|
+ $stmt = $this->pdo->prepare($sql);
|
|
|
|
|
+ $ok = $stmt->execute($params);
|
|
|
|
|
+ if (!$ok) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $this->getById($id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function delete(int $id): bool
|
|
|
|
|
+ {
|
|
|
|
|
+ $stmt = $this->pdo->prepare('DELETE FROM "cpr_monitoring" WHERE id = :id');
|
|
|
|
|
+ $stmt->execute(['id' => $id]);
|
|
|
|
|
+
|
|
|
|
|
+ return $stmt->rowCount() > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|