ILIAS  release_8 Revision v8.24
class.ilCombinationInputGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
27{
28 public const COMPARISON_NONE = 0;
29 public const COMPARISON_ASCENDING = 1;
30 public const COMPARISON_DESCENDING = 2;
31
32 protected array $items = array();
33 protected array $labels = [];
34 // BT 35500: default should be no comparison
36
37 public function __construct(
38 string $a_title = "",
39 string $a_postvar = ""
40 ) {
41 parent::__construct($a_title, $a_postvar);
42 global $DIC;
43 $this->lng = $DIC->language();
44 }
45
46 public function addCombinationItem(
47 string $id,
49 $label = ""
50 ): void {
51 $this->items[$id] = $item;
52 if ($label) {
53 $this->labels[$id] = $label;
54 }
55 }
56
57 public function getCombinationItem(string $id): ?ilFormPropertyGUI
58 {
59 if (isset($this->items[$id])) {
60 return $this->items[$id];
61 }
62 return null;
63 }
64
65 public function removeCombinationItem(string $id): void
66 {
67 if (isset($this->items[$id])) {
68 unset($this->items[$id]);
69 }
70 }
71
72 public function __call(
73 string $method,
74 array $param
75 ): array {
76 $result = array();
77 foreach ($this->items as $id => $obj) {
78 if (method_exists($obj, $method)) {
79 $result[$id] = call_user_func_array(array($obj, $method), $param);
80 }
81 }
82 return $result;
83 }
84
85 public function serializeData(): string
86 {
87 $result = array();
88 foreach ($this->items as $id => $obj) {
89 $result[$id] = $obj->serializeData();
90 }
91 return serialize($result);
92 }
93
94 public function unserializeData(string $a_data): void
95 {
96 $data = unserialize($a_data);
97
98 if ($data) {
99 foreach ($this->items as $id => $obj) {
100 $obj->unserializeData($data[$id]);
101 }
102 } else {
103 foreach ($this->items as $id => $obj) {
104 if (method_exists($obj, "setValue")) {
105 $this->setValue(null);
106 }
107 }
108 }
109 }
110
114 public function setComparisonMode(int $mode): bool
115 {
116 if (in_array($mode, array(self::COMPARISON_ASCENDING, self::COMPARISON_DESCENDING))) {
117 foreach ($this->items as $obj) {
118 if (!method_exists($obj, "getPostValueForComparison")) {
119 return false;
120 }
121 }
122 $this->comparison_mode = $mode;
123 return true;
124 }
125 return false;
126 }
127
128 public function setValue(?array $a_value): void
129 {
130 if (is_array($a_value)) {
131 foreach ($a_value as $id => $value) {
132 if (isset($this->items[$id])) {
133 if (method_exists($this->items[$id], "setValue")) {
134 // BT 35708: numeric inputs in table filters do not take floats as values
135 $value = is_float($value) ? (string) $value : $value;
136 $this->items[$id]->setValue($value);
137 }
138 // datetime
139 elseif (method_exists($this->items[$id], "setDate")) {
140 $this->items[$id]->setDate($value);
141 }
142 // duration
143 elseif (method_exists($this->items[$id], "setMonths")) {
144 $this->items[$id]->setMonths((int) ($value['MM'] ?? 0));
145 $this->items[$id]->setDays((int) ($value['dd'] ?? 0));
146 $this->items[$id]->setHours((int) ($value['hh'] ?? 0));
147 $this->items[$id]->setMinutes((int) ($value['mm'] ?? 0));
148 $this->items[$id]->setSeconds((int) ($value['ss'] ?? 0));
149 }
150 }
151 }
152 } elseif ($a_value === null) {
153 foreach ($this->items as $item) {
154 if (method_exists($item, "setValue")) {
155 $item->setValue(null);
156 }
157 // datetime
158 elseif (method_exists($item, "setDate")) {
159 $item->setDate();
160 }
161 // duration
162 elseif (method_exists($item, "setMonths")) {
163 $item->setMonths(0);
164 $item->setDays(0);
165 $item->setHours(0);
166 $item->setMinutes(0);
167 $item->setSeconds(0);
168 }
169 }
170 }
171 }
172
173 public function getValue(): ?array
174 {
175 $result = array();
176 foreach ($this->items as $id => $obj) {
177 if (method_exists($obj, "getValue")) {
178 $result[$id] = $obj->getValue();
179 }
180 // datetime
181 elseif (method_exists($obj, "setDate")) {
182 $result[$id] = $obj->getDate();
183 }
184 // duration
185 elseif (method_exists($obj, 'getValueAsArray')) {
186 $result[$id] = $obj->getValueAsArray();
187 }
188 }
189 return $result;
190 }
191
192 public function setValueByArray(array $a_values): void
193 {
194 foreach ($this->items as $obj) {
195 $obj->setValueByArray($a_values);
196 }
197 }
198
202 public function checkInput(): bool
203 {
204 if (sizeof($this->items)) {
205 foreach ($this->items as $obj) {
206 if (!$obj->checkInput()) {
207 return false;
208 }
209 }
210
211 if ($this->comparison_mode !== self::COMPARISON_NONE) {
212 $prev = null;
213 foreach ($this->items as $obj) {
214 $value = $obj->getPostValueForComparison();
215 if ($value != "") {
216 if ($prev !== null) {
217 if ($this->comparison_mode == self::COMPARISON_ASCENDING) {
218 if ($value < $prev) {
219 return false;
220 }
221 } else {
222 if ($value > $prev) {
223 return false;
224 }
225 }
226 }
227 $prev = $value;
228 }
229 }
230 }
231 }
232
233 return $this->checkSubItemsInput();
234 }
235
236 public function insert(ilTemplate $a_tpl): void
237 {
238 $html = $this->render();
239
240 $a_tpl->setCurrentBlock("prop_generic");
241 $a_tpl->setVariable("PROP_GENERIC", $html);
242 $a_tpl->parseCurrentBlock();
243 }
244
245 public function render(): string
246 {
247 $tpl = new ilTemplate("tpl.prop_combination.html", true, true, "Services/Form");
248
249 if (sizeof($this->items)) {
250 foreach ($this->items as $id => $obj) {
251 // label
252 if (isset($this->labels[$id])) {
253 $tpl->setCurrentBlock("prop_combination_label");
254 $tpl->setVariable("LABEL", $this->labels[$id]);
255 $tpl->parseCurrentBlock();
256 }
257
258 $tpl->setCurrentBlock("prop_combination");
259 $tpl->setVariable("FIELD", $obj->render());
260 $tpl->parseCurrentBlock();
261 }
262 }
263
264 return $tpl->get();
265 }
266
267 public function getTableFilterHTML(): string
268 {
269 $html = $this->render();
270 return $html;
271 }
272}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkInput()
Check input, strip slashes etc.
addCombinationItem(string $id, ilFormPropertyGUI $item, $label="")
__construct(string $a_title="", string $a_postvar="")
__call(string $method, array $param)
getTableFilterHTML()
Get input item HTML to be inserted into table filters.
setComparisonMode(int $mode)
Set mode for comparison (extended validation)
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$param
Definition: xapitoken.php:46