X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/mediawiki.git/blobdiff_plain/19e297c21b10b1b8a3acad5e73fc71dcb35db44a..6932310fd58ebef145fa01eb76edf7150284d8ea:/includes/widget/DateInputWidget.php diff --git a/includes/widget/DateInputWidget.php b/includes/widget/DateInputWidget.php new file mode 100644 index 00000000..507dab6f --- /dev/null +++ b/includes/widget/DateInputWidget.php @@ -0,0 +1,176 @@ + 'day', + 'longDisplayFormat' => false, + ], $config ); + + // Properties + if ( isset( $config['inputFormat'] ) ) { + $this->inputFormat = $config['inputFormat']; + } + if ( isset( $config['placeholderDateFormat'] ) ) { + $this->placeholderDateFormat = $config['placeholderDateFormat']; + } + $this->precision = $config['precision']; + if ( isset( $config['mustBeAfter'] ) ) { + $this->mustBeAfter = $config['mustBeAfter']; + } + if ( isset( $config['mustBeBefore'] ) ) { + $this->mustBeBefore = $config['mustBeBefore']; + } + + // Properties stored for the infused JS widget + if ( isset( $config['displayFormat'] ) ) { + $this->displayFormat = $config['displayFormat']; + } + if ( isset( $config['longDisplayFormat'] ) ) { + $this->longDisplayFormat = $config['longDisplayFormat']; + } + if ( isset( $config['placeholderLabel'] ) ) { + $this->placeholderLabel = $config['placeholderLabel']; + } + if ( isset( $config['overlay'] ) ) { + $this->overlay = $config['overlay']; + } + + // Set up placeholder text visible if the browser doesn't override it (logic taken from JS) + if ( $this->placeholderDateFormat !== null ) { + $placeholder = $this->placeholderDateFormat; + } elseif ( $this->inputFormat !== null ) { + // We have no way to display a translated placeholder for custom formats + $placeholder = ''; + } else { + $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text(); + } + + $config = array_merge( [ + // Processed config values + 'placeholder' => $placeholder, + ], $config ); + + // Parent constructor + parent::__construct( $config ); + + // Calculate min/max attributes (which are skipped by TextInputWidget) and add to + // min/max attributes are inclusive, but mustBeAfter/Before are exclusive + if ( $this->mustBeAfter !== null ) { + $min = new DateTime( $this->mustBeAfter ); + $min = $min->modify( '+1 day' ); + $min = $min->format( 'Y-m-d' ); + $this->input->setAttributes( [ 'min' => $min ] ); + } + if ( $this->mustBeBefore !== null ) { + $max = new DateTime( $this->mustBeBefore ); + $max = $max->modify( '-1 day' ); + $max = $max->format( 'Y-m-d' ); + $this->input->setAttributes( [ 'max' => $max ] ); + } + + // Initialization + $this->addClasses( [ 'mw-widget-dateInputWidget' ] ); + } + + protected function getJavaScriptClassName() { + return 'mw.widgets.DateInputWidget'; + } + + public function getConfig( &$config ) { + if ( $this->inputFormat !== null ) { + $config['inputFormat'] = $this->inputFormat; + } + if ( $this->displayFormat !== null ) { + $config['displayFormat'] = $this->displayFormat; + } + if ( $this->longDisplayFormat !== null ) { + $config['longDisplayFormat'] = $this->longDisplayFormat; + } + if ( $this->placeholderLabel !== null ) { + $config['placeholderLabel'] = $this->placeholderLabel; + } + if ( $this->placeholderDateFormat !== null ) { + $config['placeholderDateFormat'] = $this->placeholderDateFormat; + } + if ( $this->precision !== null ) { + $config['precision'] = $this->precision; + } + if ( $this->mustBeAfter !== null ) { + $config['mustBeAfter'] = $this->mustBeAfter; + } + if ( $this->mustBeBefore !== null ) { + $config['mustBeBefore'] = $this->mustBeBefore; + } + if ( $this->overlay !== null ) { + $config['overlay'] = $this->overlay; + } + return parent::getConfig( $config ); + } + + public function getInputElement( $config ) { + // Inserts date/month type attribute + return parent::getInputElement( $config ) + ->setAttributes( [ + 'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date' + ] ); + } +}