1<?php
2/*
3    Copyright (C) 2016 Volker Krause <vkrause@kde.org>
4
5    Permission is hereby granted, free of charge, to any person obtaining
6    a copy of this software and associated documentation files (the
7    "Software"), to deal in the Software without restriction, including
8    without limitation the rights to use, copy, modify, merge, publish,
9    distribute, sublicense, and/or sell copies of the Software, and to
10    permit persons to whom the Software is furnished to do so, subject to
11    the following conditions:
12
13    The above copyright notice and this permission notice shall be included
14    in all copies or substantial portions of the Software.
15
16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25require_once('compat.php');
26require_once('datastore.php');
27require_once('restexception.php');
28
29/** Represents a survey. */
30class Survey
31{
32    public $uuid = '';
33    public $name = '';
34    public $url = '';
35    public $active = false;
36    public $target = null;
37
38    private $id = -1;
39    /** Returns an array of all surveys for @p productName. */
40    public function surveysForProduct(Datastore $db, $productName)
41    {
42        $stmt = $db->prepare('
43            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
44            FROM tbl_survey JOIN tbl_product ON (tbl_survey.col_product_id = tbl_product.col_id)
45            WHERE tbl_product.col_name = :productName
46        ');
47        $stmt->bindValue(':productName', $productName, PDO::PARAM_STR);
48        $db->execute($stmt);
49
50        $surveys = array();
51        foreach ($stmt as $row) {
52            $s = new Survey();
53            $s->id = intval($row['col_id']);
54            $s->uuid = strval($row['col_uuid']);
55            $s->name = strval($row['col_name']);
56            $s->url = strval($row['col_url']);
57            $s->active = boolval($row['col_active']);
58            $s->target = strval($row['col_target']);
59            array_push($surveys, $s);
60        }
61
62        return $surveys;
63    }
64
65    /** Returns an array of all active surveys for @p product. */
66    public function activeSurveysForProduct(Datastore $db, Product $product)
67    {
68        $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');
69        $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
70        $stmt->bindValue(':active', true, PDO::PARAM_BOOL);
71        $db->execute($stmt);
72
73        $surveys = array();
74        foreach ($stmt as $row) {
75            $s = new Survey();
76            $s->id = intval($row['col_id']);
77            $s->uuid = strval($row['col_uuid']);
78            $s->name = strval($row['col_name']);
79            $s->url = strval($row['col_url']);
80            $s->active = true;
81            $s->target = strval($row['col_target']);
82            array_push($surveys, $s);
83        }
84
85        return $surveys;
86    }
87
88    /** Insert a new survey into storage for product @p product. */
89    public function insert(Datastore $db, Product $product)
90    {
91        $stmt = $db->prepare('
92            INSERT INTO tbl_survey (col_product_id, col_uuid, col_name, col_url, col_active, col_target)
93            VALUES (:productId, :uuid, :name, :url, :active, :target)
94        ');
95        $stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
96        $stmt->bindValue(':uuid', $this->uuid, PDO::PARAM_STR);
97        $stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
98        $stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
99        $stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
100        $stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
101        $db->execute($stmt);
102        $this->id = $db->pdoHandle()->lastInsertId();
103    }
104
105    /** Update an existing survey in storage. */
106    public function update(Datastore $db)
107    {
108        $stmt = $db->prepare('
109            UPDATE tbl_survey SET
110                col_name = :name,
111                col_url = :url,
112                col_active = :active,
113                col_target = :target
114            WHERE col_uuid = :surveyUuid
115        ');
116        $stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
117        $stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
118        $stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
119        $stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
120        $stmt->bindValue(':surveyUuid', $this->uuid, PDO::PARAM_STR);
121        $db->execute($stmt);
122    }
123
124    /** Delete this existing survey from storage. */
125    public function delete(Datastore $db)
126    {
127        $stmt = $db->prepare('DELETE FROM tbl_survey WHERE col_uuid = :surveyUuid');
128        $stmt->bindValue(':surveyUuid', $this->uuid, PDO::PARAM_STR);
129        $db->execute($stmt);
130    }
131
132    /** Create one Survey instance based on JSON input and verifies it is valid. */
133    public static function fromJson($jsonString)
134    {
135        $jsonObj = json_decode($jsonString);
136        if (!property_exists($jsonObj, 'uuid') || !property_exists($jsonObj, 'name') || !property_exists($jsonObj, 'url') || !property_exists($jsonObj, 'active'))
137            throw new RESTException('Incomplete survey object.', 400);
138
139        $s = new Survey();
140        $s->uuid = strval($jsonObj->uuid);
141        $s->name = $jsonObj->name;
142        $s->url = strval($jsonObj->url);
143        $s->active = boolval($jsonObj->active);
144        if (property_exists($jsonObj, 'target'))
145            $s->target = strval($jsonObj->target);
146        if (property_exists($jsonObj, 'id'))
147            $s->id = $jsonObj->id;
148
149        // verify
150        if (strlen($s->uuid) <= 0)
151            throw new RESTException('Empty survey UUID.', 400);
152        if (strlen($s->name) <= 0 || !is_string($s->name))
153            throw new RESTException('Empty product name.', 400);
154        if (!is_numeric($s->id))
155            throw new RESTException('Invalid survey id.', 400);
156
157        return $s;
158    }
159}
160
161?>
162