r106919 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106918‎ | r106919 | r106920 >
Date:01:45, 21 December 2011
Author:jeroendedauw
Status:deferred
Tags:educationprogram 
Comment:
work on term editing
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/EducationProgram.php (modified) (history)
  • /trunk/extensions/EducationProgram/resources/ep.datepicker.js (added) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialEPFormPage.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialEditTerm.php (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialTerms.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/specials/SpecialTerms.php
@@ -116,7 +116,7 @@
117117 $select->addOptions( EPCourse::getCourseOptions( $courses ) );
118118 $out->addHTML( $select->getHTML() );
119119
120 - $out->addHTML( ' ' . Xml::inputLabel( wfMsg( 'ep-terms-newyear' ), 'newyear', 'newyear' ) );
 120+ $out->addHTML( ' ' . Xml::inputLabel( wfMsg( 'ep-terms-newyear' ), 'newyear', 'newyear', 10 ) );
121121
122122 $out->addHTML( ' ' . Html::input(
123123 'addnewterm',
Index: trunk/extensions/EducationProgram/specials/SpecialEditTerm.php
@@ -20,6 +20,8 @@
2121 */
2222 public function __construct() {
2323 parent::__construct( 'EditTerm', 'epmentor', 'EPTerm', 'Terms' );
 24+
 25+ $this->getOutput()->addModules( 'ep.datepicker' );
2426 }
2527
2628 /**
@@ -44,19 +46,47 @@
4547 );
4648
4749 $fields['year'] = array (
48 - 'type' => 'text',
 50+ 'type' => 'int',
4951 'label-message' => 'ep-term-edit-year',
5052 'required' => true,
51 - 'validation-callback' => function ( $value, array $alldata = null ) {
52 - return ctype_digit( $value ) ? true : wfMsg( 'ep-term-invalid-year' );
53 - },
 53+ 'min' => 2000,
 54+ 'max' => 9001,
 55+ 'size' => 15,
5456 );
5557
 58+ $fields['start'] = array (
 59+ 'class' => 'EPHTMLDateField',
 60+ 'label-message' => 'ep-term-edit-start',
 61+ 'required' => true,
 62+ );
 63+
 64+ $fields['end'] = array (
 65+ 'class' => 'EPHTMLDateField',
 66+ 'label-message' => 'ep-term-edit-end',
 67+ 'required' => true,
 68+ );
 69+
5670 return $this->processFormFields( $fields );
5771 }
5872
5973 /**
6074 * (non-PHPdoc)
 75+ * @see SpecialEPFormPage::getDefaultFromItem()
 76+ */
 77+ protected function getDefaultFromItem( EPDBObject $item, $name ) {
 78+ $default = $item->getField( $name );
 79+
 80+ if ( in_array( $name, array( 'start', 'end' ) ) ) {
 81+ $default = wfTimestamp( TS_ISO_8601, $default );
 82+ $default = explode( 'T', $default );
 83+ $default = $default[0];
 84+ }
 85+
 86+ return $default;
 87+ }
 88+
 89+ /**
 90+ * (non-PHPdoc)
6191 * @see SpecialEPFormPage::getTitleConditions()
6292 */
6393 protected function getTitleConditions() {
@@ -74,4 +104,45 @@
75105 );
76106 }
77107
 108+ /**
 109+ * (non-PHPdoc)
 110+ * @see SpecialEPFormPage::handleKnownField()
 111+ */
 112+ protected function handleKnownField( $name, $value ) {
 113+ if ( in_array( $name, array( 'end', 'start' ) ) ) {
 114+ $value = wfTimestamp( TS_MW, strtotime( $value ) );
 115+ }
 116+
 117+ return $value;
 118+ }
 119+
78120 }
 121+
 122+class EPHTMLDateField extends HTMLTextField {
 123+
 124+ public function __construct( $params ) {
 125+ parent::__construct( $params );
 126+
 127+ $this->mClass .= " ep-datepicker";
 128+ }
 129+
 130+ function getSize() {
 131+ return isset( $this->mParams['size'] )
 132+ ? $this->mParams['size']
 133+ : 20;
 134+ }
 135+
 136+ function validate( $value, $alldata ) {
 137+ $p = parent::validate( $value, $alldata );
 138+
 139+ if ( $p !== true ) {
 140+ return $p;
 141+ }
 142+
 143+ $value = trim( $value );
 144+
 145+ // TODO: further validation
 146+
 147+ return true;
 148+ }
 149+}
Index: trunk/extensions/EducationProgram/specials/SpecialEPFormPage.php
@@ -251,7 +251,7 @@
252252 if ( $this->item !== false ) {
253253 foreach ( $fields as $name => &$data ) {
254254 if ( !array_key_exists( 'default', $data ) ) {
255 - $data['default'] = $this->item->getField( $name );
 255+ $data['default'] = $this->getDefaultFromItem( $this->item, $name );
256256 }
257257 }
258258 }
@@ -273,6 +273,20 @@
274274 }
275275
276276 /**
 277+ * Gets the default value for a field from the item.
 278+ *
 279+ * @since 0.1
 280+ *
 281+ * @param EPDBObject $item
 282+ * @param string $name
 283+ *
 284+ * @return mixed
 285+ */
 286+ protected function getDefaultFromItem( EPDBObject $item, $name ) {
 287+ return $item->getField( $name );
 288+ }
 289+
 290+ /**
277291 * Gets called after the form is saved.
278292 *
279293 * @since 0.1
@@ -313,11 +327,13 @@
314328 }
315329 }
316330
 331+ $keys = array_keys( $fields );
 332+ $fields = array_combine( $keys, array_map( array( $this, 'handleKnownField' ), $keys, $fields ) );
317333
318334 /* EPDBObject */ $item = new $c( $fields, is_null( $fields['id'] ) );
319335
320336 foreach ( $unknownValues as $name => $value ) {
321 -
 337+ $this->handleUnknownField( $item, $name, $value );
322338 }
323339
324340 $success = $item->writeToDB();
@@ -343,4 +359,19 @@
344360 // Override to use.
345361 }
346362
 363+ /**
 364+ * Gets called for evey known submitted value, so they can be dealth with if needed.
 365+ *
 366+ * @since 0.1
 367+ *
 368+ * @param string $name
 369+ * @param string $value This is a string, since it comes from request data, but might be a number or other type.
 370+ *
 371+ * @return mixed The new value
 372+ */
 373+ protected function handleKnownField( $name, $value ) {
 374+ // Override to use.
 375+ return $value;
 376+ }
 377+
