Debian Codegraph @

extensions/Echo/includes/Special/SpecialNotificationsMarkRead.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Special;
4
5 use MediaWiki\Extension\Notifications\NotifUser;
6 use MediaWiki\HTMLForm\HTMLForm;
7 use MediaWiki\SpecialPage\FormSpecialPage;
8 use MediaWiki\SpecialPage\SpecialPage;
9
10 /**
11 * Form for marking notifications as read by ID.
12 *
13 * This uses the normal HTMLForm handling when receiving POSTs.
14 * However, for a better user no-JS user experience, we integrate
15 * a version of the form into Special:Notifications. Thus, this
16 * page should normally not need to be visited directly.
17 */
18 class SpecialNotificationsMarkRead extends FormSpecialPage {
19 public function __construct() {
20 parent::__construct( 'NotificationsMarkRead' );
21 }
22
23 public function doesWrites() {
24 return true;
25 }
26
27 public function execute( $par ) {
28 parent::execute( $par );
29
30 $out = $this->getOutput();
31 $out->setPageTitleMsg( $this->msg( 'echo-specialpage-markasread' ) );
32
33 // Redirect to login page and inform user of the need to login
34 $this->requireLogin( 'echo-notification-loginrequired' );
35 }
36
37 public function isListed() {
38 return false;
39 }
40
41 public function getDisplayFormat() {
42 return 'ooui';
43 }
44
45 /**
46 * Get an HTMLForm descriptor array
47 * @return array[]
48 */
49 protected function getFormFields() {
50 return [
51 'id' => [
52 'type' => 'hidden',
53 'required' => true,
54 'default' => $this->par,
55 'filter-callback' => static function ( $value, $alldata ) {
56 // Allow for a single value or a set of values
57 return explode( ',', $value ?? '' );
58 },
59 'validation-callback' => function ( $value, $alldata ) {
60 if ( $value === [ 'ALL' ] ) {
61 return true;
62 }
63 if ( (int)$value <= 0 ) {
64 return $this->msg( 'echo-specialpage-markasread-invalid-id' );
65 }
66 foreach ( $value as $val ) {
67 if ( (int)( $val ) <= 0 ) {
68 return $this->msg( 'echo-specialpage-markasread-invalid-id' );
69 }
70 }
71 return true;
72 }
73 ]
74 ];
75 }
76
77 /**
78 * Gets a pre-filled version of the form; this should not have a legend or anything
79 * visible, except the button.
80 *
81 * @param int|array $idValue ID or array of IDs
82 * @param string $submitButtonValue Value attribute for button
83 * @param bool $framed Whether the button should be framed
84 * @param string $submitLabelHtml Raw HTML to use for button label
85 *
86 * @return HTMLForm
87 */
88 public function getMinimalForm( $idValue, $submitButtonValue, $framed, $submitLabelHtml ) {
89 if ( !is_array( $idValue ) ) {
90 $idValue = [ $idValue ];
91 }
92
93 $idString = implode( ',', $idValue );
94
95 $this->setParameter( $idString );
96
97 $form = HTMLForm::factory(
98 $this->getDisplayFormat(),
99 $this->getFormFields(),
100 $this->getContext(),
101 $this->getMessagePrefix()
102 );
103
104 // HTMLForm assumes that the main submit button is always 'primary',
105 // which means it is colored. Since this form is being embedded multiple
106 // places on the page, it has to be neutral, so we make the button
107 // manually.
108 $form->suppressDefaultSubmit();
109 $form->setTitle( $this->getPageTitle() );
110
111 $form->addButton( [
112 'name' => 'submit',
113 'value' => $submitButtonValue,
114 'label-raw' => $submitLabelHtml,
115 'framed' => $framed,
116 ] );
117
118 return $form;
119 }
120
121 /**
122 * Sets a custom label
123 *
124 * This is only called when the form is actually visited directly, which is not the
125 * main intended use.
126 * @param HTMLForm $form
127 */
128 protected function alterForm( HTMLForm $form ) {
129 $form->setSubmitText( $this->msg( 'echo-notification-markasread' )->text() );
130 }
131
132 /**
133 * Process the form on POST submission.
134 * @param array $data
135 * @return bool
136 */
137 public function onSubmit( array $data ) {
138 $notifUser = NotifUser::newFromUser( $this->getUser() );
139
140 // Allow for all IDs
141 if ( $data['id'] === [ 'ALL' ] ) {
142 return $notifUser->markAllRead();
143 }
144
145 // Allow for multiple IDs or a single ID
146 $ids = $data['id'];
147 return $notifUser->markRead( $ids );
148 }
149
150 public function onSuccess() {
151 $page = SpecialPage::getTitleFor( 'Notifications' );
152 $this->getOutput()->redirect( $page->getFullURL() );
153 }
154 }