ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCResourcesGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/COPage/classes/class.ilPCResources.php");
5 require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
6 
18 {
19 
24  function ilPCResourcesGUI(&$a_pg_obj, &$a_content_obj, $a_hier_id, $a_pc_id = "")
25  {
26  global $tree;
27 
28  $this->rep_tree = $tree;
29  parent::ilPageContentGUI($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
30  }
31 
35  function &executeCommand()
36  {
37  // get next class that processes or forwards current command
38  $next_class = $this->ctrl->getNextClass($this);
39 
40  // get current command
41  $cmd = $this->ctrl->getCmd();
42 
43  switch($next_class)
44  {
45  default:
46  $ret =& $this->$cmd();
47  break;
48  }
49 
50  return $ret;
51  }
52 
56  function insert()
57  {
58  $this->edit(true);
59  }
60 
64  function edit($a_insert = false)
65  {
66  global $ilCtrl, $tpl, $lng, $objDefinition;
67 
68  $this->displayValidationError();
69 
70  // edit form
71  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
72  $form = new ilPropertyFormGUI();
73  $form->setFormAction($ilCtrl->getFormAction($this));
74  if ($a_insert)
75  {
76  $form->setTitle($this->lng->txt("cont_insert_resources"));
77  }
78  else
79  {
80  $form->setTitle($this->lng->txt("cont_update_resources"));
81  }
82 
83  // count number of existing objects per type and collect item groups
84  $ref_id = (int) $_GET["ref_id"];
85  $childs = $this->rep_tree->getChilds($ref_id);
86  $type_counts = array();
87  $item_groups = array();
88  foreach ($childs as $c)
89  {
90  // see bug #12471
91  //echo "<br>-".$c["type"]."-".$objDefinition->getGroupOfObj($c["type"])."-";
92  $key = ($objDefinition->getGroupOfObj($c["type"]) != "")
93  ? $objDefinition->getGroupOfObj($c["type"])
94  : $c["type"];
95  $type_counts[$key] += 1;
96  if ($c["type"] == "itgr")
97  {
98  $item_groups[$c["ref_id"]] = $c["title"];
99  }
100  }
101 
102  if (count($item_groups) > 0)
103  {
104  // radio group for type selection
105  $radg = new ilRadioGroupInputGUI($lng->txt("cont_resources"), "res_type");
106  if (!$a_insert && $this->content_obj->getMainType() == "ItemGroup")
107  {
108  $radg->setValue("itgr");
109  }
110  else
111  {
112  $radg->setValue("by_type");
113  }
114 
115  $op_type = new ilRadioOption($lng->txt("cont_resources_of_type"), "by_type", "");
116  $radg->addOption($op_type);
117  $op_itemgroup = new ilRadioOption($lng->txt("obj_itgr"), "itgr", "");
118  $radg->addOption($op_itemgroup);
119  $form->addItem($radg);
120  }
121 
122  // type selection
123  $type_prop = new ilSelectInputGUI($this->lng->txt("cont_type"),
124  "type");
125  $obj_id = ilObject::_lookupObjId($_GET["ref_id"]);
126  $obj_type = ilObject::_lookupType($obj_id);
127  $sub_objs = $objDefinition->getGroupedRepositoryObjectTypes($obj_type);
128  $types = array();
129  foreach($sub_objs as $k => $so)
130  {
131  if ($k != "itgr" & $k != "icrs")
132  {
133  $types[$k] = $this->lng->txt("objs_".$k)." (".(int) $type_counts[$k].")";
134  }
135  }
136  $type_prop->setOptions($types);
137  $selected = ($a_insert)
138  ? ""
139  : $this->content_obj->getResourceListType();
140  $type_prop->setValue($selected);
141  if (count($item_groups) > 0)
142  {
143  $op_type->addSubItem($type_prop);
144  }
145  else
146  {
147  $form->addItem($type_prop);
148  }
149 
150  if (count($item_groups) > 0)
151  {
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  {
166  $form->addCommandButton("create_resources", $lng->txt("save"));
167  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
168  }
169  else
170  {
171  $form->addCommandButton("update_resources", $lng->txt("save"));
172  $form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
173  }
174  $html = $form->getHTML();
175  $tpl->setContent($html);
176  return $ret;
177 
178  }
179 
180 
184  function create()
185  {
186  $this->content_obj = new ilPCResources($this->dom);
187  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
188 
189  if ($_POST["res_type"] != "itgr")
190  {
191  $this->content_obj->setResourceListType(ilUtil::stripSlashes($_POST["type"]));
192  }
193  else
194  {
195  $this->content_obj->setItemGroupRefId(ilUtil::stripSlashes($_POST["itgr"]));
196  }
197  $this->updated = $this->pg_obj->update();
198  if ($this->updated === true)
199  {
200  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
201  }
202  else
203  {
204  $this->insert();
205  }
206  }
207 
211  function update()
212  {
213  if ($_POST["res_type"] != "itgr")
214  {
215  $this->content_obj->setResourceListType(ilUtil::stripSlashes($_POST["type"]));
216  }
217  else
218  {
219  $this->content_obj->setItemGroupRefId(ilUtil::stripSlashes($_POST["itgr"]));
220  }
221  $this->updated = $this->pg_obj->update();
222  if ($this->updated === true)
223  {
224  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
225  }
226  else
227  {
228  $this->pg_obj->addHierIDs();
229  $this->edit();
230  }
231  }
232 
239  static function insertResourcesIntoPageContent($a_content)
240  {
241  global $objDefinition, $tree, $lng;
242 
243  $ref_id = (int) $_GET["ref_id"];
244  $obj_id = (int) ilObject::_lookupObjId($ref_id);
245  $obj_type = ilObject::_lookupType($obj_id);
246 
247  // determine type -> group
248  $type_to_grp = array();
249  $type_grps =
250  $objDefinition->getGroupedRepositoryObjectTypes($obj_type);
251  foreach ($type_grps as $grp => $def)
252  {
253  foreach ($def["objs"] as $t)
254  {
255  $type_to_grp[$t] = $grp;
256  }
257  }
258 
259  $childs = $tree->getChilds($ref_id);
260  $childs_by_type = array();
261  $item_groups = array();
262  foreach ($childs as $child)
263  {
264  $childs_by_type[$type_to_grp[$child["type"]]][] = $child;
265  if ($child["type"] == "itgr")
266  {
267  $item_groups[(int) $child["ref_id"]] = $child["title"];
268  }
269  }
270 
271  // handle "by type" lists
272  foreach ($type_grps as $type => $v)
273  {
274  if (is_int(strpos($a_content, "[list-".$type."]")))
275  {
276  // render block
277  $tpl = new ilTemplate("tpl.resource_block.html", true, true, "Services/COPage");
278  $cnt = 0;
279 
280  if (is_array($childs_by_type[$type]) && count($childs_by_type[$type]) > 0)
281  {
282  foreach ($childs_by_type[$type] as $child)
283  {
284  $tpl->setCurrentBlock("row");
285  $tpl->setVariable("IMG", ilUtil::img(ilObject::_getIcon($child["obj_id"], "small")));
286  $tpl->setVariable("TITLE", $child["title"]);
287  $tpl->parseCurrentBlock();
288  $cnt++;
289  }
290  $tpl->setVariable("HEADER", $lng->txt("objs_".$type));
291  $a_content = str_replace("[list-".$type."]", $tpl->get(), $a_content);
292  }
293  else
294  {
295  $tpl->setCurrentBlock("row");
296  $tpl->setVariable("TITLE", $lng->txt("no_items"));
297  $tpl->parseCurrentBlock();
298  $tpl->setVariable("HEADER", $lng->txt("objs_".$type));
299  $a_content = str_replace("[list-".$type."]", $tpl->get(), $a_content);
300  }
301  }
302  }
303 
304  // handle item groups
305  while (eregi("\[(item-group-([0-9]*))\]", $a_content, $found))
306  {
307  $itgr_ref_id = (int) $found[2];
308 
309  // check whether this item group is child -> insert editing html
310  if (isset($item_groups[$itgr_ref_id]))
311  {
312  include_once("./Modules/ItemGroup/classes/class.ilItemGroupItems.php");
313  $itgr_items = new ilItemGroupItems($itgr_ref_id);
314  $items = $itgr_items->getValidItems();
315 
316  // render block
317  $tpl = new ilTemplate("tpl.resource_block.html", true, true, "Services/COPage");
318  foreach ($items as $it_ref_id)
319  {
320  $it_obj_id = ilObject::_lookupObjId($it_ref_id);
321  $it_title = ilObject::_lookupTitle($it_obj_id);
322  $tpl->setCurrentBlock("row");
323  $tpl->setVariable("IMG", ilUtil::img(ilObject::_getIcon($it_obj_id, "small")));
324  $tpl->setVariable("TITLE", $it_title);
325  $tpl->parseCurrentBlock();
326  }
327  $tpl->setVariable("HEADER", $item_groups[$itgr_ref_id]);
328  $html = $tpl->get();
329  }
330  else
331  {
332  $html = "<i>".$lng->txt("cont_element_refers_removed_itgr")."</i>";
333  }
334  $a_content = eregi_replace("\[".$found[1]."\]", $html, $a_content);
335  }
336 
337 
338  return $a_content;
339  }
340 
341 }
342 ?>