File indexing completed on 2024-12-15 03:45:06

0001 <?php
0002 /*
0003     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 require_once('compat.php');
0009 require_once('datastore.php');
0010 require_once('restexception.php');
0011 
0012 /** Represents a survey. */
0013 class Survey
0014 {
0015     public $uuid = '';
0016     public $name = '';
0017     public $url = '';
0018     public $active = false;
0019     public $target = null;
0020 
0021     private $id = -1;
0022     /** Returns an array of all surveys for @p productName. */
0023     public static function surveysForProduct(Datastore $db, $productName)
0024     {
0025         $stmt = $db->prepare('
0026             SELECT tbl_survey.col_id, tbl_survey.col_uuid, tbl_survey.col_name, tbl_survey.col_url, tbl_survey.col_active, tbl_survey.col_target
0027             FROM tbl_survey JOIN tbl_product ON (tbl_survey.col_product_id = tbl_product.col_id)
0028             WHERE tbl_product.col_name = :productName
0029         ');
0030         $stmt->bindValue(':productName', $productName, PDO::PARAM_STR);
0031         $db->execute($stmt);
0032 
0033         $surveys = array();
0034         foreach ($stmt as $row) {
0035             $s = new Survey();
0036             $s->id = intval($row['col_id']);
0037             $s->uuid = strval($row['col_uuid']);
0038             $s->name = strval($row['col_name']);
0039             $s->url = strval($row['col_url']);
0040             $s->active = boolval($row['col_active']);
0041             $s->target = strval($row['col_target']);
0042             array_push($surveys, $s);
0043         }
0044 
0045         return $surveys;
0046     }
0047 
0048     /** Returns an array of all active surveys for @p product. */
0049     public static function activeSurveysForProduct(Datastore $db, Product $product)
0050     {
0051         $stmt = $db->prepare('SELECT col_id, col_uuid, col_name, col_url, col_target FROM tbl_survey WHERE col_product_id = :productId AND col_active = :active');
0052         $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
0053         $stmt->bindValue(':active', true, PDO::PARAM_BOOL);
0054         $db->execute($stmt);
0055 
0056         $surveys = array();
0057         foreach ($stmt as $row) {
0058             $s = new Survey();
0059             $s->id = intval($row['col_id']);
0060             $s->uuid = strval($row['col_uuid']);
0061             $s->name = strval($row['col_name']);
0062             $s->url = strval($row['col_url']);
0063             $s->active = true;
0064             $s->target = strval($row['col_target']);
0065             array_push($surveys, $s);
0066         }
0067 
0068         return $surveys;
0069     }
0070 
0071     /** Insert a new survey into storage for product @p product. */
0072     public function insert(Datastore $db, Product $product)
0073     {
0074         $stmt = $db->prepare('
0075             INSERT INTO tbl_survey (col_product_id, col_uuid, col_name, col_url, col_active, col_target)
0076             VALUES (:productId, :uuid, :name, :url, :active, :target)
0077         ');
0078         $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
0079         $stmt->bindValue(':uuid', $this->uuid, PDO::PARAM_STR);
0080         $stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
0081         $stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
0082         $stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
0083         $stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
0084         $db->execute($stmt);
0085         $this->id = $db->pdoHandle()->lastInsertId();
0086     }
0087 
0088     /** Update an existing survey in storage. */
0089     public function update(Datastore $db)
0090     {
0091         $stmt = $db->prepare('
0092             UPDATE tbl_survey SET
0093                 col_name = :name,
0094                 col_url = :url,
0095                 col_active = :active,
0096                 col_target = :target
0097             WHERE col_uuid = :surveyUuid
0098         ');
0099         $stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
0100         $stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
0101         $stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
0102         $stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
0103         $stmt->bindValue(':surveyUuid', $this->uuid, PDO::PARAM_STR);
0104         $db->execute($stmt);
0105     }
0106 
0107     /** Delete this existing survey from storage. */
0108     public function delete(Datastore $db)
0109     {
0110         $stmt = $db->prepare('DELETE FROM tbl_survey WHERE col_uuid = :surveyUuid');
0111         $stmt->bindValue(':surveyUuid', $this->uuid, PDO::PARAM_STR);
0112         $db->execute($stmt);
0113     }
0114 
0115     /** Create one Survey instance based on JSON input and verifies it is valid. */
0116     public static function fromJson($jsonString)
0117     {
0118         $jsonObj = json_decode($jsonString);
0119         if (!property_exists($jsonObj, 'uuid') || !property_exists($jsonObj, 'name') || !property_exists($jsonObj, 'url') || !property_exists($jsonObj, 'active'))
0120             throw new RESTException('Incomplete survey object.', 400);
0121 
0122         $s = new Survey();
0123         $s->uuid = strval($jsonObj->uuid);
0124         $s->name = $jsonObj->name;
0125         $s->url = strval($jsonObj->url);
0126         $s->active = boolval($jsonObj->active);
0127         if (property_exists($jsonObj, 'target'))
0128             $s->target = strval($jsonObj->target);
0129         if (property_exists($jsonObj, 'id'))
0130             $s->id = $jsonObj->id;
0131 
0132         // verify
0133         if (strlen($s->uuid) <= 0)
0134             throw new RESTException('Empty survey UUID.', 400);
0135         if (strlen($s->name) <= 0 || !is_string($s->name))
0136             throw new RESTException('Empty product name.', 400);
0137         if (!is_numeric($s->id))
0138             throw new RESTException('Invalid survey id.', 400);
0139 
0140         return $s;
0141     }
0142 }
0143 
0144 ?>