Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ManageMentorsAbstractForm | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
canManageMentors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFormFields | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
onSubmit | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
onSuccess | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
alterForm | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
show | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Specials\Forms; |
4 | |
5 | use IContextSource; |
6 | use MediaWiki\Permissions\Authority; |
7 | use MediaWiki\SpecialPage\SpecialPage; |
8 | use MediaWiki\Status\Status; |
9 | use MediaWiki\User\UserIdentity; |
10 | use OOUIHTMLForm; |
11 | |
12 | abstract class ManageMentorsAbstractForm extends OOUIHTMLForm { |
13 | |
14 | /** @var UserIdentity */ |
15 | protected UserIdentity $mentorUser; |
16 | |
17 | /** |
18 | * @param UserIdentity $mentorUser |
19 | * @param IContextSource $context |
20 | * @param string $messagePrefix |
21 | */ |
22 | public function __construct( |
23 | UserIdentity $mentorUser, |
24 | IContextSource $context, |
25 | string $messagePrefix = '' |
26 | ) { |
27 | // must happen before calling getFormFields(), as that might make use of $mentorUser |
28 | $this->mentorUser = $mentorUser; |
29 | |
30 | parent::__construct( |
31 | $this->getFormFields(), |
32 | $context, |
33 | $messagePrefix |
34 | ); |
35 | |
36 | $this->setSubmitCallback( [ $this, 'onSubmit' ] ); |
37 | } |
38 | |
39 | /** |
40 | * Can $performer manage mentors? |
41 | * |
42 | * @param Authority $performer |
43 | * @return bool |
44 | */ |
45 | public static function canManageMentors( Authority $performer ): bool { |
46 | return $performer->isAllowed( 'managementors' ); |
47 | } |
48 | |
49 | /** |
50 | * Get an HTMLForm descriptor array |
51 | * |
52 | * @see HTMLForm's class documentation for syntax. |
53 | * @return array |
54 | */ |
55 | abstract protected function getFormFields(): array; |
56 | |
57 | /** |
58 | * Process the form on POST submission. |
59 | * |
60 | * Must check canManageMentors() or otherwise assert the user |
61 | * is authorized to change mentorship-related properties. |
62 | * |
63 | * @param array $data |
64 | * @return bool|Status |
65 | */ |
66 | abstract public function onSubmit( array $data ); |
67 | |
68 | /** |
69 | * Do something on successful processing of the form |
70 | * |
71 | * Useful to display a success message. |
72 | * |
73 | * @return void |
74 | */ |
75 | abstract protected function onSuccess(): void; |
76 | |
77 | /** |
78 | * Alter the form |
79 | * |
80 | * Form is available as $this. Method is called from show() |
81 | */ |
82 | protected function alterForm(): void { |
83 | $this->getOutput()->addBacklinkSubtitle( |
84 | SpecialPage::getTitleFor( 'ManageMentors' ) |
85 | ); |
86 | } |
87 | |
88 | /** |
89 | * @inheritDoc |
90 | */ |
91 | public function show() { |
92 | $this->alterForm(); |
93 | |
94 | $res = parent::show(); |
95 | if ( $res ) { |
96 | $this->onSuccess(); |
97 | } |
98 | return $res; |
99 | } |
100 | } |