Passed
Pull Request — master (#1834)
by
unknown
05:14
created

UK::getSearchBox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.8333
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Homepage;
4
5
class UK extends Base {
6
    protected $mp_house = 1;
7
    protected $cons_type = 'WMC';
8
    protected $page = 'overview';
9
    protected $houses = [1, 101];
10
    private $mp_url = 'yourmp';
0 ignored issues
show
introduced by
The private property $mp_url is not used, and could be removed.
Loading history...
11
12
    protected $recent_types = [
13
        'DEBATELIST' => ['recent_debates', 'debatesfront', 'Commons debates'],
14
        'LORDSDEBATELIST' => ['recent_debates', 'lordsdebatesfront', 'Lords debates'],
15
        'WHALLLIST' => ['recent_debates', 'whallfront', 'Westminster Hall debates'],
16
        'WMSLIST' => ['recent_wms', 'wmsfront', 'Written ministerial statements'],
17
        'WRANSLIST' => ['recent_wrans', 'wransfront', 'Written answers'],
18
        'StandingCommittee' => ['recent_pbc_debates', 'pbcfront', 'Public Bill committees'],
19
    ];
20
21
    public function display() {
22
        $data = parent::display();
23
        $data['calendar'] = $this->getCalendarData();
24
        $data['topics'] = $this->getFrontPageTopics();
25
        return $data;
26
    }
27
28
    protected function getSearchBox(array $data): \MySociety\TheyWorkForYou\Search\SearchBox {
29
        $search_box = new \MySociety\TheyWorkForYou\Search\SearchBox();
30
        $search_box->homepage_panel_class = "panel--homepage--overall";
31
        $search_box->homepage_subhead = "";
32
        $search_box->homepage_desc = "Understand who represents you, across the UK’s Parliaments.";
33
        $search_box->search_section = "";
34
        $search_box->quick_links = [];
35
        if (count($data["mp_data"])) {
36
            $search_box->add_quick_link('Find out more about your MP (' . $data["mp_data"]['name'] . ')', $data["mp_data"]['mp_url'], 'torso');
37
        }
38
        $search_box->add_quick_link('Create and manage email alerts', '/alert/', 'megaphone');
39
        $search_box->add_quick_link(gettext('Subscribe to our newsletter'), '/about/#about-mysociety', 'mail');
40
        $search_box->add_quick_link('Donate to support our work', '/support-us/', 'heart');
41
        $search_box->add_quick_link('Learn more about TheyWorkForYou', '/about/', 'magnifying-glass');
42
        return $search_box;
43
    }
44
45
    protected function getEditorialContent(&$data) {
46
        $debatelist = new \DEBATELIST();
47
        $featured = new \MySociety\TheyWorkForYou\Model\Featured();
48
        $gid = $featured->get_gid();
49
        $gidCheck = new \MySociety\TheyWorkForYou\Gid($gid);
50
        $gid = $gidCheck->checkForRedirect();
51
        if ($gid) {
52
            $title = $featured->get_title();
53
            $context = $featured->get_context();
54
            $related = $featured->get_related();
55
            $item = $this->getFeaturedDebate($gid, $title, $context, $related);
56
        } else {
57
            $item = $debatelist->display('recent_debates', ['days' => 7, 'num' => 1], 'none');
58
            if (count($item['data'])) {
59
                $item = $item['data'][0];
60
                $more_url = new \MySociety\TheyWorkForYou\Url('debates');
61
                $item['more_url'] = $more_url->generate();
62
                $item['desc'] = 'Commons Debates';
63
                $item['related'] = [];
64
                $item['featured'] = false;
65
            } else {
66
                $item = [];
67
            }
68
        }
69
70
        return $item;
71
    }
72
73
    public function getFeaturedDebate($gid, $title, $context, $related) {
74
        if (strpos($gid, 'lords') !== false) {
75
            $debatelist = new \LORDSDEBATELIST();
76
        } elseif (strpos($gid, 'westminhall') !== false) {
77
            $debatelist = new \WHALLLIST();
78
        } else {
79
            $debatelist = new \DEBATELIST();
80
        }
81
82
        $item = $debatelist->display('featured_gid', ['gid' => $gid], 'none');
83
        $item = $item['data'];
84
        $item['headline'] = $title;
85
        $item['context'] = $context;
86
        $item['featured'] = true;
87
88
        $related_debates = [];
89
        foreach ($related as $related_gid) {
90
            if ($related_gid) {
91
                $related_item = $debatelist->display('featured_gid', ['gid' => $related_gid], 'none');
92
                $related_debates[] = $related_item['data'];
93
            }
94
        }
95
        $item['related'] = $related_debates;
96
        return $item;
97
    }
98
99
    protected function getFrontPageTopics() {
100
        $topics = new \MySociety\TheyWorkForYou\Topics();
101
        return $topics->getFrontPageTopics();
102
    }
103
104
    private function getCalendarData() {
105
        return \MySociety\TheyWorkForYou\Utility\Calendar::fetchFuture();
106
    }
107
108
}
109