/
Games.php
144 lines (128 loc) · 3.34 KB
/
Games.php
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
<?php
/**
* @copyright Ilch 2.0
* @package ilch
*/
namespace Modules\Gamelist\Mappers;
use Modules\Gamelist\Models\Games as GamesModel;
class Games extends \Ilch\Mapper
{
/**
* Gets the entries.
*
* @param array $where
* @return GamesModel[]|array
*/
public function getEntries($where = [])
{
$array = $this->db()->select('*')
->from('gamelist')
->where($where)
->order(['title' => 'ASC'])
->execute()
->fetchRows();
if (empty($array)) {
return [];
}
$entries = [];
foreach ($array as $entry) {
$model = new GamesModel();
$model->setId($entry['id'])
->setCatId($entry['catid'])
->setTitle($entry['title'])
->setVideourl($entry['videourl'])
->setImage($entry['image'])
->setShow($entry['show']);
$entries[] = $model;
}
return $entries;
}
/**
* Gets entry by given id.
*
* @param integer $id
* @return GamesModel|null
*/
public function getEntryById($id)
{
$row = $this->db()->select('*')
->from('gamelist')
->where(['id' => $id])
->execute()
->fetchAssoc();
if (empty($row)) {
return null;
}
$model = new GamesModel();
$model->setId($row['id'])
->setCatId($row['catid'])
->setTitle($row['title'])
->setVideourl($row['videourl'])
->setImage($row['image'])
->setShow($row['show']);
return $model;
}
/**
* Inserts or updates model.
*
* @param GamesModel $entry
*/
public function save(GamesModel $entry)
{
$fields = [
'catid' => $entry->getCatId(),
'title' => $entry->getTitle(),
'videourl' => $entry->getVideourl(),
'image' => $entry->getImage(),
'show' => $entry->getShow()
];
if ($entry->getId()) {
$this->db()->update('gamelist')
->values($fields)
->where(['id' => $entry->getId()])
->execute();
} else {
$this->db()->insert('gamelist')
->values($fields)
->execute();
}
}
/**
* Updates entry with given id.
*
* @param integer $id
*/
public function update($id)
{
$show = (int) $this->db()->select('show')
->from('gamelist')
->where(['id' => $id])
->execute()
->fetchCell();
if ($show == 1) {
$this->db()->update('gamelist')
->values(['show' => 0])
->where(['id' => $id])
->execute();
} else {
$this->db()->update('gamelist')
->values(['show' => 1])
->where(['id' => $id])
->execute();
}
}
/**
* Deletes entry with given id.
*
* @param integer $id
*/
public function delete($id)
{
$this->db()->delete('gamelist')
->where(['id' => $id])
->execute();
$this->db()->delete('gamelist_entrants')
->where(['game_id' => $id])
->execute();
}
}