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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
 * Class to handle game categories
 */
 
class Category
{
    public $id = null;
    public $name = null;
    public $slug = null;
    public $priority = 0;
    public $description = "";
    public $meta_description = "";
    public $fields = "";
    public $extra_fields = 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['description'])) $this->description = $data['description'];
        if (isset($data['meta_description'])) $this->meta_description = $data['meta_description'];
        if (isset($data['fields'])) $this->fields = $data['fields'];
        if (isset($data['priority'])) $this->priority = (int)$data['priority'];
        if ( isset( $data['slug'] ) ) {
            $this->slug = strtolower(str_replace(' ', '-', str_replace('.', '', $data["slug"])));
        } else {
            if ( isset( $data['name'] ) ) $this->slug = strtolower(str_replace(' ', '-', $data["name"]));
        }
        if(isset($data['extra_fields'])){
            if(is_array($data['extra_fields'])){
                $data['extra_fields'] = json_encode($data['extra_fields']);
            }
            $this->extra_fields = $data['extra_fields'];
        }
        if($this->priority > 10000){
            // Fix possible bug
            $this->priority = 10000;
        }
        if($this->priority < -100){
            // Fix possible bug
            $this->priority = -100;
        }
    }
 
    public function storeFormValues($params)
    {
        $this->__construct($params);
    }
 
    public static function getById($id)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories WHERE id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":id", $id, PDO::PARAM_INT);
        $st->execute();
        $row = $st->fetch();
        if ($row) return new Category($row);
    }
 
    public static function getBySlug($slug)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories WHERE slug = :slug LIMIT 1";
        $st = $conn->prepare($sql);
        $st->bindValue(":slug", $slug, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        if ($row) return new Category($row);
    }
 
    public static function getByName($name)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories 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 Category($row);
    }
 
    public static function getIdByName($name)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories WHERE name = :name LIMIT 1";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $name, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        if($row){
            return $row['id'];
        } else {
            return null;
        }
    }
 
    public static function getIdBySlug($slug)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories WHERE slug = :slug limit 1";
        $st = $conn->prepare($sql);
        $st->bindValue(":slug", $slug, PDO::PARAM_STR);
        $st->execute();
        $row = $st->fetch();
        if( $row ) {
            return $row['id'];
        } else {
            return null;
        }
    }
 
    public static function getList($numRows = 1000)
    {
        $conn = open_connection();
        $sql = "SELECT * FROM categories
            ORDER BY priority DESC, name ASC LIMIT :numRows";
 
        $st = $conn->prepare($sql);
        $st->bindValue(":numRows", $numRows, PDO::PARAM_INT);
        $st->execute();
        $list = array();
        while ($row = $st->fetch())
        {
            $category = new Category($row);
            $list[] = $category;
        }
        $totalRows = $conn->query('SELECT count(*) FROM categories')->fetchColumn();
        return (array(
            "results" => $list,
            "totalRows" => $totalRows
        ));
    }
 
    public static function getCategoryCount($id)
    {
        $conn = open_connection();
        $sql = "SELECT count(*) FROM cat_links WHERE categoryid = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":id", $id, PDO::PARAM_INT);
        $st->execute();
        $totalRows = $st->fetchColumn();
        return $totalRows;
    }
 
    public static function getListByCategory($id, int $amount, int $page = 0)
    {
        $additional_condition = '';
        if(defined('IS_VISITOR_PAGE')){
            if(get_setting_value('hide_pc_on_mobile') && is_mobile_device()){
                $additional_condition = 'AND games.is_mobile = 1';
            }
        }
        $id = (int)$id;
        // Get only published games.
        $sql = "SELECT games.id FROM games 
                JOIN cat_links ON games.id = cat_links.gameid 
                WHERE cat_links.categoryid = $id AND games.published = 1 $additional_condition 
                ORDER BY cat_links.id DESC LIMIT $amount OFFSET $page";
        $cached_result = null;
        if(is_cached_query_allowed()){
            $data_value = get_cached_query($sql);
            if(!is_null($data_value)){
                $cached_result = json_decode($data_value, true);
            }
        }
        $conn = open_connection();
        $rows;
        if(!$cached_result){
            $st = $conn->prepare($sql);
            $st->execute();
            $rows = $st->fetchAll();
            if(is_cached_query_allowed()){
                set_cached_query($sql, json_encode($rows));
            }
        } else {
            $rows = $cached_result;
        }
        $list = array();
        foreach ($rows as $item) {
            $game = new Game;
            $res = $game->getById($item['id']);
            array_push($list, $res);
        }
        // Count only published games.
        $sql = "SELECT COUNT(*) FROM games 
                JOIN cat_links ON games.id = cat_links.gameid 
                WHERE cat_links.categoryid = $id AND games.published = 1 $additional_condition";
        $cached_result2 = null;
        if(is_cached_query_allowed()){
            $data_value = get_cached_query($sql);
            if(!is_null($data_value)){
                $cached_result2 = json_decode($data_value, true);
            }
        }
        $totalRows = null;
        if(is_null($cached_result2)){
            $st = $conn->prepare($sql);
            $st->execute();
            $totalRows = $st->fetchColumn();
            if(is_cached_query_allowed()){
                set_cached_query($sql, json_encode($totalRows));
            }
        } else {
            $totalRows = $cached_result2;
        }
        return (array(
            "results" => $list,
            "totalRows" => $totalRows,
            "totalPages" => ceil($totalRows / $amount)
        ));
    }
 
    public static function getListByCategories($ids, int $amount, int $page = 0, $random = true){
        // Deprecated since v1.8.7 (Previously used for similar games), replaced with Game::fetchSimilarGames() for better performance
        $conn = open_connection();
        $random_order = '';
        if ($random) {
            $random_order = ' ORDER BY rand()';
        }
        $sql = "SELECT cl1.* FROM `cat_links` as cl1 ,( SELECT DISTINCT `gameid`,`categoryid` FROM `cat_links`";
        if ($ids) {
            $sql .= " WHERE `categoryid` IN (" . implode(',', $ids) . ")";
        }
        $sql .= $random_order . " LIMIT $amount OFFSET $page ) as cl2 WHERE cl2.gameid = cl1.gameid";
        $st = $conn->prepare($sql);
        $st->execute();
        $rows = $st->fetchAll();
        $list = array();
        $gameIds = [];
        foreach ($rows as $row) {
            if (count($gameIds) > $amount) {
                break;
            }
            if (!in_array($row['gameid'], $gameIds)) {
                $gameIds[] = $row['gameid'];
            }
        }
        $is_only_mobile = false;
        if(defined('IS_VISITOR_PAGE')){
            if(get_setting_value('hide_pc_on_mobile') && is_mobile_device()){
                $is_only_mobile = true;
            }
        }
        foreach ($gameIds as $gameId) {
            if (count($list) < $amount) {
                $game = new Game;
                $res = $game->getById($gameId);
                if ($res && $res->published) {
                    if($is_only_mobile && $res->is_mobile){
                        array_push($list, $res);
                    }
                    if(!$is_only_mobile){
                        array_push($list, $res);
                    }
                }
            } else {
                break;
            }
        }
        return array(
            "results" => $list,
            "totalRows" => count($list),
            "totalPages" => 1
        );
    }
 
    public function addToCategory($gameID, $catID)
    {
        $conn = open_connection();
        $sql = "INSERT INTO cat_links ( gameid, categoryid ) VALUES ( :gameID, :catID )";
        $st = $conn->prepare($sql);
        $st->bindValue(":gameID", $gameID, PDO::PARAM_INT);
        $st->bindValue(":catID", $catID, PDO::PARAM_INT);
        $st->execute();
        $this->id = $conn->lastInsertId();
    }
 
    public function isCategoryExist($name)
    {
        $conn = open_connection();
        $sql = 'SELECT * FROM categories 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 getExtraField($key)
    {
        if($this->extra_fields != null){
            $fields = json_decode($this->extra_fields, true);
            if(isset($fields[$key])){
                return $fields[$key];
            }
        }
        return null;
    }
 
    public function get_fields()
    {
        if($this->fields != ''){
            return json_decode($this->fields, true);
        } else {
            return null;
        }
    }
 
    public function get_field($key)
    {
        if($this->fields != ''){
            $fields = json_decode($this->fields, true);
            if(isset($fields[$key])){
                return $fields[$key];
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
 
    public function insert()
    { 
        if (!is_null($this->id)) trigger_error("Category::insert(): Attempt to insert a Category object that already has its ID property set (to $this->id).", E_USER_ERROR);
        // Clean-up slug string
        $this->slug = preg_replace('/-+/', '-', preg_replace('/^-+|-+$/', '', $this->slug));
        $conn = open_connection();
        $sql = "INSERT INTO categories ( name, slug, description, meta_description, extra_fields, priority ) VALUES ( :name, :slug, :description, :meta_description, :extra_fields, :priority )";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $this->name, PDO::PARAM_STR);
        $st->bindValue(":slug", esc_slug($this->slug), PDO::PARAM_STR);
        $st->bindValue(":description", $this->description, PDO::PARAM_STR);
        $st->bindValue(":meta_description", $this->meta_description, PDO::PARAM_STR);
        $st->bindValue(":extra_fields", $this->extra_fields, PDO::PARAM_STR);
        $st->bindValue(":priority", $this->priority, PDO::PARAM_INT);
        $st->execute();
        $this->id = $conn->lastInsertId();
    }
 
    public function update()
    {
        if (is_null($this->id)) trigger_error("Category::update(): Attempt to update a Category object that does not have its ID property set.", E_USER_ERROR);
        //$prev_name = Category::getById($this->id)->name;
        //
        // Clean-up slug string
        $this->slug = preg_replace('/-+/', '-', preg_replace('/^-+|-+$/', '', $this->slug));
        $conn = open_connection();
        $sql = "UPDATE categories SET name=:name, slug=:slug, priority=:priority, description=:description, meta_description=:meta_description, fields=:fields, extra_fields=:extra_fields WHERE id = :id";
        $st = $conn->prepare($sql);
        $st->bindValue(":name", $this->name, PDO::PARAM_STR);
        $st->bindValue(":slug", $this->slug, PDO::PARAM_STR);
        $st->bindValue(":description", $this->description, PDO::PARAM_STR);
        $st->bindValue(":meta_description", $this->meta_description, PDO::PARAM_STR);
        $st->bindValue(":fields", $this->fields, PDO::PARAM_STR);
        $st->bindValue(":extra_fields", $this->extra_fields, PDO::PARAM_STR);
        $st->bindValue(":id", $this->id, PDO::PARAM_INT);
        $st->bindValue(":priority", $this->priority, PDO::PARAM_INT);
        $st->execute();
    }
 
    public function delete()
    {
        if (is_null($this->id)) trigger_error("Category::delete(): Attempt to delete a Category object that does not have its ID property set.", E_USER_ERROR);
 
        $conn = open_connection();
        $st = $conn->prepare("DELETE FROM categories WHERE id = :id LIMIT 1");
        $st->bindValue(":id", $this->id, PDO::PARAM_INT);
        $st->execute();
    }
 
}
 
?>