|
1
|
|
|
<?php |
|
2
|
|
|
namespace AL\Common\DAO; |
|
3
|
|
|
|
|
4
|
|
|
require_once __DIR__."/../../lib/Db.php" ; |
|
5
|
|
|
require_once __DIR__."/../Model/Location/Location.php" ; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* DAO for Locations |
|
9
|
|
|
*/ |
|
10
|
|
|
class LocationDAO { |
|
11
|
|
|
private $mysqli; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($mysqli) { |
|
14
|
|
|
$this->mysqli = $mysqli; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Get all locations |
|
19
|
|
|
* |
|
20
|
|
|
* @return \AL\Common\Model\Location\Location[] A list of locations |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getAllLocations() { |
|
23
|
|
|
$stmt = \AL\Db\execute_query( |
|
24
|
|
|
"LocationDAO: getAllLocations", |
|
25
|
|
|
$this->mysqli, |
|
26
|
|
|
"SELECT id, continent_code, name, country_iso2, country_iso3, type |
|
27
|
|
|
FROM location |
|
28
|
|
|
ORDER by continent_code, type, name", |
|
29
|
|
|
null, null |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
\AL\Db\bind_result( |
|
33
|
|
|
"LocationDAO: getAllLocations", |
|
34
|
|
|
$stmt, |
|
35
|
|
|
$id, $continent_code, $name, $country_iso2, $country_iso3, $type |
|
|
|
|
|
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$locations = []; |
|
39
|
|
|
while ($stmt->fetch()) { |
|
40
|
|
|
$locations[] = new \AL\Common\Model\Location\Location( |
|
41
|
|
|
$id, $continent_code, $name, $country_iso2, $country_iso3, $type |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$stmt->close(); |
|
46
|
|
|
|
|
47
|
|
|
return $locations; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get all the locations IDs for a game release |
|
52
|
|
|
* |
|
53
|
|
|
* @param integer Release ID |
|
|
|
|
|
|
54
|
|
|
* @return \AL\Common\Model\Location\Location[] A list of locations |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getLocationsIdsForRelease($release_id) { |
|
57
|
|
|
$stmt = \AL\Db\execute_query( |
|
58
|
|
|
"LocationDAO: getLocationsIdsForRelease", |
|
59
|
|
|
$this->mysqli, |
|
60
|
|
|
"SELECT location_id |
|
61
|
|
|
FROM game_release_location |
|
62
|
|
|
LEFT JOIN location on location.id = game_release_location.location_id |
|
63
|
|
|
WHERE game_release_id = ? |
|
64
|
|
|
ORDER by continent_code, type, name", |
|
65
|
|
|
"i", $release_id |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
\AL\Db\bind_result( |
|
69
|
|
|
"LocationDAO: getLocationsIdsForRelease", |
|
70
|
|
|
$stmt, |
|
71
|
|
|
$location_id |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
$locations = []; |
|
75
|
|
|
while ($stmt->fetch()) { |
|
76
|
|
|
$locations[] = $location_id; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$stmt->close(); |
|
80
|
|
|
|
|
81
|
|
|
return $locations; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get all the locations for a release |
|
86
|
|
|
* |
|
87
|
|
|
* @param integer Release ID |
|
88
|
|
|
* @return \AL\Common\Model\Location\Location[] A list of locations |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getLocationsForRelease($release_id) { |
|
91
|
|
|
$stmt = \AL\Db\execute_query( |
|
92
|
|
|
"LocationDAO: getLocationsIdsForRelease", |
|
93
|
|
|
$this->mysqli, |
|
94
|
|
|
"SELECT location.id, continent_code, name, country_iso2, country_iso3, type |
|
95
|
|
|
FROM game_release_location |
|
96
|
|
|
LEFT JOIN location on location.id = game_release_location.location_id |
|
97
|
|
|
WHERE game_release_id = ? |
|
98
|
|
|
ORDER by continent_code, type, name", |
|
99
|
|
|
"i", $release_id |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
\AL\Db\bind_result( |
|
103
|
|
|
"LocationDAO: getLocationsIdsForRelease", |
|
104
|
|
|
$stmt, |
|
105
|
|
|
$id, $continent_code, $name, $country_iso2, $country_iso3, $type |
|
|
|
|
|
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$locations = []; |
|
109
|
|
|
while ($stmt->fetch()) { |
|
110
|
|
|
$locations[] = new \AL\Common\Model\Location\Location( |
|
111
|
|
|
$id, $continent_code, $name, $country_iso2, $country_iso3, $type |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$stmt->close(); |
|
116
|
|
|
|
|
117
|
|
|
return $locations; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set the list of locations a release was released in |
|
122
|
|
|
* |
|
123
|
|
|
* @param integer Release ID |
|
124
|
|
|
* @param integer[] List of location IDs |
|
|
|
|
|
|
125
|
|
|
* @return \AL\Common\Model\Location\Location[] A list of locations |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setLocationsForRelease($release_id, $locations) { |
|
128
|
|
|
$stmt = \AL\Db\execute_query( |
|
129
|
|
|
"LocationDAO: setLocationsForRelease", |
|
130
|
|
|
$this->mysqli, |
|
131
|
|
|
"DELETE FROM game_release_location WHERE game_release_id = ?", |
|
132
|
|
|
"i", $release_id |
|
133
|
|
|
); |
|
134
|
|
|
|
|
135
|
|
|
foreach ($locations as $id) { |
|
136
|
|
|
$stmt = \AL\Db\execute_query( |
|
137
|
|
|
"LocationDAO: setLocationsForRelease", |
|
138
|
|
|
$this->mysqli, |
|
139
|
|
|
"INSERT INTO game_release_location (game_release_id, location_id) VALUES (?, ?)", |
|
140
|
|
|
"ii", $release_id, $id |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$stmt->close(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get a map containing all locations, indexed by ID |
|
149
|
|
|
* |
|
150
|
|
|
* @return \AL\Common\Model\Location\Location[] A map of locations |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getAllLocationsAsMap() { |
|
153
|
|
|
$locations = $this->getAllLocations(); |
|
154
|
|
|
$locationsMap = array(); |
|
155
|
|
|
foreach ($locations as $location) { |
|
156
|
|
|
$locationsMap[$location->getId()] = $location; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $locationsMap; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|