1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3require_once('./Services/Repository/classes/class.ilObjectPlugin.php');
4
5/**
6 * Render add new item selector
7 *
8 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
9 * @version $Id: class.ilContainerGUI.php 43751 2013-07-30 10:07:45Z jluetzen $
10 *
11 * @ingroup ServicesObject
12 */
13class ilObjectAddNewItemGUI
14{
15    /**
16     * @var ilLanguage
17     */
18    protected $lng;
19
20    /**
21     * @var ilObjectDefinition
22     */
23    protected $obj_definition;
24
25    /**
26     * @var ilSetting
27     */
28    protected $settings;
29
30    /**
31     * @var ilAccessHandler
32     */
33    protected $access;
34
35    /**
36     * @var ilCtrl
37     */
38    protected $ctrl;
39
40    /**
41     * @var ilToolbarGUI
42     */
43    protected $toolbar;
44
45    /**
46     * @var ilTemplate
47     */
48    protected $tpl;
49
50    protected $mode; // [int]
51    protected $parent_ref_id; // [int]
52    protected $disabled_object_types; // [array]
53    protected $sub_objects; // [array]
54    protected $url_creation_callback; // [int]
55    protected $url_creation; // [string]
56
57    /**
58     * Constructor
59     *
60     * @param int $a_parent_ref_id
61     * @return ilObjectAddNewItemGUI
62     */
63    public function __construct($a_parent_ref_id)
64    {
65        global $DIC;
66
67        $this->lng = $DIC->language();
68        $this->obj_definition = $DIC["objDefinition"];
69        $this->settings = $DIC->settings();
70        $this->access = $DIC->access();
71        $this->ctrl = $DIC->ctrl();
72        $this->toolbar = $DIC->toolbar();
73        $this->tpl = $DIC["tpl"];
74        $lng = $DIC->language();
75
76        $this->parent_ref_id = (int) $a_parent_ref_id;
77        $this->mode = ilObjectDefinition::MODE_REPOSITORY;
78
79        $lng->loadLanguageModule("rep");
80        $lng->loadLanguageModule("cntr");
81    }
82
83    public function setMode($a_mode)
84    {
85        $this->mode = (int) $a_mode;
86    }
87
88    /**
89     * Set object types which may not be created
90     *
91     * @param array $a_types
92     */
93    public function setDisabledObjectTypes(array $a_types)
94    {
95        $this->disabled_object_types = $a_types;
96    }
97
98    /**
99     * Set after creation callback
100     *
101     * @param int $a_ref_id
102     */
103    public function setAfterCreationCallback($a_ref_id)
104    {
105        $this->url_creation_callback = $a_ref_id;
106    }
107
108    /**
109     * Set (custom) url for object creation
110     *
111     * @param string $a_url
112     */
113    public function setCreationUrl($a_url)
114    {
115        $this->url_creation = $a_url;
116    }
117
118    /**
119     * Parse creatable sub objects for personal workspace
120     *
121     * Grouping is not supported here, order is alphabetical (!)
122     *
123     * @return bool
124     */
125    protected function parsePersonalWorkspace()
126    {
127        $objDefinition = $this->obj_definition;
128        $lng = $this->lng;
129        $ilSetting = $this->settings;
130
131        $this->sub_objects = array();
132
133        $settings_map = array("blog" => "blogs",
134                "file" => "files",
135                "tstv" => "certificates",
136                "excv" => "certificates",
137                "crsv" => "certificates",
138                "cmxv" => "certificates",
139                "ltiv" => "certificates",
140                "scov" => "certificates",
141                "webr" => "links");
142
143        $subtypes = $objDefinition->getCreatableSubObjects("wfld", ilObjectDefinition::MODE_WORKSPACE);
144        if (count($subtypes) > 0) {
145            foreach (array_keys($subtypes) as $type) {
146                if (isset($settings_map[$type]) &&
147                    $ilSetting->get("disable_wsp_" . $settings_map[$type])) {
148                    continue;
149                }
150
151                $this->sub_objects[] = array("type" => "object",
152                    "value" => $type,
153                    "title" => $lng->txt("wsp_type_" . $type));
154            }
155        }
156
157        $this->sub_objects = ilUtil::sortArray($this->sub_objects, "title", 1);
158
159        return (bool) sizeof($this->sub_objects);
160    }
161
162    /**
163     * Parse creatable sub objects for repository incl. grouping
164     *
165     * @return bool
166     */
167    protected function parseRepository()
168    {
169        $objDefinition = $this->obj_definition;
170        $lng = $this->lng;
171        $ilAccess = $this->access;
172
173        $this->sub_objects = array();
174
175        if (!is_array($this->disabled_object_types)) {
176            $this->disabled_object_types = array();
177        }
178        $this->disabled_object_types[] = "rolf";
179
180        $parent_type = ilObject::_lookupType($this->parent_ref_id, true);
181        $subtypes = $objDefinition->getCreatableSubObjects($parent_type, $this->mode, $this->parent_ref_id);
182        if (count($subtypes) > 0) {
183            // grouping of object types
184
185            $grp_map = $pos_group_map = array();
186
187            include_once("Services/Repository/classes/class.ilObjRepositorySettings.php");
188            $groups = ilObjRepositorySettings::getNewItemGroups();
189
190            // no groups => use default
191            if (!$groups) {
192                $default = ilObjRepositorySettings::getDefaultNewItemGrouping();
193                $groups = $default["groups"];
194                $grp_map = $default["items"];
195
196                // reset positions (9999 = "other"/unassigned)
197                $pos = 0;
198                foreach ($subtypes as $item_type => $item) {
199                    // see ilObjectDefinition
200                    if (substr($item_type, 0, 1) == "x") {
201                        $subtypes[$item_type]["pos"] = "99992000";
202                    } else {
203                        $subtypes[$item_type]["pos"] = "9999" . str_pad(++$pos, 4, "0", STR_PAD_LEFT);
204                    }
205                }
206
207                // assign default positions
208                foreach ($default["sort"] as $item_type => $pos) {
209                    if (array_key_exists($item_type, $subtypes)) {
210                        $subtypes[$item_type]["pos"] = $pos;
211                    }
212                }
213
214                // sort by default positions
215                $subtypes = ilUtil::sortArray($subtypes, "pos", "asc", true, true);
216            }
217            // use group assignment
218            else {
219                foreach (ilObjRepositorySettings::getNewItemGroupSubItems() as $grp_id => $subitems) {
220                    foreach ($subitems as $subitem) {
221                        $grp_map[$subitem] = $grp_id;
222                    }
223                }
224            }
225
226            $group_separators = array();
227            $pos_group_map[0] = $lng->txt("rep_new_item_group_other");
228            $old_grp_ids = array();
229            foreach ($groups as $item) {
230                if ($item["type"] == ilObjRepositorySettings::NEW_ITEM_GROUP_TYPE_GROUP) {
231                    $pos_group_map[$item["id"]] = $item["title"];
232                } elseif (sizeof($old_grp_ids)) {
233                    $group_separators[$item["id"]] = $old_grp_ids;
234                }
235                $old_grp_ids[] = $item["id"];
236            }
237
238            $current_grp = null;
239            foreach ($subtypes as $type => $subitem) {
240                if (!in_array($type, $this->disabled_object_types)) {
241                    // #9950
242                    if ($ilAccess->checkAccess("create_" . $type, "", $this->parent_ref_id, $parent_type)) {
243                        // if only assigned - do not add groups
244                        if (sizeof($pos_group_map) > 1) {
245                            $obj_grp_id = (int) $grp_map[$type];
246                            if ($obj_grp_id !== $current_grp) {
247                                // add seperator after last group?
248                                $sdone = false;
249                                foreach ($group_separators as $idx => $spath) {
250                                    // #11986 - all separators up to next group
251                                    if ($current_grp && !in_array($obj_grp_id, $spath)) {
252                                        // 1 only separator between groups
253                                        if (!$sdone) {
254                                            $this->sub_objects[] = array("type" => "column_separator");
255                                            $sdone = true;
256                                        }
257                                        unset($group_separators[$idx]);
258                                    }
259                                }
260
261                                $title = $pos_group_map[$obj_grp_id];
262
263                                $this->sub_objects[] = array("type" => "group",
264                                    "title" => $title);
265
266                                $current_grp = $obj_grp_id;
267                            }
268                        }
269
270                        if ($subitem["plugin"]) {
271                            include_once("./Services/Component/classes/class.ilPlugin.php");
272                            $title = ilObjectPlugin::lookupTxtById($type, "obj_" . $type);
273                        } else {
274                            // #13088
275                            $title = $lng->txt("obj_" . $type);
276                        }
277
278                        $this->sub_objects[] = array("type" => "object",
279                            "value" => $type,
280                            "title" => $title);
281                    }
282                }
283            }
284        }
285
286        return (bool) sizeof($this->sub_objects);
287    }
288
289    /**
290     * Get rendered html of sub object list
291     *
292     * @return string
293     */
294    protected function getHTML()
295    {
296        $ilCtrl = $this->ctrl;
297
298        if ($this->mode != ilObjectDefinition::MODE_WORKSPACE && !isset($this->url_creation)) {
299            $base_url = "ilias.php?baseClass=ilRepositoryGUI&ref_id=" . $this->parent_ref_id . "&cmd=create";
300        } else {
301            $base_url = $this->url_creation;
302        }
303        $base_url = $ilCtrl->appendRequestTokenParameterString($base_url);
304
305        if ($this->url_creation_callback) {
306            $base_url .= "&crtcb=" . $this->url_creation_callback;
307        }
308
309        include_once("./Services/UIComponent/GroupedList/classes/class.ilGroupedListGUI.php");
310        $gl = new ilGroupedListGUI();
311        $gl->setAsDropDown(true, false);
312
313        foreach ($this->sub_objects as $item) {
314            switch ($item["type"]) {
315                case "column_separator":
316                    $gl->nextColumn();
317                    break;
318
319                /*
320                case "separator":
321                    $gl->addSeparator();
322                    break;
323                */
324
325                case "group":
326                    $gl->addGroupHeader($item["title"]);
327                    break;
328
329                case "object":
330                    $type = $item["value"];
331
332                    $path = ilObject::_getIcon('', 'tiny', $type);
333                    $icon = ($path != "")
334                        ? ilUtil::img($path, "") . " "
335                        : "";
336
337                    $url = $base_url . "&new_type=" . $type;
338
339                    $ttip = ilHelp::getObjCreationTooltipText($type);
340
341                    $gl->addEntry(
342                        $icon . $item["title"],
343                        $url,
344                        "_top",
345                        "",
346                        "",
347                        $type,
348                        $ttip,
349                        "bottom center",
350                        "top center",
351                        false
352                    );
353
354                    break;
355            }
356        }
357        $this->gl = $gl;
358
359        return $gl->getHTML();
360    }
361
362    /**
363     * Add new item selection to current page incl. toolbar (trigger) and overlay
364     */
365    public function render()
366    {
367        $ilToolbar = $this->toolbar;
368        $tpl = $this->tpl;
369        $lng = $this->lng;
370
371        if ($this->mode == ilObjectDefinition::MODE_WORKSPACE) {
372            if (!$this->parsePersonalWorkspace()) {
373                return;
374            }
375        } elseif (!$this->parseRepository()) {
376            return;
377        }
378
379        $ov_id = "il_add_new_item_ov";
380        $ov_trigger_id = $ov_id . "_tr";
381
382
383        include_once("./Services/UIComponent/AdvancedSelectionList/classes/class.ilAdvancedSelectionListGUI.php");
384        $adv = new ilAdvancedSelectionListGUI();
385        $adv->setListTitle($lng->txt("cntr_add_new_item"));
386        $this->getHTML();
387        $adv->setGroupedList($this->gl);
388        $adv->setStyle(ilAdvancedSelectionListGUI::STYLE_EMPH);
389        $tpl->setVariable("SELECT_OBJTYPE_REPOS", $adv->getHTML());
390        //$ilToolbar->addDropDown($lng->txt("cntr_add_new_item"), $this->getHTML());
391
392        return;
393
394        // toolbar
395        include_once "Services/UIComponent/Button/classes/class.ilLinkButton.php";
396        $button = ilLinkButton::getInstance();
397        $button->setId($ov_trigger_id);
398        $button->setCaption("cntr_add_new_item");
399        $button->setPrimary(true);
400        $ilToolbar->addButtonInstance($button);
401
402        // css?
403        $tpl->setVariable(
404            "SELECT_OBJTYPE_REPOS",
405            '<div id="' . $ov_id . '" style="display:none;" class="ilOverlay">' .
406            $this->getHTML() . '</div>'
407        );
408    }
409}
410