ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
10 function base()
11 {
12 
13  //Step 0: Declare dependencies
14  global $DIC;
15 
16  $ui = $DIC->ui()->factory();
17  $data = new \ILIAS\Data\Factory();
18  $renderer = $DIC->ui()->renderer();
19  $request = $DIC->http()->request();
20  $ctrl = $DIC->ctrl();
21 
22  //Step 1: define the inputs
23  $date = $ui->input()->field()->dateTime("Pick a date/time", "Pick any date you want. It will be shown in format YYYY-MM-DD");
24 
25  $formatted = $date
26  ->withMinValue(new \DateTimeImmutable())
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($format))
63  ->withDisabled(true)
64  ->withLabel('disabled')
65  ->withByline('You cannot pick anything, as the field is disabled');
66 
67  //Step 2: define form and form actions
68  $form = $ui->input()->container()->form()->standard('#', [
69  'date' => $date,
70  'formatted' => $formatted,
71  'time_only' => $time,
72  'both_datetime' => $both,
73  'to_tokyotime' => $timezoned,
74  'tokyotime_local_preset' => $timezoned_preset1,
75  'tokyotime' => $timezoned_preset2,
76  'disabled' => $disabled
77  ]);
78 
79  //Step 3: implement some form data processing.
80  if ($request->getMethod() == "POST") {
81  $form = $form->withRequest($request);
82  $result = $form->getData();
83  } else {
84  $result = "No result yet.";
85  }
86 
87  //Step 4: Render the form.
88  return
89  "<pre>" . print_r($result, true) . "</pre><br/>" .
90  $renderer->render($form);
91 }
base()
Base example showing how to plug date-inputs into a form.
Definition: base.php:10
global $DIC
Definition: feed.php:28
$format
Definition: metadata.php:235