1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
# For looking up a postcode and redirecting or displaying appropriately |
4
|
|
|
|
5
|
|
|
include_once '../../includes/easyparliament/init.php'; |
6
|
|
|
include_once INCLUDESPATH . 'easyparliament/member.php'; |
7
|
|
|
|
8
|
|
|
$data = array(); |
9
|
|
|
$errors = array(); |
10
|
|
|
|
11
|
|
|
// handling to switch the GE message based either on time or a query string |
12
|
|
|
|
13
|
|
|
$pc = get_http_var('pc'); |
14
|
|
|
if (!$pc) { |
15
|
|
|
postcode_error('Please supply a postcode!'); |
16
|
|
|
} |
17
|
|
|
$data['pc'] = $pc; |
18
|
|
|
|
19
|
|
|
$pc = preg_replace('#[^a-z0-9]#i', '', $pc); |
20
|
|
|
if (!validate_postcode($pc)) { |
21
|
|
|
twfy_debug ('MP', "Can't display an MP because the submitted postcode wasn't of a valid form."); |
22
|
|
|
postcode_error("Sorry, " . _htmlentities($pc) . " isn't a valid postcode"); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
# 2024 ELECTION EXTRA |
26
|
|
|
|
27
|
|
|
$constituencies = mapit_postcode($pc); |
28
|
|
|
if (!$constituencies) { |
29
|
|
|
postcode_error("Sorry, " . _htmlentities($pc) . " isn't a known postcode"); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (isset($constituencies['SPE']) || isset($constituencies['SPC'])) { |
33
|
|
|
$data['multi'] = "scotland"; |
34
|
|
|
$MEMBER = fetch_mp($pc, $constituencies); |
35
|
|
|
pick_multiple($pc, $constituencies, 'SPE', HOUSE_TYPE_SCOTLAND); |
36
|
|
|
} elseif (isset($constituencies['WAE']) || isset($constituencies['WAC'])) { |
37
|
|
|
$data['multi'] = "wales"; |
38
|
|
|
$MEMBER = fetch_mp($pc, $constituencies); |
39
|
|
|
pick_multiple($pc, $constituencies, 'WAE', HOUSE_TYPE_WALES); |
40
|
|
|
} elseif (isset($constituencies['NIE'])) { |
41
|
|
|
$data['multi'] = "northern-ireland"; |
42
|
|
|
$MEMBER = fetch_mp($pc, $constituencies); |
43
|
|
|
pick_multiple($pc, $constituencies, 'NIE', HOUSE_TYPE_NI); |
44
|
|
|
} else { |
45
|
|
|
$data['multi'] = "uk"; |
46
|
|
|
$MEMBER = fetch_mp($pc, $constituencies, 1); |
47
|
|
|
$data['mp'] = [ |
48
|
|
|
'name' => $MEMBER->full_name(), |
49
|
|
|
'person_id' => $MEMBER->person_id(), |
50
|
|
|
'constituency' => $MEMBER->constituency(), |
51
|
|
|
'former' => !$MEMBER->current_member(HOUSE_TYPE_COMMONS), |
52
|
|
|
'standing_down_2024' => $MEMBER->extra_info['standing_down_2024'] ?? '', |
53
|
|
|
]; |
54
|
|
|
$data['MPSURL'] = new \MySociety\TheyWorkForYou\Url('mps'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$data['mapit_ids'] = $mapit_ids; |
58
|
|
|
MySociety\TheyWorkForYou\Renderer::output('postcode/index', $data); |
59
|
|
|
|
60
|
|
|
# --- |
61
|
|
|
|
62
|
|
|
function postcode_error($error) { |
63
|
|
|
global $PAGE; |
64
|
|
|
$PAGE->page_start(); |
65
|
|
|
$PAGE->stripe_start(); |
66
|
|
|
$PAGE->error_message($error); |
67
|
|
|
$PAGE->postcode_form(); |
68
|
|
|
$PAGE->stripe_end(); |
69
|
|
|
$PAGE->page_end(); |
70
|
|
|
exit; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
function fetch_mp($pc, $constituencies, $house=null) { |
74
|
|
|
global $THEUSER; |
75
|
|
|
$args = array('constituency' => $constituencies['WMC']); |
76
|
|
|
if ($house) { |
77
|
|
|
$args['house'] = $house; |
78
|
|
|
} |
79
|
|
|
try { |
80
|
|
|
$MEMBER = new MEMBER($args); |
81
|
|
|
} catch (MySociety\TheyWorkForYou\MemberException $e){ |
82
|
|
|
postcode_error($e->getMessage()); |
83
|
|
|
} |
84
|
|
|
if ($MEMBER->person_id()) { |
85
|
|
|
$THEUSER->set_postcode_cookie($pc); |
86
|
|
|
} |
87
|
|
|
return $MEMBER; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
function pick_multiple($pc, $areas, $area_type, $house) { |
|
|
|
|
91
|
|
|
global $PAGE, $data; |
92
|
|
|
$db = new ParlDB; |
93
|
|
|
|
94
|
|
|
$member_names = \MySociety\TheyWorkForYou\Utility\House::house_to_members($house); |
95
|
|
|
if ($house == HOUSE_TYPE_SCOTLAND) { |
96
|
|
|
$urlp = 'msp'; |
97
|
|
|
$a = [ $areas['SPC'], $areas['SPE'] ]; |
98
|
|
|
} elseif ($house == HOUSE_TYPE_WALES) { |
99
|
|
|
$urlp = 'ms'; |
100
|
|
|
$a = [ $areas['WAC'], $areas['WAE'] ]; |
101
|
|
|
} elseif ($house == HOUSE_TYPE_NI) { |
102
|
|
|
$urlp = 'mla'; |
103
|
|
|
$a = [ $areas['NIE'] ]; |
104
|
|
|
} |
105
|
|
|
$urlpl = $urlp . 's'; |
|
|
|
|
106
|
|
|
$urlp = "/$urlp/?p="; |
107
|
|
|
|
108
|
|
|
$q = $db->query("SELECT member.person_id, given_name, family_name, constituency, left_house |
109
|
|
|
FROM member, person_names pn |
110
|
|
|
WHERE constituency = :constituency |
111
|
|
|
AND member.person_id = pn.person_id AND pn.type = 'name' |
112
|
|
|
AND pn.end_date = (SELECT MAX(end_date) from person_names where person_names.person_id = member.person_id) |
113
|
|
|
AND house = 1 ORDER BY left_house DESC LIMIT 1", array( |
114
|
|
|
':constituency' => MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($areas['WMC']) |
115
|
|
|
))->first(); |
116
|
|
|
$mp = array(); |
117
|
|
|
if ($q) { |
118
|
|
|
$mp = $q; |
119
|
|
|
$mp['former'] = ($mp['left_house'] != '9999-12-31'); |
120
|
|
|
$q = $db->query("SELECT * FROM personinfo where person_id=:person_id AND data_key='standing_down_2024'", [':person_id' => $mp['person_id']]); |
121
|
|
|
$mp['standing_down_2024'] = $q['data_value'] ?? 0; |
122
|
|
|
$mp['name'] = $mp['given_name'] . ' ' . $mp['family_name']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$query_base = "SELECT member.person_id, given_name, family_name, constituency, house |
126
|
|
|
FROM member, person_names pn |
127
|
|
|
WHERE constituency IN ('" . join("','", $a) . "') |
|
|
|
|
128
|
|
|
AND member.person_id = pn.person_id AND pn.type = 'name' |
129
|
|
|
AND pn.end_date = (SELECT MAX(end_date) from person_names where person_names.person_id = member.person_id) |
130
|
|
|
AND house = $house"; |
131
|
|
|
$q = $db->query($query_base . " AND left_reason = 'still_in_office'"); |
132
|
|
|
$current = true; |
133
|
|
|
if (!$q->rows() && ($dissolution = MySociety\TheyWorkForYou\Dissolution::db())) { |
134
|
|
|
$current = false; |
135
|
|
|
$q = $db->query($query_base . " AND $dissolution[query]", |
136
|
|
|
$dissolution['params']); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$mcon = array(); $mreg = array(); |
140
|
|
|
foreach ($q as $row) { |
141
|
|
|
$cons = $row['constituency']; |
142
|
|
|
if ($house == HOUSE_TYPE_NI) { |
143
|
|
|
$mreg[] = $row; |
144
|
|
|
} elseif ($house == HOUSE_TYPE_SCOTLAND) { |
145
|
|
|
if ($cons == $areas['SPC']) { |
146
|
|
|
$mcon = $row; |
147
|
|
|
} elseif ($cons == $areas['SPE']) { |
148
|
|
|
$mreg[] = $row; |
149
|
|
|
} |
150
|
|
|
} elseif ($house == HOUSE_TYPE_WALES) { |
151
|
|
|
if ($cons == $areas['WAC']) { |
152
|
|
|
$mcon = $row; |
153
|
|
|
} elseif ($cons == $areas['WAE']) { |
154
|
|
|
$mreg[] = $row; |
155
|
|
|
} |
156
|
|
|
} else { |
157
|
|
|
$PAGE->error_message('Odd result returned, please let us know!'); |
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
$data['mcon'] = $mcon; |
162
|
|
|
$data['mreg'] = $mreg; |
163
|
|
|
$data['house'] = $house; |
164
|
|
|
$data['urlp'] = $urlp; |
165
|
|
|
$data['current'] = $current; |
166
|
|
|
$data['areas'] = $areas; |
167
|
|
|
$data['area_type'] = $area_type; |
168
|
|
|
$data['member_names'] = $member_names; |
169
|
|
|
$data['mp'] = $mp; |
170
|
|
|
|
171
|
|
|
$data['MPSURL'] = new \MySociety\TheyWorkForYou\Url('mps'); |
172
|
|
|
$data['REGURL'] = new \MySociety\TheyWorkForYou\Url($urlpl); |
173
|
|
|
$data['browse_text'] = sprintf(gettext('Browse all %s'), $member_names['plural']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
function member_redirect(&$MEMBER) { |
177
|
|
|
if ($MEMBER->valid) { |
178
|
|
|
$url = $MEMBER->url(); |
179
|
|
|
header("Location: $url"); |
180
|
|
|
exit; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function mapit_postcode($postcode) { |
185
|
|
|
$filename = 'postcode/' . rawurlencode($postcode); |
186
|
|
|
return mapit_lookup('postcode', $filename); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
function mapit_lookup($type, $filename) { |
190
|
|
|
global $mapit_ids; |
191
|
|
|
$file = web_lookup(OPTION_MAPIT_URL . $filename); |
192
|
|
|
$r = json_decode($file); |
|
|
|
|
193
|
|
|
if (isset($r->error)) return ''; |
194
|
|
|
if ($type == 'postcode' && !isset($r->areas)) return ''; |
195
|
|
|
|
196
|
|
|
$input = ($type == 'postcode') ? $r->areas : $r; |
197
|
|
|
$areas = array(); |
198
|
|
|
foreach ($input as $row) { |
199
|
|
|
if (in_array($row->type, array('WMC', 'WMCF', 'SPC', 'SPE', 'NIE', 'WAC', 'WAE'))) |
200
|
|
|
$areas[$row->type] = $row->name; |
201
|
|
|
if ($row->type == 'WMC') { |
202
|
|
|
$mapit_ids['old'] = $row->id; |
203
|
|
|
} |
204
|
|
|
if ($row->type == 'WMCF') { |
205
|
|
|
$mapit_ids['new'] = $row->id; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
if (!isset($areas['WMC'])) { |
209
|
|
|
return ''; |
210
|
|
|
} |
211
|
|
|
return $areas; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
function web_lookup($url) { |
215
|
|
|
$ch = curl_init($url); |
216
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
217
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); |
218
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
219
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
220
|
|
|
$file = curl_exec($ch); |
221
|
|
|
curl_close($ch); |
222
|
|
|
return $file; |
223
|
|
|
} |
224
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.