ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilContentStyleSettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
32 {
36  protected int $obj_id;
38  protected ilSetting $settings;
39  protected ilTree $tree;
40  protected ilCtrl $ctrl;
43  protected ilLanguage $lng;
46  protected int $ref_id;
47 
48  public function __construct(ilObjStyleSettingsGUI $a_parent_gui)
49  {
50  global $DIC;
51 
52  $this->tree = $DIC->repositoryTree();
53  $this->settings = $DIC->settings();
54 
55  $this->parent_gui = $a_parent_gui;
56  $this->ctrl = $DIC->ctrl();
57  $this->rbacsystem = $DIC->rbac()->system();
58  $this->toolbar = $DIC->toolbar();
59  $this->lng = $DIC->language();
60  $this->tpl = $DIC->ui()->mainTemplate();
61  $this->request = $DIC->contentStyle()
62  ->internal()
63  ->gui()
64  ->standardRequest();
65 
66  $this->ref_id = $this->request->getRefId();
67  $this->obj_id = $this->request->getObjId(); // note that reference ID is the id of the style settings node and object ID may be a style sheet object ID
68 
69  $this->cs_settings = new ilContentStyleSettings();
70  $this->gui = $DIC->contentStyle()->internal()->gui();
71  }
72 
73  public function executeCommand(): void
74  {
75  $next_class = $this->ctrl->getNextClass($this);
76  $cmd = $this->ctrl->getCmd("edit");
77 
78  switch ($next_class) {
79  case "ilobjstylesheetgui":
80  $this->ctrl->setReturn($this, "edit");
81  $style_gui = new ilObjStyleSheetGUI("", $this->obj_id, false);
82  $this->ctrl->forwardCommand($style_gui);
83  break;
84 
85  case strtolower(ilRepoStandardUploadHandlerGUI::class):
86  $form = $this->getImportForm();
87  $gui = $form->getRepoStandardUploadHandlerGUI("import_file");
88  $this->ctrl->forwardCommand($gui);
89  break;
90 
91 
92  default:
93  $this->parent_gui->prepareOutput();
94  if (in_array($cmd, array("edit",
95  "delete",
96  "toggleGlobalDefault",
97  "toggleGlobalFixed",
98  "setScope",
99  "saveScope",
100  "saveActiveStyles",
101  "createStyle",
102  "moveLMStyles",
103  "moveIndividualStyles",
104  "deleteStyle",
105  "cancelDelete",
106  "confirmedDelete",
107  "import"
108  ))) {
109  $this->$cmd();
110  } else {
111  die("Unknown command " . $cmd);
112  }
113  }
114  }
115 
120  public function checkPermission(string $a_perm, bool $a_throw_exc = true): bool
121  {
122  if (!$this->rbacsystem->checkAccess($a_perm, $this->ref_id)) {
123  if ($a_throw_exc) {
124  throw new ilObjectException($this->lng->txt("permission_denied"));
125  }
126  return false;
127  }
128  return true;
129  }
130 
131  public function createStyle(): void
132  {
133  $ilCtrl = $this->ctrl;
134 
135  // $ilCtrl->setParameterByClass("ilobjstylesheetgui", "new_type", "sty");
136  $ilCtrl->redirectByClass("ilobjstylesheetgui", "create");
137  }
138 
142  public function edit(): void
143  {
144  $this->checkPermission("visible,read");
145 
146  // @todo: check these, they are checked later, but never (ILIAS 6) set
147  $fixed_style = 0;
148  $default_style = 0;
149 
150  // this may not be cool, if styles are organised as (independent) Service
151  $from_styles = $to_styles = $data = array();
152  $styles = $this->cs_settings->getStyles();
153  foreach ($styles as $style) {
154  $style["active"] = ilObjStyleSheet::_lookupActive((int) $style["id"]);
155  $style["lm_nr"] = ilObjContentObject::_getNrOfAssignedLMs((int) $style["id"]);
156  $data[$style["title"] . ":" . $style["id"]]
157  = $style;
158  if ($style["lm_nr"] > 0) {
159  $from_styles[$style["id"]] = $style["title"];
160  }
161  if ($style["active"] > 0) {
162  $to_styles[$style["id"]] = $style["title"];
163  }
164  }
165 
166  // number of individual styles
167  if ($fixed_style <= 0) {
168  $data[-1] =
169  array("title" => $this->lng->txt("sty_individual_styles"),
170  "id" => 0,
172  );
173  $from_styles[-1] = $this->lng->txt("sty_individual_styles");
174  }
175 
176  // number of default style (fallback default style)
177  if ($default_style <= 0 && $fixed_style <= 0) {
178  $data[0] =
179  array("title" => $this->lng->txt("sty_default_style"),
180  "id" => 0,
182  );
183  $from_styles[0] = $this->lng->txt("sty_default_style");
184  $to_styles[0] = $this->lng->txt("sty_default_style");
185  }
186 
187  $rendered_modal = "";
188  if ($this->checkPermission("sty_write_content", false)) {
189  $this->toolbar->addButton(
190  $this->lng->txt("sty_add_content_style"),
191  $this->ctrl->getLinkTarget($this, "createStyle")
192  );
193 
194  /*
195  $modal = $this->gui->modal($this->lng->txt("import"))
196  ->form($this->getImportForm());
197  $modal_c = $modal->getTriggerButtonComponents($this->lng->txt("import"), false);
198  $this->toolbar->addComponent($modal_c["button"]);
199  $rendered_modal = $this->gui->ui()->renderer()->render($modal_c["modal"]);*/
200 
201  $this->toolbar->addSeparator();
202 
203  // from styles selector
204  $si = new ilSelectInputGUI(
205  $this->lng->txt("sty_move_lm_styles") . ": " . $this->lng->txt("sty_from"),
206  "from_style"
207  );
208  $si->setOptions($from_styles);
209  $this->toolbar->addInputItem($si, true);
210 
211  // from styles selector
212  $si = new ilSelectInputGUI($this->lng->txt("sty_to"), "to_style");
213  $si->setOptions($to_styles);
214  $this->toolbar->addInputItem($si, true);
215  $this->toolbar->addFormButton($this->lng->txt("sty_move_style"), "moveLMStyles");
216 
217  $this->toolbar->setFormAction($this->ctrl->getFormAction($this));
218  }
219 
220  $table = new ilContentStylesTableGUI($this, "edit", $data);
221  $this->tpl->setContent($table->getHTML() . $rendered_modal);
222  }
223 
224  protected function getImportForm(): FormAdapterGUI
225  {
226  $gui = new ilObjStyleSheetGUI("", 0, false);
227  return $this->gui->form([self::class], "import")
228  ->file(
229  "import_file",
230  $this->lng->txt("import"),
231  $gui->handleImport(...), // Placeholder for upload handler
232  "obj_id",
233  "",
234  1,
235  ["application/zip"]
236  );
237  }
238 
239  public function import(): void
240  {
241  $form = $this->getImportForm();
242  if ($form->isValid()) {
243  if ($this->request->getRefId() > 0) {
244  $fold = ilObjectFactory::getInstanceByRefId($this->request->getRefId());
245  if ($fold->getType() == "stys") {
246  $obj_id = (int) $form->getData("import_file");
247  $cont_style_settings = new ilContentStyleSettings();
248  $cont_style_settings->addStyle($obj_id);
249  $cont_style_settings->update();
250  ilObjStyleSheet::_writeStandard($obj_id, true);
251  $this->ctrl->redirectByClass(self::class, "");
252  }
253  }
254  }
255  }
256 
257 
261  public function moveLMStyles(): void
262  {
263  $this->checkPermission("sty_write_content");
264 
265  if ($this->request->getFromStyleId() == -1) {
267  return;
268  }
269 
271  $this->request->getFromStyleId(),
272  $this->request->getToStyleId()
273  );
274  $this->ctrl->redirect($this, "edit");
275  }
276 
280  public function moveIndividualStyles(): void
281  {
282  $this->checkPermission("sty_write_content");
283 
284  ilObjContentObject::_moveLMStyles(-1, $this->request->getToStyleId());
285  $this->ctrl->redirect($this, "edit");
286  }
287 
288  public function confirmDeleteIndividualStyles(): void
289  {
290  $this->checkPermission("sty_write_content");
291 
292  $this->ctrl->setParameter($this, "to_style", $this->request->getToStyleId());
293 
294  $cgui = new ilConfirmationGUI();
295  $cgui->setFormAction($this->ctrl->getFormAction($this));
296  $cgui->setHeaderText($this->lng->txt("sty_confirm_del_ind_styles") . ": " .
297  sprintf(
298  $this->lng->txt("sty_confirm_del_ind_styles_desc"),
299  ilObject::_lookupTitle($this->request->getToStyleId())
300  ));
301  $cgui->setCancel($this->lng->txt("cancel"), "edit");
302  $cgui->setConfirm($this->lng->txt("ok"), "moveIndividualStyles");
303  $this->tpl->setContent($cgui->getHTML());
304  }
305 
309  public function deleteStyle(): void
310  {
311  $this->checkPermission("sty_write_content");
312 
313  $ids = $this->request->getIds();
314  if (count($ids) == 0) {
315  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("no_checkbox"), true);
316  $this->ctrl->redirect($this, "edit");
317  }
318 
319  // display confirmation message
320  $cgui = new ilConfirmationGUI();
321  $cgui->setFormAction($this->ctrl->getFormAction($this));
322  $cgui->setHeaderText($this->lng->txt("info_delete_sure"));
323  $cgui->setCancel($this->lng->txt("cancel"), "cancelDelete");
324  $cgui->setConfirm($this->lng->txt("confirm"), "confirmedDelete");
325 
326  foreach ($ids as $id) {
327  $caption = ilObject::_lookupTitle($id);
328 
329  $cgui->addItem("id[]", (string) $id, $caption);
330  }
331 
332  $this->tpl->setContent($cgui->getHTML());
333  }
334 
338  public function confirmedDelete(): void
339  {
340  $this->checkPermission("sty_write_content");
341 
342  $ids = $this->request->getIds();
343  foreach ($ids as $id) {
344  $set = new ilContentStyleSettings();
345  $set->removeStyle($id);
346  $set->update();
347 
348  $style_obj = ilObjectFactory::getInstanceByObjId($id);
349  $style_obj->delete();
350  }
351 
352  $this->ctrl->redirect($this, "edit");
353  }
354 
358  public function toggleGlobalDefault(): void
359  {
361  $lng = $this->lng;
362 
363  $this->checkPermission("sty_write_content");
364 
365  if ($this->request->getId() > 0) {
366  $ilSetting->delete("fixed_content_style_id");
367  $def_style = $ilSetting->get("default_content_style_id");
368 
369  if ($def_style != $this->request->getId()) {
370  $ilSetting->set("default_content_style_id", (string) $this->request->getId());
371  } else {
372  $ilSetting->delete("default_content_style_id");
373  }
374  $this->tpl->setOnScreenMessage('success', $lng->txt("msg_obj_modified"), true);
375  }
376  ilUtil::redirect($this->ctrl->getLinkTarget($this, "edit", ""));
377  }
378 
382  public function toggleGlobalFixed(): void
383  {
385  $lng = $this->lng;
386 
387  $this->checkPermission("sty_write_content");
388 
389  if ($this->request->getId() > 0) {
390  $ilSetting->delete("default_content_style_id");
391  $fixed_style = $ilSetting->get("fixed_content_style_id");
392  if ($fixed_style == $this->request->getId()) {
393  $ilSetting->delete("fixed_content_style_id");
394  } else {
395  $ilSetting->set("fixed_content_style_id", (string) $this->request->getId());
396  }
397  $this->tpl->setOnScreenMessage('success', $lng->txt("msg_obj_modified"), true);
398  }
399  ilUtil::redirect($this->ctrl->getLinkTarget($this, "edit", ""));
400  }
401 
402  public function saveActiveStyles(): void
403  {
404  $styles = $this->cs_settings->getStyles();
405  foreach ($styles as $style) {
406  if ($this->request->getSelectedStandard($style["id"]) == 1) {
407  ilObjStyleSheet::_writeActive((int) $style["id"], true);
408  } else {
409  ilObjStyleSheet::_writeActive((int) $style["id"], false);
410  }
411  }
412  ilUtil::redirect($this->ctrl->getLinkTarget($this, "edit", ""));
413  }
414 
418  public function showActions(bool $with_subobjects = false): void
419  {
420  // delete
421  $this->tpl->setCurrentBlock("tbl_action_btn");
422  $this->tpl->setVariable("BTN_NAME", "deleteStyle");
423  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("delete"));
424  $this->tpl->parseCurrentBlock();
425 
426  // set global default
427  $this->tpl->setCurrentBlock("tbl_action_btn");
428  $this->tpl->setVariable("BTN_NAME", "toggleGlobalDefault");
429  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("toggleGlobalDefault"));
430  $this->tpl->parseCurrentBlock();
431 
432  // set global default
433  $this->tpl->setCurrentBlock("tbl_action_btn");
434  $this->tpl->setVariable("BTN_NAME", "toggleGlobalFixed");
435  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("toggleGlobalFixed"));
436  $this->tpl->parseCurrentBlock();
437 
438  // set global default
439  $this->tpl->setCurrentBlock("tbl_action_btn");
440  $this->tpl->setVariable("BTN_NAME", "setScope");
441  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("sty_set_scope"));
442  $this->tpl->parseCurrentBlock();
443 
444  // save active styles
445  $this->tpl->setCurrentBlock("tbl_action_btn");
446  $this->tpl->setVariable("BTN_NAME", "saveActiveStyles");
447  $this->tpl->setVariable("BTN_VALUE", $this->lng->txt("sty_save_active_styles"));
448  $this->tpl->parseCurrentBlock();
449 
450  $this->tpl->setCurrentBlock("tbl_action_row");
451  $this->tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("nav/arrow_downright.svg"));
452  $this->tpl->parseCurrentBlock();
453  }
454 
455  public function cancelDelete(): void
456  {
457  $this->tpl->setOnScreenMessage('info', $this->lng->txt("msg_cancel"), true);
458  $this->ctrl->redirect($this, "edit");
459  }
460 
461  public function setScope(): void
462  {
463  $tpl = $this->tpl;
464  $ilCtrl = $this->ctrl;
465 
466  $this->checkPermission("sty_write_content");
467 
468  $ilCtrl->saveParameter($this, "id");
470  $this,
471  "setScope",
472  $this,
473  "saveScope",
474  "cat"
475  );
476  $exp->setTypeWhiteList(array("root", "cat"));
477  if (!$exp->handleCommand()) {
478  $tpl->setContent($exp->getHTML());
479  }
480  }
481 
482  public function saveScope(): void
483  {
484  $tree = $this->tree;
485 
486  $this->checkPermission("sty_write_content");
487 
488  $cat_id = $this->request->getCatId();
489  if ($cat_id == $tree->readRootId()) {
490  $cat_id = 0;
491  }
492 
493  ilObjStyleSheet::_writeScope($this->request->getId(), $cat_id);
494 
495  $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
496 
497  ilUtil::redirect($this->ctrl->getLinkTarget($this, "edit", ""));
498  }
499 }
static _getNrOfAssignedLMs(int $a_style_id)
gets the number of learning modules assigned to a content style
static _getNrLMsNoStyle()
get number of learning modules assigned no style
static _writeActive(int $a_id, bool $a_active)
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...
toggleGlobalFixed()
Toggle global fixed style.
Content style internal ui factory.
static _writeScope(int $a_id, int $a_scope)
confirmedDelete()
delete selected style objects
Base exception class for object service.
moveLMStyles()
move learning modules from one style to another
setContent(string $a_html)
Sets content for standard template.
toggleGlobalDefault()
Toggle global default style.
setOptions(array $a_options)
static _writeStandard(int $a_id, bool $a_std)
Write standard flag.
deleteStyle()
display deletion confirmation screen
redirectByClass( $a_class, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static _moveLMStyles(int $a_from_style, int $a_to_style)
move learning modules from one style to another
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static _lookupActive(int $a_id)
Lookup active flag.
static redirect(string $a_script)
__construct(ilObjStyleSettingsGUI $a_parent_gui)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
global $ilSetting
Definition: privfeed.php:31
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _getNrLMsIndividualStyles()
get number of learning modules with individual styles
moveIndividualStyles()
move all learning modules with individual styles to new style
checkPermission(string $a_perm, bool $a_throw_exc=true)
Check permission.
showActions(bool $with_subobjects=false)
show possible action (form buttons)
Settings UI class for system styles.