ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilScheduleInputGUI.php
Go to the documentation of this file.
1<?php
2
25{
26 protected \ILIAS\DI\UIServices $ui;
28 protected array $value = [];
29 protected string $validationFailureMessage;
30
31 public function __construct(
32 string $a_title = "",
33 string $a_postvar = ""
34 ) {
35 global $DIC;
36
37 $this->lng = $DIC->language();
38 $this->tpl = $DIC["tpl"];
39 parent::__construct($a_title, $a_postvar);
40 $this->ui = $DIC->ui();
41 }
42
43 public function setValue(array $a_value): void
44 {
45 $this->value = $a_value;
46 }
47
48 public function getValue(): array
49 {
50 return $this->value;
51 }
52
53 public function setValidationFailureMessage(string $a_msg): void
54 {
55 $this->validationFailureMessage = $a_msg;
56 }
57
58 public function getValidationFailureMessage(): string
59 {
61 }
62
68 public function setValueByArray(array $a_values): void
69 {
70 $this->setValue($this->getPostData($this->getPostVar(), false));
71 }
72
73 public function checkInput(): bool
74 {
76 $data = $this->getPostData($this->getPostVar(), false);
77 if (count($data)) {
78 // slots may not overlap
79 foreach ($data as $slot => $days) {
80 if (!$days) {
81 $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
82 return false;
83 }
84
85 $parts = explode("-", $slot);
86 $from = str_replace(":", "", $parts[0]);
87 $to = str_replace(":", "", $parts[1]);
88 if ($from >= $to) {
89 $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
90 return false;
91 }
92
93 foreach ($data as $rslot => $rdays) {
94 if ($slot != $rslot && $rdays && array_intersect($days, $rdays)) {
95 $rparts = explode("-", $rslot);
96 $rfrom = str_replace(":", "", $rparts[0]);
97 $rto = str_replace(":", "", $rparts[1]);
98
99 if (($rfrom > $from && $rfrom < $to) ||
100 ($rto > $from && $rto < $to) ||
101 ($rfrom < $from && $rto > $to)) {
102 $this->setAlert($lng->txt("msg_input_does_not_match_regexp"));
103 return false;
104 }
105 }
106 }
107 }
108
109 return true;
110 }
111
112 if ($this->getRequired()) {
113 $this->setAlert($lng->txt("msg_input_is_required"));
114 return false;
115 }
116
117 return true;
118 }
119
120 public function getInput(): array
121 {
122 return $this->getPostData($this->getPostVar());
123 }
124
125 public function getPostData(
126 string $a_post_var,
127 $a_remove_invalid = true
128 ): array {
129 $res = array();
130 for ($loop = 0; $loop < 240; $loop++) {
131 $days = $this->strArray($a_post_var . "_days~" . $loop);
132 $from = self::parseTime(
133 $this->str($a_post_var . "_from_hh~" . $loop),
134 $this->str($a_post_var . "_from_mm~" . $loop)
135 );
136 $to = self::parseTime(
137 $this->str($a_post_var . "_to_hh~" . $loop),
138 $this->str($a_post_var . "_to_mm~" . $loop)
139 );
140
141 // only if any part was edited (js based gui)
142 if ($days || $from !== "00:00" || $to !== "00:00") {
143 $slot = $from . "-" . $to;
144 if ($days) {
145 if (isset($res[$slot])) {
146 $res[$slot] = array_unique(array_merge($res[$slot], $days));
147 } else {
148 $res[$slot] = $days;
149 }
150 } else {
151 $res[$slot] = array();
152 }
153
154 if ($a_remove_invalid && !($days && $from && $to && $from !== $to)) {
155 unset($res[$slot]);
156 }
157 }
158 }
159 return $res;
160 }
161
162 protected function render(
163 string $a_mode = ""
164 ): string {
165 $lng = $this->lng;
166
167 $tpl = new ilTemplate("tpl.schedule_input.html", true, true, "components/ILIAS/BookingManager");
168
169 $lng->loadLanguageModule("dateplaner");
170
171 $def = $this->getValue();
172 if (!$def) {
173 $def = array(null => null);
174 }
175
176 $days = array("Mo", "Tu", "We", "Th", "Fr", "Sa", "Su");
177 $row = 0;
178 foreach ($def as $slot => $days_select) {
179 $tpl->setCurrentBlock("days");
180 foreach ($days as $day) {
181 $day_value = strtolower($day);
182
183 $tpl->setVariable("ROW", $row);
184 $tpl->setVariable("ID", $this->getFieldId());
185 $tpl->setVariable("POST_VAR", $this->getPostVar());
186 $tpl->setVariable("DAY", $day_value);
187 $tpl->setVariable("TXT_DAY", $lng->txt($day . "_short"));
188
189 if ($days_select && in_array($day_value, $days_select, true)) {
190 $tpl->setVariable("DAY_STATUS", " checked=\"checked\"");
191 }
192
193 $tpl->parseCurrentBlock();
194 }
195
196 $add_gl = $this->ui->factory()->symbol()->glyph()->add();
197 $rem_gl = $this->ui->factory()->symbol()->glyph()->remove();
198 $r = $this->ui->renderer();
199 $tpl->setCurrentBlock("row");
200 $tpl->setVariable("ROW", $row);
201 $tpl->setVariable("ID", $this->getFieldId());
202 $tpl->setVariable("POST_VAR", $this->getPostVar());
203 $tpl->setVariable("TXT_FROM", $lng->txt("cal_from"));
204 $tpl->setVariable("TXT_TO", $lng->txt("cal_until"));
205 $tpl->setVariable("IMG_MULTI_ADD", $r->render($add_gl));
206 $tpl->setVariable("IMG_MULTI_REMOVE", $r->render($rem_gl));
207 $tpl->setVariable("TXT_MULTI_ADD", $lng->txt("add"));
208 $tpl->setVariable("TXT_MULTI_REMOVE", $lng->txt("remove"));
209
210 if ($slot) {
211 $parts = explode("-", $slot);
212 $from = explode(":", $parts[0]);
213 $to = explode(":", $parts[1]);
214
215 $tpl->setVariable("FROM_HH_VALUE", $from[0]);
216 $tpl->setVariable("FROM_MM_VALUE", $from[1]);
217 $tpl->setVariable("TO_HH_VALUE", $to[0]);
218 $tpl->setVariable("TO_MM_VALUE", $to[1]);
219 }
220
221 // manage hidden buttons
222 if ($row > 0) {
223 // inline needed because of JS
224 $tpl->setVariable("ADD_STYLE", " style=\"display:none\"");
225 // $tpl->setVariable("ADD_CLASS", "ilNoDisplay");
226 } else {
227 // inline needed because of JS
228 $tpl->setVariable("RMV_STYLE", " style=\"display:none\"");
229 // $tpl->setVariable("RMV_CLASS", "ilNoDisplay");
230 }
231
232 $tpl->parseCurrentBlock();
233
234 $row++;
235 }
236
237 return $tpl->get();
238 }
239
240 public function insert(ilTemplate $a_tpl): void
241 {
242 $tpl = $this->tpl;
243
244 $tpl->addJavascript("assets/js/ScheduleInput.js");
245
246 $html = $this->render();
247
248 $a_tpl->setCurrentBlock("prop_generic");
249 $a_tpl->setVariable("PROP_GENERIC", $html);
250 $a_tpl->parseCurrentBlock();
251 }
252
256 protected static function parseTime(
257 string $a_hours,
258 string $a_minutes
259 ): string {
260 $hours = (int) $a_hours;
261 $min = (int) $a_minutes;
262 if ($hours > 23) {
263 $hours = 23;
264 $min = 59;
265 }
266 if ($min > 59) {
267 $min = 59;
268 }
269 return str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" .
270 str_pad($min, 2, "0", STR_PAD_LEFT);
271 }
272}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
This class represents a property in a property form.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilGlobalTemplateInterface $tpl
checkInput()
Check input, strip slashes etc.
setValueByArray(array $a_values)
Set value by array.
setValidationFailureMessage(string $a_msg)
__construct(string $a_title="", string $a_postvar="")
static parseTime(string $a_hours, string $a_minutes)
Parse/normalize incoming time values.
getPostData(string $a_post_var, $a_remove_invalid=true)
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setVariable(string $variable, $value='')
Sets the given variable to the given value.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
$res
Definition: ltiservices.php:69
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26