1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3require_once('./Services/Repository/classes/class.ilObjectPlugin.php');
4
5/**
6* Repository GUI Utilities
7*
8* @author Alex Killing <alex.killing@gmx.de>
9* @version $Id$
10* @ingroup ServicesRepository
11*/
12class ilRepUtilGUI
13{
14    /**
15     * @var ilLanguage
16     */
17    protected $lng;
18
19    /**
20     * @var ilSetting
21     */
22    protected $settings;
23
24    /**
25     * @var ilCtrl
26     */
27    protected $ctrl;
28
29    /**
30     * @var ilTemplate
31     */
32    protected $tpl;
33
34    /**
35     * @var ilObjectDefinition
36     */
37    protected $obj_definition;
38
39    /**
40     * @var ilAccessHandler
41     */
42    protected $access;
43
44    /**
45     * @var ilTree
46     */
47    protected $tree;
48
49
50    /**
51    * Constructor
52    *
53    * @param	object		parent gui object
54    * @param	string		current parent command (like in table2gui)
55    */
56    public function __construct($a_parent_gui, $a_parent_cmd = "")
57    {
58        global $DIC;
59
60        $this->lng = $DIC->language();
61        $this->settings = $DIC->settings();
62        $this->ctrl = $DIC->ctrl();
63        $this->tpl = $DIC["tpl"];
64        $this->obj_definition = $DIC["objDefinition"];
65        $this->access = $DIC->access();
66        $this->tree = $DIC->repositoryTree();
67        $this->parent_gui = $a_parent_gui;
68        $this->parent_cmd = $a_parent_cmd;
69    }
70
71
72    /**
73    * Show delete confirmation table
74    */
75    public function showDeleteConfirmation($a_ids, $a_supress_message = false)
76    {
77        $lng = $this->lng;
78        $ilSetting = $this->settings;
79        $ilCtrl = $this->ctrl;
80        $tpl = $this->tpl;
81        $objDefinition = $this->obj_definition;
82
83        if (!is_array($a_ids) || count($a_ids) == 0) {
84            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
85            return false;
86        }
87
88        // Remove duplicate entries
89        $a_ids = array_unique((array) $a_ids);
90
91        include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
92        $cgui = new ilConfirmationGUI();
93
94        if (!$a_supress_message) {
95            $msg = $lng->txt("info_delete_sure");
96
97            if (!$ilSetting->get('enable_trash')) {
98                $msg .= "<br/>" . $lng->txt("info_delete_warning_no_trash");
99            }
100
101            $cgui->setHeaderText($msg);
102        }
103        $cgui->setFormAction($ilCtrl->getFormAction($this->parent_gui));
104        $cgui->setCancel($lng->txt("cancel"), "cancelDelete");
105        $cgui->setConfirm($lng->txt("confirm"), "confirmedDelete");
106
107        $form_name = "cgui_" . md5(uniqid());
108        $cgui->setFormName($form_name);
109
110        $deps = array();
111        foreach ($a_ids as $ref_id) {
112            $obj_id = ilObject::_lookupObjId($ref_id);
113            $type = ilObject::_lookupType($obj_id);
114            $title = call_user_func(array(ilObjectFactory::getClassByType($type),'_lookupTitle'), $obj_id);
115            $alt = ($objDefinition->isPlugin($type))
116                ? $lng->txt("icon") . " " . ilObjectPlugin::lookupTxtById($type, "obj_" . $type)
117                : $lng->txt("icon") . " " . $lng->txt("obj_" . $type);
118
119            $title .= $this->handleMultiReferences($obj_id, $ref_id, $form_name);
120
121            $cgui->addItem(
122                "id[]",
123                $ref_id,
124                $title,
125                ilObject::_getIcon($obj_id, "small", $type),
126                $alt
127            );
128
129            ilObject::collectDeletionDependencies($deps, $ref_id, $obj_id, $type);
130        }
131        $deps_html = "";
132
133        if (is_array($deps) && count($deps) > 0) {
134            include_once("./Services/Repository/classes/class.ilRepDependenciesTableGUI.php");
135            $tab = new ilRepDependenciesTableGUI($deps);
136            $deps_html = "<br/><br/>" . $tab->getHTML();
137        }
138
139        $tpl->setContent($cgui->getHTML() . $deps_html);
140        return true;
141    }
142
143    /**
144     * Build subitem list for multiple references
145     *
146     * @param int $a_obj_id
147     * @param int $a_ref_id
148     * @param string $a_form_name
149     * @return string
150     */
151    public function handleMultiReferences($a_obj_id, $a_ref_id, $a_form_name)
152    {
153        $lng = $this->lng;
154        $ilAccess = $this->access;
155        $tree = $this->tree;
156
157        // process
158
159        $all_refs = ilObject::_getAllReferences($a_obj_id);
160        if (sizeof($all_refs) > 1) {
161            $lng->loadLanguageModule("rep");
162
163            $may_delete_any = 0;
164            $counter = 0;
165            $items = array();
166            foreach ($all_refs as $mref_id) {
167                // not the already selected reference, no refs from trash
168                if ($mref_id != $a_ref_id && !$tree->isDeleted($mref_id)) {
169                    if ($ilAccess->checkAccess("read", "", $mref_id)) {
170                        $may_delete = false;
171                        if ($ilAccess->checkAccess("delete", "", $mref_id)) {
172                            $may_delete = true;
173                            $may_delete_any++;
174                        }
175
176                        $items[] = array("id" => $mref_id,
177                            "path" => array_shift($this->buildPath(array($mref_id))),
178                            "delete" => $may_delete);
179                    } else {
180                        $counter++;
181                    }
182                }
183            }
184
185
186            // render
187
188            $tpl = new ilTemplate("tpl.rep_multi_ref.html", true, true, "Services/Repository");
189
190            $tpl->setVariable("TXT_INTRO", $lng->txt("rep_multiple_reference_deletion_intro"));
191
192            if ($may_delete_any) {
193                $tpl->setVariable("TXT_INSTRUCTION", $lng->txt("rep_multiple_reference_deletion_instruction"));
194            }
195
196            if ($items) {
197                $var_name = "mref_id[]";
198
199                foreach ($items as $item) {
200                    if ($item["delete"]) {
201                        $tpl->setCurrentBlock("cbox");
202                        $tpl->setVariable("ITEM_NAME", $var_name);
203                        $tpl->setVariable("ITEM_VALUE", $item["id"]);
204                        $tpl->parseCurrentBlock();
205                    } else {
206                        $tpl->setCurrentBlock("item_info");
207                        $tpl->setVariable("TXT_ITEM_INFO", $lng->txt("rep_no_permission_to_delete"));
208                        $tpl->parseCurrentBlock();
209                    }
210
211                    $tpl->setCurrentBlock("item");
212                    $tpl->setVariable("ITEM_TITLE", $item["path"]);
213                    $tpl->parseCurrentBlock();
214                }
215
216                if ($may_delete_any > 1) {
217                    $tpl->setCurrentBlock("cbox");
218                    $tpl->setVariable("ITEM_NAME", "sall_" . $a_ref_id);
219                    $tpl->setVariable("ITEM_VALUE", "");
220                    $tpl->setVariable("ITEM_ADD", " onclick=\"il.Util.setChecked('" .
221                        $a_form_name . "', '" . $var_name . "', document." . $a_form_name .
222                        ".sall_" . $a_ref_id . ".checked)\"");
223                    $tpl->parseCurrentBlock();
224
225                    $tpl->setCurrentBlock("item");
226                    $tpl->setVariable("ITEM_TITLE", $lng->txt("select_all"));
227                    $tpl->parseCurrentBlock();
228                }
229            }
230
231            if ($counter) {
232                $tpl->setCurrentBlock("add_info");
233                $tpl->setVariable(
234                    "TXT_ADDITIONAL_INFO",
235                    sprintf($lng->txt("rep_object_references_cannot_be_read"), $counter)
236                );
237                $tpl->parseCurrentBlock();
238            }
239
240            return $tpl->get();
241        }
242    }
243
244    /**
245    * Get trashed objects for a container
246    *
247    * @param	interger	ref id of container
248    */
249    public function showTrashTable($a_ref_id)
250    {
251        $tpl = $this->tpl;
252        $tree = $this->tree;
253        $lng = $this->lng;
254
255        $objects = $tree->getSavedNodeData($a_ref_id);
256
257        if (count($objects) == 0) {
258            ilUtil::sendInfo($lng->txt("msg_trash_empty"));
259            return;
260        }
261        include_once("./Services/Repository/classes/class.ilTrashTableGUI.php");
262        $ttab = new ilTrashTableGUI($this->parent_gui, "trash");
263        $ttab->setData($objects);
264
265        $tpl->setContent($ttab->getHTML());
266    }
267
268    /**
269    * Restore objects from trash
270    *
271    * @param	integer		current ref id
272    * @param	array		array of ref ids to be restored
273    */
274    public function restoreObjects($a_cur_ref_id, $a_ref_ids)
275    {
276        $lng = $this->lng;
277
278        if (!is_array($a_ref_ids) || count($a_ref_ids) == 0) {
279            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
280            return false;
281        } else {
282            try {
283                include_once("./Services/Repository/classes/class.ilRepUtil.php");
284                ilRepUtil::restoreObjects($a_cur_ref_id, $a_ref_ids);
285                ilUtil::sendSuccess($lng->txt("msg_undeleted"), true);
286            } catch (Exception $e) {
287                ilUtil::sendFailure($e->getMessage(), true);
288                return false;
289            }
290        }
291        return true;
292    }
293
294    /**
295    * Delete objects
296    */
297    public function deleteObjects($a_cur_ref_id, $a_ref_ids)
298    {
299        $ilSetting = $this->settings;
300        $lng = $this->lng;
301
302        if (!is_array($a_ref_ids) || count($a_ref_ids) == 0) {
303            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
304            return false;
305        } else {
306            include_once("./Services/Repository/classes/class.ilRepUtil.php");
307            try {
308                ilRepUtil::deleteObjects($a_cur_ref_id, $a_ref_ids);
309                if ($ilSetting->get('enable_trash')) {
310                    ilUtil::sendSuccess($lng->txt("info_deleted"), true);
311                } else {
312                    ilUtil::sendSuccess($lng->txt("msg_removed"), true);
313                }
314            } catch (Exception $e) {
315                ilUtil::sendFailure($e->getMessage(), true);
316                return false;
317            }
318        }
319    }
320
321    /**
322    * Remove objects from system
323    */
324    public function removeObjectsFromSystem($a_ref_ids, $a_from_recovery_folder = false)
325    {
326        $lng = $this->lng;
327
328        if (!is_array($a_ref_ids) || count($a_ref_ids) == 0) {
329            ilUtil::sendFailure($lng->txt("no_checkbox"), true);
330            return false;
331        } else {
332            include_once("./Services/Repository/classes/class.ilRepUtil.php");
333            try {
334                ilRepUtil::removeObjectsFromSystem($a_ref_ids, $a_from_recovery_folder);
335                ilUtil::sendSuccess($lng->txt("msg_removed"), true);
336            } catch (Exception $e) {
337                ilUtil::sendFailure($e->getMessage(), true);
338                return false;
339            }
340        }
341
342        return true;
343    }
344
345    /**
346     * Build path with deep-link
347     *
348     * @param	array	$ref_ids
349     * @return	array
350     */
351    protected function buildPath($ref_ids)
352    {
353        $tree = $this->tree;
354
355        include_once 'Services/Link/classes/class.ilLink.php';
356
357        if (!count($ref_ids)) {
358            return false;
359        }
360
361        $result = array();
362        foreach ($ref_ids as $ref_id) {
363            $path = "";
364            $path_full = $tree->getPathFull($ref_id);
365            foreach ($path_full as $idx => $data) {
366                if ($idx) {
367                    $path .= " &raquo; ";
368                }
369                if ($ref_id != $data['ref_id']) {
370                    $path .= $data['title'];
371                } else {
372                    $path .= ('<a target="_top" href="' .
373                              ilLink::_getLink($data['ref_id'], $data['type']) . '">' .
374                              $data['title'] . '</a>');
375                }
376            }
377
378            $result[] = $path;
379        }
380        return $result;
381    }
382
383    /**
384     * Confirmation for trash
385     *
386     * @param array $a_ids ref_ids
387     */
388    public function confirmRemoveFromSystemObject($a_ids)
389    {
390        $ilCtrl = $this->ctrl;
391        $lng = $this->lng;
392        $objDefinition = $this->obj_definition;
393        $tpl = $this->tpl;
394        include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
395
396        if (!is_array($a_ids)) {
397            $a_ids = array($a_ids);
398        }
399
400        $cgui = new ilConfirmationGUI();
401        $cgui->setFormAction($ilCtrl->getFormAction($this->parent_gui));
402        $cgui->setCancel($lng->txt("cancel"), "trash");
403        $cgui->setConfirm($lng->txt("confirm"), "removeFromSystem");
404        $cgui->setFormName("trash_confirmation");
405        $cgui->setHeaderText($lng->txt("info_delete_sure"));
406
407        foreach ($a_ids as $id) {
408            $obj_id = ilObject::_lookupObjId($id);
409            $type = ilObject::_lookupType($obj_id);
410            $title = call_user_func(array(ilObjectFactory::getClassByType($type),'_lookupTitle'), $obj_id);
411            $alt = ($objDefinition->isPlugin($type))
412                ? $lng->txt("icon") . " " . ilObjectPlugin::lookupTxtById($type, "obj_" . $type)
413                : $lng->txt("icon") . " " . $lng->txt("obj_" . $type);
414
415            $cgui->addItem(
416                "trash_id[]",
417                $id,
418                $title,
419                ilObject::_getIcon($obj_id, "small", $type),
420                $alt
421            );
422        }
423
424        $tpl->setContent($cgui->getHTML());
425    }
426}
427