Passed
Pull Request — master (#1700)
by Struan
04:10
created

MEMBER::party()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 1
rs 10
ccs 0
cts 0
cp 0
crap 2
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 1
    private function houses_pretty() {
28
        return array(
29 1
            0 => gettext('Royal Family'),
30 1
            1 => gettext('House of Commons'),
31 1
            2 => gettext('House of Lords'),
32 1
            3 => gettext('Northern Ireland Assembly'),
33 1
            4 => gettext('Scottish Parliament'),
34 1
            5 => gettext('Senedd'),
35 1
            6 => gettext('London Assembly'),
36
        );
37
    }
38
39
    // Mapping member table reasons to text.
40 36
    private function reasons() {
41
        return array(
42 36
            'became_peer'		=> gettext('Became peer'),
43 36
            'by_election'		=> gettext('Byelection'),
44 36
            'changed_party'		=> gettext('Changed party'),
45 36
            'changed_name' 		=> gettext('Changed name'),
46 36
            'declared_void'		=> gettext('Declared void'),
47 36
            'died'			=> gettext('Died'),
48 36
            'disqualified'		=> gettext('Disqualified'),
49 36
            'general_election' 	=> gettext('General election'),
50 36
            'general_election_standing' 	=> array(gettext('General election (standing again)'), gettext('General election (stood again)')),
51 36
            'general_election_not_standing' 	=> gettext('did not stand for re-election'),
52 36
            'reinstated'		=> gettext('Reinstated'),
53 36
            'resigned'		=> gettext('Resigned'),
54 36
            'recall_petition'   => gettext('Removed from office by a recall petition'),
55 36
            'still_in_office'	=> gettext('Still in office'),
56 36
            'dissolution'		=> gettext('Dissolved for election'),
57 36
            'regional_election'	=> gettext('Election'), # Scottish Parliament
58 36
            'replaced_in_region'	=> gettext('Appointed, regional replacement'),
59
        );
60
    }
61
62
    private $db;
63
64
    /*
65
     * Is given house higher priority than current?
66
     *
67
     * Determine if the given house is a higher priority than the currently displayed one.
68
     *
69
     * @param int $house The number of the house to evaluate.
70
     *
71
     * @return boolean
72
     */
73
74 36
    private function isHigherPriorityHouse(int $house)
75
    {
76
        # The monarch always takes priority, so if the house is royal always say "yes"
77 36
        if ($house == HOUSE_TYPE_ROYAL) {
78 1
            return true;
79
        }
80
81
        # If the current house is *not* Lords, and the house to check is, Lords is next
82 35
        if ($this->house_disp != HOUSE_TYPE_LORDS && $house == HOUSE_TYPE_LORDS) {
83 2
            return true;
84
        }
85
86
        # All the following only happen if the house to display isn't yet set.
87
        # TODO: This relies on interpreting the default value of 0 as a false, which may be error-prone.
88 34
        if (! (bool) $this->house_disp) {
89 34
            if ($house == HOUSE_TYPE_LONDON_ASSEMBLY # London Assembly
90 34
                || $house == HOUSE_TYPE_SCOTLAND     # MSPs and
91 32
                || $house == HOUSE_TYPE_WALES        # MSs and
92 32
                || $house == HOUSE_TYPE_NI           # MLAs have lowest priority
93 34
                || $house == HOUSE_TYPE_COMMONS      # MPs
94
            ) {
95 34
                return true;
96
            }
97
        }
98
99 22
        return false;
100
    }
101
102 37
    public function __construct($args) {
103
        // $args is a hash like one of:
104
        // member_id 		=> 237
105
        // person_id 		=> 345
106
        // constituency 	=> 'Braintree'
107
        // postcode			=> 'e9 6dw'
108
109
        // If just a constituency we currently just get the current member for
110
        // that constituency.
111
112 37
        global $this_page;
113
114 37
        $house = isset($args['house']) ? $args['house'] : null;
115
116 37
        $this->db = new ParlDB;
117
118 37
        $person_id = '';
119 37
        if (isset($args['member_id']) && is_numeric($args['member_id'])) {
120 17
            $person_id = $this->member_id_to_person_id($args['member_id']);
121 35
        } elseif (isset($args['name'])) {
122 3
            $con = isset($args['constituency']) ? $args['constituency'] : '';
123 3
            $person_id = $this->name_to_person_id($args['name'], $con);
124 32
        } elseif (isset($args['constituency'])) {
125 1
            $still_in_office = isset($args['still_in_office']) ? $args['still_in_office'] : false;
126 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

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

581
        } elseif (checkdate($month, $day, /** @scrutinizer ignore-type */ $year) && $year != '9999') {
Loading history...
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

581
        } elseif (checkdate(/** @scrutinizer ignore-type */ $month, $day, $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

581
        } elseif (checkdate($month, /** @scrutinizer ignore-type */ $day, $year) && $year != '9999') {
Loading history...
582 35
            return format_date($entered_house, LONGDATEFORMAT);
583
        } else {
584
            return "n/a";
585
        }
586
    }
587
588 1
    public function left_house($house = null) {
589 1
        if ( isset($house) ) {
590 1
            if ( array_key_exists($house, $this->left_house) ) {
591 1
                return $this->left_house[$house];
592
            } else {
593 1
                return null;
594
            }
595
        }
596
        return $this->left_house;
597
    }
598
599 36
    public function left_house_text($left_house) {
600 36
        if (!$left_house) {
601
            return '';
602
        }
603 36
        list($year, $month, $day) = explode('-', $left_house);
604 36
        if (checkdate($month, $day, $year) && $year != '9999') {
0 ignored issues
show
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

604
        if (checkdate($month, /** @scrutinizer ignore-type */ $day, $year) && $year != '9999') {
Loading history...
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

604
        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

604
        if (checkdate($month, $day, /** @scrutinizer ignore-type */ $year) && $year != '9999') {
Loading history...
605 2
            return format_date($left_house, LONGDATEFORMAT);
606 34
        } elseif ($month==0 && $day==0) {
607
            # Left house date is stored as 1942-00-00 to mean "at some point in 1941"
608
            return $year - 1;
609
        } else {
610 34
            return "n/a";
611
        }
612
    }
613
614
    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...
615 36
    public function entered_reason_text($entered_reason) {
616 36
        if (isset($this->reasons()[$entered_reason])) {
617 34
            return $this->reasons()[$entered_reason];
618
        } else {
619 2
            return $entered_reason;
620
        }
621
    }
622
623
    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...
624 36
    public function left_reason_text($left_reason, $left_house, $house) {
625 36
        if (isset($this->reasons()[$left_reason])) {
626 36
            $left_reason = $this->reasons()[$left_reason];
627 36
            if (is_array($left_reason)) {
628
                $q = $this->db->query("SELECT MAX(left_house) AS max FROM member WHERE house=$house")->first();
629
                $max = $q['max'];
630
                if ($max == $left_house) {
631
                    return $left_reason[0];
632
                } else {
633
                    return $left_reason[1];
634
                }
635
            } else {
636 36
                return $left_reason;
637
            }
638
        } else {
639
            return $left_reason;
640
        }
641
    }
642
643
    public function extra_info() { return $this->extra_info; }
644
645 1
    public function current_member($house = 0) {
646 1
        $current = array();
647 1
        foreach (array_keys($this->houses_pretty()) as $h) {
648 1
            $lh = $this->left_house($h);
649 1
            $current[$h] = ($lh && $lh['date'] == '9999-12-31');
650
        }
651 1
        if ($house) {
652 1
            return $current[$house];
653
        }
654
        return $current;
655
    }
656
657
    public function the_users_mp() { return $this->the_users_mp; }
658
659 6
    public function url($absolute = false) {
660 6
        $house = $this->house_disp;
661
662
        switch ($house) {
663 6
            case HOUSE_TYPE_LORDS:
664 1
                $URL = new \MySociety\TheyWorkForYou\Url('peer');
665 1
                break;
666
667 5
            case HOUSE_TYPE_NI:
668 1
                $URL = new \MySociety\TheyWorkForYou\Url('mla');
669 1
                break;
670
671 4
            case HOUSE_TYPE_SCOTLAND:
672 1
                $URL = new \MySociety\TheyWorkForYou\Url('msp');
673 1
                break;
674
675 3
            case HOUSE_TYPE_WALES:
676
                $URL = new \MySociety\TheyWorkForYou\Url('ms');
677
                break;
678
679 3
            case HOUSE_TYPE_LONDON_ASSEMBLY:
680
                $URL = new \MySociety\TheyWorkForYou\Url('london-assembly-member');
681
                break;
682
683 3
            case HOUSE_TYPE_ROYAL:
684 1
                $URL = new \MySociety\TheyWorkForYou\Url('royal');
685 1
                break;
686
687
            default:
688 2
                $URL = new \MySociety\TheyWorkForYou\Url('mp');
689 2
                break;
690
        }
691
692 6
        $member_url = make_member_url($this->full_name(true), $this->constituency(), $house, $this->person_id());
693 6
        if ($absolute) {
694
            $protocol = 'https://';
695
            if (DEVSITE) {
696
                $protocol = 'http://';
697
            }
698
            return $protocol . DOMAIN . $URL->generate('none') . $member_url;
699
        } else {
700 6
            return $URL->generate('none') . $member_url;
701
        }
702
    }
703
704
    private function _previous_future_mps_query($direction) {
705
        $entered_house = $this->entered_house(HOUSE_TYPE_COMMONS);
706
        if (is_null($entered_house)) {
707
            return '';
708
        }
709
        if ($direction == '>') {
710
            $order = '';
711
        } else {
712
            $order = 'DESC';
713
        }
714
        $q = $this->db->query('SELECT *
715
            FROM member, person_names pn
716
            WHERE member.person_id = pn.person_id AND pn.type = "name"
717
                AND pn.start_date <= member.left_house AND member.left_house <= pn.end_date
718
                AND house = :house AND constituency = :cons
719
                AND member.person_id != :pid AND entered_house ' . $direction . ' :date ORDER BY entered_house ' . $order,
720
            array(
721
                ':house' => HOUSE_TYPE_COMMONS,
722
                ':cons' => $this->constituency(),
723
                ':pid' => $this->person_id(),
724
                ':date' => $entered_house['date'],
725
            ));
726
        $mships = array(); $last_pid = null;
727
        foreach ($q as $row) {
728
            $pid = $row['person_id'];
729
            $name = $row['given_name'] . ' ' . $row['family_name'];
730
            if ($last_pid != $pid) {
731
                $mships[] = array(
732
                    'href' => WEBPATH . 'mp/?pid='.$pid,
733
                    'text' => $name
734
                );
735
                $last_pid = $pid;
736
            }
737
        }
738
        return $mships;
739
    }
740
741
    public function previous_mps() {
742
        return $this->_previous_future_mps_query('<');
743
    }
744
745
    public function future_mps() {
746
        return $this->_previous_future_mps_query('>');
747
    }
748
749
    public function current_member_anywhere() {
750
        $is_current = false;
751
        $current_memberships = $this->current_member();
752
        foreach ($current_memberships as $current_memberships) {
753
            if ($current_memberships === true) {
754
                $is_current = true;
755
            }
756
        }
757
758
        return $is_current;
759
    }
760
}
761