forked from mysociety/theyworkforyou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
people.php
235 lines (197 loc) · 7.37 KB
/
people.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
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
<?php
/* For displaying lists of people. Currently just MPs and Peers.
Do:
$PEOPLE = new PEOPLE;
$PEOPLE->display('mps');
*/
class PEOPLE {
function PEOPLE () {
$this->db = new ParlDB;
}
function display ($view, $args=array(), $format='html') {
global $PAGE;
$validviews = array('mps', 'peers', 'mlas', 'msps');
if (in_array($view, $validviews)) {
// What function do we call for this view?
$function = '_get_data_by_'.$view;
// Get all the data that's to be rendered.
$data = $this->$function($args);
} else {
$PAGE->error_message ("You haven't specified a view type.");
return false;
}
$return = $this->render($view, $data, $format);
return $return;
}
function render ($view, $data, $format='html') {
// Once we have the data that's to be rendered,
// include the template.
// No format, so don't use the template sets.
if ($format == 'none') {
return $data;
}
//This should really be a single template? (rjp)
include (INCLUDESPATH."easyparliament/templates/$format/people_$view" . ".php");
return true;
}
function _get_data_by_msps($args) {
$args['house'] = 4;
return $this->_get_data_by_group($args);
}
function _get_data_by_mlas($args) {
$args['house'] = 3;
return $this->_get_data_by_group($args);
}
function _get_data_by_peers($args) {
$args['house'] = 2;
return $this->_get_data_by_group($args);
}
function _get_data_by_mps ($args) {
$args['house'] = 1;
return $this->_get_data_by_group($args);
}
function _get_data_by_group($args) {
// $args can have an optional 'order' element.
$use_extracol = (isset($args['order']) && in_array($args['order'], array('expenses', 'debates', 'safety')));
$use_personinfo = $use_extracol;
# Defaults
$order = 'last_name';
$sqlorder = 'last_name, first_name';
$query = 'SELECT distinct member.person_id, title, first_name, last_name, constituency, party, left_reason, dept, position ';
if ($use_extracol) {
$query .= ', data_value ';
$order = $args['order'];
$sqlorder = 'data_value+0 DESC, last_name, first_name';
unset($args['date']);
$key_lookup = array(
'expenses' => 'expenses2004_total',
'debates' => 'debate_sectionsspoken_inlastyear',
'safety' => 'swing_to_lose_seat_today',
);
$personinfo_key = $key_lookup[$order];
}
$query .= 'FROM member LEFT OUTER JOIN moffice ON member.person_id = moffice.person ';
if (isset($args['date']))
$query .= 'AND from_date <= date("' . $args['date'] . '") AND date("' . $args['date'] . '") <= to_date ';
else
$query .= 'AND to_date="9999-12-31" ';
if ($use_personinfo) {
$query .= 'LEFT OUTER JOIN personinfo ON member.person_id = personinfo.person_id AND data_key="' . $personinfo_key . '" ';
}
$query .= 'WHERE house=' . $args['house'] . ' ';
if (isset($args['date']))
$query .= 'AND entered_house <= date("' . $args['date'] . '") AND date("' . $args['date'] . '") <= left_house ';
elseif (!isset($args['all']) || $args['house'] == 1)
$query .= 'AND left_house = (SELECT MAX(left_house) FROM member) ';
if (isset($args['order'])) {
$order = $args['order'];
if ($args['order'] == 'first_name') {
$sqlorder = 'first_name, last_name';
} elseif ($args['order'] == 'constituency') {
$sqlorder = 'constituency';
} elseif ($args['order'] == 'party') {
$sqlorder = 'party, last_name, first_name, constituency';
}
}
$q = $this->db->query($query . "ORDER BY $sqlorder");
$data = array();
for ($row=0; $row<$q->rows(); $row++) {
$p_id = $q->field($row, 'person_id');
$dept = $q->field($row, 'dept');
$pos = $q->field($row, 'position');
if (isset($data[$p_id])) {
$data[$p_id]['dept'] = array_merge((array)$data[$p_id]['dept'], (array)$dept);
$data[$p_id]['pos'] = array_merge((array)$data[$p_id]['pos'], (array)$pos);
} else {
$narray = array (
'person_id' => $p_id,
'title' => $q->field($row, 'title'),
'first_name' => $q->field($row, 'first_name'),
'last_name' => $q->field($row, 'last_name'),
'constituency' => $q->field($row, 'constituency'),
'party' => $q->field($row, 'party'),
'left_reason' => $q->field($row, 'left_reason'),
'dept' => $dept,
'pos' => $pos
);
if ($use_extracol) {
$narray['data_value'] = $q->field($row, 'data_value');
}
if ($narray['party'] == 'SPK') {
$narray['party'] = '-';
$narray['pos'] = 'Speaker';
$narray['dept'] = 'House of Commons';
} elseif ($narray['party'] == 'CWM' || $narray['party'] == 'DCWM') {
$narray['party'] = '-';
$narray['pos'] = 'Deputy Speaker';
$narray['dept'] = 'House of Commons';
}
$data[$p_id] = $narray;
}
}
if ($args['house'] == 2 && ($order == 'name' || $order == 'constituency'))
uasort($data, array($this, 'by_peer_name'));
$data = array (
'info' => array (
'order' => $order
),
'data' => $data
);
return $data;
}
function by_peer_name($a, $b) {
if (!$a['last_name'] && !$b['last_name'])
return strcmp($a['constituency'], $b['constituency']);
if (!$a['last_name'])
return strcmp($a['constituency'], $b['last_name']);
if (!$b['last_name'])
return strcmp($a['last_name'], $b['constituency']);
if (strcmp($a['last_name'], $b['last_name']))
return strcmp($a['last_name'], $b['last_name']);
return strcmp($a['constituency'], $b['constituency']);
}
function listoptions($args) {
global $THEUSER;
$data = $this->_get_data_by_mps($args);
if ($THEUSER->isloggedin() && $THEUSER->postcode() != '' || $THEUSER->postcode_is_set()) {
$MEMBER = new MEMBER(array('postcode'=>$THEUSER->postcode(), 'house' => 1));
print '<option value="'.$MEMBER->person_id().'">Your MP, '.$MEMBER->full_name().'</option>';
}
print '<optgroup label="MPs">';
foreach ($data['data'] as $row) {
print '<option';
if (isset($args['pid']) && $args['pid']==$row['person_id']) print ' selected';
print ' value="'.$row['person_id'].'">' . $row['first_name'].' '.$row['last_name'];
print ', ' . $row['constituency'];
print '</option>';
}
print '</optgroup> <optgroup label="Peers">';
$data = $this->_get_data_by_peers($args);
foreach ($data['data'] as $row) {
print '<option';
if (isset($args['pid']) && $args['pid']==$row['person_id']) print ' selected';
print ' value="'.$row['person_id'].'">';
print ucfirst(member_full_name(2, $row['title'], $row['first_name'], $row['last_name'], $row['constituency']));
print '</option>';
}
print '</optgroup> <optgroup label="MLAs">';
$data = $this->_get_data_by_mlas($args);
foreach ($data['data'] as $row) {
print '<option';
if (isset($args['pid']) && $args['pid']==$row['person_id']) print ' selected';
print ' value="'.$row['person_id'].'">';
print ucfirst(member_full_name(3, $row['title'], $row['first_name'], $row['last_name'], $row['constituency']));
print '</option>';
}
print '</optgroup> <optgroup label="MSPs">';
$data = $this->_get_data_by_msps($args);
foreach ($data['data'] as $row) {
print '<option';
if (isset($args['pid']) && $args['pid']==$row['person_id']) print ' selected';
print ' value="'.$row['person_id'].'">';
print ucfirst(member_full_name(4, $row['title'], $row['first_name'], $row['last_name'], $row['constituency']));
print '</option>';
}
print '</optgroup>';
}
}