Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ChangeRegistrationSpecialPageBase | |
0.00% |
0 / 63 |
|
0.00% |
0 / 9 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
42 | |||
checkRegistrationPrecondition | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
checkEventIsValid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getMessagePrefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
checkEventExists | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | |
5 | namespace MediaWiki\Extension\CampaignEvents\Special; |
6 | |
7 | use MediaWiki\DAO\WikiAwareEntity; |
8 | use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration; |
9 | use MediaWiki\Extension\CampaignEvents\Event\Store\EventNotFoundException; |
10 | use MediaWiki\Extension\CampaignEvents\Event\Store\IEventLookup; |
11 | use MediaWiki\Extension\CampaignEvents\MWEntity\CampaignsCentralUserLookup; |
12 | use MediaWiki\Html\Html; |
13 | use MediaWiki\SpecialPage\FormSpecialPage; |
14 | use MediaWiki\WikiMap\WikiMap; |
15 | use OOUI\HtmlSnippet; |
16 | use OOUI\MessageWidget; |
17 | use StatusValue; |
18 | |
19 | abstract class ChangeRegistrationSpecialPageBase extends FormSpecialPage { |
20 | private IEventLookup $eventLookup; |
21 | |
22 | protected CampaignsCentralUserLookup $centralUserLookup; |
23 | protected ?ExistingEventRegistration $event = null; |
24 | |
25 | /** |
26 | * @param string $name |
27 | * @param IEventLookup $eventLookup |
28 | * @param CampaignsCentralUserLookup $centralUserLookup |
29 | */ |
30 | public function __construct( |
31 | string $name, |
32 | IEventLookup $eventLookup, |
33 | CampaignsCentralUserLookup $centralUserLookup |
34 | ) { |
35 | parent::__construct( $name ); |
36 | $this->eventLookup = $eventLookup; |
37 | $this->centralUserLookup = $centralUserLookup; |
38 | } |
39 | |
40 | /** |
41 | * @inheritDoc |
42 | */ |
43 | public function execute( $par ): void { |
44 | $this->requireNamedUser(); |
45 | $this->addHelpLink( 'Extension:CampaignEvents' ); |
46 | $eventExists = $this->checkEventExists( $par ); |
47 | if ( !$eventExists ) { |
48 | return; |
49 | } |
50 | $validationResult = $this->checkEventIsValid(); |
51 | if ( !$validationResult->isGood() ) { |
52 | $this->setHeaders(); |
53 | foreach ( $validationResult->getMessages( 'error' ) as $error ) { |
54 | $this->getOutput()->addHTML( Html::errorBox( |
55 | $this->msg( $error )->escaped() |
56 | ) ); |
57 | } |
58 | return; |
59 | } |
60 | |
61 | $eventPage = $this->event->getPage(); |
62 | $wikiID = $eventPage->getWikiId(); |
63 | if ( $wikiID !== WikiAwareEntity::LOCAL ) { |
64 | $foreignEditURL = WikiMap::getForeignURL( |
65 | $wikiID, 'Special:' . SpecialRegisterForEvent::PAGE_NAME . "/{$this->event->getID()}" |
66 | ); |
67 | |
68 | $this->setHeaders(); |
69 | $messageWidget = new MessageWidget( [ |
70 | 'type' => 'notice', |
71 | 'label' => new HtmlSnippet( |
72 | $this->msg( 'campaignevents-register-page-nonlocal' ) |
73 | ->params( [ |
74 | $foreignEditURL, WikiMap::getWikiName( $wikiID ) |
75 | ] )->parse() |
76 | ) |
77 | ] ); |
78 | |
79 | $this->getOutput()->addHTML( $messageWidget ); |
80 | return; |
81 | } |
82 | |
83 | $preconditionResult = $this->checkRegistrationPrecondition(); |
84 | if ( is_string( $preconditionResult ) ) { |
85 | $this->setHeaders(); |
86 | $this->getOutput()->addHTML( Html::errorBox( |
87 | $this->msg( $preconditionResult )->escaped() |
88 | ) ); |
89 | return; |
90 | } |
91 | parent::execute( $par ); |
92 | } |
93 | |
94 | /** |
95 | * Checks whether the user can perform this action. In particular, this is used to show an error if the user |
96 | * is not registered for this event and they try to cancel their registration. |
97 | * |
98 | * @return string|true Error message key, or true if OK. |
99 | */ |
100 | protected function checkRegistrationPrecondition() { |
101 | return true; |
102 | } |
103 | |
104 | /** |
105 | * Checks whether the event is in a state which can currently accept registrations. Specifically, that it |
106 | * is not over, deleted or closed. |
107 | * |
108 | * @return StatusValue |
109 | */ |
110 | protected function checkEventIsValid(): StatusValue { |
111 | return StatusValue::newGood(); |
112 | } |
113 | |
114 | /** |
115 | * @inheritDoc |
116 | */ |
117 | protected function getDisplayFormat(): string { |
118 | return 'ooui'; |
119 | } |
120 | |
121 | /** |
122 | * @inheritDoc |
123 | */ |
124 | protected function getGroupName(): string { |
125 | return 'campaignevents'; |
126 | } |
127 | |
128 | /** |
129 | * @inheritDoc |
130 | */ |
131 | public function doesWrites(): bool { |
132 | return true; |
133 | } |
134 | |
135 | /** |
136 | * @inheritDoc |
137 | */ |
138 | protected function getMessagePrefix() { |
139 | return ''; |
140 | } |
141 | |
142 | /** |
143 | * @param string|null $par |
144 | * @return bool |
145 | */ |
146 | protected function checkEventExists( ?string $par ) { |
147 | if ( $par === null ) { |
148 | $this->setHeaders(); |
149 | $this->getOutput()->addHTML( Html::errorBox( |
150 | $this->msg( 'campaignevents-register-error-no-event' )->escaped() |
151 | ) ); |
152 | return false; |
153 | } |
154 | $eventID = (int)$par; |
155 | try { |
156 | $this->event = $this->eventLookup->getEventByID( $eventID ); |
157 | } catch ( EventNotFoundException $_ ) { |
158 | $this->setHeaders(); |
159 | $this->getOutput()->addHTML( Html::errorBox( |
160 | $this->msg( 'campaignevents-register-error-event-not-found' )->escaped() |
161 | ) ); |
162 | return false; |
163 | } |
164 | return true; |
165 | } |
166 | } |