ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
10 function base()
11 {
12  //Step 0: Declare dependencies
13  global $DIC;
14 
15  $ui = $DIC->ui()->factory();
16  $data = new \ILIAS\Data\Factory();
17  $renderer = $DIC->ui()->renderer();
18  $request = $DIC->http()->request();
19  $ctrl = $DIC->ctrl();
20 
21  //Step 1: define the inputs
22  $date = $ui->input()->field()->dateTime("Pick a date", "Pick any date you want. It will be shown in format YYYY-MM-DD");
23 
24  $date_now = new \DateTimeImmutable('now');
25  $formatted = $date
26  ->withMinValue($date_now)
27  ->withFormat($data->dateFormat()->germanShort())
28  ->withLabel('future only')
29  ->withByline('Only allows to pick a date in the future. It will be shown in format DD.MM.YYYY');
30 
31  $time = $date->withTimeOnly(true)
32  ->withLabel('time only')
33  ->withByline('Only pick a time. It will be shown in format HH:mm');
34 
35  $both = $date->withUseTime(true)
36  ->withLabel('both date and time')
37  ->withByline('Pick any date and time you want. It will be shown in format YYYY-MM-DD HH:mm and be saved for your local time zone.');
38 
39  //setting a timezone will return a date with this timezone.
40  $tz = 'Asia/Tokyo';
41  $timezoned = $both->withTimezone($tz)
42  ->withValue('')
43  ->withLabel('to Tokyo time')
44  ->withByline('Pick any date and time you want. It will be shown in format YYYY-MM-DD HH:mm and be saved for Tokyo time zone.');
45 
46  //if you want a date converted to the timezone, do it on the date:
47  $date_now = new \DateTime('now');
48  $date_zoned = new \DateTime('now', new \DateTimeZone($tz));
49 
50  //here is the usage of Data/DateFormat
51  $format = $timezoned->getFormat()->toString() . ' H:i';
52  $timezoned_preset1 = $timezoned
53  ->withValue($date_now->format($format))
54  ->withLabel('to Tokyo time with local preset')
55  ->withByline('Local time+date is preset. However, output will be in Tokyo timezone');
56  $timezoned_preset2 = $timezoned
57  ->withValue($date_zoned->format($format))
58  ->withLabel('Tokyo time, both preset and output')
59  ->withByline('Tokyo time+date is preset. Output is also Tokyo time.');
60 
61  $disabled = $date
62  ->withValue($date_now->format($timezoned->getFormat()->toString()))
63  ->withDisabled(true)
64  ->withLabel('disabled')
65  ->withByline('You cannot pick anything, as the field is disabled');
66 
67  $required = $date->withRequired(true);
68 
69  //Step 2: define form and form actions
70  $form = $ui->input()->container()->form()->standard('#', [
71  'date' => $date,
72  'formatted' => $formatted,
73  'time_only' => $time,
74  'both_datetime' => $both,
75  'to_tokyotime' => $timezoned,
76  'tokyotime_local_preset' => $timezoned_preset1,
77  'tokyotime' => $timezoned_preset2,
78  'disabled' => $disabled,
79  'required' => $required
80  ]);
81 
82  //Step 3: implement some form data processing.
83  if ($request->getMethod() == "POST") {
84  $form = $form->withRequest($request);
85  $result = $form->getData();
86  } else {
87  $result = "No result yet.";
88  }
89 
90  //Step 4: Render the form.
91  return
92  "<pre>" . print_r($result, true) . "</pre><br/>" .
93  $renderer->render($form);
94 }
base()
Base example showing how to plug date-inputs into a form.
Definition: base.php:10
global $DIC
Definition: feed.php:28