Completed
Push — master ( 922be9...285e65 )
by Nick
41s queued 31s
created

MEMBER::current_member()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 8
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
c 0
b 0
f 0
1
<?php
2
3
include_once INCLUDESPATH."easyparliament/glossary.php";
4
5
class MEMBER {
6
7
    public $valid = false;
8
    public $member_id;
9
    public $person_id;
10
    public $title;
11
    public $given_name;
12
    public $family_name;
13
    public $lordofname;
14
    public $constituency;
15
    public $party;
16
    public $other_parties = array();
17
    public $other_constituencies;
18
    public $houses = array();
19
    public $entered_house = array();
20
    public $left_house = array();
21
    public $extra_info = array();
22
    // Is this MP THEUSERS's MP?
23
    public $the_users_mp = false;
24
    public $house_disp = 0; # Which house we should display this person in
25
26
    // Mapping member table 'house' numbers to text.
27
    public $houses_pretty = array(
28
        0 => 'Royal Family',
29
        1 => 'House of Commons',
30
        2 => 'House of Lords',
31
        3 => 'Northern Ireland Assembly',
32
        4 => 'Scottish Parliament',
33
    );
34
35
    // Mapping member table reasons to text.
36
    public $reasons = array(
37
        'became_peer'		=> 'Became peer',
38
        'by_election'		=> 'Byelection',
39
        'changed_party'		=> 'Changed party',
40
        'changed_name' 		=> 'Changed name',
41
        'declared_void'		=> 'Declared void',
42
        'died'			=> 'Died',
43
        'disqualified'		=> 'Disqualified',
44
        'general_election' 	=> 'General election',
45
        'general_election_standing' 	=> array('General election (standing again)', 'General election (stood again)'),
46
        'general_election_not_standing' 	=> 'did not stand for re-election',
47
        'reinstated'		=> 'Reinstated',
48
        'resigned'		=> 'Resigned',
49
        'still_in_office'	=> 'Still in office',
50
        'dissolution'		=> 'Dissolved for election',
51
        'regional_election'	=> 'Election', # Scottish Parliament
52
        'replaced_in_region'	=> 'Appointed, regional replacement',
53
54
    );
55
56 16
    public function __construct($args) {
57
        // $args is a hash like one of:
58
        // member_id 		=> 237
59
        // person_id 		=> 345
60
        // constituency 	=> 'Braintree'
61
        // postcode			=> 'e9 6dw'
62
63
        // If just a constituency we currently just get the current member for
64
        // that constituency.
65
66 16
        global $this_page;
67
68 16
        $house = isset($args['house']) ? $args['house'] : null;
69
70 16
        $this->db = new ParlDB;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71 16
        $person_id = '';
72 16
        if (isset($args['member_id']) && is_numeric($args['member_id'])) {
73 1
            $person_id = $this->member_id_to_person_id($args['member_id']);
74 15
        } elseif (isset($args['name'])) {
75 3
            $con = isset($args['constituency']) ? $args['constituency'] : '';
76 3
            $person_id = $this->name_to_person_id($args['name'], $con);
77 12
        } elseif (isset($args['constituency'])) {
78 1
            $still_in_office = isset($args['still_in_office']) ? $args['still_in_office'] : false;
79 1
            $person_id = $this->constituency_to_person_id($args['constituency'], $house, $still_in_office);
0 ignored issues
show
Unused Code introduced by
The call to MEMBER::constituency_to_person_id() has too many arguments starting with $still_in_office. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            /** @scrutinizer ignore-call */ 
80
            $person_id = $this->constituency_to_person_id($args['constituency'], $house, $still_in_office);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80 11
        } elseif (isset($args['postcode'])) {
81
            $person_id = $this->postcode_to_person_id($args['postcode'], $house);
82 11
        } elseif (isset($args['person_id']) && is_numeric($args['person_id'])) {
83 11
            $person_id = $args['person_id'];
84 11
            $q = $this->db->query("SELECT gid_to FROM gidredirect
85
                    WHERE gid_from = :gid_from",
86 11
                array(':gid_from' => "uk.org.publicwhip/person/$person_id")
87 11
            )->first();
88 11
            if ($q) {
89
                $person_id = str_replace('uk.org.publicwhip/person/', '', $q['gid_to']);
90
            }
91
        }
92
93 15
        if (!$person_id) {
94
            return;
95
        }
96
97 15
        $q = $this->db->query("SELECT member_id, house, title,
98
            given_name, family_name, lordofname, constituency, party, lastupdate,
99
            entered_house, left_house, entered_reason, left_reason, member.person_id
100
            FROM member, person_names pn
101
            WHERE member.person_id = :person_id
102
                AND member.person_id = pn.person_id AND pn.type = 'name' AND pn.start_date <= left_house AND left_house <= pn.end_date
103
            ORDER BY left_house DESC, house", array(
104 15
                ':person_id' => $person_id
105
            ));
106
107 15
        if (!$q->rows() > 0) {
108
            return;
109
        }
110
111 15
        $this->valid = true;
112
113 15
        $this->house_disp = 0;
114 15
        $last_party = null;
115 15
        foreach ($q as $row) {
116 15
            $house = $row['house'];
117 15
            if (!in_array($house, $this->houses)) $this->houses[] = $house;
118 15
            $const = $row['constituency'];
119 15
            $party = $row['party'];
120 15
            $entered_house = $row['entered_house'];
121 15
            $left_house = $row['left_house'];
122 15
            $entered_reason = $row['entered_reason'];
123 15
            $left_reason = $row['left_reason'];
124
125 15
            if (!isset($this->entered_house[$house]) || $entered_house < $this->entered_house[$house]['date']) {
126 15
                $this->entered_house[$house] = array(
127 15
                    'date' => $entered_house,
128 15
                    'date_pretty' => $this->entered_house_text($entered_house),
129 15
                    'reason' => $this->entered_reason_text($entered_reason),
130 15
                    'house' => $house
131
                );
132
            }
133
134 15
            if (!isset($this->left_house[$house])) {
135 15
                $this->left_house[$house] = array(
136 15
                    'date' => $left_house,
137 15
                    'date_pretty' => $this->left_house_text($left_house),
138 15
                    'reason' => $this->left_reason_text($left_reason, $left_house, $house),
139 15
                    'constituency' => $const,
140 15
                    'party' => $this->party_text($party),
141 15
                    'house' => $house
142
                );
143
            }
144
145 15
            if ( $house==HOUSE_TYPE_ROYAL 					# The Monarch
146 14
                || (!$this->house_disp && $house==HOUSE_TYPE_SCOTLAND)	# MSPs and
147 12
                || (!$this->house_disp && $house==HOUSE_TYPE_NI)	# MLAs have lowest priority
148 11
                || ($this->house_disp!=HOUSE_TYPE_LORDS && $house==HOUSE_TYPE_LORDS)	# Lords have highest priority
149 15
                || (!$this->house_disp && $house==HOUSE_TYPE_COMMONS) # MPs
150
            ) {
151 15
                $this->house_disp = $house;
152 15
                $this->constituency = $const;
153 15
                $this->party = $party;
154
155 15
                $this->member_id = $row['member_id'];
156 15
                $this->title = $row['title'];
157 15
                $this->given_name = $row['given_name'];
158 15
                $this->family_name = $row['family_name'];
159 15
                $this->lordofname = $row['lordofname'];
160 15
                $this->person_id = $row['person_id'];
161
            }
162
163 15
            if (($last_party && $party && $party != $last_party) || $left_reason == 'changed_party') {
164
                $this->other_parties[] = array(
165
                    'from' => $this->party_text($party),
166
                    'date' => $left_house,
167
                );
168
            }
169 15
            $last_party = $party;
170
171 15
            if ($const != $this->constituency) {
172 15
                $this->other_constituencies[$const] = true;
173
            }
174
        }
175 15
        $this->other_parties = array_reverse($this->other_parties);
176
177
        // Loads extra info from DB - you now have to call this from outside
178
            // when you need it, as some uses of MEMBER are lightweight (e.g.
179
            // in searchengine.php)
180
        // $this->load_extra_info();
181
182 15
        $this->set_users_mp();
183 15
    }
