| 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 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected function alterForm( HTMLForm $form ) { |
| 129 |
$form->setSubmitText( $this->msg( 'echo-notification-markasread' )->text() ); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function onSubmit( array $data ) { |
| 138 |
$notifUser = NotifUser::newFromUser( $this->getUser() ); |
| 139 |
|
| 140 |
|
| 141 |
if ( $data['id'] === [ 'ALL' ] ) { |
| 142 |
return $notifUser->markAllRead(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 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 |
} |