Header::get_page_title()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 13
nop 0
dl 0
loc 41
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
namespace MySociety\TheyWorkForYou\Renderer;
4
5
/**
6
 * Template Header Renderer
7
 *
8
 * Prepares variables for inclusion in a template header.
9
 */
10
11
class Header {
12
    public $nav_highlights;
13
14
    public $data;
15
16
    private $keywords_title;
17
18
    public function __construct() {
19
        $this->data = [];
20
21
        $this->get_page_url();
22
        $this->get_page_title();
23
        $this->get_page_keywords();
24
        $this->get_header_links();
25
        $this->get_next_prev_links();
26
        $this->get_rss_link();
27
        $this->get_menu_highlights();
28
        $this->get_top_and_bottom_links();
29
        $this->add_robots_metadata();
30
    }
31
32
    private function add_robots_metadata() {
33
        global $DATA, $this_page;
34
35
        # Robots header
36
        if (DEVSITE) {
37
            $this->data['robots'] = 'noindex,nofollow';
38
        } elseif ($robots = $DATA->page_metadata($this_page, 'robots')) {
39
            $this->data['robots'] = $robots;
40
        }
41
    }
42
43
    /**
44
     * Work out what the page url
45
     */
46
    private function get_page_url() {
47
        $protocol = 'https://';
48
        if (DEVSITE) {
49
            $protocol = 'http://';
50
        }
51
        $url = $protocol . DOMAIN;
52
        if (array_key_exists('REQUEST_URI', $_SERVER)) {
53
            $url = $url . $_SERVER['REQUEST_URI'];
54
        }
55
        $this->data['page_url'] = $url;
56
    }
57
58
    /**
59
     * Work out what the page title and the keywords for it should be
60
     */
61
    private function get_page_title() {
62
        global $DATA, $this_page;
63
64
        $this->data['page_title'] = '';
65
        $sitetitle = $DATA->page_metadata($this_page, "sitetitle");
66
        $og_title = '';
67
        $this->keywords_title = '';
68
69
        if ($this_page == 'overview') {
70
            $this->data['page_title'] = $sitetitle . ': ' . $DATA->page_metadata($this_page, "title");
71
72
        } else {
73
74
            if ($page_title = $DATA->page_metadata($this_page, "title")) {
75
                $this->data['page_title'] = $page_title;
76
            }
77
            // We'll put this in the meta keywords tag.
78
            $this->keywords_title = $this->data['page_title'];
79
80
            $parent_page = $DATA->page_metadata($this_page, 'parent');
81
            if ($parent_title = $DATA->page_metadata($parent_page, 'title')) {
82
                if ($this->data['page_title']) {
83
                    $this->data['page_title'] .= ': ';
84
                }
85
                $this->data['page_title'] .= $parent_title;
86
            }
87
88
            if ($this->data['page_title'] == '') {
89
                $this->data['page_title'] = $sitetitle;
90
            } else {
91
                $og_title = $this->data['page_title'];
92
                $this->data['page_title'] .= ' - ' . $sitetitle;
93
            }
94
        }
95
96
        # for overriding the OpenGraph image
97
        $this->data['og_image'] = '';
98
99
        // Pages can specify a custom OpenGraph title if they want, otherwise
100
        // we use the page title without the trailing " - $sitetitle".
101
        $this->data['og_title'] = $DATA->page_metadata($this_page, "og_title") ?: $og_title;
102
103
    }
104
105
    private function get_page_keywords() {
106
        global $DATA, $this_page;
107
        ////////////////////////////////////////////////////////////
108
        // Meta keywords
109
        if (!$this->data['meta_keywords'] = $DATA->page_metadata($this_page, "meta_keywords")) {
110
            $this->data['meta_keywords'] = $this->keywords_title;
111
            if ($this->data['meta_keywords']) {
112
                $this->data['meta_keywords'] .= ', ';
113
            }
114
            $this->data['meta_keywords'] .= 'Hansard, Official Report, Parliament, government, House of Commons, House of Lords, MP, Peer, Member of Parliament, MPs, Peers, Lords, Commons, Scottish Parliament, Northern Ireland Assembly, MSP, MLA, MSPs, MLAs, London Assembly Members, MS, MSs, Welsh Parliament, Senedd Cymru, Senedd, Member of the Senedd';
115
        }
116
117
        $this->data['meta_description'] = gettext("Making it easy to keep an eye on the UK’s parliaments. Discover who represents you, how they’ve voted and what they’ve said in debates.");
118
        if ($DATA->page_metadata($this_page, "meta_description")) {
119
            $this->data['meta_description'] = $DATA->page_metadata($this_page, "meta_description");
120
        }
121
    }
122
123
    private function get_header_links() {
124
        global $this_page;
125
126
        $this->data['header_links'] = [];
127
        if ($this_page != 'overview') {
128
129
            $URL = new \MySociety\TheyWorkForYou\Url('overview');
130
131
            $this->data['header_links'][] = [
132
                'rel'   => 'start',
133
                'title' => 'Home',
134
                'href'  => $URL->generate(),
135
            ];
136
137
        }
138
    }
139
140
    private function generate_next_prev_link($nextprev, $linktype) {
141
        $link = null;
142
        if (isset($nextprev[$linktype]) && isset($nextprev[$linktype]['url'])) {
143
144
            if (isset($nextprev[$linktype]['body'])) {
145
                $linktitle = _htmlentities(trim_characters($nextprev[$linktype]['body'], 0, 40));
146
                if (isset($nextprev[$linktype]['speaker']) &&
147
                    count($nextprev[$linktype]['speaker']) > 0) {
148
                    $linktitle = $nextprev[$linktype]['speaker']['name'] . ': ' . $linktitle;
149
                }
150
151
            } elseif (isset($nextprev[$linktype]['hdate'])) {
152
                $linktitle = format_date($nextprev[$linktype]['hdate'], SHORTDATEFORMAT);
153
            }
154
155
            $link = [
156
                'rel'   => $linktype,
157
                'title' => $linktitle,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $linktitle does not seem to be defined for all execution paths leading up to this point.
Loading history...
158
                'href'  => $nextprev[$linktype]['url'],
159
            ];
160
        }
161
162
        return $link;
163
    }
164
165
    private function get_next_prev_links() {
166
        global $DATA, $this_page;
167
168
        $nextprev = $DATA->page_metadata($this_page, "nextprev");
169
170
        if ($nextprev) {
171
            // Four different kinds of back/forth links we might build.
172
            $links =  ["first", "prev", "up", "next", "last"];
173
174
            foreach ($links as $type) {
175
                if ($link = $this->generate_next_prev_link($nextprev, $type)) {
176
177
                    $this->data['header_links'][] = $link;
178
                }
179
            }
180
        }
181
    }
182
183
    private function get_rss_link() {
184
        global $DATA, $this_page;
185
186
        if ($DATA->page_metadata($this_page, 'rss')) {
187
            // If this page has an RSS feed set.
188
            $this->data['page_rss_url'] = 'https://' . DOMAIN . WEBPATH . $DATA->page_metadata($this_page, 'rss');
189
        }
190
    }
191
192
    // We work out which of the items in the top and bottom menus
193
    // are highlighted
194
    private function get_menu_highlights() {
195
        global $DATA, $this_page;
196
197
        $parent = $DATA->page_metadata($this_page, 'parent');
198
199
        if (!$parent) {
200
201
            $top_highlight = $this_page;
202
            $bottom_highlight = '';
203
204
            $selected_top_link = $DATA->page_metadata('hansard', 'menu');
205
            $url = new \MySociety\TheyWorkForYou\Url('hansard');
206
            $selected_top_link['link'] = $url->generate();
207
208
        } else {
209
210
            $parents = [$parent];
211
            $p = $parent;
212
            while ($p) {
213
                $p = $DATA->page_metadata($p, 'parent');
214
                if ($p) {
215
                    $parents[] = $p;
216
                }
217
            }
218
219
            $top_highlight = array_pop($parents);
220
            if (!$parents) {
221
                // No grandparent - this page's parent is in the top menu.
222
                // We're on one of the pages linked to by the bottom menu.
223
                // So highlight it and its parent.
224
                $bottom_highlight = $this_page;
225
            } else {
226
                // This page is not in either menu. So highlight its parent
227
                // (in the bottom menu) and its grandparent (in the top).
228
                $bottom_highlight = array_pop($parents);
229
            }
230
231
            $selected_top_link = $DATA->page_metadata($top_highlight, 'menu');
232
            if (!$selected_top_link) {
233
                # Just in case something's gone wrong
234
                $selected_top_link = $DATA->page_metadata('hansard', 'menu');
235
            }
236
            $url = new \MySociety\TheyWorkForYou\Url($top_highlight);
237
            $selected_top_link['link'] = $url->generate();
238
239
        }
240
241
        if ($top_highlight == 'hansard') {
242
            $section = 'uk';
243
            $selected_top_link['text'] = 'UK';
244
        } elseif ($top_highlight == 'ni_home') {
245
            $section = 'ni';
246
            $selected_top_link['text'] = 'Northern Ireland';
247
        } elseif ($top_highlight == 'sp_home') {
248
            $section = 'scotland';
249
            $selected_top_link['text'] = 'Scotland';
250
        } elseif ($top_highlight == 'wales_home') {
251
            $section = 'wales';
252
            $selected_top_link['text'] = gettext('Wales');
253
        } elseif ($top_highlight == 'london_home') {
254
            $section = 'london';
255
            $selected_top_link['text'] = 'London Assembly';
256
        } else {
257
            $section = '';
258
        }
259
260
        // if we're searching within a parliament, put this in the top bar
261
        if ($this_page == "search") {
262
            if ($section = get_http_var('section')) {
263
                if ($section == 'scotland') {
264
                    $selected_top_link['text'] = 'Scotland';
265
                } elseif ($section == 'ni') {
266
                    $selected_top_link['text'] = 'Northern Ireland';
267
                } elseif ($section == 'wales') {
268
                    $selected_top_link['text'] = gettext('Wales');
269
                } elseif ($section == 'london') {
270
                    $selected_top_link['text'] = 'London Assembly';
271
                } else {
272
                    $selected_top_link['text'] = 'UK';
273
                }
274
            }
275
        }
276
277
        // for the alerts page, put the most recent membership's house
278
        // in the top bar
279
        if ($this_page == "alert") {
280
            if ($pid = get_http_var('pid')) {
281
                $person = new \MySociety\TheyWorkForYou\Member(['person_id' => $pid]);
282
                $membership = $person->getMostRecentMembership();
283
                $parliament = $membership['house'];
284
                if ($parliament == 'ni') {
285
                    $selected_top_link['text'] = 'Northern Ireland';
286
                } elseif ($parliament == HOUSE_TYPE_SCOTLAND) {
287
                    $selected_top_link['text'] = 'Scotland';
288
                } elseif ($parliament == HOUSE_TYPE_WALES) {
289
                    $selected_top_link['text'] = gettext('Wales');
290
                } elseif ($parliament == HOUSE_TYPE_LONDON_ASSEMBLY) {
291
                    $selected_top_link['text'] = 'London Assembly';
292
                }
293
            }
294
        }
295
296
        $this->nav_highlights = [
297
            'top' => $top_highlight,
298
            'bottom' => $bottom_highlight,
299
            'top_selected' => $selected_top_link,
300
            'section' => $section,
301
        ];
302
    }
303
304
    private function get_top_and_bottom_links() {
305
        global $DATA;
306
307
        // Page names mapping to those in metadata.php.
308
        // Links in the top menu, and the sublinks we see if
309
        // we're within that section.
310
        $nav_items =  [
311
            ['home'],
312
            ['hansard', 'mps', 'peers', 'alldebatesfront', 'interests_home', 'wranswmsfront', 'pbc_front', 'divisions_recent_commons', 'divisions_recent_lords',  'calendar_summary'],
313
            ['sp_home', 'spoverview', 'msps', 'interest_category_sp', 'spdebatesfront', 'divisions_recent_sp'], #'spwransfront'
314
            ['ni_home', 'nioverview', 'mlas', 'interest_category_ni'],
315
            ['wales_home', 'seneddoverview', 'mss', 'interest_category_wp', 'wales_debates', 'divisions_recent_wales'],
316
            ['london_home', 'lmqsfront', 'london-assembly-members'],
317
        ];
318
319
        $this->data['assembly_nav_links'] = [];
320
        $this->data['section_nav_links'] = [];
321
322
        //get the top and bottom links
323
        foreach ($nav_items as $bottompages) {
324
            $toppage = array_shift($bottompages);
325
326
            // Generate the links for the top menu.
327
328
            // What gets displayed for this page.
329
            $menudata = $DATA->page_metadata($toppage, 'menu');
330
            $title = '';
331
            if ($menudata) {
332
                $text = $menudata['text'];
333
                $title = $menudata['title'];
334
            }
335
            if (!$title) {
336
                continue;
337
            }
338
339
            //get link and description for the menu ans add it to the array
340
            $class = $toppage == $this->nav_highlights['top'] ? 'on' : '';
341
342
            // if current language is cy and the page does not start with /senedd/
343
            // we need to escape back to the english site
344
            $URL = new \MySociety\TheyWorkForYou\Url($toppage);
345
            $url = $URL->generate();
346
            if (LANGUAGE == 'cy' && strpos($url, '/senedd/') === false) {
347
                $url = "//" . DOMAIN . $url;
348
            }
349
350
            $top_link = [
351
                'href'    => $url,
352
                'title'   => $title,
353
                'classes' => $class,
354
                'text'    => $text,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text does not seem to be defined for all execution paths leading up to this point.
Loading history...
355
            ];
356
            if ($toppage != "london_home") {
357
                array_push($this->data['assembly_nav_links'], $top_link);
358
            }
359
360
            if ($toppage == $this->nav_highlights['top']) {
361
362
                // This top menu link is highlighted, so generate its bottom menu.
363
                foreach ($bottompages as $bottompage) {
364
                    $menudata = $DATA->page_metadata($bottompage, 'menu');
365
                    $text = $menudata['text'];
366
                    $title = $menudata['title'];
367
                    // Where we're linking to.
368
                    $URL = new \MySociety\TheyWorkForYou\Url($bottompage);
369
                    $class = $bottompage == $this->nav_highlights['bottom'] ? 'on' : '';
370
                    $this->data['section_nav_links'][] = [
371
                        'href'    => $URL->generate(),
372
                        'title'   => $title,
373
                        'classes' => $class,
374
                        'text'    => $text,
375
                    ];
376
                }
377
            }
378
        }
379
380
        $this->data['assembly_nav_current'] = $this->nav_highlights['top_selected']['text'];
381
    }
382
383
}
384