Template::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
//error_reporting(0);
3
class Template {
4
5
    private $conn;
6
    private $db_table = "templates";
7
8
    public $id;
9
    public $userid;
10
    public $title;
11
    public $calories;
12
    public $amount;
13
    public $image;
14
15
    public function __construct($db) {
16
        $this->conn = $db;
17
    }
18
19
    public function read($amount = false) {
20
21
        $query = "
22
        SELECT ID as id, Title as title, DefaultAmout as amount, Calories as calories, Image as image
23
        FROM ". $this->db_table . "
24
        WHERE UserID = :userid";
25
26
        if ($amount) {
27
            $query .= " LIMIT " . $amount;
28
        }
29
30
        $stmt = $this->conn->prepare($query);
31
        $stmt->bindParam(':userid', $this->userid);
32
        $stmt->execute();
33
34
        return $stmt;
35
36
    }
37
38
    public function create() {
39
40
        $query = "
41
            INSERT INTO " . $this->db_table . " SET
42
            UserID = :userid,
43
            Title = :title,
44
            DefaultAmout = :amount,
45
            Calories = :calories,
46
            Image = :image
47
        ";
48
49
        $this->userid = htmlspecialchars(strip_tags($this->userid));
50
        $this->title = htmlspecialchars(strip_tags($this->title));
51
        $this->calories = htmlspecialchars(strip_tags($this->calories));
52
        $this->amount = htmlspecialchars(strip_tags($this->amount));
53
        $this->image = htmlspecialchars(strip_tags($this->image));
54
55
        $stmt = $this->conn->prepare($query);
56
        $stmt->bindParam(":userid", $this->userid);
57
        $stmt->bindParam(":title", $this->title);
58
        $stmt->bindParam(":amount", $this->amount);
59
        $stmt->bindParam(":calories", $this->calories);
60
        $stmt->bindParam(":image", $this->image);
61
62
        if ($stmt->execute()) {
63
            $this->id = $this->conn->lastInsertId();
64
            return true;
65
        }
66
67
        return false;
68
69
    }
70
71
    public function delete() {
72
73
        $query = "
74
        DELETE FROM " . $this->db_table . "
75
        WHERE ID = :id AND UserID = :userid
76
        ";
77
78
        $this->id = htmlspecialchars(strip_tags($this->id));
79
        $this->userid = htmlspecialchars(strip_tags($this->userid));
80
81
        $stmt = $this->conn->prepare($query);
82
        $stmt->bindParam(":id", $this->id);
83
        $stmt->bindParam(":userid", $this->userid);
84
85
        if ($stmt->execute()) {
86
87
            return true;
88
89
        }
90
91
        return false;
92
93
    }
94
95
}
96