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