1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6* Export User Interface Class
7*
8* @author	Alex Killing <alex.killing@gmx.de>
9* @version	$Id$
10* @ingroup	ServicesExport
11*
12* @ilCtrl_Calls ilExportGUI:
13*/
14class ilExportGUI
15{
16    protected $formats = array();
17    protected $custom_columns = array();
18    protected $custom_multi_commands = array();
19
20    private $parent_gui = null;
21
22    /**
23     * Constuctor
24     *
25     * @param
26     * @return
27     */
28    public function __construct($a_parent_gui, $a_main_obj = null)
29    {
30        global $DIC;
31
32        $lng = $DIC['lng'];
33        $tpl = $DIC['tpl'];
34
35        $this->parent_gui = $a_parent_gui;
36        if ($a_main_obj == null) {
37            $this->obj = $a_parent_gui->object;
38        } else {
39            $this->obj = $a_main_obj;
40        }
41        $lng->loadLanguageModule("exp");
42        $this->tpl = $tpl;
43    }
44
45    /**
46     * @return ilExportTableGUI
47     */
48    protected function buildExportTableGUI()
49    {
50        include_once("./Services/Export/classes/class.ilExportTableGUI.php");
51        $table = new ilExportTableGUI($this, "listExportFiles", $this->obj);
52        return $table;
53    }
54
55    /**
56     * get parent gui
57     * @return
58     */
59    protected function getParentGUI()
60    {
61        return $this->parent_gui;
62    }
63
64    /**
65     * Add formats
66     *
67     * @param	array	formats
68     */
69    public function addFormat($a_key, $a_txt = "", $a_call_obj = null, $a_call_func = "")
70    {
71        global $DIC;
72
73        $lng = $DIC['lng'];
74
75        if ($a_txt == "") {
76            $a_txt = $lng->txt("exp_" . $a_key);
77        }
78        $this->formats[] = array("key" => $a_key, "txt" => $a_txt,
79            "call_obj" => $a_call_obj, "call_func" => $a_call_func);
80    }
81
82    /**
83     * Get formats
84     *
85     * @return	array	formats
86     */
87    public function getFormats()
88    {
89        return $this->formats;
90    }
91
92    /**
93     * Add custom column
94     *
95     * @param
96     * @return
97     */
98    public function addCustomColumn($a_txt, $a_obj, $a_func)
99    {
100        $this->custom_columns[] = array("txt" => $a_txt,
101                                        "obj" => $a_obj,
102                                        "func" => $a_func);
103    }
104
105    /**
106     * Add custom multi command
107     *
108     * @param
109     * @return
110     */
111    public function addCustomMultiCommand($a_txt, $a_obj, $a_func)
112    {
113        $this->custom_multi_commands[] = array("txt" => $a_txt,
114                                        "obj" => $a_obj,
115                                        "func" => $a_func);
116    }
117
118    /**
119     * Get custom multi commands
120     */
121    public function getCustomMultiCommands()
122    {
123        return $this->custom_multi_commands;
124    }
125
126    /**
127     * Get custom columns
128     *
129     * @param
130     * @return
131     */
132    public function getCustomColumns()
133    {
134        return $this->custom_columns;
135    }
136
137    /**
138     * Execute command
139     *
140     * @param
141     * @return
142     */
143    public function executeCommand()
144    {
145        global $DIC;
146
147        $ilCtrl = $DIC['ilCtrl'];
148        $ilAccess = $DIC['ilAccess'];
149        $ilErr = $DIC['ilErr'];
150        $lng = $DIC['lng'];
151        /** @var ilObjectDefinition $objDefinition */
152        $objDefinition = $DIC["objDefinition"];
153
154        // this should work (at least) for repository objects
155        if (method_exists($this->obj, 'getRefId') and $this->obj->getRefId()) {
156            if (!$ilAccess->checkAccess('write', '', $this->obj->getRefId())) {
157                $ilErr->raiseError($lng->txt('permission_denied'), $ilErr->WARNING);
158            }
159
160            // check export activation of container
161            $exp_limit = new ilExportLimitation();
162            if ($objDefinition->isContainer(ilObject::_lookupType($this->obj->getRefId(), true)) &&
163                $exp_limit->getLimitationMode() == ilExportLimitation::SET_EXPORT_DISABLED) {
164                ilUtil::sendFailure($lng->txt("exp_error_disabled"));
165                return;
166            }
167        }
168
169        $cmd = $ilCtrl->getCmd("listExportFiles");
170
171        switch ($cmd) {
172            case "listExportFiles":
173                $this->$cmd();
174                break;
175
176            default:
177                if (substr($cmd, 0, 7) == "create_") {
178                    $this->createExportFile();
179                } elseif (substr($cmd, 0, 6) == "multi_") {	// custom multi command
180                    $this->handleCustomMultiCommand();
181                } else {
182                    $this->$cmd();
183                }
184                break;
185        }
186    }
187
188    /**
189     * List export files
190     *
191     * @param
192     * @return
193     */
194    public function listExportFiles()
195    {
196        global $DIC;
197
198        $tpl = $DIC['tpl'];
199        $ilToolbar = $DIC['ilToolbar'];
200        $ilCtrl = $DIC['ilCtrl'];
201        $lng = $DIC['lng'];
202
203        include_once "Services/UIComponent/Button/classes/class.ilSubmitButton.php";
204        $button = ilSubmitButton::getInstance();
205
206        // creation buttons
207        $ilToolbar->setFormAction($ilCtrl->getFormAction($this));
208        if (count($this->getFormats()) > 1) {
209            // type selection
210            foreach ($this->getFormats() as $f) {
211                $options[$f["key"]] = $f["txt"];
212            }
213            include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
214            $si = new ilSelectInputGUI($lng->txt("type"), "format");
215            $si->setOptions($options);
216            $ilToolbar->addInputItem($si, true);
217
218            $button->setCaption("exp_create_file");
219            $button->setCommand("createExportFile");
220        } else {
221            $format = $this->getFormats();
222            $format = $format[0];
223
224            $button->setCaption($lng->txt("exp_create_file") . " (" . $format["txt"] . ")", false);
225            $button->setCommand("create_" . $format["key"]);
226        }
227
228        $ilToolbar->addButtonInstance($button);
229
230        $table = $this->buildExportTableGUI();
231        $table->setSelectAllCheckbox("file");
232        foreach ($this->getCustomColumns() as $c) {
233            $table->addCustomColumn($c["txt"], $c["obj"], $c["func"]);
234        }
235        foreach ($this->getCustomMultiCommands() as $c) {
236            $table->addCustomMultiCommand($c["txt"], "multi_" . $c["func"]);
237        }
238        $tpl->setContent($table->getHTML());
239    }
240
241    /**
242     * Create export file
243     *
244     * @param
245     * @return
246     */
247    public function createExportFile()
248    {
249        global $DIC;
250
251        $ilCtrl = $DIC['ilCtrl'];
252        $lng = $DIC['lng'];
253
254        if ($ilCtrl->getCmd() == "createExportFile") {
255            $format = ilUtil::stripSlashes($_POST["format"]);
256        } else {
257            $format = substr($ilCtrl->getCmd(), 7);
258        }
259        foreach ($this->getFormats() as $f) {
260            if ($f["key"] == $format) {
261                if (is_object($f["call_obj"])) {
262                    $f["call_obj"]->{$f["call_func"]}();
263                } elseif ($this->getParentGUI() instanceof ilContainerGUI) {
264                    return $this->showItemSelection();
265                } elseif ($format == "xml") {		// standard procedure
266                    include_once("./Services/Export/classes/class.ilExport.php");
267                    $exp = new ilExport();
268                    $exp->exportObject($this->obj->getType(), $this->obj->getId());
269                }
270            }
271        }
272
273        ilUtil::sendSuccess($lng->txt("exp_file_created"), true);
274        $ilCtrl->redirect($this, "listExportFiles");
275    }
276
277    /**
278     * Confirm file deletion
279     */
280    public function confirmDeletion()
281    {
282        global $DIC;
283
284        $ilCtrl = $DIC['ilCtrl'];
285        $tpl = $DIC['tpl'];
286        $lng = $DIC['lng'];
287
288        if (!is_array($_POST["file"]) || count($_POST["file"]) == 0) {
289            ilUtil::sendInfo($lng->txt("no_checkbox"), true);
290            $ilCtrl->redirect($this, "listExportFiles");
291        } else {
292            include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
293            $cgui = new ilConfirmationGUI();
294            $cgui->setFormAction($ilCtrl->getFormAction($this));
295            $cgui->setHeaderText($lng->txt("exp_really_delete"));
296            $cgui->setCancel($lng->txt("cancel"), "listExportFiles");
297            $cgui->setConfirm($lng->txt("delete"), "delete");
298
299            foreach ($_POST["file"] as $i) {
300                if (strpos($i, ':') !== false) {
301                    $iarr = explode(":", $i);
302                    $filename = $iarr[1];
303                } else {
304                    $filename = $i;
305                }
306                $cgui->addItem("file[]", $i, $filename);
307            }
308
309            $tpl->setContent($cgui->getHTML());
310        }
311    }
312
313    /**
314     * Delete files
315     */
316    public function delete()
317    {
318        global $DIC;
319
320        $ilCtrl = $DIC['ilCtrl'];
321
322        foreach ($_POST["file"] as $file) {
323            $file = explode(":", $file);
324
325            $file[1] = basename($file[1]);
326
327            include_once("./Services/Export/classes/class.ilExport.php");
328            $export_dir = ilExport::_getExportDirectory(
329                $this->obj->getId(),
330                str_replace("..", "", $file[0]),
331                $this->obj->getType()
332            );
333
334            $exp_file = $export_dir . "/" . str_replace("..", "", $file[1]);
335            $exp_dir = $export_dir . "/" . substr($file[1], 0, strlen($file[1]) - 4);
336            if (@is_file($exp_file)) {
337                unlink($exp_file);
338            }
339            if (@is_dir($exp_dir)) {
340                ilUtil::delDir($exp_dir);
341            }
342
343            // delete entry in database
344            include_once './Services/Export/classes/class.ilExportFileInfo.php';
345            $info = new ilExportFileInfo($this->obj->getId(), $file[0], $file[1]);
346            $info->delete();
347        }
348        $ilCtrl->redirect($this, "listExportFiles");
349    }
350
351    /**
352     * Download file
353     */
354    public function download()
355    {
356        global $DIC;
357
358        $ilCtrl = $DIC['ilCtrl'];
359        $lng = $DIC['lng'];
360
361        if (!isset($_GET["file"]) ||
362            is_array($_GET["file"])) {
363            $ilCtrl->redirect($this, "listExportFiles");
364        }
365
366        $file = explode(":", trim($_GET["file"]));
367        include_once("./Services/Export/classes/class.ilExport.php");
368        $export_dir = ilExport::_getExportDirectory(
369            $this->obj->getId(),
370            str_replace("..", "", $file[0]),
371            $this->obj->getType()
372        );
373
374        $file[1] = basename($file[1]);
375
376        ilUtil::deliverFile(
377            $export_dir . "/" . $file[1],
378            $file[1]
379        );
380    }
381
382    /**
383     * Handle custom multi command
384     *
385     * @param
386     * @return
387     */
388    public function handleCustomMultiCommand()
389    {
390        global $DIC;
391
392        $ilCtrl = $DIC['ilCtrl'];
393
394        $cmd = substr($ilCtrl->getCmd(), 6);
395        foreach ($this->getCustomMultiCommands() as $c) {
396            if ($c["func"] == $cmd) {
397                $c["obj"]->{$c["func"]}($_POST["file"]);
398            }
399        }
400    }
401
402    /**
403     * Show container item selection table
404     * @return
405     */
406    protected function showItemSelection()
407    {
408        global $DIC;
409
410        $tpl = $DIC['tpl'];
411
412        $tpl->addJavaScript('./Services/CopyWizard/js/ilContainer.js');
413        $tpl->setVariable('BODY_ATTRIBUTES', 'onload="ilDisableChilds(\'cmd\');"');
414
415        include_once './Services/Export/classes/class.ilExportSelectionTableGUI.php';
416        $table = new ilExportSelectionTableGUI($this, 'listExportFiles');
417        $table->parseContainer($this->getParentGUI()->object->getRefId());
418        $this->tpl->setContent($table->getHTML());
419    }
420
421    /**
422     * Save selection of subitems
423     * @return
424     */
425    protected function saveItemSelection()
426    {
427        global $DIC;
428
429        $tree = $DIC['tree'];
430        $objDefinition = $DIC['objDefinition'];
431        $ilAccess = $DIC['ilAccess'];
432        $ilCtrl = $DIC['ilCtrl'];
433        $lng = $DIC['lng'];
434
435        include_once './Services/Export/classes/class.ilExportOptions.php';
436        $eo = ilExportOptions::newInstance(ilExportOptions::allocateExportId());
437        $eo->addOption(ilExportOptions::KEY_ROOT, 0, 0, $this->obj->getId());
438
439        // check export limitation
440        $exp_limit = new ilExportLimitation();
441        try {
442            $exp_limit->checkLimitation(
443                $this->getParentGUI()->object->getRefId(),
444                $_POST['cp_options']
445            );
446        } catch (Exception $e) {
447            ilUtil::sendFailure($e->getMessage());
448            $this->showItemSelection();
449            return;
450        }
451
452        $items_selected = false;
453        foreach ($tree->getSubTree($root = $tree->getNodeData($this->getParentGUI()->object->getRefId())) as $node) {
454            if ($node['type'] == 'rolf') {
455                continue;
456            }
457            if ($node['ref_id'] == $this->getParentGUI()->object->getRefId()) {
458                $eo->addOption(
459                    ilExportOptions::KEY_ITEM_MODE,
460                    $node['ref_id'],
461                    $node['obj_id'],
462                    ilExportOptions::EXPORT_BUILD
463                );
464                continue;
465            }
466            // no export available or no access
467            if (!$objDefinition->allowExport($node['type']) or !$ilAccess->checkAccess('write', '', $node['ref_id'])) {
468                $eo->addOption(
469                    ilExportOptions::KEY_ITEM_MODE,
470                    $node['ref_id'],
471                    $node['obj_id'],
472                    ilExportOptions::EXPORT_OMIT
473                );
474                continue;
475            }
476
477            $mode = isset($_POST['cp_options'][$node['ref_id']]['type']) ?
478                $_POST['cp_options'][$node['ref_id']]['type'] :
479                ilExportOptions::EXPORT_OMIT;
480            $eo->addOption(
481                ilExportOptions::KEY_ITEM_MODE,
482                $node['ref_id'],
483                $node['obj_id'],
484                $mode
485            );
486            if ($mode != ilExportOptions::EXPORT_OMIT) {
487                $items_selected = true;
488            }
489        }
490
491        include_once("./Services/Export/classes/class.ilExport.php");
492        if ($items_selected) {
493            // TODO: move this to background soap
494            $eo->read();
495            $exp = new ilExport();
496            foreach ($eo->getSubitemsForCreation($this->obj->getRefId()) as $ref_id) {
497                $obj_id = ilObject::_lookupObjId($ref_id);
498                $type = ilObject::_lookupType($obj_id);
499                $exp->exportObject($type, $obj_id);
500            }
501            // Fixme: there is a naming conflict between the container settings xml and the container subitem xml.
502            sleep(1);
503            // Export container
504            include_once './Services/Export/classes/class.ilExportContainer.php';
505            $cexp = new ilExportContainer($eo);
506            $cexp->exportObject($this->obj->getType(), $this->obj->getId());
507        } else {
508            $exp = new ilExport();
509            $exp->exportObject($this->obj->getType(), $this->obj->getId());
510        }
511
512        // Delete export options
513        $eo->delete();
514
515        ilUtil::sendSuccess($lng->txt('export_created'), true);
516        $ilCtrl->redirect($this, "listExportFiles");
517    }
518}
519