184
185 1
    public function member_id_to_person_id($member_id) {
186 1
        $q = $this->db->query("SELECT person_id FROM member
187
                    WHERE member_id = :member_id",
188 1
            array(':member_id' => $member_id)
189 1
        )->first();
190 1
        if (!$q) {
191
            $q = $this->db->query("SELECT person_id FROM gidredirect, member
192
                    WHERE gid_from = :gid_from AND
193
                        CONCAT('uk.org.publicwhip/member/', member_id) = gid_to",
194
                array(':gid_from' => "uk.org.publicwhip/member/$member_id")
195
            )->first();
196
        }
197 1
        if ($q) {
198 1
            return $q['person_id'];
199
        } else {
200
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, there is no member with a member ID of "' . _htmlentities($member_id) . '".');
201
        }
202
    }
203
204
    public function postcode_to_person_id($postcode, $house=null) {
205
        twfy_debug ('MP', "postcode_to_person_id converting postcode to person");
206
        $constituency = strtolower(MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($postcode));
207
        return $this->constituency_to_person_id($constituency, $house);
208
    }
209
210 1
    public function constituency_to_person_id($constituency, $house=null) {
211 1
        if ($constituency == '') {
212
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, no constituency was found.');
213
        }
214
215 1
        if ($constituency == 'Orkney ') {
216
            $constituency = 'Orkney & Shetland';
217
        }
218
219 1
        $normalised = MySociety\TheyWorkForYou\Utility\Constituencies::normaliseConstituencyName($constituency);
220 1
        if ($normalised) $constituency = $normalised;
221
222 1
        $params = array();
223
224 1
        $left = "left_reason = 'still_in_office'";
225 1
        if ($dissolution = MySociety\TheyWorkForYou\Dissolution::db()) {
226
            $left = "($left OR $dissolution[query])";
227
            $params = $dissolution['params'];
228
        }
229
        $query = "SELECT person_id FROM member
230
                WHERE constituency = :constituency
231 1
                AND $left";
232
233 1
        $params[':constituency'] = $constituency;
234
235 1
        if ($house) {
236
            $query .= ' AND house = :house';
237
            $params[':house'] = $house;
238
        }
239
240 1
        $q = $this->db->query($query, $params)->first();
241
242 1
        if ($q) {
243 1
            return $q['person_id'];
244
        } else {
245
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, there is no current member for the "' . _htmlentities(ucwords($constituency)) . '" constituency.');
246
        }
247
    }
