summaryrefslogtreecommitdiff
blob: bac4d8a844ebd46898044b542ce254c8ee3b9cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

/**
 * Form for marking notifications as read by ID.
 *
 * This uses the normal HTMLForm handling when receiving POSTs.
 * However, for a better user no-JS user experience, we integrate
 * a version of the form into Special:Notifications.  Thus, this
 * page should normally not need to be visited directly.
 */
class SpecialNotificationsMarkRead extends FormSpecialPage {
	protected $eventId;

	public function __construct() {
		parent::__construct( 'NotificationsMarkRead' );
	}

	public function doesWrites() {
		return true;
	}

	public function execute( $par ) {
		parent::execute( $par );

		$out = $this->getOutput();
		$out->setPageTitle( $this->msg( 'echo-specialpage-markasread' )->text() );

		// Redirect to login page and inform user of the need to login
		$this->requireLogin( 'echo-notification-loginrequired' );
	}

	public function isListed() {
		return false;
	}

	public function getDisplayFormat() {
		return 'ooui';
	}

	/**
	 * Get an HTMLForm descriptor array
	 * @return array[]
	 */
	protected function getFormFields() {
		return [
			'id' => [
				'type' => 'hidden',
				'required' => true,
				'default' => $this->par,
				'filter-callback' => function ( $value, $alldata ) {
					// Allow for a single value or a set of values
					$result = explode( ',', $value );
					return $result;
				},
				'validation-callback' => function ( $value, $alldata ) {
					if ( $value === [ 'ALL' ] ) {
						return true;
					}
					if ( (int)$value <= 0 ) {
						return $this->msg( 'echo-specialpage-markasread-invalid-id' );
					}
					foreach ( $value as $val ) {
						if ( (int)( $val ) <= 0 ) {
							return $this->msg( 'echo-specialpage-markasread-invalid-id' );
						}
					}
					return true;
				}
			]
		];
	}

	/**
	 * Gets a pre-filled version of the form; this should not have a legend or anything
	 *   visible, except the button.
	 *
	 * @param int|array $idValue ID or array of IDs
	 * @param string $submitButtonValue Value attribute for button
	 * @param bool $framed Whether the button should be framed
	 * @param string $submitLabelHtml Raw HTML to use for button label
	 *
	 * @return HTMLForm
	 */
	public function getMinimalForm( $idValue, $submitButtonValue, $framed, $submitLabelHtml ) {
		if ( !is_array( $idValue ) ) {
			$idValue = [ $idValue ];
		}

		$idString = implode( ',', $idValue );

		$this->setParameter( $idString );

		$form = HTMLForm::factory(
			$this->getDisplayFormat(),
			$this->getFormFields(),
			$this->getContext(),
			$this->getMessagePrefix()
		);

		// HTMLForm assumes that the main submit button is always 'primary',
		// which means it is colored.  Since this form is being embedded multiple
		// places on the page, it has to be neutral, so we make the button
		// manually.
		$form->suppressDefaultSubmit();

		$pageTitle = $this->getPageTitle();
		$form->setTitle( $pageTitle );
		$form->setAction( $pageTitle->getLocalURL() );

		$form->addButton( [
			'name' => 'submit',
			'value' => $submitButtonValue,
			'label-raw' => $submitLabelHtml,
			'framed' => $framed,
		] );

		return $form;
	}

	/**
	 * Sets a custom label
	 *
	 * This is only called when the form is actually visited directly, which is not the
	 *   main intended use.
	 * @param HTMLForm $form
	 */
	protected function alterForm( HTMLForm $form ) {
		$form->setSubmitText( $this->msg( 'echo-notification-markasread' )->text() );
	}

	/**
	 * Process the form on POST submission.
	 * @param array $data
	 * @return bool
	 */
	public function onSubmit( array $data ) {
		$notifUser = MWEchoNotifUser::newFromUser( $this->getUser() );

		// Allow for all IDs
		if ( $data['id'] === [ 'ALL' ] ) {
			return $notifUser->markAllRead();
		}

		// Allow for multiple IDs or a single ID
		$ids = $data['id'];
		return $notifUser->markRead( $ids );
	}

	public function onSuccess() {
		$page = SpecialPage::getTitleFor( 'Notifications' );
		$this->getOutput()->redirect( $page->getFullURL() );
	}
}