MediaWiki  1.30.0
DateInputWidget.php
Go to the documentation of this file.
1 <?php
9 namespace MediaWiki\Widget;
10 
11 use DateTime;
12 
18 class DateInputWidget extends \OOUI\TextInputWidget {
19 
20  protected $inputFormat = null;
21  protected $displayFormat = null;
22  protected $longDisplayFormat = null;
23  protected $placeholderLabel = null;
24  protected $placeholderDateFormat = null;
25  protected $precision = null;
26  protected $mustBeAfter = null;
27  protected $mustBeBefore = null;
28  protected $overlay = null;
29 
61  public function __construct( array $config = [] ) {
62  $config = array_merge( [
63  // Default config values
64  'precision' => 'day',
65  'longDisplayFormat' => false,
66  ], $config );
67 
68  // Properties
69  if ( isset( $config['inputFormat'] ) ) {
70  $this->inputFormat = $config['inputFormat'];
71  }
72  if ( isset( $config['placeholderDateFormat'] ) ) {
73  $this->placeholderDateFormat = $config['placeholderDateFormat'];
74  }
75  $this->precision = $config['precision'];
76  if ( isset( $config['mustBeAfter'] ) ) {
77  $this->mustBeAfter = $config['mustBeAfter'];
78  }
79  if ( isset( $config['mustBeBefore'] ) ) {
80  $this->mustBeBefore = $config['mustBeBefore'];
81  }
82 
83  // Properties stored for the infused JS widget
84  if ( isset( $config['displayFormat'] ) ) {
85  $this->displayFormat = $config['displayFormat'];
86  }
87  if ( isset( $config['longDisplayFormat'] ) ) {
88  $this->longDisplayFormat = $config['longDisplayFormat'];
89  }
90  if ( isset( $config['placeholderLabel'] ) ) {
91  $this->placeholderLabel = $config['placeholderLabel'];
92  }
93  if ( isset( $config['overlay'] ) ) {
94  $this->overlay = $config['overlay'];
95  }
96 
97  // Set up placeholder text visible if the browser doesn't override it (logic taken from JS)
98  if ( $this->placeholderDateFormat !== null ) {
99  $placeholder = $this->placeholderDateFormat;
100  } elseif ( $this->inputFormat !== null ) {
101  // We have no way to display a translated placeholder for custom formats
102  $placeholder = '';
103  } else {
104  $placeholder = wfMessage( "mw-widgets-dateinput-placeholder-$this->precision" )->text();
105  }
106 
107  $config = array_merge( [
108  // Processed config values
109  'placeholder' => $placeholder,
110  ], $config );
111 
112  // Parent constructor
113  parent::__construct( $config );
114 
115  // Calculate min/max attributes (which are skipped by TextInputWidget) and add to <input>
116  // min/max attributes are inclusive, but mustBeAfter/Before are exclusive
117  if ( $this->mustBeAfter !== null ) {
118  $min = new DateTime( $this->mustBeAfter );
119  $min = $min->modify( '+1 day' );
120  $min = $min->format( 'Y-m-d' );
121  $this->input->setAttributes( [ 'min' => $min ] );
122  }
123  if ( $this->mustBeBefore !== null ) {
124  $max = new DateTime( $this->mustBeBefore );
125  $max = $max->modify( '-1 day' );
126  $max = $max->format( 'Y-m-d' );
127  $this->input->setAttributes( [ 'max' => $max ] );
128  }
129 
130  // Initialization
131  $this->addClasses( [ 'mw-widget-dateInputWidget' ] );
132  }
133 
134  protected function getJavaScriptClassName() {
135  return 'mw.widgets.DateInputWidget';
136  }
137 
138  public function getConfig( &$config ) {
139  if ( $this->inputFormat !== null ) {
140  $config['inputFormat'] = $this->inputFormat;
141  }
142  if ( $this->displayFormat !== null ) {
143  $config['displayFormat'] = $this->displayFormat;
144  }
145  if ( $this->longDisplayFormat !== null ) {
146  $config['longDisplayFormat'] = $this->longDisplayFormat;
147  }
148  if ( $this->placeholderLabel !== null ) {
149  $config['placeholderLabel'] = $this->placeholderLabel;
150  }
151  if ( $this->placeholderDateFormat !== null ) {
152  $config['placeholderDateFormat'] = $this->placeholderDateFormat;
153  }
154  if ( $this->precision !== null ) {
155  $config['precision'] = $this->precision;
156  }
157  if ( $this->mustBeAfter !== null ) {
158  $config['mustBeAfter'] = $this->mustBeAfter;
159  }
160  if ( $this->mustBeBefore !== null ) {
161  $config['mustBeBefore'] = $this->mustBeBefore;
162  }
163  if ( $this->overlay !== null ) {
164  $config['overlay'] = $this->overlay;
165  }
166  return parent::getConfig( $config );
167  }
168 
169  public function getInputElement( $config ) {
170  // Inserts date/month type attribute
171  return parent::getInputElement( $config )
172  ->setAttributes( [
173  'type' => ( $config['precision'] === 'month' ) ? 'month' : 'date'
174  ] );
175  }
176 }
MediaWiki\Widget\DateInputWidget\getJavaScriptClassName
getJavaScriptClassName()
Definition: DateInputWidget.php:134
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
MediaWiki\Widget\DateInputWidget\$longDisplayFormat
$longDisplayFormat
Definition: DateInputWidget.php:22
MediaWiki\Widget\DateInputWidget\$inputFormat
$inputFormat
Definition: DateInputWidget.php:20
MediaWiki\Widget\DateInputWidget\getConfig
getConfig(&$config)
Definition: DateInputWidget.php:138
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Widget\DateInputWidget\$mustBeAfter
$mustBeAfter
Definition: DateInputWidget.php:26
MediaWiki\Widget\DateInputWidget
Date input widget.
Definition: DateInputWidget.php:18
MediaWiki\Widget\DateInputWidget\$displayFormat
$displayFormat
Definition: DateInputWidget.php:21
MediaWiki\Widget\DateInputWidget\$placeholderDateFormat
$placeholderDateFormat
Definition: DateInputWidget.php:24
MediaWiki\Widget\DateInputWidget\$overlay
$overlay
Definition: DateInputWidget.php:28
MediaWiki\Widget
MediaWiki Widgets – ComplexNamespaceInputWidget class.
Definition: ComplexNamespaceInputWidget.php:8
MediaWiki\Widget\DateInputWidget\getInputElement
getInputElement( $config)
Definition: DateInputWidget.php:169
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
MediaWiki\Widget\DateInputWidget\__construct
__construct(array $config=[])
Definition: DateInputWidget.php:61
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
MediaWiki\Widget\DateInputWidget\$precision
$precision
Definition: DateInputWidget.php:25
MediaWiki\Widget\DateInputWidget\$placeholderLabel
$placeholderLabel
Definition: DateInputWidget.php:23
MediaWiki\Widget\DateInputWidget\$mustBeBefore
$mustBeBefore
Definition: DateInputWidget.php:27
array
the array() calling protocol came about after MediaWiki 1.4rc1.