248
249 3
    public function name_to_person_id($name, $const='') {
250 3
        global $this_page;
251 3
        if ($name == '') {
252
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, no name was found.');
253
        }
254
255 3
        $params = array();
256 3
        $q = "SELECT person_id FROM person_names WHERE type = 'name' ";
257 3
        if ($this_page == 'peer') {
258
            $success = preg_match('#^(.*?) (.*?) of (.*?)$#', $name, $m);
259
            if (!$success)
260
                $success = preg_match('#^(.*?)() of (.*?)$#', $name, $m);
261
            if (!$success)
262
                $success = preg_match('#^(.*?) (.*?)()$#', $name, $m);
263
            if (!$success) {
264
                throw new MySociety\TheyWorkForYou\MemberException('Sorry, that name was not recognised.');
265
            }
266
            $params[':title'] = $m[1];
267
            $params[':family_name'] = $m[2];
268
            $params[':lordofname'] = $m[3];
269
            $q .= "AND title = :title AND family_name = :family_name AND lordofname = :lordofname";
270 3
        } elseif ($this_page == 'msp' || $this_page == 'mla' || strstr($this_page, 'mp')) {
271 3
            $success = preg_match('#^(.*?) (.*?) (.*?)$#', $name, $m);
272 3
            if (!$success)
273 3
                $success = preg_match('#^(.*?)() (.*)$#', $name, $m);
274 3
            if (!$success) {
275
                throw new MySociety\TheyWorkForYou\MemberException('Sorry, that name was not recognised.');
276
            }
277 3
            $params[':given_name'] = $m[1];
278 3
            $params[':middle_name'] = $m[2];
279 3
            $params[':family_name'] = $m[3];
280 3
            $params[':first_and_middle_names'] = $m[1] . ' ' . $m[2];
281 3
            $params[':middle_and_last_names'] = $m[2] . ' ' . $m[3];
282
            # Note this works only because MySQL ignores trailing whitespace
283 3
            $q .= "AND (
284
                (given_name=:first_and_middle_names AND family_name=:family_name)
285
                OR (given_name=:given_name AND family_name=:middle_and_last_names)
286
                OR (title=:given_name AND given_name=:middle_name AND family_name=:family_name)
287
            )";
288
        }
