ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
44 function base()
45 {
46  //Step 0: Declare dependencies
47  global $DIC;
48 
49  $ui = $DIC->ui()->factory();
50  $data = new \ILIAS\Data\Factory();
51  $renderer = $DIC->ui()->renderer();
52  $request = $DIC->http()->request();
53  $ctrl = $DIC->ctrl();
54 
55  //Step 1: define the inputs
56  $date = $ui->input()->field()->dateTime("Pick a date", "Pick any date you want. It will be shown in format YYYY-MM-DD");
57 
58  $date_now = new \DateTimeImmutable('now');
59  $formatted = $date
60  ->withMinValue($date_now)
61  ->withFormat($data->dateFormat()->germanShort())
62  ->withLabel('future only')
63  ->withByline('Only allows to pick a date in the future. It will be shown in format DD.MM.YYYY');
64 
65  $time = $date->withTimeOnly(true)
66  ->withLabel('time only')
67  ->withByline('Only pick a time. It will be shown in format HH:mm');
68 
69  $both = $date->withUseTime(true)
70  ->withLabel('both date and time')
71  ->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.');
72 
73  //setting a timezone will return a date with this timezone.
74  $tz = 'Asia/Tokyo';
75  $timezoned = $both->withTimezone($tz)
76  ->withValue('')
77  ->withLabel('to Tokyo time')
78  ->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.');
79 
80  //if you want a date converted to the timezone, do it on the date:
81  $date_now = new \DateTime('now');
82  $date_zoned = new \DateTime('now', new \DateTimeZone($tz));
83 
84  //here is the usage of Data/DateFormat
85  $format = $timezoned->getFormat()->toString() . ' H:i';
86  $timezoned_preset1 = $timezoned
87  ->withValue($date_now->format($format))
88  ->withLabel('to Tokyo time with local preset')
89  ->withByline('Local time+date is preset. However, output will be in Tokyo timezone');
90  $timezoned_preset2 = $timezoned
91  ->withValue($date_zoned->format($format))
92  ->withLabel('Tokyo time, both preset and output')
93  ->withByline('Tokyo time+date is preset. Output is also Tokyo time.');
94 
95  $disabled = $date
96  ->withValue($date_now->format($timezoned->getFormat()->toString()))
97  ->withDisabled(true)
98  ->withLabel('disabled')
99  ->withByline('You cannot pick anything, as the field is disabled');
100 
101  $required = $date->withRequired(true);
102 
103  //Step 2: define form and form actions
104  $form = $ui->input()->container()->form()->standard('#', [
105  'date' => $date,
106  'formatted' => $formatted,
107  'time_only' => $time,
108  'both_datetime' => $both,
109  'to_tokyotime' => $timezoned,
110  'tokyotime_local_preset' => $timezoned_preset1,
111  'tokyotime' => $timezoned_preset2,
112  'disabled' => $disabled,
113  'required' => $required
114  ]);
115 
116  //Step 3: implement some form data processing.
117  if ($request->getMethod() == "POST") {
118  $form = $form->withRequest($request);
119  $result = $form->getData();
120  } else {
121  $result = "No result yet.";
122  }
123 
124  //Step 4: Render the form.
125  return
126  "<pre>" . print_r($result, true) . "</pre><br/>" .
127  $renderer->render($form);
128 }
$renderer
base()
description: > Base example showing how to plug date-inputs into a form.
Definition: base.php:44
global $DIC
Definition: shib_login.php:25