SectionView::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 12
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
# For displaying any debate calendar, day, debate, speech page or related.
4
5
namespace MySociety\TheyWorkForYou\SectionView;
6
7
class SectionView {
8
    public $major;
9
    protected $class;
10
    protected $list;
11
    private $major_data;
12
    private $page_base;
13
    protected $index_template = '';
14
    protected $location;
15
    protected $assembly;
16
17
    public function __construct() {
18
        global $hansardmajors;
19
        $this->major_data = $hansardmajors[$this->major];
20
        $this->page_base = str_replace('year', '', $this->major_data['page_year']);
21
        if (!$this->list) {
22
            $this->class = "\\" . $this->class;
23
            $this->list = new $this->class();
24
        }
25
26
        [$country, $location, $assembly] = $this->getCountryDetails();
27
        $this->location = $location;
28
        $this->assembly = $assembly;
29
    }
30
31
    public function display() {
32
        global $THEUSER;
33
34
        if ($year = get_http_var('y')) {
35
            $data = $this->display_year($year);
36
            $data['year'] = $year;
37
            $data['template'] = 'section/year';
38
            $data = $this->addCommonData($data);
39
        } elseif (($date = get_http_var('d')) && ($column = get_http_var('c'))) {
40
            $data = $this->display_column($date, $column);
41
            $data = $this->addCommonData($data);
42
        } elseif ($date = get_http_var('d')) {
43
            $data = $this->display_day($date);
44
            if (!isset($data['template'])) {
45
                $data['template'] = 'section/day';
46
            }
47
            $data = $this->addCommonData($data);
48
        } elseif (get_http_var('id')) {
49
            $data = $this->display_section_or_speech();
50
        } else {
51
            $data = $this->display_front();
52
            if (!isset($data['template'])) {
53
                $data['template'] = 'section/recent';
54
            }
55
            $data['search_sections'] = $this->getSearchSections();
56
            $data = $this->addCommonData($data);
57
        }
58
59
        $data['location'] = $this->location;
60
        $data['current_assembly'] = $this->assembly;
61
62
        return $data;
63
    }
64
65
    protected function addCommonData($data) {
66
        global $DATA, $this_page;
67
68
        $common = new \MySociety\TheyWorkForYou\Common();
0 ignored issues
show
Unused Code introduced by
The assignment to $common is dead and can be removed.
Loading history...
69
        $data['urls'] = $this->getURLs($data);
70
        $data['popular_searches'] = []; #$common->getPopularSearches();
71
        $data['recess_major'] = $this->getRecessMajor();
72
73
        $nextprev = $DATA->page_metadata($this_page, 'nextprev');
74
        if (isset($nextprev['next']['url'])) {
75
            $data['next'] = $nextprev['next'];
76
        }
77
        if (isset($nextprev['prev']['url'])) {
78
            $data['prev'] = $nextprev['prev'];
79
        }
80
81
        $parent_page = $DATA->page_metadata($this_page, 'parent');
82
        $data['title'] = $DATA->page_metadata($this_page, 'title');
83
        if (!isset($data['title']) && $parent_page != '') {
84
            $data['title'] = $DATA->page_metadata($parent_page, 'title');
85
        }
86
        if ($parent_page) {
87
            $data['parent_title'] = $DATA->page_metadata($parent_page, 'title');
88
        }
89
90
        $data['section'] = $this->pageToSection();
91
92
        return $data;
93
    }
94
95
    protected function getURLs($data) {
96
        global $DATA, $this_page;
97
98
        $urls = [];
99
100
        $urls = array_merge($urls, $this->getViewUrls());
101
102
        if (isset($data['info']['page'])) {
103
            $day = new \MySociety\TheyWorkForYou\Url($data['info']['page']);
104
            $urls['day'] = $day;
105
        }
106
107
        return $urls;
108
    }
109
110
    protected function getViewUrls() {
111
        $urls = [];
112
        $day = new \MySociety\TheyWorkForYou\Url('debates');
113
        $urls['debatesday'] = $day;
114
        $urls['day'] = $day;
115
        $day = new \MySociety\TheyWorkForYou\Url('whall');
116
        $urls['whallday'] = $day;
117
        $day = new \MySociety\TheyWorkForYou\Url('lordsdebates');
118
        $urls['lordsday'] = $day;
119
        return $urls;
120
    }
121
122
    protected function getSearchSections() {
123
        return [];
124
    }
125
126
    protected function getRecessMajor() {
127
        $recess_major = 1; # For all of UK Parliament
128
        if ($this->major_data['location'] == 'NI') {
129
            $recess_major = 5;
130
        } elseif ($this->major_data['location'] == 'Scotland') {
131
            $recess_major = 4; # For all of Scotland
132
        } elseif ($this->major == 101) {
133
            $recess_major = 101; # Lords slightly different
134
        }
135
136
        return $recess_major;
137
    }
138
139
    protected function display_year($year) {
140
        global $this_page, $PAGE, $DATA;
141
        $this_page = $this->page_base . 'year';
142
        if (is_numeric($year)) {
143
            $DATA->set_page_metadata($this_page, 'title', $year);
144
        }
145
146
        $args =  [ 'year' => $year ];
147
        $data = $this->list->display('calendar', $args, 'none');
148
        return $data;
149
    }
150
151
    protected function display_column($date, $column) {
152
        global $this_page;
153
        $this_page = $this->page_base;
154
        $args = [ 'date' => $date, 'column' => $column ];
155
        $content = $this->list->display('column', $args, 'none');
156
157
        $data = [];
158
        $data['column'] = $column;
159
        $URL = new \MySociety\TheyWorkForYou\Url($this->list->listpage);
160
        $URL->insert(['d' => $date]);
161
        $data['debate_day_link'] = $URL->generate();
162
        $data['debate_day_human'] = format_date($date, LONGDATEFORMAT);
163
164
        $data['rows'] = $content;
165
        $data['info'] = ['major' => $this->major, 'date' => $date];
166
167
        $data['template'] = 'section/column';
168
        return $data;
169
    }
170
171
    protected function display_day($date) {
172
        global $this_page;
173
        $this_page = $this->page_base . 'day';
174
        $args =  [ 'date' => get_http_var('d') ];
175
        $data = $this->list->display('date', $args, 'none');
176
        [$year, $month, $day] = explode('-', $date);
177
        $args = [ 'year' => $year, 'month' => $month, 'day' => $day];
178
        $calendar = $this->list->display('calendar', $args, 'none');
179
        if (isset($calendar['years'])) {
180
            $data['calendar'] = $calendar['years'];
181
        }
182
        return $data;
183
    }
184
185
    protected function display_section_or_speech($args = []) {
186
        global $DATA, $this_page, $THEUSER;
187
188
        # += as we *don't* want to override any already supplied argument
189
        $args +=  [
190
            'gid' => get_http_var('id'),
191
            's' => get_http_var('s'), // Search terms to be highlighted.
192
            'member_id' => get_http_var('m'), // Member's speeches to be highlighted.
193
        ];
194
195
        if (preg_match('/speaker:(\d+)/', get_http_var('s'), $mmm)) {
196
            $args['person_id'] = $mmm[1];
197
        }
198
199
        try {
200
            $data = $this->list->display('gid', $args, 'none');
201
        } catch (\RedirectException $e) {
202
            $URL = new \MySociety\TheyWorkForYou\Url($this->major_data['page_all']);
203
            if ($this->major == 6) {
204
                # Magically (as in I can't remember quite why), pbc_clause will
205
                # contain the new URL without any change...
206
                $URL->remove(['id']);
207
            } else {
208
                $URL->insert(['id' => $e->getMessage()]);
209
            }
210
            # put the search term back in so highlighting works.
211
            # NB: as we don't see the # part of the URL we lose this :(
212
            if ($args['s'] !== '') {
213
                $URL->insert(['s' => $args['s']]);
214
            }
215
            redirect($URL->generate('none'), 301);
216
        }
217
218
        $data['individual_item'] = ($this->list->commentspage == $this_page);
219
220
        if ($data['individual_item']) {
221
            $COMMENTLIST = new \COMMENTLIST();
222
            $args['user_id'] = get_http_var('u');
223
            $args['epobject_id'] = $this->list->epobject_id();
224
            $data['comments']['object'] = $COMMENTLIST;
225
            $data['comments']['args'] = $args;
226
            $data['comments']['commentdata'] = [
227
                'epobject_id' => $this->list->epobject_id(),
228
                'gid' => get_http_var('id'), # wrans is LIST->gid?
229
                'return_page' => $this_page,
230
            ];
231
        } else {
232
            if ($THEUSER->isloggedin() && $THEUSER->is_able_to('addcomment')) {
233
                if (get_http_var("addcomment")) {
234
                    $data['show_comment_form'] = true;
235
                    $COMMENTLIST = new \COMMENTLIST();
236
                    $args['user_id'] = get_http_var('u');
237
                    $args['epobject_id'] = $this->list->epobject_id();
238
                    $data['comments']['object'] = $COMMENTLIST;
239
                    $data['comments']['args'] = $args;
240
                    $data['comments']['commentdata'] = [
241
                        'epobject_id' => $this->list->epobject_id(),
242
                        'gid' => get_http_var('id'), # wrans is LIST->gid?
243
                        'return_page' => $this_page,
244
                    ];
245
                } else {
246
                    $URL = new \MySociety\TheyWorkForYou\Url($this_page);
247
                    # do not add an id to PBCs
248
                    if ($this->major != 6) {
249
                        $URL->insert(['id' => $args['gid']]);
250
                    }
251
                    $URL->insert(['addcomment' => 1]);
252
                    $data['section_annotation_url'] = $URL->generate();
253
                }
254
            }
255
            // add section level comments
256
            $comments = new \COMMENTLIST();
257
            $comment_data = $comments->display('ep', ['epobject_id' => $this->list->epobject_id], 'none');
258
            $data['section_comments'] = $comment_data['comments'];
259
        }
260
261
        if (!isset($data['info'])) {
262
            header("HTTP/1.0 404 Not Found");
263
            $data['template'] = 'error';
264
            return $data;
265
        }
266
267
        # Okay, let's set up highlighting and glossarisation
268
269
        [$bodies, $speeches, $expansions] = $this->highlightSpeeches($data);
270
        $data['expansions'] = $expansions;
271
272
        [$data, $first_speech, $subsection_title] = $this->annotateSpeeches($data, $bodies, $speeches);
273
274
        $data = $this->setTitleAndAlertText($data, $subsection_title);
275
276
        if ($first_speech) {
277
            $data['debate_time_human'] = format_time($first_speech['htime'], TIMEFORMAT);
278
        }
279
        $data['debate_day_human'] = format_date($data['info']['date'], LONGDATEFORMAT);
280
281
        $URL = new \MySociety\TheyWorkForYou\Url($this->list->listpage);
282
        $URL->insert(['d' => $data['info']['date']]);
283
        $URL->remove(['id']);
284
        $data['debate_day_link'] = $URL->generate();
285
286
        $data['nextprev'] = $DATA->page_metadata($this_page, 'nextprev');
287
288
        if (isset($THEUSER) && $THEUSER->postcode_is_set()) {
289
            $user = new \MySociety\TheyWorkForYou\User();
290
            $house = \MySociety\TheyWorkForYou\Utility\House::majorToHouse($this->major)[0];
291
            $data['mp_data'] = $user->getRep($this->majorToConsType(), $house);
292
        }
293
294
        $DATA->set_page_metadata($this_page, 'meta_description', sprintf(
295
            "%s %s%s %s",
296
            $data['intro'],
297
            $this->location,
298
            isset($data['debate_time_human']) ? " at $data[debate_time_human]" : '',
299
            $data['debate_day_human']
300
        ));
301
302
        return $data;
303
    }
304
305
    private function setTitleAndAlertText($data, $subsection_title) {
306
        if ($subsection_title) {
307
            $data['heading'] = $subsection_title;
308
        } else {
309
            $data['heading'] = $data['section_title'];
310
        }
311
312
        if ($subsection_title) {
313
            $data['intro'] = "$data[section_title]";
314
        } else {
315
            $data['intro'] = "";
316
        }
317
318
        if (array_key_exists('text_heading', $data['info'])) {
319
            // avoid having Clause 1 etc as the alert text search string on PBC pages as it's
320
            // almost certainly not what the person wants
321
            if ($this->major == 6) {
322
                $data['email_alert_text'] = $data['section_title'];
323
            } else {
324
                $data['email_alert_text'] = $data['info']['text_heading'];
325
            }
326
        } else {
327
            // The user has requested only part of a debate, so find a suitable title
328
            if ($subsection_title) {
329
                $data['intro'] = "Part of $data[section_title]";
330
            } else {
331
                $data['intro'] = "Part of the debate";
332
            }
333
            foreach ($data['rows'] as $row) {
334
                if ($row['htype'] == 10 || $row['htype'] == 11) {
335
                    $data['email_alert_text'] = $row['body'];
336
                    $data['full_debate_url'] = $row['listurl'];
337
                    break;
338
                }
339
            }
340
        }
341
        // strip a couple of common characters that result in encode junk in the
342
        // search string
343
        $data['email_alert_text'] = preg_replace('/(?:[:()\[\]]|&#\d+;)/', '', $data['email_alert_text']);
344
345
        return $data;
346
    }
347
348
    private function highlightSpeeches($data) {
349
        $SEARCHENGINE = null;
350
        if (isset($data['info']['searchstring']) && $data['info']['searchstring'] != '') {
351
            $SEARCHENGINE = new \SEARCHENGINE($data['info']['searchstring']);
352
        }
353
354
        // Before we print the body text we need to insert glossary links
355
        // and highlight search string words.
356
357
        $speeches = 0;
358
        $bodies = [];
359
        foreach ($data['rows'] as $row) {
360
            $htype = $row['htype'];
361
            if ($htype == 12 || $htype == 13 || $htype == 14) {
362
                $speeches++;
363
            }
364
            $body = $row['body'];
365
            $body = preg_replace('#<phrase class="honfriend" id="uk.org.publicwhip/member/(\d+)" name="([^"]*?)">(.*?\s*\((.*?)\))</phrase>#', '<a href="/mp/?m=$1" title="Our page on $2 - \'$3\'">$4</a>', $body);
366
            $body = preg_replace('#<phrase class="honfriend" name="([^"]*?)" person_id="uk.org.publicwhip/person/(\d+)">(.*?\s*\((.*?)\))</phrase>#', '<a href="/mp/?p=$2" title="Our page on $1 - \'$3\'">$4</a>', $body);
367
            $body = preg_replace_callback('#<phrase class="offrep" id="(.*?)/(\d+)-(\d+)-(\d+)\.(.*?)">(.*?)</phrase>#', function ($matches) {
368
                return '<a href="/search/?pop=1&s=date:' . $matches[2] . $matches[3] . $matches[4] . '+column:' . $matches[5] . '+section:' . $matches[1] . '">' . str_replace("Official Report", "Hansard", $matches[6]) . '</a>';
369
            }, $body);
370
            #$body = preg_replace('#<phrase class="offrep" id="((.*?)/(\d+)-(\d+)-(\d+)\.(.*?))">(.*?)</phrase>#e', "\"<a href='/search/?pop=1&amp;s=date:$3$4$5+column:$6+section:$2&amp;match=$1'>\" . str_replace('Official Report', 'Hansard', '$7') . '</a>'", $body);
371
            $bodies[] = $body;
372
        }
373
374
        if ($SEARCHENGINE) {
375
            // We have some search terms to highlight.
376
            twfy_debug_timestamp('Before highlight');
377
            $bodies = $SEARCHENGINE->highlight($bodies);
378
            twfy_debug_timestamp('After highlight');
379
        }
380
        // Do all this unless the glossary is turned off in the URL
381
        if (get_http_var('ug') != 1) {
382
            // And glossary phrases
383
            twfy_debug_timestamp('Before glossarise');
384
385
            $args['sort'] = "regexp_replace";
0 ignored issues
show
Comprehensibility Best Practice introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.
Loading history...
386
            $GLOSSARY = new \GLOSSARY($args);
387
            [$bodies, $expansions] = $GLOSSARY->glossarise($bodies, 1, 0, 1);
388
389
            twfy_debug_timestamp('After glossarise');
390
        }
391
392
        return [$bodies, $speeches, $expansions];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $expansions does not seem to be defined for all execution paths leading up to this point.
Loading history...
393
    }
394
395
    private function annotateSpeeches($data, $bodies, $speeches) {
0 ignored issues
show
Unused Code introduced by
The parameter $speeches is not used and could be removed. ( Ignorable by Annotation )

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

395
    private function annotateSpeeches($data, $bodies, /** @scrutinizer ignore-unused */ $speeches) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
396
        global $THEUSER;
397
        $first_speech = null;
398
        $data['section_title'] = '';
399
        $subsection_title = '';
400
        $rows = count($data['rows']);
401
        for ($i = 0; $i < $rows; $i++) {
402
            $row = $data['rows'][$i];
403
            $htype = $row['htype'];
404
            // HPOS should be defined below if it's needed; otherwise default to 0
405
            $heading_hpos = 0;
406
            if ($htype == 10) {
407
                $data['section_title'] = $row['body'];
408
                $heading_hpos = $row['hpos'];
0 ignored issues
show
Unused Code introduced by
The assignment to $heading_hpos is dead and can be removed.
Loading history...
409
            } elseif ($htype == 11) {
410
                $subsection_title = $row['body'];
411
                $heading_hpos = $row['hpos'];
412
            } elseif ($htype == 12) {
413
                # Splitting out highlighting results back into individual bits
414
                $data['rows'][$i]['body'] = $bodies[$i];
415
            }
416
            if ($htype == 12 || $htype == 13 || $htype == 14) {
417
                if (!$first_speech) {
418
                    $first_speech = $data['rows'][$i];
419
                }
420
421
                # Voting links
422
                $data['rows'][$i]['voting_data'] = '';
423
                if (isset($row['votes'])) {
424
                    $data['rows'][$i]['voting_data'] = $this->generate_votes($row['votes'], $row['epobject_id'], $row['gid']);
0 ignored issues
show
Bug introduced by
The method generate_votes() does not exist on MySociety\TheyWorkForYou\SectionView\SectionView. It seems like you code against a sub-type of MySociety\TheyWorkForYou\SectionView\SectionView such as MySociety\TheyWorkForYou\SectionView\WransView. ( Ignorable by Annotation )

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

424
                    /** @scrutinizer ignore-call */ 
425
                    $data['rows'][$i]['voting_data'] = $this->generate_votes($row['votes'], $row['epobject_id'], $row['gid']);
Loading history...
425
                }
426
427
                # Annotation link
428
                if ($this->is_debate_section_page()) {
429
                    // Build the 'Add an annotation' link.
430
                    if ($THEUSER->isloggedin() && $THEUSER->is_able_to('addcomment')) {
431
                        $data['rows'][$i]['annotation_url'] = $row['commentsurl'];
432
                    }
433
434
                    $data['rows'][$i]['commentteaser'] = $this->generate_commentteaser($row);
435
                }
436
437
                if (isset($row['mentions'])) {
438
                    $data['rows'][$i]['mentions'] = $this->get_question_mentions_html($row['mentions']);
439
                }
440
441
                if (array_key_exists('name', $data['rows'][$i]['speaker'])) {
442
                    $data['rows'][$i]['socialteaser'] = sprintf(
443
                        '%s on %s, at TheyWorkForYou',
444
                        ucfirst($data['rows'][$i]['speaker']['name']),
445
                        $data['section_title']
446
                    );
447
                } else {
448
                    $data['rows'][$i]['socialteaser'] = sprintf(
449
                        '%s on TheyWorkForYou',
450
                        $data['section_title']
451
                    );
452
                }
453
454
                $data['rows'][$i]['socialurl'] = sprintf(
455
                    'https://www.theyworkforyou.com%s',
456
                    $row['commentsurl']
457
                );
458
            }
459
        }
460
461
        return [$data, $first_speech, $subsection_title];
462
    }
463
464
    private function getCountryDetails() {
465
        $details = [
466
            1 =>  [
467
                'country' => 'UK',
468
                'assembly' => 'uk-commons',
469
                'location' => '&ndash; in the House of Commons',
470
            ],
471
            2 =>  [
472
                'country' => 'UK',
473
                'assembly' => 'uk-commons',
474
                'location' => '&ndash; in Westminster Hall',
475
            ],
476
            3 =>  [
477
                'country' => 'UK',
478
                'assembly' => 'uk-commons',
479
                'location' => 'written question &ndash; answered',
480
            ],
481
            4 =>  [
482
                'country' => 'UK',
483
                'assembly' => 'uk-commons',
484
                'location' => 'written statement &ndash; made',
485
            ],
486
            5 =>  [
487
                'country' => 'NORTHERN IRELAND',
488
                'assembly' => 'ni',
489
                'location' => '&ndash; in the Northern Ireland Assembly',
490
            ],
491
            6 =>  [
492
                'country' => 'UK',
493
                'assembly' => 'uk-commons',
494
                'location' => '&ndash; in a Public Bill Committee',
495
            ],
496
            7 =>  [
497
                'country' => 'SCOTLAND',
498
                'assembly' => 'scotland',
499
                'location' => '&ndash; in the Scottish Parliament',
500
            ],
501
            8 =>  [
502
                'country' => 'SCOTLAND',
503
                'assembly' => 'scotland',
504
                'location' => '&ndash; Scottish Parliament written question &ndash; answered',
505
            ],
506
            9 =>  [
507
                'country' => 'LONDON',
508
                'assembly' => 'london-assembly',
509
                'location' => 'Questions to the Mayor of London &ndash; answered',
510
            ],
511
            10 =>  [
512
                'country' => 'WALES',
513
                'assembly' => 'senedd',
514
                'location' => '&ndash; in the Senedd',
515
            ],
516
            11 =>  [
517
                'country' => 'WALES',
518
                'assembly' => 'senedd',
519
                'location' => '&ndash; Senedd Cymru',
520
            ],
521
            101 =>  [
522
                'country' => 'UK',
523
                'assembly' => 'uk-lords',
524
                'location' => '&ndash; in the House of Lords',
525
            ],
526
        ];
527
528
        $detail = $details[$this->major];
529
        return [$detail['country'], $detail['location'], $detail['assembly']];
530
    }
531
532
    private function majorToConsType() {
533
        $major_to_cons_type = [
534
            1 => 'WMC',
535
            2 => 'WMC',
536
            3 => 'WMC',
537
            4 => 'WMC',
538
            5 => 'NIE',
539
            6 => 'WMC',
540
            7 => 'SPC',
541
            8 => 'SPC',
542
            10 => 'WAC',
543
            11 => 'WAC',
544
            101 => '',
545
        ];
546
547
        return $major_to_cons_type[$this->major];
548
    }
549
550
    protected function display_front() {
551
        global $DATA, $this_page;
552
        $this_page = $this->page_base . 'front';
553
        $data = [];
554
        if ($this->index_template) {
555
            $data['template'] = $this->index_template;
556
        }
557
558
        $class = new $this->class();
559
        $content = [];
560
        $content['data'] = $this->front_content();
0 ignored issues
show
Bug introduced by
The method front_content() does not exist on MySociety\TheyWorkForYou\SectionView\SectionView. It seems like you code against a sub-type of said class. However, the method does not exist in MySociety\TheyWorkForYou\SectionView\DebatesView. Are you sure you never get one of those? ( Ignorable by Annotation )

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

560
        /** @scrutinizer ignore-call */ 
561
        $content['data'] = $this->front_content();
Loading history...
561
562
        $content['calendar'] = $class->display('calendar', ['months' => 1], 'none');
563
564
        if ($rssurl = $DATA->page_metadata($this_page, 'rss')) {
565
            $content['rssurl'] = $rssurl;
566
        }
567
568
        $data['content'] = $content;
569
        return $this->addCommonData($data);
570
571
    }
572
573
    public function is_debate_section_page() {
574
        global $this_page;
575
        return ($this->major_data['type'] == 'debate' && $this->major_data['page_all'] == $this_page);
576
    }
577
578
    //$totalcomments, $comment, $commenturl
579
    public function generate_commentteaser($row) {
580
        // Returns HTML for the one fragment of comment and link for the sidebar.
581
        // $totalcomments is the number of comments this item has on it.
582
        // $comment is an array like:
583
        /* $comment = array (
584
            'comment_id' => 23,
585
            'user_id'    => 34,
586
            'body'        => 'Blah blah...',
587
            'posted'    => '2004-02-24 23:45:30',
588
            'username'    => 'phil'
589
            )
590
        */
591
        // $url is the URL of the item's page, which contains comments.
592
593
        if ($row['totalcomments'] == 0) {
594
            return;
595
        }
596
597
        //Add existing annotations
598
        $comment = $row['comment'];
599
600
        // If the comment is longer than the speech body, we want to trim it
601
        // to be the same length so they fit next to each other.
602
        // But the comment typeface is smaller, so we scale things slightly too...
603
        $targetsize = round(strlen($row['body']) * 0.6);
604
605
        $linktext = '';
606
        if ($targetsize > strlen($comment['body'])) {
607
            // This comment will fit in its entirety.
608
            $commentbody = $comment['body'];
609
610
            if ($row['totalcomments'] > 1) {
611
                $morecount = $row['totalcomments'] - 1;
612
                $plural = $morecount == 1 ? 'annotation' : 'annotations';
613
                $linktext = "Read $morecount more $plural";
614
            }
615
616
        } else {
617
            // This comment needs trimming.
618
            $commentbody = trim_characters($comment['body'], 0, $targetsize, 1000);
619
            if ($row['totalcomments'] > 1) {
620
                $morecount = $row['totalcomments'] - 1;
621
                $plural = $morecount == 1 ? 'annotation' : 'annotations';
622
                $linktext = "Continue reading (and $morecount more $plural)";
623
            } else {
624
                $linktext = 'Continue reading';
625
            }
626
        }
627
628
        return [
629
            'body' => prepare_comment_for_display($commentbody),
630
            'username' => _htmlentities($comment['username']),
631
            'linktext' => $linktext,
632
            'commentsurl' => $row['commentsurl'],
633
            'comment_id' => $comment['comment_id'],
634
        ];
635
    }
636
637
    protected function get_question_mentions_html($row_data) {
0 ignored issues
show
Unused Code introduced by
The parameter $row_data is not used and could be removed. ( Ignorable by Annotation )

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

637
    protected function get_question_mentions_html(/** @scrutinizer ignore-unused */ $row_data) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
638
        throw new \Exception('get_question_mentions_html called from class with no implementation');
639
    }
640
641
    private function pageToSection() {
642
        $sections = [
643
            1 => 'debates',
644
            2 => 'whall',
645
            3 => 'wrans',
646
            4 => 'wms',
647
            5 => 'ni',
648
            6 => 'standing',
649
            7 => 'sp',
650
            8 => 'spwrans',
651
            9 => 'lmqs',
652
            10 => 'senedd',
653
            11 => 'senedd',
654
            101 => 'lords',
655
        ];
656
657
        return $sections[$this->major];
658
    }
659
660
}
661