289
290 3
        $q = $this->db->query($q, $params);
291 3
        if (!$q->rows()) {
292
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, we could not find anyone with that name.');
293 3
        } elseif ($q->rows() == 1) {
294 2
            return $q->first()['person_id'];
295
        }
296
297
        # More than one person ID matching the given name
298 1
        $person_ids = array();
299 1
        foreach ($q as $row) {
300 1
            $pid = $row['person_id'];
301 1
            $person_ids[$pid] = 1;
302
        }
303 1
        $pids = array_keys($person_ids);
304
305 1
        $params = array();
306 1
        if ($this_page == 'peer') {
307
            $params[':house'] = HOUSE_TYPE_LORDS;
308 1
        } elseif ($this_page == 'msp') {
309
            $params[':house'] = HOUSE_TYPE_SCOTLAND;
310 1
        } elseif ($this_page == 'mla') {
311
            $params[':house'] = HOUSE_TYPE_NI;
312 1
        } elseif ($this_page == 'royal') {
313
            $params[':house'] = HOUSE_TYPE_ROYAL;
314
        } else {
315 1
            $params[':house'] = HOUSE_TYPE_COMMONS;
316
        }
317
318 1
        $pids_str = join(',', $pids);
319 1
        $q = "SELECT person_id, constituency FROM member WHERE person_id IN ($pids_str) AND house = :house";
320 1
        if ($const) {
321
            $params[':constituency'] = $const;
322
            $q .= ' AND constituency=:constituency';
323
        }
324 1
        $q .= ' GROUP BY person_id';
325
326 1
        $q = $this->db->query($q, $params);
327 1
        if ($q->rows() > 1) {
328 1
            $person_ids = array();
329 1
            foreach ($q as $row) {
330 1
                $person_ids[$row['person_id']] = $row['constituency'];
331
            }
332 1
            throw new MySociety\TheyWorkForYou\MemberMultipleException($person_ids);
333
        } elseif ($q->rows() > 0) {
334
            return $q->first()['person_id'];
335
        } elseif ($const) {
336
            return $this->name_to_person_id($name);
337
        } else {
338
            throw new MySociety\TheyWorkForYou\MemberException('Sorry, there is no current member with that name.');
339
        }
340
    }
341
342 15
    public function set_users_mp() {
343
        // Is this MP THEUSER's MP?
344 15
        global $THEUSER;
345 15
        if (is_object($THEUSER) && $THEUSER->postcode_is_set() && $this->current_member(1)) {
346
            $pc = $THEUSER->postcode();
347
            twfy_debug ('MP', "set_users_mp converting postcode to person");
348
            $constituency = strtolower(MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituency($pc));
349
            if ($constituency == strtolower($this->constituency())) {
350
                $this->the_users_mp = true;
351
            }
352
        }
353 15
    }
354
355
    // Grabs extra information (e.g. external links) from the database
356
    # DISPLAY is whether it's to be displayed on MP page.
