Issues (404)

classes/Homepage.php (2 issues)

1
<?php
2
3
namespace MySociety\TheyWorkForYou;
4
5
class Homepage {
6
    private $db;
7
8
    protected $mp_house = 1;
9
    protected $cons_type = 'WMC';
10
    protected $mp_url = 'yourmp';
11
    protected $page = 'overview';
12
    protected $houses = [1, 101];
13
14
    protected $recent_types = [
15
        'DEBATELIST' => ['recent_debates', 'debatesfront', 'Commons debates'],
16
        'LORDSDEBATELIST' => ['recent_debates', 'lordsdebatesfront', 'Lords debates'],
17
        'WHALLLIST' => ['recent_debates', 'whallfront', 'Westminster Hall debates'],
18
        'WMSLIST' => ['recent_wms', 'wmsfront', 'Written ministerial statements'],
19
        'WRANSLIST' => ['recent_wrans', 'wransfront', 'Written answers'],
20
        'StandingCommittee' => ['recent_pbc_debates', 'pbcfront', 'Public Bill committees'],
21
    ];
22
23
    public function __construct() {
24
        $this->db = new \ParlDB();
25
    }
26
27
    public function display() {
28
        global $this_page;
29
        $this_page = $this->page;
30
31
        $data = [];
32
33
        $common = new Common();
0 ignored issues
show
The assignment to $common is dead and can be removed.
Loading history...
34
        $dissolution = Dissolution::dates();
35
36
        $data['debates'] = $this->getDebatesData();
37
38
        $user = new User();
39
        $data['mp_data'] = $user->getRep($this->cons_type, $this->mp_house);
40
        $data["commons_dissolved"] = isset($dissolution[1]);
41
42
        $data['regional'] = $this->getRegionalList();
0 ignored issues
show
Are you sure the assignment to $data['regional'] is correct as $this->getRegionalList() targeting MySociety\TheyWorkForYou...page::getRegionalList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
        $data['popular_searches'] = []; # $common->getPopularSearches();
44
        $data['urls'] = $this->getURLs();
45
        $data['calendar'] = $this->getCalendarData();
46
        $data['featured'] = $this->getEditorialContent();
47
        $data['topics'] = $this->getFrontPageTopics();
48
        $data['divisions'] = $this->getRecentDivisions();
49
        $data['search_box'] = $this->getSearchBox($data);
50
        $data['current_assembly'] = $this->getCurrentAssembly();
51
52
        return $data;
53
    }
54
55
    /**
56
     * Gets the current assembly.
57
     * Subclass this function to define the parliament for social cards.
58
     */
59
    protected function getCurrentAssembly(): string {
60
        return "uk";
61
    }
62
63
    protected function getSearchBox(array $data): Search\SearchBox {
64
        $search_box = new Search\SearchBox();
65
        $search_box->homepage_panel_class = "panel--homepage--overall";
66
        $search_box->homepage_subhead = "";
67
        $search_box->homepage_desc = "Understand who represents you, across the UK’s Parliaments.";
68
        $search_box->search_section = "";
69
        $search_box->quick_links = [];
70
        if (count($data["mp_data"])) {
71
            $search_box->add_quick_link('Find out more about your MP (' . $data["mp_data"]['name'] . ')', $data["mp_data"]['mp_url'], 'torso');
72
        }
73
        $search_box->add_quick_link('Create and manage email alerts', '/alert/', 'megaphone');
74
        $search_box->add_quick_link(gettext('Subscribe to our newsletter'), '/about/#about-mysociety', 'mail');
75
        $search_box->add_quick_link('Donate to support our work', '/support-us/', 'heart');
76
        $search_box->add_quick_link('Learn more about TheyWorkForYou', '/about/', 'magnifying-glass');
77
        return $search_box;
78
    }
79
80
    protected function getRegionalList() {
81
        return null;
82
    }
83
84
    protected function getEditorialContent() {
85
        $debatelist = new \DEBATELIST();
86
        $featured = new Model\Featured();
87
        $gid = $featured->get_gid();
88
        $gidCheck = new Gid($gid);
89
        $gid = $gidCheck->checkForRedirect();
90
        if ($gid) {
91
            $title = $featured->get_title();
92
            $context = $featured->get_context();
93
            $related = $featured->get_related();
94
            $item = $this->getFeaturedDebate($gid, $title, $context, $related);
95
        } else {
96
            $item = $debatelist->display('recent_debates', ['days' => 7, 'num' => 1], 'none');
97
            if (isset($item['data']) && count($item['data'])) {
98
                $item = $item['data'][0];
99
                $more_url = new Url('debates');
100
                $item['more_url'] = $more_url->generate();
101
                $item['desc'] = 'Commons Debates';
102
                $item['related'] = [];
103
                $item['featured'] = false;
104
            } else {
105
                $item = [];
106
            }
107
        }
108
109
        return $item;
110
    }
111
112
    public function getFeaturedDebate($gid, $title, $context, $related) {
113
        if (strpos($gid, 'lords') !== false) {
114
            $debatelist = new \LORDSDEBATELIST();
115
        } elseif (strpos($gid, 'westminhall') !== false) {
116
            $debatelist = new \WHALLLIST();
117
        } else {
118
            $debatelist = new \DEBATELIST();
119
        }
120
121
        $item = $debatelist->display('featured_gid', ['gid' => $gid], 'none');
122
        $item = $item['data'];
123
        $item['headline'] = $title;
124
        $item['context'] = $context;
125
        $item['featured'] = true;
126
127
        $related_debates = [];
128
        foreach ($related as $related_gid) {
129
            if ($related_gid) {
130
                $related_item = $debatelist->display('featured_gid', ['gid' => $related_gid], 'none');
131
                $related_debates[] = $related_item['data'];
132
            }
133
        }
134
        $item['related'] = $related_debates;
135
        return $item;
136
    }
137
138
    protected function getFrontPageTopics() {
139
        $topics = new Topics();
140
        return $topics->getFrontPageTopics();
141
    }
142
143
    private function getRecentDivisions() {
144
        $divisions = new Divisions();
145
        return $divisions->getRecentDebatesWithDivisions(5, $this->houses);
146
    }
147
148
    protected function getURLs() {
149
        $urls = [];
150
151
        return $urls;
152
    }
153
154
    protected function getDebatesData() {
155
        $debates = []; // holds the most recent data there is data for, indexed by type
156
157
        $recent_content = [];
158
159
        foreach ($this->recent_types as $class => $recent) {
160
            $class = "\\$class";
161
            $instance = new $class();
162
            $more_url = new Url($recent[1]);
163
            if ($recent[0] == 'recent_pbc_debates') {
164
                $content = [ 'data' => $instance->display($recent[0], ['num' => 5], 'none') ];
165
            } else {
166
                $content = $instance->display($recent[0], ['days' => 7, 'num' => 1], 'none');
167
                if (isset($content['data']) && count($content['data'])) {
168
                    $content = $content['data'][0];
169
                } else {
170
                    $content = [];
171
                }
172
            }
173
            if ($content) {
174
                $content['more_url'] = $more_url->generate();
175
                $content['desc'] = $recent[2];
176
                $recent_content[] = $content;
177
            }
178
        }
179
180
        $debates['recent'] = $recent_content;
181
182
        return $debates;
183
    }
184
185
    private function getCalendarData() {
186
        return Utility\Calendar::fetchFuture();
187
    }
188
189
}
190