Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClickwrapFormModule
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 createContent
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
2
 createForm
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 processInput
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare( strict_types=1 );
4namespace MediaWiki\Extension\CampaignEvents\FrontendModules;
5
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration;
8use MediaWiki\Extension\CampaignEvents\MWEntity\CampaignsCentralUserLookup;
9use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
10use MediaWiki\Extension\CampaignEvents\Organizers\OrganizersStore;
11use MediaWiki\HTMLForm\HTMLForm;
12use MediaWiki\Language\Language;
13use MediaWiki\Permissions\Authority;
14use OOUI\HtmlSnippet;
15use OOUI\Tag;
16use Wikimedia\Message\IMessageFormatterFactory;
17use Wikimedia\Message\MessageValue;
18
19class 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}