357 1
    public function load_extra_info($display = false, $force = false) {
358 1
        $memcache = new MySociety\TheyWorkForYou\Memcache;
359 1
        $memcache_key = 'extra_info:' . $this->person_id . ($display ? '' : ':plain');
360 1
        $this->extra_info = $memcache->get($memcache_key);
361 1
        if (!DEVSITE && !$force && $this->extra_info) {
362
            return;
363
        }
364 1
        $this->extra_info = array();
365
366 1
        $q = $this->db->query('SELECT * FROM moffice WHERE person=:person_id ORDER BY from_date DESC, moffice_id',
367 1
                              array(':person_id' => $this->person_id));
368 1
        $this->extra_info['office'] = $q->fetchAll();
369
370
        // Info specific to member id (e.g. attendance during that period of office)
371 1
        $q = $this->db->query("SELECT data_key, data_value
372
                        FROM 	memberinfo
373
                        WHERE	member_id = :member_id",
374 1
            array(':member_id' => $this->member_id));
375 1
        foreach ($q as $row) {
376 1
            $this->extra_info[$row['data_key']] = $row['data_value'];
377
            #		if ($row['joint'] > 1)
378
            #			$this->extra_info[$row['data_key'].'_joint'] = true;
379
        }
380
381
        // Info specific to person id (e.g. their permanent page on the Guardian website)
382 1
        $q = $this->db->query("SELECT data_key, data_value
383
                        FROM 	personinfo
384
                        WHERE	person_id = :person_id",
385 1
            array(':person_id' => $this->person_id));
386 1
        foreach ($q as $row) {
387 1
            $this->extra_info[$row['data_key']] = $row['data_value'];
388
        #	    if ($row['count'] > 1)
389
        #	    	$this->extra_info[$row['data_key'].'_joint'] = true;
390
        }
391
392
        // Info specific to constituency (e.g. election results page on Guardian website)
393 1
        if ($this->house(HOUSE_TYPE_COMMONS)) {
394
395 1
            $q = $this->db->query("SELECT data_key, data_value FROM consinfo
396
            WHERE constituency = :constituency",
397 1
                array(':constituency' => $this->constituency));
398 1
            foreach ($q as $row) {
399 1
                $this->extra_info[$row['data_key']] = $row['data_value'];
400
            }
401
        }
402
403 1
        if (array_key_exists('public_whip_rebellions', $this->extra_info)) {
404
            $rebellions = $this->extra_info['public_whip_rebellions'];
405
            $rebel_desc = "<unknown>";
406
            if ($rebellions == 0)
407
                $rebel_desc = "never";
408
            elseif ($rebellions <= 1)
409
                $rebel_desc = "hardly ever";
410
            elseif ($rebellions <= 3)
411
                $rebel_desc = "occasionally";
412
            elseif ($rebellions <= 5)
413
                $rebel_desc = "sometimes";
414
            elseif ($rebellions > 5)
415
                $rebel_desc = "quite often";
416
            $this->extra_info['public_whip_rebel_description'] = $rebel_desc;
417
        }
418
419 1
        if (isset($this->extra_info['public_whip_attendrank'])) {
420
            $prefix = ($this->house(HOUSE_TYPE_LORDS) ? 'L' : '');
421
            $this->extra_info[$prefix.'public_whip_division_attendance_rank'] = $this->extra_info['public_whip_attendrank'];
422
            $this->extra_info[$prefix.'public_whip_division_attendance_rank_outof'] = $this->extra_info['public_whip_attendrank_outof'];
423
            $this->extra_info[$prefix.'public_whip_division_attendance_quintile'] = floor($this->extra_info['public_whip_attendrank'] / ($this->extra_info['public_whip_attendrank_outof']+1) * 5);
424
        }
425 1
        if ($this->house(HOUSE_TYPE_LORDS) && isset($this->extra_info['public_whip_division_attendance'])) {
426
            $this->extra_info['Lpublic_whip_division_attendance'] = $this->extra_info['public_whip_division_attendance'];
427
            unset($this->extra_info['public_whip_division_attendance']);
428
        }
429
430 1
        if ($display && array_key_exists('register_member_interests_html', $this->extra_info) && ($this->extra_info['register_member_interests_html'] != '')) {
431
            $args = array (
432
                "sort" => "regexp_replace"
433
            );
434
            $GLOSSARY = new GLOSSARY($args);
435
            $this->extra_info['register_member_interests_html'] =
436
        $GLOSSARY->glossarise($this->extra_info['register_member_interests_html']);
437
        }
438
439 1
        $q = $this->db->query('select count(*) as c from alerts where criteria like "%speaker:'.$this->person_id.'%" and confirmed and not deleted')->first();
440 1
        $this->extra_info['number_of_alerts'] = $q['c'];
441
442 1
        if (isset($this->extra_info['reading_ease'])) {
443
            $this->extra_info['reading_ease'] = round($this->extra_info['reading_ease'], 2);
444
            $this->extra_info['reading_year'] = round($this->extra_info['reading_year'], 0);
445
            $this->extra_info['reading_age'] = $this->extra_info['reading_year'] + 4;
446
            $this->extra_info['reading_age'] .= '&ndash;' . ($this->extra_info['reading_year'] + 5);
447
        }
448
449
        # Public Bill Committees
450 1
        $q = $this->db->query('select bill_id,session,title,sum(attending) as a,sum(chairman) as c
451
            from pbc_members, bills
452 1
            where bill_id = bills.id and person_id = ' . $this->person_id()
453 1
             . ' group by bill_id order by session desc');
454 1
        $this->extra_info['pbc'] = array();
455 1
        foreach ($q as $row) {
456 1
            $bill_id = $row['bill_id'];
457 1
            $c = $this->db->query('select count(*) as c from hansard where major=6 and minor='.$bill_id.' and htype=10')->first();
458 1
            $c = $c['c'];
459 1
            $title = $row['title'];
460 1
            $attending = $row['a'];
461 1
            $chairman = $row['c'];
462 1
            $this->extra_info['pbc'][$bill_id] = array(
463 1
                'title' => $title, 'session' => $row['session'],
464 1
                'attending'=>$attending, 'chairman'=>($chairman>0), 'outof' => $c
465
            );
466
        }
467
468 1
        $memcache->set($memcache_key, $this->extra_info);
469 1
    }