347378 }
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -138,6 +138,11 @@
139139 'ep-term-edit-legend' => 'Edit term',
140140 'ep-term-edit-year' => 'Year',
141141 'ep-term-edit-course' => 'Course',
 142+ 'ep-term-edit-start' => 'Start date',
 143+ 'ep-term-edit-end' => 'End date',
 144+
 145+ 'ep-term-invalid-year' => 'The year needs to be number.',
 146+ 'ep-term-invalid-course' => 'This course does not exist.',
142147 );
143148
144149 /** Message documentation (Message documentation)
Index: trunk/extensions/EducationProgram/EducationProgram.php
@@ -170,6 +170,15 @@
171171 ),
172172 );
173173
 174+$wgResourceModules['ep.datepicker'] = $moduleTemplate + array(
 175+ 'scripts' => array(
 176+ 'ep.datepicker.js',
 177+ ),
 178+ 'dependencies' => array(
 179+ 'jquery.ui.datepicker',
 180+ ),
 181+);
 182+
174183 $wgResourceModules['ep.formpage'] = $moduleTemplate + array(
175184 'scripts' => array(
176185 'ep.formpage.js',
Index: trunk/extensions/EducationProgram/resources/ep.datepicker.js
@@ -0,0 +1,19 @@
 2+/**
 3+ * JavasSript for the Education Program MediaWiki extension.
 4+ * @see https://www.mediawiki.org/wiki/Extension:Education_Program
 5+ *
 6+ * @licence GNU GPL v3 or later
 7+ * @author Jeroen De Dauw <jeroendedauw at gmail dot com>
 8+ */
 9+
 10+(function( $, mw ) {
 11+
 12+ $( document ).ready( function() {
 13+
 14+ $( '.ep-datepicker' ).datepicker( {
 15+ 'dateFormat': 'yy-mm-dd'
 16+ } );
 17+
 18+ } );
 19+
 20+})( window.jQuery );
\ No newline at end of file

Status & tagging log