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