470
471
    // Functions for accessing things about this Member.
472
473
    public function member_id() { return $this->member_id; }
474
    public function person_id() { return $this->person_id; }
475
    public function given_name() { return $this->given_name; }
476
    public function family_name() { return $this->family_name; }
477 6
    public function full_name($no_mp_title = false) {
478 6
        $title = $this->title;
479 6
        if ($no_mp_title && ($this->house_disp==HOUSE_TYPE_COMMONS || $this->house_disp==HOUSE_TYPE_NI || $this->house_disp==HOUSE_TYPE_SCOTLAND))
480 4
            $title = '';
481 6
        return member_full_name($this->house_disp, $title, $this->given_name, $this->family_name, $this->lordofname);
482
    }
483
    public function houses() {
484
        return $this->houses;
485
    }
486 12
    public function house($house) {
487 12
        return in_array($house, $this->houses) ? true : false;
488
    }
489
    public function house_text($house) {
490
        return $this->houses_pretty[$house];
491
    }
492
    public function constituency() { return $this->constituency; }
493
    public function party() { return $this->party; }
494 15
    public function party_text($party = null) {
495 15
        global $parties;
496 15
        if (!$party)
497 8
            $party = $this->party;
498 15
        if (isset($parties[$party])) {
499 7
            return $parties[$party];
500
        } else {
501 8
            return $party;
502
        }
503
    }
504
505 2
    public function entered_house($house = null) {
506 2
        if ( isset($house) ) {
507 2
            if ( array_key_exists($house, $this->entered_house) ) {
508 2
                return $this->entered_house[$house];
509
            } else {
510 1
                return null;
511
            }
512
        }
513
        return $this->entered_house;
514
    }
