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