ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DateTime.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\UI\Component as C;
31 use Closure;
32 
36 class DateTime extends FormInput implements C\Input\Field\DateTime
37 {
38  use ComponentHelper;
40 
41  public const TIME_FORMAT = 'HH:mm';
42 
43  protected DateFormat $format;
44  protected ?DateTimeImmutable $min_date = null;
45  protected ?DateTimeImmutable $max_date = null;
46  protected bool $with_time = false;
47  protected bool $with_time_only = false;
48  protected ?string $timezone = null;
49 
53  protected array $additional_picker_config = [];
54 
55  public function __construct(
56  DataFactory $data_factory,
58  string $label,
59  ?string $byline
60  ) {
61  parent::__construct($data_factory, $refinery, $label, $byline);
62 
63  $this->format = $data_factory->dateFormat()->standard();
64 
65  $datetime_trafo = $refinery->to()->dateTime();
66  $trafo = $this->getOptionalNullTransformation($datetime_trafo);
67  $this->setAdditionalTransformation($trafo);
68  }
69 
71  {
72  return $this->refinery->custom()->transformation(
73  function ($v) use ($or_trafo) {
74  if (!$v) {
75  return null;
76  }
77  return $or_trafo->transform($v);
78  }
79  );
80  }
81 
87  public function withValue($value)
88  {
89  // TODO: It would be a lot nicer if the value would be held as DateTimeImmutable
90  // internally, but currently this is just to much. Added to the roadmap.
91  if ($value instanceof \DateTimeImmutable) {
92  $value = $this->format->applyTo($value);
93  }
94 
95  $clone = clone $this;
96  $clone->value = $value;
97  return $clone;
98  }
99 
100  public function withFormat(DateFormat $format): C\Input\Field\DateTime
101  {
102  $clone = clone $this;
103  $clone->format = $format;
104  return $clone;
105  }
106 
107  public function getFormat(): DateFormat
108  {
109  return $this->format;
110  }
111 
112 
113  public function withTimezone(string $tz): C\Input\Field\DateTime
114  {
115  $timezone_trafo = $this->refinery->dateTime()->changeTimezone($tz);
116  $clone = clone $this;
117  $clone->timezone = $tz;
118 
119  $trafo = $this->getOptionalNullTransformation($timezone_trafo);
120  $clone = $clone->withAdditionalTransformation($trafo);
121  return $clone;
122  }
123 
124  public function getTimezone(): ?string
125  {
126  return $this->timezone;
127  }
128 
129  public function withMinValue(DateTimeImmutable $datetime): C\Input\Field\DateTime
130  {
131  $clone = clone $this;
132  $clone->min_date = $datetime;
133  return $clone;
134  }
135 
136  public function getMinValue(): ?DateTimeImmutable
137  {
138  return $this->min_date;
139  }
140 
141  public function withMaxValue(DateTimeImmutable $datetime): C\Input\Field\DateTime
142  {
143  $clone = clone $this;
144  $clone->max_date = $datetime;
145  return $clone;
146  }
147 
148  public function getMaxValue(): ?DateTimeImmutable
149  {
150  return $this->max_date;
151  }
152 
153  public function withUseTime(bool $with_time): C\Input\Field\DateTime
154  {
155  $clone = clone $this;
156  $clone->with_time = $with_time;
157  return $clone;
158  }
159 
160  public function getUseTime(): bool
161  {
162  return $this->with_time;
163  }
164 
165  public function withTimeOnly(bool $time_only): C\Input\Field\DateTime
166  {
167  $clone = clone $this;
168  $clone->with_time_only = $time_only;
169  return $clone;
170  }
171 
172  public function getTimeOnly(): bool
173  {
174  return $this->with_time_only;
175  }
176 
177  protected function isClientSideValueOk($value): bool
178  {
179  return is_string($value);
180  }
181 
183  {
184  if ($this->requirement_constraint !== null) {
185  return $this->requirement_constraint;
186  }
187 
188  return $this->refinery->string()->hasMinLength(1)
189  ->withProblemBuilder(fn ($txt, $value) => $txt("datetime_required"));
190  }
191 
196  public function getAdditionalPickerconfig(): array
197  {
199  }
200 
205  public function withAdditionalPickerconfig(array $config): C\Input\Field\DateTime
206  {
207  $clone = clone $this;
208  $clone->additional_picker_config = array_merge($clone->additional_picker_config, $config);
209  return $clone;
210  }
211 
212  public function getUpdateOnLoadCode(): Closure
213  {
214  return fn ($id) => "$('#$id').on('input dp.change', function(event) {
215  il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());
216  });
217  il.UI.input.onFieldUpdate(event, '$id', $('#$id').find('input').val());";
218  }
219 }
withValue($value)
Get an input like this with another value displayed on the client side.if value does not fit client s...
Definition: DateTime.php:87
Transform values according to custom configuration.
getAdditionalPickerconfig()
Get config to be passed to the bootstrap picker.
Definition: DateTime.php:196
Class ChatMainBarProvider .
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
__construct(DataFactory $data_factory, \ILIAS\Refinery\Factory $refinery, string $label, ?string $byline)
Definition: DateTime.php:55
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
A Date Format provides a format definition akin to PHP&#39;s date formatting options, but stores the sing...
Definition: DateFormat.php:26
getOptionalNullTransformation(\ILIAS\Refinery\Transformation $or_trafo)
Definition: DateTime.php:70
$txt
Definition: error.php:13
__construct(Container $dic, ilPlugin $plugin)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This describes inputs that can be used in forms.
Definition: FormInput.php:31
withAdditionalPickerconfig(array $config)
The bootstrap picker can be configured, e.g.
Definition: DateTime.php:205
Refinery Factory $refinery