ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPCResourcesGUI.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected ilTree $rep_tree;
27 
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->ctrl = $DIC->ctrl();
38  $this->tpl = $DIC["tpl"];
39  $this->lng = $DIC->language();
40  $this->obj_definition = $DIC["objDefinition"];
41  $tree = $DIC->repositoryTree();
42 
43  $this->rep_tree = $tree;
44  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
45  }
46 
47  public function executeCommand(): void
48  {
49  // get next class that processes or forwards current command
50  $next_class = $this->ctrl->getNextClass($this);
51 
52  // get current command
53  $cmd = $this->ctrl->getCmd();
54 
55  switch ($next_class) {
56  default:
57  $this->$cmd();
58  break;
59  }
60  }
61 
62  public function insert(): void
63  {
64  $this->edit(true);
65  }
66 
67  public function edit(bool $a_insert = false): void
68  {
69  $ilCtrl = $this->ctrl;
70  $tpl = $this->tpl;
71  $lng = $this->lng;
72  $objDefinition = $this->obj_definition;
73 
74  $op_type = null;
75  $op_itemgroup = null;
76 
77  $this->displayValidationError();
78 
79  // edit form
80  $form = new ilPropertyFormGUI();
81  $form->setFormAction($ilCtrl->getFormAction($this));
82  if ($a_insert) {
83  $form->setTitle($this->lng->txt("cont_insert_resources"));
84  } else {
85  $form->setTitle($this->lng->txt("cont_update_resources"));
86  }
87 
88  // count number of existing objects per type and collect item groups
90  $childs = $this->rep_tree->getChilds($ref_id);
91  $type_counts = array();
92  $item_groups = array();
93  foreach ($childs as $c) {
94  // see bug #12471
95  //echo "<br>-".$c["type"]."-".$objDefinition->getGroupOfObj($c["type"])."-";
96  $key = ($objDefinition->getGroupOfObj($c["type"]) != "")
97  ? $objDefinition->getGroupOfObj($c["type"])
98  : $c["type"];
99  $type_counts[$key] = ($type_counts[$key] ?? 0) + 1;
100  if ($c["type"] == "itgr") {
101  $item_groups[$c["ref_id"]] = $c["title"];
102  }
103  }
104 
105  if (count($item_groups) > 0) {
106  // radio group for type selection
107  $radg = new ilRadioGroupInputGUI($lng->txt("cont_resources"), "res_type");
108  if (!$a_insert && $this->content_obj->getMainType() == "ItemGroup") {
109  $radg->setValue("itgr");
110  } else {
111  $radg->setValue("by_type");
112  }
113 
114  $op_type = new ilRadioOption($lng->txt("cont_resources_of_type"), "by_type", "");
115  $radg->addOption($op_type);
116  $op_itemgroup = new ilRadioOption($lng->txt("obj_itgr"), "itgr", "");
117  $radg->addOption($op_itemgroup);
118  $form->addItem($radg);
119  }
120 
121  // type selection
122  $type_prop = new ilSelectInputGUI(
123  $this->lng->txt("cont_type"),
124  "type"
125  );
126  $obj_id = ilObject::_lookupObjId($this->requested_ref_id);
127  $obj_type = ilObject::_lookupType($obj_id);
128  $sub_objs = $objDefinition->getGroupedRepositoryObjectTypes($obj_type);
129  $types = array();
130  foreach ($sub_objs as $k => $so) {
131  if (!$objDefinition->isPlugin($k)) {
132  if ($k != "itgr") {
133  $types[$k] = $this->lng->txt("objs_" . $k) . " (" . (int) ($type_counts[$k] ?? 0) . ")";
134  }
135  } else {
137  $types[$k] = $pl->txt("objs_" . $k) . " (" . (int) ($type_counts[$k] ?? 0) . ")";
138  }
139  }
140  $type_prop->setOptions($types);
141  $selected = ($a_insert)
142  ? ""
143  : $this->content_obj->getResourceListType();
144  $type_prop->setValue($selected);
145  if (count($item_groups) > 0) {
146  $op_type->addSubItem($type_prop);
147  } else {
148  $form->addItem($type_prop);
149  }
150 
151  if (count($item_groups) > 0) {
152  // item groups
153  $options = $item_groups;
154  $si = new ilSelectInputGUI($this->lng->txt("obj_itgr"), "itgr");
155  $si->setOptions($options);
156  $selected = ($a_insert)
157  ? ""
158  : $this->content_obj->getItemGroupRefId();
159  $op_itemgroup->addSubItem($si);
160  }
161 
162 
163  // save/cancel buttons
164  if ($a_insert) {
165  $form->addCommandButton("create_resources", $lng->txt("save"));
166  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
167  } else {
168  $form->addCommandButton("update_resources", $lng->txt("save"));
169  $form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
170  }
171  $html = $form->getHTML();
172  $tpl->setContent($html);
173  }
174 
175  public function create(): void
176  {
177  $this->content_obj = new ilPCResources($this->getPage());
178  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
179 
180  if ($this->request->getString("res_type") != "itgr") {
181  $this->content_obj->setResourceListType(
182  $this->request->getString("type")
183  );
184  } else {
185  $this->content_obj->setItemGroupRefId(
186  $this->request->getString("itgr")
187  );
188  }
189  $this->updated = $this->pg_obj->update();
190  if ($this->updated === true) {
191  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
192  } else {
193  $this->insert();
194  }
195  }
196 
197  public function update(): void
198  {
199  if ($this->request->getString("res_type") != "itgr") {
200  $this->content_obj->setResourceListType(
201  $this->request->getString("type")
202  );
203  } else {
204  $this->content_obj->setItemGroupRefId(
205  $this->request->getString("itgr")
206  );
207  }
208  $this->updated = $this->pg_obj->update();
209  if ($this->updated === true) {
210  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
211  } else {
212  $this->pg_obj->addHierIDs();
213  $this->edit();
214  }
215  }
216 
220  public static function insertResourcesIntoPageContent(
221  string $a_content
222  ): string {
223  global $DIC;
224 
225  $objDefinition = $DIC["objDefinition"];
226  $tree = $DIC->repositoryTree();
227  $lng = $DIC->language();
228 
229  $ref_id = $DIC
230  ->copage()
231  ->internal()
232  ->gui()
233  ->pc()
234  ->editRequest()
235  ->getRefId();
236  $obj_id = ilObject::_lookupObjId($ref_id);
237  $obj_type = ilObject::_lookupType($obj_id);
238 
239  // determine type -> group
240  $type_to_grp = array();
241  $type_grps =
242  $objDefinition->getGroupedRepositoryObjectTypes($obj_type);
243  foreach ($type_grps as $grp => $def) {
244  foreach ($def["objs"] as $t) {
245  $type_to_grp[$t] = $grp;
246  }
247  }
248 
249  $childs = $tree->getChilds($ref_id);
250  $childs_by_type = array();
251  $item_groups = array();
252  foreach ($childs as $child) {
253  if (isset($type_to_grp[$child["type"]])) {
254  $childs_by_type[$type_to_grp[$child["type"]]][] = $child;
255  if ($child["type"] == "itgr") {
256  $item_groups[(int) $child["ref_id"]] = $child["title"];
257  }
258  }
259  }
260 
261  // handle "by type" lists
262  foreach ($type_grps as $type => $v) {
263  if (is_int(strpos($a_content, "[list-" . $type . "]"))) {
264  // render block
265  $tpl = new ilTemplate("tpl.resource_block.html", true, true, "Services/COPage");
266  $cnt = 0;
267 
268  if (isset($childs_by_type[$type]) && count($childs_by_type[$type]) > 0) {
269  foreach ($childs_by_type[$type] as $child) {
270  $tpl->setCurrentBlock("row");
271  $tpl->setVariable("IMG",
272  ilUtil::img(
273  ilObject::_getIcon((int) $child["obj_id"], "small"),
274  null,
275  "",
276  "",
277  0,
278  "",
279  "ilListItemIcon"
280  )
281  );
282  $tpl->setVariable("TITLE", $child["title"]);
284  $cnt++;
285  }
286  } else {
287  $tpl->setCurrentBlock("row");
288  $tpl->setVariable("TITLE", $lng->txt("no_items"));
290  }
291  $tpl->setVariable("HEADER", $lng->txt("objs_" . $type));
292  $a_content = str_replace("[list-" . $type . "]", $tpl->get(), $a_content);
293  }
294  }
295 
296  // handle item groups
297  while (preg_match('/\[(item-group-([0-9]*))\]/i', $a_content, $found)) {
298  $itgr_ref_id = (int) $found[2];
299 
300  // check whether this item group is child -> insert editing html
301  if (isset($item_groups[$itgr_ref_id])) {
302  $itgr_items = new ilItemGroupItems($itgr_ref_id);
303  $items = $itgr_items->getValidItems();
304 
305  // render block
306  $tpl = new ilTemplate("tpl.resource_block.html", true, true, "Services/COPage");
307  foreach ($items as $it_ref_id) {
308  $it_obj_id = ilObject::_lookupObjId($it_ref_id);
309  $it_title = ilObject::_lookupTitle($it_obj_id);
310  $it_type = ilObject::_lookupType($it_obj_id);
311 
312  // TODO: Handle this switch by module.xml definitions
313  if (in_array($it_type, array("catr", "crsr", "grpr"))) {
314  $it_title = ilContainerReference::_lookupTitle($it_obj_id);
315  }
316 
317 
318  $tpl->setCurrentBlock("row");
319  $tpl->setVariable("IMG", ilUtil::img(ilObject::_getIcon($it_obj_id, "small")));
320  $tpl->setVariable("TITLE", $it_title);
322  }
323  $tpl->setVariable("HEADER", $item_groups[$itgr_ref_id]);
324  $html = $tpl->get();
325  } else {
326  $html = "<i>" . $lng->txt("cont_element_refers_removed_itgr") . "</i>";
327  }
328  $a_content = preg_replace('/\[' . $found[1] . '\]/i', $html, $a_content);
329  }
330 
331 
332  return $a_content;
333  }
334 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupTitle(int $obj_id)
$c
Definition: cli.php:38
static _getIcon(int $obj_id=0, string $size="big", string $type="", bool $offline=false)
Get icon for repository item.
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...
$type
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
static img(string $a_src, ?string $a_alt=null, $a_width="", $a_height="", $a_border=0, $a_id="", $a_class="")
Build img tag.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupObjId(int $ref_id)
static insertResourcesIntoPageContent(string $a_content)
Insert resources (see also ilContainerContentGUI::determinePageEmbeddedBlocks for presentation) ...
global $DIC
Definition: feed.php:28
parses the objects.xml it handles the xml-description of all ilias objects
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
$ref_id
Definition: ltiauth.php:67
This class represents a property in a property form.
static _lookupTitle(int $obj_id)
setContent(string $a_html)
Sets content for standard template.
edit(bool $a_insert=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
string $key
Consumer key/client ID value.
Definition: System.php:193
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
static getPluginObjectByType(string $type)
Return either a repoObject plugin or a orgunit extension plugin or null if the type is not a plugin...
ilObjectDefinition $obj_definition
__construct(Container $dic, ilPlugin $plugin)
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
static _lookupType(int $id, bool $reference=false)