1
difenduandada
2024-10-15 7fd2948ee35c8e147ed35ce6d8502f94a98ddd22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
 * Class to handle game collections
 */
 
class Collection
{
    public $id = null;
    public $name = null;
    public $data = null;
 
    public function __construct($data = array())
    {
        if (isset($data['id'])) $this->id = (int)$data['id'];
        if (isset($data['name'])) $this->name = $data['name'];
        if (isset($data['data'])) $this->data = $data['data'];
    }
 
    public function storeFormValues($params)
    {
        $this->__construct($params);
    }
 
    public static function getById($id)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM collections WHERE id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":id", $id, PDO::PARAM_INT);
        $st->execute();
        $row = $st->fetch();
        if ($row) return new Collection($row);
    }
 
    public static function getByName($name)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM collections WHERE name = :name LIMIT 1";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $name, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        if ($row) return new Collection($row);
    }
 
    public static function getIdByName($name)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM collections WHERE name = :name limit 1";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $name, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        return $row['id'];
    }
 
    public static function getList($numRows = 1000000)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM collections
            ORDER BY name ASC LIMIT :numRows";
 
        $st = $conn->prepare($sql);
        $st->bindValue(":numRows", $numRows, PDO::PARAM_INT);
        $st->execute();
        $list = array();
 
        while ($row = $st->fetch())
        {
            $Collection = new Collection($row);
            $list[] = $Collection;
        }
        $totalRows = $conn->query('SELECT count(*) FROM collections')->fetchColumn();
        return (array(
            "results" => $list,
            "totalRows" => $totalRows
        ));
    }
 
    public static function getListByCollection($name, $amount = 12, $page = 0)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM collections WHERE name = :name";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $name, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch(PDO::FETCH_ASSOC);
        $list = array();
        if($row){
            // The data is exist
            if(isset($row['data'])){
                $data = explode(',', $row['data']);
                $i = 0;
                foreach ($data as $id)
                {
                    if($i < $amount){
                        $game = new Game;
                        $res = $game->getById($id);
                        if($res){
                            array_push($list, $res);
                        }
                    }
                    $i++;
                }
                return (array(
                    "results" => $list,
                    "totalRows" => count($list),
                ));
            }
        }
        return null;
    }
 
    public function isCollectionExist($name)
    {
        $conn = open_connection();
        $sql = 'SELECT * FROM collections WHERE name = :name limit 1';
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $name, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        if ($row)
        {
            $this->id = $row['id'];
        }
        if ($row)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
    public function insert()
    { 
        if (!is_null($this->id)) trigger_error("Collection::insert(): Attempt to insert a Collection object that already has its ID property set (to $this->id).", E_USER_ERROR);
 
        $conn = open_connection();
        $sql = "INSERT INTO collections ( name, data ) VALUES ( :name, :data )";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $this->name, PDO::PARAM_STR);
        $st->bindValue(":data", $this->data, PDO::PARAM_STR);
        $st->execute();
        $this->id = $conn->lastInsertId();
    }
 
    public function update()
    {
        if (is_null($this->id)) trigger_error("Collection::update(): Attempt to update a Collection object that does not have its ID property set.", E_USER_ERROR);
        //$prev_name = Collection::getById($this->id)->name;
        //
        $conn = open_connection();
        $sql = "UPDATE collections SET name=:name, data=:data WHERE id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $this->name, PDO::PARAM_STR);
        $st->bindValue(":data", $this->data, PDO::PARAM_STR);
        $st->bindValue(":id", $this->id, PDO::PARAM_INT);
        $st->execute();
    }
 
    public function delete()
    {
        if (is_null($this->id)) trigger_error("Collection::delete(): Attempt to delete a Collection object that does not have its ID property set.", E_USER_ERROR);
 
        $conn = open_connection();
        $st = $conn->prepare("DELETE FROM collections WHERE id = :id LIMIT 1");
        $st->bindValue(":id", $this->id, PDO::PARAM_INT);
        $st->execute();
    }
 
}
 
?>