515
516 15
    public function entered_house_text($entered_house) {
517 15
        if (!$entered_house) return '';
518 15
        list($year, $month, $day) = explode('-', $entered_house);
519 15
        if ($month==1 && $day==1 && $this->house(HOUSE_TYPE_LORDS)) {
520 1
            return $year;
521 14
        } elseif ($month==0 && $day==0) {
522
            return $year;
523 14
        } elseif (checkdate($month, $day, $year) && $year != '9999') {
0 ignored issues
show
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

523
        } elseif (checkdate(/** @scrutinizer ignore-type */ $month, $day, $year) && $year != '9999') {
Loading history...
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

523
        } elseif (checkdate($month, $day, /** @scrutinizer ignore-type */ $year) && $year != '9999') {
Loading history...
Bug introduced by
$day of type string is incompatible with the type integer expected by parameter $day of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

523
        } elseif (checkdate($month, /** @scrutinizer ignore-type */ $day, $year) && $year != '9999') {
Loading history...
524 14
            return format_date($entered_house, LONGDATEFORMAT);
525
        } else {
526
            return "n/a";
527
        }
528
    }
529
530 1
    public function left_house($house = null) {
531 1
        if ( isset($house) ) {
532 1
            if ( array_key_exists($house, $this->left_house) ) {
533 1
                return $this->left_house[$house];
534
            } else {
535 1
                return null;
536
            }
537
        }
538
        return $this->left_house;
539
    }
