-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathutil.inc
93 lines (87 loc) · 2.54 KB
/
util.inc
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
<?php
function query_countries($order_by = 'name'): array {
$ret = [];
$conn = new mysqli('localhost', 'root', 'root', 'countries');
$rows = $conn->query('SELECT * FROM country ORDER BY ' . $order_by);
foreach($rows as $row) {
$r = [];
$r['id'] = $row['id'];
$r['enabled'] = $row['id'] == '1' ?? FALSE;
$r['code3l'] = $row['code3l'];
$r['code2l'] = $row['code2l'];
$r['name'] = $row['name'];
$r['name_official'] = $row['name_official'];
$r['center'] = [];
if (!empty($row['latitude']) && !empty($row['longitude']) && !empty($row['zoom'])) {
$r['center']['latitude'] = $row['latitude'];
$r['center']['longitude'] = $row['longitude'];
$r['center']['zoom'] = $row['zoom'];
}
$ret[] = $r;
}
$rows->close();
$conn->close();
return $ret;
}
function query_names($country_id): array {
static $data;
if (empty($data)) {
$conn = new mysqli('localhost', 'root', 'root', 'countries');
$rows = $conn->query('SELECT * FROM country_name ORDER BY code2l, language');
foreach($rows as $row) {
$id = $row['country_id'];
$language = $row['language'];
if (strtolower($language) != 'en') {
$name = $row['name'];
$name_official = $row['name_official'];
$data[$id][$language]['name'] = $name;
$data[$id][$language]['name_official'] = $name_official;
$data[$id][$language]['source'] = $row['source'];
}
}
$rows->close();
$conn->close();
}
return $data[$country_id] ?? [];
}
function query_full_countries($order_by = 'name'): array {
$data = [];
$countries = query_countries($order_by);
foreach ($countries as $c) {
$id = $c['id'];
$names = query_names($id);
$c['names'] = $names;
$data[] = $c;
}
return $data;
}
function query_regions($order_by = 'name'): array {
$ret = [];
$conn = new mysqli('localhost', 'root', 'root', 'countries');
$rows = $conn->query('SELECT * FROM region ORDER BY ' . $order_by);
foreach($rows as $row) {
$r = [];
$r['id'] = $row['id'];
$r['name'] = $row['name'];
$r['is_unep'] = $row['is_unep'];
$ret[] = $r;
}
$rows->close();
$conn->close();
return $ret;
}
function query_country_regions(): array {
$ret = [];
$conn = new mysqli('localhost', 'root', 'root', 'countries');
$rows = $conn->query('SELECT * FROM country_region');
foreach($rows as $row) {
$r = [];
$r['id'] = $row['id'];
$r['country_id'] = $row['country_id'];
$r['region_id'] = $row['region_id'];
$ret[] = $r;
}
$rows->close();
$conn->close();
return $ret;
}