ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPCDataTableGUI.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected \ILIAS\HTTP\Services $http;
28 
29  public function __construct(
30  ilPageObject $a_pg_obj,
31  ?ilPageContent $a_content_obj,
32  string $a_hier_id,
33  string $a_pc_id = ""
34  ) {
35  global $DIC;
36 
37  $this->main_tpl = $DIC->ui()->mainTemplate();
38  $this->lng = $DIC->language();
39  $this->ctrl = $DIC->ctrl();
40  $this->tabs = $DIC->tabs();
41  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
42  $this->setCharacteristics(array("StandardTable" => $this->lng->txt("cont_StandardTable")));
43  $this->tool_context = $DIC->globalScreen()->tool()->context();
44  $this->http = $DIC->http();
45  }
46 
47  protected function getFormTitle(string $a_mode = "edit"): string
48  {
49  if ($a_mode === "create") {
50  return $this->lng->txt("cont_ed_insert_dtab");
51  }
52  return $this->lng->txt("cont_table_properties");
53  }
54 
59  public function executeCommand()
60  {
61  $this->getCharacteristicsOfCurrentStyle(["table"]); // scorm-2004
62 
63  // get next class that processes or forwards current command
64  $next_class = $this->ctrl->getNextClass($this);
65 
66  // get current command
67  $cmd = $this->ctrl->getCmd();
68 
69  switch ($next_class) {
70  default:
71  $ret = $this->$cmd();
72  break;
73  }
74 
75  return $ret;
76  }
77 
78 
82 
86  public function update(bool $a_redirect = true): void
87  {
88  $lng = $this->lng;
89 
90  // handle input data
91  $data = array();
92  $cell = $this->request->getArrayArray("cell");
93  if (is_array($cell)) {
94  foreach ($cell as $i => $row) {
95  if (is_array($row)) {
96  foreach ($row as $j => $cell) {
97  $data[$i][$j] =
99  $cell,
100  $this->content_obj->getLanguage()
101  );
102  }
103  }
104  }
105  }
106 
107  $this->updated = $this->content_obj->setData($data);
108 
109  if ($this->updated !== true) {
110  $this->editData();
111  return;
112  }
113 
114  $this->updated = $this->pg_obj->update();
115 
116  if ($a_redirect) {
117  $this->main_tpl->setOnScreenMessage('success', $lng->txt("msg_obj_modified"), true);
118  $this->ctrl->redirect($this, "editData");
119  }
120  }
121 
125  public function updateJS(): void
126  {
127  $lng = $this->lng;
128  $ilCtrl = $this->ctrl;
129 
130  if ($this->request->getString("cancel_update") != "") {
131  // $this->ctrl->redirect($this, "editData");
132  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
133  }
134 
135  // handle input data
136  $data = array();
137  $post = $this->http->request()->getParsedBody();
138  foreach ($post as $k => $content) {
139  if (substr($k, 0, 5) != "cell_") {
140  continue;
141  }
142 
143  // determine cell content
144  $div = ilUtil::stripSlashes($content, false);
145  $p1 = strpos($div, '>');
146  $div = substr($div, $p1 + 1);
147  $div = "<div class='ilc_text_block_TableContent'>" . $div;
148  $text = ilPCParagraph::handleAjaxContent($div);
149  if ($text === false) {
150  $ilCtrl->returnToParent($this, "jump" . $this->hier_id);
151  }
152  $text = $text["text"];
153 
155  $text,
156  $this->content_obj->getLanguage(),
157  true,
158  false
159  );
161 
162  // set content in data array
163  $id = explode("_", $k);
164  $data[(int) $id[1]][(int) $id[2]] = $text;
165  }
166 
167  // update data
168  $this->updated = $this->content_obj->setData($data);
169 
170  if ($this->updated !== true) {
171  $this->editData();
172  return;
173  }
174 
175  $this->updated = $this->pg_obj->update();
176 
177  $tab_cmd_id = $this->request->getInt("tab_cmd_id");
178  $tab_cmd_type = $this->request->getString("tab_cmd_type");
179  $tab_cmd = $this->request->getString("tab_cmd");
180 
181  // perform table action? (move...?)
182  //$this->update(false);
183  $this->pg_obj->addHierIDs();
184  $failed = false;
185  if ($tab_cmd != "") {
186  $cell_hier_id = ($tab_cmd_type == "col")
187  ? $this->hier_id . "_1_" . ($tab_cmd_id + 1)
188  : $this->hier_id . "_" . ($tab_cmd_id + 1) . "_1";
189  $cell_obj = $this->pg_obj->getContentObject($cell_hier_id);
190  if (is_object($cell_obj)) {
191  $cell_obj->$tab_cmd();
192  $ret = $this->pg_obj->update();
193  if ($ret !== true) {
194  $this->main_tpl->setOnScreenMessage('failure', $ret[0][1], true);
195  $failed = true;
196  }
197  }
198  }
199 
200  if (!$failed) {
201  $this->main_tpl->setOnScreenMessage('success', $lng->txt("msg_obj_modified"), true);
202  }
203  if ($this->request->getString("save_return") != "") {
204  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
205  } else {
206  $this->ctrl->redirect($this, "editData");
207  }
208  }
209 
210 
214  public function getNewTableObject(): ilPCDataTable
215  {
216  return new ilPCDataTable($this->getPage());
217  }
218 
222  public function afterCreation(): void
223  {
224  $ilCtrl = $this->ctrl;
225 
226  $this->pg_obj->stripHierIDs();
227  $this->pg_obj->addHierIDs();
228  $ilCtrl->setParameter($this, "hier_id", $this->content_obj->readHierId());
229  $ilCtrl->setParameter($this, "pc_id", $this->content_obj->readPCId());
230  $ilCtrl->redirect($this, "editData");
231  }
232 
236  public function tableAction(): void
237  {
238  $ilCtrl = $this->ctrl;
239 
240  $this->update(false);
241  $this->pg_obj->addHierIDs();
242 
243  $type = $this->request->getString("type");
244  $action = $this->request->getString("action");
245  $id = $this->request->getInt("id");
246 
247  $cell_hier_id = ($type == "col")
248  ? $this->hier_id . "_1_" . ($id + 1)
249  : $this->hier_id . "_" . ($id + 1) . "_1";
250  $cell_obj = $this->pg_obj->getContentObject($cell_hier_id);
251  if (is_object($cell_obj)) {
252  $cell_obj->$action();
253  $this->edit_repo->setPageError($this->pg_obj->update());
254  }
255  $ilCtrl->redirect($this, "editData");
256  }
257 
261  public function setTabs(string $data_tab_txt_key = ""): void
262  {
263  parent::setTabs("cont_ed_edit_data");
264  }
265 
266  protected function getCellContent(int $i, int $j): string
267  {
268  $cmd = $this->ctrl->getCmd();
269  if ($cmd == "update") {
270  $s_text = ilUtil::stripSlashes("cell_" . $i . "_" . $j, false);
271  } else {
272  $s_text = ilPCParagraph::xml2output(
273  $this->content_obj->getCellText($i, $j),
274  true,
275  false
276  );
278  $s_text,
279  "TableContent",
280  $this->content_obj->readPCId() . "_" . $i . "_" . $j
281  );
282  }
283 
284  // #20628
285  $s_text = str_replace("{", "&#123;", $s_text);
286  $s_text = str_replace("}", "&#125;", $s_text);
287  return $s_text;
288  }
289 
290  public function initCreationForm(
291  ): ilPropertyFormGUI {
292 
293  $a_seleted_value = "";
294  $ilCtrl = $this->ctrl;
295  $lng = $this->lng;
296  $ilUser = $this->user;
297 
298  $form = new ilPropertyFormGUI();
299  $form->setFormAction($ilCtrl->getFormAction($this));
300  $form->setShowTopButtons(false);
301  $form->setTitle($this->getFormTitle("create"));
302 
303  $nr = array();
304  for ($i = 1; $i <= 20; $i++) {
305  $nr[$i] = $i;
306  }
307 
308  // cols
309  $cols = new ilSelectInputGUI($this->lng->txt("cont_nr_cols"), "nr_cols");
310  $cols->setOptions($nr);
311  $cols->setValue(2);
312  $form->addItem($cols);
313 
314  // rows
315  $rows = new ilSelectInputGUI($this->lng->txt("cont_nr_rows"), "nr_rows");
316  $rows->setOptions($nr);
317  $rows->setValue(2);
318  $form->addItem($rows);
319 
320  // table templates and table classes
321  $char_prop = new ilAdvSelectInputGUI(
322  $this->lng->txt("cont_table_style"),
323  "characteristic"
324  );
325  $chars = $this->getCharacteristics();
326  $templates = $this->getTemplateOptions();
327  $chars = array_merge($templates, $chars);
328  if (is_object($this->content_obj)) {
329  if (($chars[$a_seleted_value] ?? "") == "" && ($this->content_obj->getClass() != "")) {
330  $chars = array_merge(
331  array($this->content_obj->getClass() => $this->content_obj->getClass()),
332  $chars
333  );
334  }
335  }
336  foreach ($chars as $k => $char) {
337  if (strpos($k, ":") > 0) {
338  $t = explode(":", $k);
339  $html = $this->style->lookupTemplatePreview($t[1]) . '<div style="clear:both;" class="small">' . $char . "</div>";
340  } else {
341  $html = '<table class="ilc_table_' . $k . '"><tr><td class="small">' .
342  $char . '</td></tr></table>';
343  }
344  $char_prop->addOption($k, $char, $html);
345  }
346  if (count($chars) > 1) {
347  $char_prop->setValue("StandardTable");
348  $form->addItem($char_prop);
349  }
350 
351  // row header
352  $cb = new ilCheckboxInputGUI($lng->txt("cont_has_row_header"), "has_row_header");
353  $form->addItem($cb);
354 
355  $form->addCommandButton("create_tab", $lng->txt("save"));
356  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
357 
358  return $form;
359  }
360 
361  public function initEditingForm(
362  ): ilPropertyFormGUI {
363 
364  $a_seleted_value = "";
365  $ilCtrl = $this->ctrl;
366  $lng = $this->lng;
367  $ilUser = $this->user;
368 
369  $form = new ilPropertyFormGUI();
370  $form->setFormAction($ilCtrl->getFormAction($this));
371  $form->setShowTopButtons(false);
372  $form->setTitle($this->getFormTitle("edit"));
373 
374  // table templates and table classes
375  $char_prop = new ilAdvSelectInputGUI(
376  $this->lng->txt("cont_table_style"),
377  "characteristic"
378  );
379  $chars = $this->getCharacteristics();
380  $templates = $this->getTemplateOptions();
381  $chars = array_merge($templates, $chars);
382  if (is_object($this->content_obj)) {
383  if (($chars[$a_seleted_value] ?? "") == "" && ($this->content_obj->getClass() != "")) {
384  $chars = array_merge(
385  array($this->content_obj->getClass() => $this->content_obj->getClass()),
386  $chars
387  );
388  }
389  }
390  foreach ($chars as $k => $char) {
391  if (strpos($k, ":") > 0) {
392  $t = explode(":", $k);
393  $html = $this->style->lookupTemplatePreview($t[1]) . '<div style="clear:both;" class="small">' . $char . "</div>";
394  } else {
395  $html = '<table class="ilc_table_' . $k . '"><tr><td class="small">' .
396  $char . '</td></tr></table>';
397  }
398  $char_prop->addOption($k, $char, $html);
399  }
400  if (count($chars) > 1) {
401  if ($this->content_obj->getTemplate() !== "") {
402  $val = "t:" .
403  ilObjStyleSheet::_lookupTemplateIdByName($this->getStyleId(), $this->content_obj->getTemplate()) . ":" .
404  $this->content_obj->getTemplate();
405  } else {
406  $val = $this->content_obj->getClass();
407  }
408  $char_prop->setValue($val);
409  $form->addItem($char_prop);
410  }
411 
412  // row header
413  $cb = new ilCheckboxInputGUI($lng->txt("cont_has_row_header"), "has_row_header");
414  if ($this->content_obj->getHeaderRows() > 0) {
415  $cb->setChecked(true);
416  }
417  $form->addItem($cb);
418 
419  return $form;
420  }
421 
422  public function initImportForm(
423  ): ilPropertyFormGUI {
424 
425  $a_seleted_value = "";
426  $ilCtrl = $this->ctrl;
427  $lng = $this->lng;
428  $ilUser = $this->user;
429 
430  $form = new ilPropertyFormGUI();
431  $form->setFormAction($ilCtrl->getFormAction($this));
432  $form->setShowTopButtons(false);
433  $form->setTitle($this->getFormTitle("create"));
434 
435  $hi = new ilHiddenInputGUI("import");
436  $hi->setValue("1");
437  $form->addItem($hi);
438 
439  $import_data = new ilTextAreaInputGUI("", "import_table");
440  $import_data->setInfo($this->lng->txt("cont_table_import_info"));
441  $import_data->setRows(8);
442  $import_data->setCols(50);
443  $form->addItem($import_data);
444 
445  // table templates and table classes
446  $char_prop = new ilAdvSelectInputGUI(
447  $this->lng->txt("cont_table_style"),
448  "import_characteristic"
449  );
450  $chars = $this->getCharacteristics();
451  $templates = $this->getTemplateOptions();
452  $chars = array_merge($templates, $chars);
453  if (is_object($this->content_obj)) {
454  if (($chars[$a_seleted_value] ?? "") == "" && ($this->content_obj->getClass() != "")) {
455  $chars = array_merge(
456  array($this->content_obj->getClass() => $this->content_obj->getClass()),
457  $chars
458  );
459  }
460  }
461  foreach ($chars as $k => $char) {
462  if (strpos($k, ":") > 0) {
463  $t = explode(":", $k);
464  $html = $this->style->lookupTemplatePreview($t[1]) . '<div style="clear:both;" class="small">' . $char . "</div>";
465  } else {
466  $html = '<table class="ilc_table_' . $k . '"><tr><td class="small">' .
467  $char . '</td></tr></table>';
468  }
469  $char_prop->addOption($k, $char, $html);
470  }
471  if (count($chars) > 1) {
472  $char_prop->setValue("StandardTable");
473  $form->addItem($char_prop);
474  }
475 
476  // row header
477  $cb = new ilCheckboxInputGUI($lng->txt("cont_has_row_header"), "has_row_header");
478  $form->addItem($cb);
479 
480 
481  $form->addCommandButton("create_tab", $lng->txt("save"));
482  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
483 
484  return $form;
485  }
486 
487  public function initCellPropertiesForm(
488  ): ilPropertyFormGUI {
489 
490  $ilCtrl = $this->ctrl;
491  $lng = $this->lng;
492  $ilUser = $this->user;
493 
494  $form = new ilPropertyFormGUI();
495  $form->setFormAction($ilCtrl->getFormAction($this));
496  $form->setShowTopButtons(false);
497  $form->setTitle($this->lng->txt("cont_cell_properties"));
498 
499  $style_cb = new ilCheckboxInputGUI($lng->txt("cont_change_style"), "style_cb");
500 
502  $this->lng->txt("cont_style"),
503  "style"
504  );
505  $style->setPullRight(false);
506  $this->setBasicTableCellStyles();
507  $this->getCharacteristicsOfCurrentStyle(["table_cell"]); // scorm-2004
508  $chars = $this->getCharacteristics(); // scorm-2004
509  $options = array_merge(array("" => $this->lng->txt("none")), $chars); // scorm-2004
510  foreach ($options as $k => $option) {
511  $html = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="ilc_table_cell_' . $k . '">' .
512  $option . '</td></tr></table>';
513  $style->addOption($k, $option, $html);
514  }
515 
516  if (count($options) > 1) {
517  $style_cb->addSubItem($style);
518  $form->addItem($style_cb);
519  }
520 
521  $width_cb = new ilCheckboxInputGUI($lng->txt("cont_change_width"), "width_cb");
522  $ti = new ilTextInputGUI($lng->txt("cont_width"), "width");
523  $ti->setMaxLength(20);
524  $ti->setSize(7);
525  $width_cb->addSubItem($ti);
526  $form->addItem($width_cb);
527 
528  // alignment
529  $al_cb = new ilCheckboxInputGUI($lng->txt("cont_change_alignment"), "al_cb");
530  $options = array(
531  "" => $lng->txt("default"),
532  "Left" => $lng->txt("cont_left"),
533  "Center" => $lng->txt("cont_center"),
534  "Right" => $lng->txt("cont_right")
535  );
536  $si = new ilSelectInputGUI($lng->txt("cont_alignment"), "alignment");
537  $si->setOptions($options);
538  $al_cb->addSubItem($si);
539  $form->addItem($al_cb);
540 
541  return $form;
542  }
543 
544 }
tableAction()
Perform operation on table (adding, moving, deleting rows/cols)
editData()
Edit data of table.
static xml2outputJS(string $s_text)
Prepare content for js output.
updateJS()
Update via JavaScript.
static handleAjaxContent(string $a_content)
Handle ajax content.
executeCommand()
execute command
getCharacteristicsOfCurrentStyle(array $a_type)
Get characteristics of current style and call setCharacteristics, if style is given.
static _lookupTemplateIdByName(int $a_style_id, string $a_name)
Lookup table template preview.
This class represents a selection list 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...
getFormTitle(string $a_mode="edit")
ilPropertyFormGUI $form
ILIAS HTTP Services $http
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
ilGlobalTemplateInterface $main_tpl
static xml2output(string $a_text, bool $a_wysiwyg=false, bool $a_replace_lists=true, bool $unmask=true)
Converts xml from DB to output in edit textarea.
setTabs(string $data_tab_txt_key="")
Set tabs.
setOptions(array $a_options)
setCharacteristics(array $a_chars)
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
Content object of ilPageObject (see ILIAS DTD).
global $DIC
Definition: feed.php:28
static handleAjaxContentPost(string $text)
Post input2xml handling of ajax content.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
__construct(VocabulariesInterface $vocabularies)
afterCreation()
After creation processing.
setFormAction(string $a_formaction)
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.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getNewTableObject()
Get new table object.
getTemplateOptions(string $a_type="")
This class represents a text area property in a property form.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setParameter(object $a_gui_obj, string $a_parameter, $a_value)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$post
Definition: ltitoken.php:49
getCellContent(int $i, int $j)
update(bool $a_redirect=true)
Update table data in dom and update page in db.
Class ilPCDataTable.