ILIAS  release_7 Revision v7.30-3-g800a261c036
DateTime.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2019 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
8use ILIAS\Data\Factory as DataFactory;
10
11
15
19class DateTime extends Input implements C\Input\Field\DateTime
20{
23
24 public const TIME_FORMAT = 'HH:mm';
25
29 protected $format;
30
34 protected $min_date;
35
39 protected $max_date;
40
44 protected $with_time = false;
45
49 protected $with_time_only = false;
50
55
59 protected $timezone;
60
67 public function __construct(
68 DataFactory $data_factory,
69 \ILIAS\Refinery\Factory $refinery,
70 $label,
71 $byline
72 ) {
73 parent::__construct($data_factory, $refinery, $label, $byline);
74
75 $this->format = $data_factory->dateFormat()->standard();
76
77 $datetime_trafo = $refinery->to()->dateTime();
78 $trafo = $this->getOptionalNullTransformation($datetime_trafo);
79 $this->setAdditionalTransformation($trafo);
80 }
81
82
83 protected function getOptionalNullTransformation($or_trafo)
84 {
85 return $this->refinery->custom()->transformation(
86 function ($v) use ($or_trafo) {
87 if (!$v) {
88 return null;
89 }
90 return $or_trafo->transform($v);
91 }
92 );
93 }
94
99 {
100 $clone = clone $this;
101 $clone->format = $format;
102 return $clone;
103 }
104
108 public function getFormat() : DateFormat\DateFormat
109 {
110 return $this->format;
111 }
112
113
114 public function withTimezone(string $tz) : C\Input\Field\DateTime
115 {
116 $timezone_trafo = $this->refinery->dateTime()->changeTimezone($tz);
117 $clone = clone $this;
118 $clone->timezone = $tz;
119
120 $trafo = $this->getOptionalNullTransformation($timezone_trafo);
124 $clone = $clone->withAdditionalTransformation($trafo);
125 return $clone;
126 }
127
131 public function getTimezone()
132 {
133 return $this->timezone;
134 }
135
139 public function withMinValue(\DateTimeImmutable $date) : C\Input\Field\DateTime
140 {
141 $clone = clone $this;
142 $clone->min_date = $date;
143 return $clone;
144 }
145
149 public function getMinValue()
150 {
151 return $this->min_date;
152 }
153
157 public function withMaxValue(\DateTimeImmutable $date) : C\Input\Field\DateTime
158 {
159 $clone = clone $this;
160 $clone->max_date = $date;
161 return $clone;
162 }
163
167 public function getMaxValue()
168 {
169 return $this->max_date;
170 }
171
175 public function withUseTime(bool $with_time) : C\Input\Field\DateTime
176 {
177 $clone = clone $this;
178 $clone->with_time = $with_time;
179 return $clone;
180 }
181
185 public function getUseTime() : bool
186 {
187 return $this->with_time;
188 }
189
193 public function withTimeOnly(bool $with_time_only) : C\Input\Field\DateTime
194 {
195 $clone = clone $this;
196 $clone->with_time_only = $with_time_only;
197 return $clone;
198 }
199
203 public function getTimeOnly() : bool
204 {
205 return $this->with_time_only;
206 }
207
211 protected function isClientSideValueOk($value) : bool
212 {
213 return is_string($value);
214 }
215
219 protected function getConstraintForRequirement()
220 {
221 return $this->refinery->string()->hasMinLength(1)
222 ->withProblemBuilder(function ($txt, $value) {
223 return $txt("datetime_required");
224 });
225 }
226
231 public function getAdditionalPickerconfig() : array
232 {
233 return $this->additional_picker_config;
234 }
235
242 {
243 $clone = clone $this;
244 $clone->additional_picker_config = array_merge($clone->additional_picker_config, $config);
245 return $clone;
246 }
247
251 public function getUpdateOnLoadCode() : \Closure
252 {
253 return function ($id) {
254 $code = "$('#$id').on('input dp.change', function(event) {
255 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
256 });
257 il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
258 return $code;
259 };
260 }
261}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
getAdditionalPickerconfig()
Get config to be passed to the bootstrap picker.
Definition: DateTime.php:231
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, $label, $byline)
Definition: DateTime.php:67
withAdditionalPickerconfig(array $config)
The bootstrap picker can be configured, e.g.
Definition: DateTime.php:241
$txt
Definition: error.php:13
This describes the datetime-field.
Definition: DateTime.php:14
withTimezone(string $tz)
Get an input like this using the given timezone.
Definition: DateTime.php:114
This describes commonalities between all inputs.
Definition: Input.php:32
Interface to be extended by components that have the possibility to bind to Javascript.
$format
Definition: metadata.php:218
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
trait ComponentHelper
Provides common functionality for component implementations.
Class ChatMainBarProvider \MainMenu\Provider.