Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ElectionPager
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 8
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isFieldSortable
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getRowClass
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 formatValue
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
30
 formatRow
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getLinks
n/a
0 / 0
n/a
0 / 0
0
 getDefaultSort
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFieldNames
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getTitle
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Pages;
4
5use MediaWiki\Extension\SecurePoll\Entities\Election;
6use MediaWiki\Pager\TablePager;
7use stdClass;
8
9/**
10 * Parent class for election pagers:
11 * - unarchived elections
12 * - archived elections
13 */
14abstract class ElectionPager extends TablePager {
15    /** @var array[] */
16    private $subpages = [];
17    /** @var bool|null */
18    public $isAdmin;
19    /** @var Election|null */
20    public $election;
21    /** @var ArchivedPage|EntryPage */
22    public $page;
23    /** @var string[] */
24    private const FIELDS = [
25        'el_title',
26        'el_start_date',
27        'el_end_date',
28        'el_links'
29    ];
30
31    public function __construct() {
32        parent::__construct();
33    }
34
35    protected function isFieldSortable( $field ) {
36        return in_array(
37            $field,
38            [
39                'el_title',
40                'el_start_date',
41                'el_end_date'
42            ]
43        );
44    }
45
46    /**
47     * Add classes based on whether the poll is open or closed
48     * @param stdClass $row database object
49     * @return string
50     * @see TablePager::getRowClass()
51     */
52    protected function getRowClass( $row ) {
53        return $row->el_end_date > wfTimestampNow()
54            ? 'securepoll-election-open' : 'securepoll-election-closed';
55    }
56
57    /**
58     * @param string $name
59     * @param string $value
60     * @return string HTML
61     */
62    public function formatValue( $name, $value ) {
63        switch ( $name ) {
64            case 'el_start_date':
65            case 'el_end_date':
66                return htmlspecialchars( $this->getLanguage()->timeanddate( $value ) );
67            case 'el_links':
68                return $this->getLinks();
69            default:
70                return htmlspecialchars( $value );
71        }
72    }
73
74    public function formatRow( $row ) {
75        $id = $row->el_entity;
76        $this->election = $this->page->context->getElection( $id );
77        if ( !$this->election ) {
78            $this->isAdmin = false;
79        } else {
80            $this->isAdmin = $this->election->isAdmin( $this->getUser() );
81        }
82
83        return parent::formatRow( $row );
84    }
85
86    /**
87     * Return html for election-specific links
88     * @return string HTML
89     */
90    abstract public function getLinks(): string;
91
92    public function getDefaultSort() {
93        return 'el_start_date';
94    }
95
96    protected function getFieldNames() {
97        $names = [];
98        foreach ( self::FIELDS as $field ) {
99            if ( $field == 'links' ) {
100                $names[$field] = '';
101            } else {
102                // Give grep a chance to find the usages:
103                // securepoll-header-title, securepoll-header-start-date,
104                // securepoll-header-end-date, securepoll-header-links
105                $msgName = 'securepoll-header-' . strtr(
106                        $field,
107                        [
108                            'el_' => '',
109                            '_' => '-'
110                        ]
111                    );
112                $names[$field] = $this->msg( $msgName )->text();
113            }
114        }
115
116        return $names;
117    }
118
119    public function getTitle() {
120        return $this->page->getTitle();
121    }
122}