Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialDeleteEventRegistration
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 10
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
30
 userCanExecute
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getFormFields
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 alterForm
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSubmit
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onSuccess
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDisplayFormat
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Special;
6
7use MediaWiki\DAO\WikiAwareEntity;
8use MediaWiki\Extension\CampaignEvents\Event\DeleteEventCommand;
9use MediaWiki\Extension\CampaignEvents\Event\ExistingEventRegistration;
10use MediaWiki\Extension\CampaignEvents\Event\Store\EventNotFoundException;
11use MediaWiki\Extension\CampaignEvents\Event\Store\IEventLookup;
12use MediaWiki\Extension\CampaignEvents\MWEntity\MWAuthorityProxy;
13use MediaWiki\Extension\CampaignEvents\Permissions\PermissionChecker;
14use MediaWiki\Html\Html;
15use MediaWiki\HTMLForm\HTMLForm;
16use MediaWiki\SpecialPage\FormSpecialPage;
17use MediaWiki\Status\Status;
18use MediaWiki\User\User;
19use MediaWiki\WikiMap\WikiMap;
20use OOUI\HtmlSnippet;
21use OOUI\MessageWidget;
22
23class SpecialDeleteEventRegistration extends FormSpecialPage {
24
25    public const PAGE_NAME = 'DeleteEventRegistration';
26
27    private IEventLookup $eventLookup;
28    private DeleteEventCommand $deleteEventCommand;
29    private PermissionChecker $permissionChecker;
30
31    private ?ExistingEventRegistration $event;
32
33    /**
34     * @param IEventLookup $eventLookup
35     * @param DeleteEventCommand $deleteEventCommand
36     * @param PermissionChecker $permissionChecker
37     */
38    public function __construct(
39        IEventLookup $eventLookup,
40        DeleteEventCommand $deleteEventCommand,
41        PermissionChecker $permissionChecker
42    ) {
43        parent::__construct( self::PAGE_NAME );
44        $this->eventLookup = $eventLookup;
45        $this->deleteEventCommand = $deleteEventCommand;
46        $this->permissionChecker = $permissionChecker;
47    }
48
49    /**
50     * @inheritDoc
51     */
52    public function execute( $par ): void {
53        $this->addHelpLink( 'Extension:CampaignEvents' );
54        if ( $par === null ) {
55            $this->setHeaders();
56            $this->getOutput()->addHTML( Html::errorBox(
57                $this->msg( 'campaignevents-delete-error-no-event' )->escaped()
58            ) );
59            return;
60        }
61        $eventID = (int)$par;
62        try {
63            $this->event = $this->eventLookup->getEventByID( $eventID );
64        } catch ( EventNotFoundException $_ ) {
65            $this->setHeaders();
66            $this->getOutput()->addHTML( Html::errorBox(
67                $this->msg( 'campaignevents-delete-error-event-not-found' )->escaped()
68            ) );
69            return;
70        }
71
72        if ( $this->event->getDeletionTimestamp() !== null ) {
73            $this->setHeaders();
74            $this->getOutput()->addHTML( Html::errorBox(
75                $this->msg( 'campaignevents-delete-error-already-deleted' )->escaped()
76            ) );
77            return;
78        }
79
80        $eventPage = $this->event->getPage();
81        $wikiID = $eventPage->getWikiId();
82        if ( $wikiID !== WikiAwareEntity::LOCAL ) {
83            $foreignDeleteURL = WikiMap::getForeignURL(
84                $wikiID, 'Special:' . self::PAGE_NAME . "/{$eventID}"
85            );
86
87            $this->setHeaders();
88            $this->getOutput()->enableOOUI();
89            $messageWidget = new MessageWidget( [
90                'type' => 'notice',
91                'label' => new HtmlSnippet(
92                    $this->msg( 'campaignevents-delete-registration-page-nonlocal' )
93                        ->params( [
94                            $foreignDeleteURL, WikiMap::getWikiName( $wikiID )
95                        ] )->parse()
96                )
97            ] );
98
99            $this->getOutput()->addHTML( $messageWidget );
100            return;
101        }
102
103        parent::execute( $par );
104    }
105
106    /**
107     * @inheritDoc
108     */
109    public function userCanExecute( User $user ): bool {
110        $mwAuthority = new MWAuthorityProxy( $this->getAuthority() );
111        return $this->permissionChecker->userCanDeleteRegistration( $mwAuthority, $this->event );
112    }
113
114    /**
115     * @inheritDoc
116     */
117    protected function getFormFields(): array {
118        return [
119            'Confirm' => [
120                'type' => 'info',
121                'default' => $this->msg( 'campaignevents-delete-confirmation-text' )->text(),
122            ],
123        ];
124    }
125
126    /**
127     * @inheritDoc
128     */
129    protected function alterForm( HTMLForm $form ): void {
130        $form->setSubmitDestructive();
131        $form->setSubmitTextMsg( 'campaignevents-delete-submit-btn' );
132    }
133
134    /**
135     * @inheritDoc
136     */
137    public function onSubmit( array $data ): Status {
138        $performer = new MWAuthorityProxy( $this->getAuthority() );
139        return Status::wrap( $this->deleteEventCommand->deleteIfAllowed( $this->event, $performer ) );
140    }
141
142    /**
143     * @inheritDoc
144     */
145    public function onSuccess(): void {
146        $this->getOutput()->addHTML( Html::successBox(
147            $this->msg( 'campaignevents-delete-success' )->escaped()
148        ) );
149    }
150
151    /**
152     * @inheritDoc
153     */
154    protected function getDisplayFormat(): string {
155        return 'ooui';
156    }
157
158    /**
159     * @inheritDoc
160     */
161    protected function getGroupName(): string {
162        return 'campaignevents';
163    }
164
165    /**
166     * @inheritDoc
167     */
168    public function doesWrites(): bool {
169        return true;
170    }
171}