Index: trunk/extensions/EducationProgram/specials/SpecialTerms.php |
— | — | @@ -116,7 +116,7 @@ |
117 | 117 | $select->addOptions( EPCourse::getCourseOptions( $courses ) ); |
118 | 118 | $out->addHTML( $select->getHTML() ); |
119 | 119 | |
120 | | - $out->addHTML( ' ' . Xml::inputLabel( wfMsg( 'ep-terms-newyear' ), 'newyear', 'newyear' ) ); |
| 120 | + $out->addHTML( ' ' . Xml::inputLabel( wfMsg( 'ep-terms-newyear' ), 'newyear', 'newyear', 10 ) ); |
121 | 121 | |
122 | 122 | $out->addHTML( ' ' . Html::input( |
123 | 123 | 'addnewterm', |
Index: trunk/extensions/EducationProgram/specials/SpecialEditTerm.php |
— | — | @@ -20,6 +20,8 @@ |
21 | 21 | */ |
22 | 22 | public function __construct() { |
23 | 23 | parent::__construct( 'EditTerm', 'epmentor', 'EPTerm', 'Terms' ); |
| 24 | + |
| 25 | + $this->getOutput()->addModules( 'ep.datepicker' ); |
24 | 26 | } |
25 | 27 | |
26 | 28 | /** |
— | — | @@ -44,19 +46,47 @@ |
45 | 47 | ); |
46 | 48 | |
47 | 49 | $fields['year'] = array ( |
48 | | - 'type' => 'text', |
| 50 | + 'type' => 'int', |
49 | 51 | 'label-message' => 'ep-term-edit-year', |
50 | 52 | '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, |
54 | 56 | ); |
55 | 57 | |
| 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 | + |
56 | 70 | return $this->processFormFields( $fields ); |
57 | 71 | } |
58 | 72 | |
59 | 73 | /** |
60 | 74 | * (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) |
61 | 91 | * @see SpecialEPFormPage::getTitleConditions() |
62 | 92 | */ |
63 | 93 | protected function getTitleConditions() { |
— | — | @@ -74,4 +104,45 @@ |
75 | 105 | ); |
76 | 106 | } |
77 | 107 | |
| 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 | + |
78 | 120 | } |
| 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 @@ |
252 | 252 | if ( $this->item !== false ) { |
253 | 253 | foreach ( $fields as $name => &$data ) { |
254 | 254 | if ( !array_key_exists( 'default', $data ) ) { |
255 | | - $data['default'] = $this->item->getField( $name ); |
| 255 | + $data['default'] = $this->getDefaultFromItem( $this->item, $name ); |
256 | 256 | } |
257 | 257 | } |
258 | 258 | } |
— | — | @@ -273,6 +273,20 @@ |
274 | 274 | } |
275 | 275 | |
276 | 276 | /** |
| 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 | + /** |
277 | 291 | * Gets called after the form is saved. |
278 | 292 | * |
279 | 293 | * @since 0.1 |
— | — | @@ -313,11 +327,13 @@ |
314 | 328 | } |
315 | 329 | } |
316 | 330 | |
| 331 | + $keys = array_keys( $fields ); |
| 332 | + $fields = array_combine( $keys, array_map( array( $this, 'handleKnownField' ), $keys, $fields ) ); |
317 | 333 | |
318 | 334 | /* EPDBObject */ $item = new $c( $fields, is_null( $fields['id'] ) ); |
319 | 335 | |
320 | 336 | foreach ( $unknownValues as $name => $value ) { |
321 | | - |
| 337 | + $this->handleUnknownField( $item, $name, $value ); |
322 | 338 | } |
323 | 339 | |
324 | 340 | $success = $item->writeToDB(); |
— | — | @@ -343,4 +359,19 @@ |
344 | 360 | // Override to use. |
345 | 361 | } |
346 | 362 | |
| 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 | + |
347 | 378 | } |
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php |
— | — | @@ -138,6 +138,11 @@ |
139 | 139 | 'ep-term-edit-legend' => 'Edit term', |
140 | 140 | 'ep-term-edit-year' => 'Year', |
141 | 141 | '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.', |
142 | 147 | ); |
143 | 148 | |
144 | 149 | /** Message documentation (Message documentation) |
Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -170,6 +170,15 @@ |
171 | 171 | ), |
172 | 172 | ); |
173 | 173 | |
| 174 | +$wgResourceModules['ep.datepicker'] = $moduleTemplate + array( |
| 175 | + 'scripts' => array( |
| 176 | + 'ep.datepicker.js', |
| 177 | + ), |
| 178 | + 'dependencies' => array( |
| 179 | + 'jquery.ui.datepicker', |
| 180 | + ), |
| 181 | +); |
| 182 | + |
174 | 183 | $wgResourceModules['ep.formpage'] = $moduleTemplate + array( |
175 | 184 | 'scripts' => array( |
176 | 185 | '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 |