Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ClickwrapFormModule | |
0.00% |
0 / 43 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
createContent | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
createForm | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
processInput | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare( strict_types=1 ); |
4 | namespace MediaWiki\Extension\CampaignEvents\FrontendModules; |
5 | |
6 | use MediaWiki\Context\IContextSource; |
7 | use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration; |
8 | use MediaWiki\Extension\CampaignEvents\MWEntity\CampaignsCentralUserLookup; |
9 | use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy; |
10 | use MediaWiki\Extension\CampaignEvents\Organizers\OrganizersStore; |
11 | use MediaWiki\HTMLForm\HTMLForm; |
12 | use MediaWiki\Language\Language; |
13 | use MediaWiki\Permissions\Authority; |
14 | use OOUI\HtmlSnippet; |
15 | use OOUI\Tag; |
16 | use Wikimedia\Message\IMessageFormatterFactory; |
17 | use Wikimedia\Message\MessageValue; |
18 | |
19 | class ClickwrapFormModule { |
20 | private ExistingEventRegistration $event; |
21 | private OrganizersStore $organiserStore; |
22 | private IMessageFormatterFactory $messageFormatterFactory; |
23 | private Language $language; |
24 | private CampaignsCentralUserLookup $centralUserLookup; |
25 | private Authority $authority; |
26 | |
27 | public function __construct( |
28 | ExistingEventRegistration $event, |
29 | OrganizersStore $organizersStore, |
30 | IMessageFormatterFactory $messageFormatterFactory, |
31 | Language $language, |
32 | CampaignsCentralUserLookup $centralUserLookup |
33 | ) { |
34 | $this->event = $event; |
35 | $this->organiserStore = $organizersStore; |
36 | $this->messageFormatterFactory = $messageFormatterFactory; |
37 | $this->language = $language; |
38 | $this->centralUserLookup = $centralUserLookup; |
39 | } |
40 | |
41 | /** |
42 | * @param IContextSource $context |
43 | * @return array |
44 | * @phan-return array{isSubmitted:bool,content:string} |
45 | */ |
46 | public function createContent( IContextSource $context, string $action ): array { |
47 | $msgFormatter = $this->messageFormatterFactory->getTextFormatter( $this->language->getCode() ); |
48 | $container = new Tag(); |
49 | $label = new Tag( 'p' ); |
50 | $label->appendContent( |
51 | $msgFormatter->format( |
52 | MessageValue::new( 'campaignevents-edit-field-clickwrap-checkbox-pretext' ) |
53 | ) |
54 | ); |
55 | $container->appendContent( $label ); |
56 | $form = $this->createForm( $context ) |
57 | ->setAction( $action ) |
58 | ->setSubmitCallback( [ $this, 'processInput' ] ) |
59 | ->suppressDefaultSubmit() |
60 | ->setPreHtml( $container ) |
61 | ->prepareForm(); |
62 | $isFormSubmitted = $form->tryAuthorizedSubmit(); |
63 | |
64 | return [ |
65 | 'isSubmitted' => $isFormSubmitted, |
66 | 'content' => new HtmlSnippet( $form->getHTML( $isFormSubmitted ) ) |
67 | ]; |
68 | } |
69 | |
70 | /** |
71 | * @param IContextSource $context |
72 | * @return HTMLForm |
73 | */ |
74 | private function createForm( IContextSource $context ): HTMLForm { |
75 | $this->authority = $context->getAuthority(); |
76 | $formDescriptor = [ |
77 | 'Acceptance' => [ |
78 | 'label-message' => 'campaignevents-edit-field-clickwrap-checkbox-label', |
79 | 'type' => 'check' |
80 | ], |
81 | 'Submit' => [ |
82 | 'buttonlabel-message' => 'campaignevents-edit-field-clickwrap-form-continue', |
83 | 'disable-if' => [ '!==', 'Acceptance', '1' ], |
84 | 'type' => 'submit' |
85 | ] |
86 | ]; |
87 | |
88 | return HTMLForm::factory( 'ooui', $formDescriptor, $context ); |
89 | } |
90 | |
91 | /** |
92 | * @param array $data |
93 | * @return bool |
94 | */ |
95 | public function processInput( array $data ): bool { |
96 | if ( $data['Acceptance'] ) { |
97 | $centralUser = $this->centralUserLookup->newFromAuthority( new MWAuthorityProxy( $this->authority ) ); |
98 | $this->organiserStore->updateClickwrapAcceptance( $this->event->getID(), $centralUser ); |
99 | return true; |
100 | } else { |
101 | return false; |
102 | } |
103 | } |
104 | |
105 | } |