r99351 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99350‎ | r99351 | r99352 >
Date:13:15, 9 October 2011
Author:werdna
Status:resolved (Comments)
Tags:
Comment:
svn add DashboardForms.php
Modified paths:
  • /trunk/extensions/MoodBar/DashboardForms.php (added) (history)

Diff [purge]

Index: trunk/extensions/MoodBar/DashboardForms.php
@@ -0,0 +1,194 @@
 2+<?php
 3+/**
 4+ * Contains assorted forms associated with the Feedback Dashboards
 5+ */
 6+
 7+
 8+abstract class MBDashboardForm {
 9+ /**
 10+ * Gets an HTMLForm object suitable for showing this form
 11+ */
 12+ public function getForm() {
 13+ $form = new HTMLForm( $this->getFormDescriptor() );
 14+ $form->setSubmitCallback( array( $this, 'submit' ) );
 15+
 16+ return $form;
 17+ }
 18+
 19+ /**
 20+ * Shows the form, but returns the output as HTML instead of sending it to $wgOut
 21+ */
 22+ public function show() {
 23+ global $wgOut;
 24+
 25+ $oldText = $wgOut->getHTML();
 26+ $wgOut->clearHTML();
 27+
 28+ $result = $this->getForm()->show();
 29+
 30+ $output = $wgOut->getHTML();
 31+ $wgOut->clearHTML( );
 32+ $wgOut->addHTML( $oldText );
 33+
 34+ if ( $result === true ) {
 35+ return true;
 36+ }
 37+
 38+ return $output;
 39+ }
 40+
 41+ /**
 42+ * Gets the HTMLForm form descriptor
 43+ * @return A structured array suitable for the HTMLForm constructor.
 44+ */
 45+ public abstract function getFormDescriptor();
 46+
 47+ /**
 48+ * Submits the form.
 49+ */
 50+ public abstract function submit( $data );
 51+}
 52+
 53+abstract class MBActionForm extends MBDashboardForm {
 54+ public function __construct( $id ) {
 55+ $this->id = $id;
 56+ }
 57+
 58+ public function getForm() {
 59+ $form = parent::getForm();
 60+
 61+ $title = SpecialPage::getTitleFor( 'FeedbackDashboard', $this->id );
 62+ $form->setTitle( $title );
 63+
 64+ return $form;
 65+ }
 66+
 67+ /**
 68+ * Adds an 'item' field to provide built in support for doing actions to feedback items.
 69+ */
 70+ public function getFormDescriptor() {
 71+ $template = array(
 72+ 'item' => array(
 73+ 'type' => 'hidden',
 74+ 'required' => true,
 75+ 'readonly' => 'readonly',
 76+ 'label-message' => 'moodbar-action-item',
 77+ 'default' => $this->id,
 78+ ),
 79+ 'reason' => array(
 80+ 'type' => 'text',
 81+ 'size' => '60',
 82+ 'maxlength' => '200',
 83+ 'label-message' => 'movereason',
 84+ ),
 85+ );
 86+
 87+ return $template;
 88+ }
 89+
 90+ /**
 91+ * Load our item and do our thing
 92+ */
 93+ public function submit( $data ) {
 94+ $id = $data['item'];
 95+ $dbr = wfGetDB( DB_SLAVE );
 96+
 97+ $row = $dbr->selectRow( 'moodbar_feedback', '*',
 98+ array( 'mbf_id' => $id ), __METHOD__ );
 99+
 100+ if ( ! $row ) {
 101+ return wfMessage( 'moodbar-invalid-item' )->parse();
 102+ }
 103+
 104+ $feedbackItem = MBFeedbackItem::load( $row );
 105+
 106+ $this->manipulateItem( $feedbackItem, $data );
 107+
 108+ return true;
 109+ }
 110+
 111+ /**
 112+ * Do whatever action you need to do to the $feedbackItem in this function
 113+ * @param $feedbackItem MBFeedbackItem to manipulate.
 114+ * @param $data The form data.
 115+ */
 116+ protected abstract function manipulateItem( $feedbackItem, $data );
 117+}
 118+
 119+class MBHideForm extends MBActionForm {
 120+ public function getFormDescriptor() {
 121+ $desc = parent::getFormDescriptor();
 122+ $desc += array(
 123+ 'hide-feedback' => array(
 124+ 'type' => 'hidden',
 125+ 'default' => '1',
 126+ 'name' => 'hide-feedback',
 127+ ),
 128+ );
 129+
 130+ return $desc;
 131+ }
 132+
 133+ protected function manipulateItem( $feedbackItem, $data ) {
 134+ $feedbackItem->setProperty('hidden-state', 255);
 135+ $feedbackItem->save();
 136+
 137+ $title = SpecialPage::getTitleFor( 'FeedbackDashboard',
 138+ $feedbackItem->getProperty('id') );
 139+
 140+ $logPage = new LogPage('moodbar');
 141+ $logPage->addEntry( 'hide', $title, $data['reason'] );
 142+ }
 143+
 144+ public function getForm() {
 145+ $form = parent::getForm();
 146+
 147+ $header = Html::element( 'h3', null,
 148+ wfMessage( 'moodbar-hide-header' )->parse() );
 149+
 150+ $header .= wfMessage( 'moodbar-hide-intro' )->parse();
 151+
 152+ $form->addPreText( $header );
 153+
 154+ return $form;
 155+ }
 156+}
 157+
 158+class MBRestoreForm extends MBActionForm {
 159+ public function getFormDescriptor() {
 160+ $desc = parent::getFormDescriptor();
 161+ $desc += array(
 162+ 'restore-feedback' => array(
 163+ 'type' => 'hidden',
 164+ 'default' => '1',
 165+ 'name' => 'restore-feedback',
 166+ ),
 167+ );
 168+
 169+ return $desc;
 170+ }
 171+
 172+ protected function manipulateItem( $feedbackItem, $data ) {
 173+ $feedbackItem->setProperty('hidden-state', 0);
 174+ $feedbackItem->save();
 175+
 176+ $title = SpecialPage::getTitleFor( 'FeedbackDashboard',
 177+ $feedbackItem->getProperty('id') );
 178+
 179+ $logPage = new LogPage('moodbar');
 180+ $logPage->addEntry( 'restore', $title, $data['reason'] );
 181+ }
 182+
 183+ public function getForm() {
 184+ $form = parent::getForm();
 185+
 186+ $header = Html::element( 'h3', null,
 187+ wfMessage( 'moodbar-restore-header' )->parse() );
 188+
 189+ $header .= wfMessage( 'moodbar-restore-intro' )->parse();
 190+
 191+ $form->addPreText( $header );
 192+
 193+ return $form;
 194+ }
 195+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r100025Adjustments to r99351 r99350 r99200 per code review:...werdna13:10, 17 October 2011

Comments

#Comment by Catrope (talk | contribs)   19:13, 15 October 2011
+		$header = Html::element( 'h3', null,
+			wfMessage( 'moodbar-hide-header' )->parse() );
[...]
+		$header = Html::element( 'h3', null,
+			wfMessage( 'moodbar-restore-header' )->parse() );

That'll double-escape things, use rawElement.

#Comment by Werdna (talk | contribs)   13:10, 17 October 2011

Fixed in r100025.

Status & tagging log