ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.TableCommandActionHandler.php
Go to the documentation of this file.
1<?php
2
20
23
27class TableCommandActionHandler implements Server\CommandActionHandler
28{
29 protected \ILIAS\DI\UIServices $ui;
30 protected \ilLanguage $lng;
31 protected \ilPageObjectGUI $page_gui;
32 protected \ilObjUser $user;
33 protected Server\UIWrapper $ui_wrapper;
34
36 {
37 global $DIC;
38
39 $this->ui = $DIC->ui();
40 $this->lng = $DIC->language();
41 $this->page_gui = $page_gui;
42 $this->user = $DIC->user();
43
44 $this->ui_wrapper = new Server\UIWrapper($this->ui, $this->lng);
45 }
46
47 public function handle(array $query, array $body): Server\Response
48 {
49 switch ($body["action"]) {
50 case "insert":
51 return $this->insertCommand($body);
52
53 case "update.data":
54 return $this->updateDataCommand($body);
55
56 case "modify.table":
57 return $this->modifyTableCommand($body);
58
59 case "update":
60 return $this->updateCommand($body);
61
62 case "set.properties":
63 return $this->setCellProperties($body);
64
65 case "toggle.merge":
66 return $this->toggleMerge($body);
67
68 default:
69 throw new Exception("Unknown action " . $body["action"]);
70 }
71 }
72
73 protected function insertCommand(array $body): Server\Response
74 {
75 if (($body["import"] ?? "") === "1") {
76 return $this->importSpreadsheet($body);
77 }
78
79 $page = $this->page_gui->getPageObject();
80
81 $hier_id = "pg";
82 $pc_id = "";
83 if (!in_array($body["after_pcid"], ["", "pg"])) {
84 $hier_ids = $page->getHierIdsForPCIds([$body["after_pcid"]]);
85 $hier_id = $hier_ids[$body["after_pcid"]];
86 $pc_id = $body["after_pcid"];
87 }
88
89 $tab = new \ilPCDataTable($page);
90 $tab->create($page, $hier_id, $pc_id);
91 $lang = $this->user->getLanguage();
92 if ($lang === "") {
93 $lang = "en";
94 }
95 $tab->setLanguage($lang);
96
97
98 $tab->addRows(
99 (int) ($body["nr_rows"] ?? 1),
100 (int) ($body["nr_cols"] ?? 1)
101 );
102
103
104 $this->setRowHeaderAndCharacteristic($tab, $body);
105
106 $updated = $page->update();
107
108 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
109 }
110
111 protected function setRowHeaderAndCharacteristic(\ilPCTable $tab, array $body): void
112 {
113 if ($body["has_row_header"] ?? false) {
114 $tab->setHeaderRows(1);
115 }
116 $characteristic = ($body["characteristic"] ?? "");
117 if ($characteristic === "" && isset($body["import_characteristic"])) {
118 $characteristic = $body["import_characteristic"];
119 }
120 if ($characteristic === "") {
121 $characteristic = "StandardTable";
122 }
123 if (strpos($characteristic, ":") > 0) {
124 $t = explode(":", $characteristic);
125 $tab->setTemplate($t[2]);
126 $tab->setClass("");
127 } else {
128 $tab->setClass($characteristic);
129 $tab->setTemplate("");
130 }
131 }
132
133 protected function importSpreadsheet(array $body): Server\Response
134 {
135 $page = $this->page_gui->getPageObject();
136
137 $hier_id = "pg";
138 $pc_id = "";
139 if (!in_array($body["after_pcid"], ["", "pg"])) {
140 $hier_ids = $page->getHierIdsForPCIds([$body["after_pcid"]]);
141 $hier_id = $hier_ids[$body["after_pcid"]];
142 $pc_id = $body["after_pcid"];
143 }
144
145 $tab = new \ilPCDataTable($page);
146 $tab->create($page, $hier_id, $pc_id);
147 $tab->setLanguage($this->user->getLanguage());
148
149 $this->setRowHeaderAndCharacteristic($tab, $body);
150
151 $table_data = $body["import_table"] ?? "";
152
153 $tab->importSpreadsheet($this->user->getLanguage(), trim($table_data));
154
155 $updated = $page->update();
156
157 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
158 }
159
160 protected function updateDataCommand(array $body): Server\Response
161 {
162 $updated = $this->updateData($body["data"]["pcid"], $body["data"]["content"]);
163 if ($body["data"]["redirect"]) {
164 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
165 } else {
166 return $this->sendUpdateResponse($this->page_gui, $updated, $body["data"]["pcid"]);
167 }
168 }
169
174 public function sendUpdateResponse(
176 $updated,
177 string $pcid
178 ): Server\Response {
179 $error = null;
180
181 $last_change = null;
182 if ($updated !== true) {
183 if (is_array($updated)) {
184 $error = implode("<br />", $updated);
185 } elseif (is_string($updated)) {
186 $error = $updated;
187 } else {
188 $error = print_r($updated, true);
189 }
190 } else {
191 $last_change = $page_gui->getPageObject()->getLastChange();
192 }
193
194 $data = new \stdClass();
195 $data->error = $error;
196 if ($last_change) {
197 $lu = new \ilDateTime($last_change, IL_CAL_DATETIME);
199 $data->last_update = \ilDatePresentation::formatDate($lu, true);
200 }
201
202 return new Server\Response($data);
203 }
204
205
210 protected function updateData(
211 string $pcid,
212 array $content
213 ) {
214 $page = $this->page_gui->getPageObject();
215 $table = $page->getContentObjectForPcId($pcid);
216
217 $data = [];
218 $updated = true;
219 foreach ($content as $i => $row) {
220 if (is_array($row)) {
221 foreach ($row as $j => $cell) {
222 $text = "<div>" . $cell . "</div>";
223 if ($updated) {
224 // determine cell content
226 $data[$i][$j] = $text;
227 $updated = (!is_null($text));
228 $text = $text["text"];
229 }
230 if ($updated) {
232 $text,
233 $table->getLanguage(),
234 true,
235 false
236 );
238
239 $data[$i][$j] = $text;
240 }
241 }
242 }
243 }
244
245 if ($updated) {
246 $table->setData($data);
247 $updated = $page->update();
248 }
249
250 return $updated;
251 }
252
253
254 protected function modifyTableCommand(array $body): Server\Response
255 {
256 $page = $this->page_gui->getPageObject();
257 $page->addHierIDs();
258
260 $table = $page->getContentObjectForPcId($body["data"]["tablePcid"]);
261
262
263 if ($table->getType() == "dtab" && $body["data"]["modification"] !== "none") {
264 $this->updateData($body["data"]["tablePcid"], $body["data"]["content"]);
265 }
266
267 $page->addHierIDs();
268
269
271 if ($body["data"]["modification"] !== "none") {
272 $td = $page->getContentObjectForPcId($body["data"]["cellPcid"]);
273 }
274
275 $cnt = $body["data"]["cnt"] ?? 1;
276 switch ($body["data"]["modification"]) {
277 case "col.before":
278 $td->newColBefore($cnt);
279 break;
280 case "col.after":
281 $td->newColAfter($cnt);
282 break;
283 case "col.left":
284 $td->moveColLeft();
285 break;
286 case "col.right":
287 $td->moveColRight();
288 break;
289 case "col.delete":
290 $td->deleteCol();
291 break;
292 case "row.before":
293 $td->newRowBefore($cnt);
294 break;
295 case "row.after":
296 $td->newRowAfter($cnt);
297 break;
298 case "row.up":
299 $td->moveRowUp();
300 break;
301 case "row.down":
302 $td->moveRowDown();
303 break;
304 case "row.delete":
305 $td->deleteRow();
306 break;
307 case "none":
308 break;
309 }
310
311 $page->update();
312
313 return $this->sendTable($this->page_gui, $body["data"]["tablePcid"]);
314 }
315
319 public function sendTable(
320 \ilPageObjectGUI $page_gui,
321 string $pcid
322 ): Server\Response {
323 $page = $page_gui->getPageObject();
324 $page->addHierIDs();
325 $table = $page->getContentObjectForPcId($pcid);
326 if ($table->getType() == "dtab") {
327 $table_gui = new \ilPCDataTableGUI(
328 $page_gui->getPageObject(),
329 $table,
330 $page->getHierIdForPcId($pcid),
331 $pcid
332 );
333 } else {
334 $table_gui = new \ilPCTableGUI(
335 $page_gui->getPageObject(),
336 $table,
337 $page->getHierIdForPcId($pcid),
338 $pcid
339 );
340 }
341 $table_gui->setStyleId($page_gui->getStyleId());
342 $data = new \stdClass();
343 $data->renderedContent = $table_gui->getEditDataTable();
344 $data->pcModel = $page_gui->getPageObject()->getPCModel();
345 return new Server\Response($data);
346 }
347
348 protected function updateCommand(array $body): Server\Response
349 {
350 $page = $this->page_gui->getPageObject();
351
353 $tab = $page->getContentObjectForPcId($body["pcid"]);
354
355 $this->setRowHeaderAndCharacteristic($tab, $body);
356
357 $header_row = (bool) ($body["has_row_header"] ?? false);
358 if ($tab->getHeaderRows() === 0 && $header_row) {
359 $tab->setHeaderRows(1);
360 }
361 if ($tab->getHeaderRows() > 0 && !$header_row) {
362 $tab->setHeaderRows(0);
363 }
364
365 $updated = $page->update();
366 return $this->ui_wrapper->sendPage($this->page_gui, $updated);
367 }
368
369 protected function setCellProperties(array $body): Server\Response
370 {
371 $page = $this->page_gui->getPageObject();
372
374 $tab = $page->getContentObjectForPcId($body["pcid"]);
375 $top = (int) ($body["top"] ?? -1);
376 $bottom = (int) ($body["bottom"] ?? -1);
377 $left = (int) ($body["left"] ?? -1);
378 $right = (int) ($body["right"] ?? -1);
379 if ($top !== -1 && $bottom !== -1 && $left !== -1 && $right !== -1) {
380 for ($i = $top; $i <= $bottom; $i++) {
381 for ($j = $left; $j <= $right; $j++) {
382 $td_node = $tab->getTableDataNode($i, $j);
383 if ($td_node) {
384 // set class
385 if (isset($body["style_cb"])) {
386 $class = $body["style"] ?? "";
387 if ($class === "") {
388 $td_node->removeAttribute("Class");
389 } else {
390 $td_node->setAttribute("Class", $class);
391 }
392 }
393 // set width
394 if (isset($body["width_cb"])) {
395 $width = $body["width"] ?? "";
396 if ($width === "") {
397 $td_node->removeAttribute("Width");
398 } else {
399 $td_node->setAttribute("Width", $width);
400 }
401 }
402 // set alignment
403 if (isset($body["al_cb"])) {
404 $alignment = $body["alignment"] ?? "";
405 if ($alignment === "") {
406 $td_node->removeAttribute("HorizontalAlign");
407 } else {
408 $td_node->setAttribute("HorizontalAlign", $alignment);
409 }
410 }
411
412 }
413 }
414 }
415 }
416 $updated = $page->update();
417 return $this->sendTable($this->page_gui, $body["pcid"]);
418 }
419
420 protected function toggleMerge(array $body): Server\Response
421 {
422 $page = $this->page_gui->getPageObject();
423
424 $data = $body["data"];
425
427 $tab = $page->getContentObjectForPcId($data["pcid"]);
428 $top = (int) ($data["top"] ?? -1);
429 $bottom = (int) ($data["bottom"] ?? -1);
430 $left = (int) ($data["left"] ?? -1);
431 $right = (int) ($data["right"] ?? -1);
432
433 $td_node = $tab->getTableDataNode($top, $left);
434 $td_node->setAttribute("ColSpan", $right - $left + 1);
435 $td_node->setAttribute("RowSpan", $bottom - $top + 1);
436
437 $tab->fixHideAndSpans();
438
439 $updated = $page->update();
440 return $this->sendTable($this->page_gui, $data["pcid"]);
441 }
442}
Page editor json server.
sendTable(\ilPageObjectGUI $page_gui, string $pcid)
Send whole table as response.
sendUpdateResponse(\ilPageObjectGUI $page_gui, $updated, string $pcid)
ilErrorHandling $error
Definition: class.ilias.php:69
const IL_CAL_DATETIME
static setUseRelativeDates(bool $a_status)
set use relative dates
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
static handleAjaxContentPost(string $text)
Post input2xml handling of ajax content.
static _input2xml(string $a_text, string $a_lang, bool $a_wysiwyg=false, bool $a_handle_lists=true)
Converts user input to xml User input comes as bb code information, e.g.
static handleAjaxContent(string $a_content)
Handle ajax content.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setTemplate(string $a_template)
setClass(string $a_class)
Set Style Class of table.
setHeaderRows(int $a_nr)
Class ilPageObjectGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
$lang
Definition: xapiexit.php:25