540
541 15
    public function left_house_text($left_house) {
542 15
        if (!$left_house) return '';
543 15
        list($year, $month, $day) = explode('-', $left_house);
544 15
        if (checkdate($month, $day, $year) && $year != '9999') {
0 ignored issues
show
Bug introduced by
$month of type string is incompatible with the type integer expected by parameter $month of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

544
        if (checkdate(/** @scrutinizer ignore-type */ $month, $day, $year) && $year != '9999') {
Loading history...
Bug introduced by
$year of type string is incompatible with the type integer expected by parameter $year of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

544
        if (checkdate($month, $day, /** @scrutinizer ignore-type */ $year) && $year != '9999') {
Loading history...
Bug introduced by
$day of type string is incompatible with the type integer expected by parameter $day of checkdate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

544
        if (checkdate($month, /** @scrutinizer ignore-type */ $day, $year) && $year != '9999') {
Loading history...
545 2
            return format_date($left_house, LONGDATEFORMAT);
546 13
        } elseif ($month==0 && $day==0) {
547
            # Left house date is stored as 1942-00-00 to mean "at some point in 1941"
548
            return $year - 1;
549
        } else {
550 13
            return "n/a";
551
        }
552
    }
553
554
    public function entered_reason() { return $this->entered_reason; }
0 ignored issues
show
Bug introduced by
The property entered_reason does not exist on MEMBER. Did you mean entered_house?
Loading history...
555 15
    public function entered_reason_text($entered_reason) {
556 15
        if (isset($this->reasons[$entered_reason])) {
557 13
            return $this->reasons[$entered_reason];
558
        } else {
559 2
            return $entered_reason;
560
        }
561
    }
562
563
    public function left_reason() { return $this->left_reason; }
0 ignored issues
show
Bug Best Practice introduced by
The property left_reason does not exist on MEMBER. Did you maybe forget to declare it?
Loading history...
564 15
    public function left_reason_text($left_reason, $left_house, $house) {
565 15
        if (isset($this->reasons[$left_reason])) {
566 15
            $left_reason = $this->reasons[$left_reason];
567 15
            if (is_array($left_reason)) {
568
                $q = $this->db->query("SELECT MAX(left_house) AS max FROM member WHERE house=$house")->first();
569
                $max = $q['max'];
570
                if ($max == $left_house) {
571
                    return $left_reason[0];
572
                } else {
573
                    return $left_reason[1];
574
                }
575
            } else {
576 15
                return $left_reason;
577
            }
578
        } else {
579
            return $left_reason;
580
        }
581
    }
582
583
    public function extra_info() { return $this->extra_info; }
584
585 1
    public function current_member($house = 0) {
586 1
        $current = array();
587 1
        foreach (array_keys($this->houses_pretty) as $h) {
588 1
            $lh = $this->left_house($h);
589 1
            $current[$h] = ($lh['date'] == '9999-12-31');
590
        }
591 1
        if ($house) return $current[$house];
592
        return $current;
593
    }
594
595
    public function the_users_mp() { return $this->the_users_mp; }
596
597 6
    public function url($absolute = false) {
598 6
        $house = $this->house_disp;
599 6
        if ($house == HOUSE_TYPE_COMMONS) {
600 2
            $URL = new \MySociety\TheyWorkForYou\Url('mp');
601 4
        } elseif ($house == HOUSE_TYPE_LORDS) {
602 1
            $URL = new \MySociety\TheyWorkForYou\Url('peer');
603 3
        } elseif ($house == HOUSE_TYPE_NI) {
604 1
            $URL = new \MySociety\TheyWorkForYou\Url('mla');
605 2
        } elseif ($house == HOUSE_TYPE_SCOTLAND) {
606 1
            $URL = new \MySociety\TheyWorkForYou\Url('msp');
607 1
        } elseif ($house == HOUSE_TYPE_ROYAL) {
608 1
            $URL = new \MySociety\TheyWorkForYou\Url('royal');
609
        }
610 6
        $member_url = make_member_url($this->full_name(true), $this->constituency(), $house, $this->person_id());
611 6
        if ($absolute) {
612
            $protocol = 'https://';
613
            if (DEVSITE) {
614
                $protocol = 'http://';
615
            }
616
            return $protocol . DOMAIN . $URL->generate('none') . $member_url;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $URL does not seem to be defined for all execution paths leading up to this point.
Loading history...
617
        } else {
618 6
            return $URL->generate('none') . $member_url;
619
        }
620
    }
621
622
    private function _previous_future_mps_query($direction) {
623
        $entered_house = $this->entered_house(HOUSE_TYPE_COMMONS);
624
        if (is_null($entered_house)) return '';
625
        if ($direction == '>') {
626
            $order = '';
627
        } else {
628
            $order = 'DESC';
629
        }
630
        $q = $this->db->query('SELECT *
631
            FROM member, person_names pn
632
            WHERE member.person_id = pn.person_id AND pn.type = "name"
633
                AND pn.start_date <= member.left_house AND member.left_house <= pn.end_date
634
                AND house = :house AND constituency = :cons
635
                AND member.person_id != :pid AND entered_house ' . $direction . ' :date ORDER BY entered_house ' . $order,
636
            array(
637
                ':house' => HOUSE_TYPE_COMMONS,
638
                ':cons' => $this->constituency(),
639
                ':pid' => $this->person_id(),
640
                ':date' => $entered_house['date'],
641
            ));
642
        $mships = array(); $last_pid = null;
643
        foreach ($q as $row) {
644
            $pid = $row['person_id'];
645
            $name = $row['given_name'] . ' ' . $row['family_name'];
646
            if ($last_pid != $pid) {
647
                $mships[] = array(
648
                    'href' => WEBPATH . 'mp/?pid='.$pid,
649
                    'text' => $name
650
                );
651
                $last_pid = $pid;
652
            }
653
        }
654
        return $mships;
655
    }
656
657
    public function previous_mps() {
658
        return $this->_previous_future_mps_query('<');
659
    }
660
661
    public function future_mps() {
662
        return $this->_previous_future_mps_query('>');
663
    }
664
665
    public function current_member_anywhere() {
666
        $is_current = false;
667
        $current_memberships = $this->current_member();
668
        foreach ($current_memberships as $current_memberships) {
669
            if ($current_memberships === true) {
670
                $is_current = true;
671
            }
672
        }
673
674
        return $is_current;
675
    }
676
}
677