1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24include_once('./Services/Object/classes/class.ilObjectGUI.php');
25
26/**
27*
28*
29* @author Stefan Meyer <meyer@leifos.com>
30* @version $Id$
31*
32*
33* @ingroup ServicesContainerReference
34*/
35class ilContainerReferenceGUI extends ilObjectGUI
36{
37    /**
38     * @var ilTabsGUI
39     */
40    protected $tabs;
41
42    /**
43     * @var ilLocatorGUI
44     */
45    protected $locator;
46
47    /**
48     * @var ilObjUser
49     */
50    protected $user;
51
52    /**
53     * @var ilAccessHandler
54     */
55    protected $access;
56
57    /**
58     * @var ilErrorHandling
59     */
60    protected $error;
61
62    /**
63     * @var ilSetting
64     */
65    protected $settings;
66
67    const MAX_SELECTION_ENTRIES = 50;
68
69    const MODE_CREATE = 1;
70    const MODE_EDIT = 2;
71
72    protected $existing_objects = array();
73
74    /** @var string */
75    protected $target_type;
76    /** @var string */
77    protected $reference_type;
78    /** @var ilPropertyFormGUI */
79    protected $form;
80
81    /**
82     * Constructor
83     * @param
84     */
85    public function __construct($a_data, $a_id, $a_call_by_reference = true, $a_prepare_output = true)
86    {
87        global $DIC;
88
89        $this->lng = $DIC->language();
90        $this->ctrl = $DIC->ctrl();
91        $this->tabs = $DIC->tabs();
92        $this->locator = $DIC["ilLocator"];
93        $this->user = $DIC->user();
94        $this->access = $DIC->access();
95        $this->error = $DIC["ilErr"];
96        $this->settings = $DIC->settings();
97        $lng = $DIC->language();
98        parent::__construct($a_data, $a_id, $a_call_by_reference, $a_prepare_output);
99
100        $lng->loadLanguageModule('objref');
101    }
102
103    /**
104     * Execute command
105     *
106     * @access public
107     *
108     * @return bool|mixed
109     * @throws ilCtrlException
110     */
111    public function executeCommand()
112    {
113        $ilCtrl = $this->ctrl;
114        $ilTabs = $this->tabs;
115
116
117        if (isset($_GET['creation_mode']) && $_GET['creation_mode'] == self::MODE_CREATE) {
118            $this->setCreationMode(true);
119        }
120
121        $next_class = $ilCtrl->getNextClass($this);
122        $cmd = $ilCtrl->getCmd();
123
124        $this->prepareOutput();
125
126        switch ($next_class) {
127            case "ilpropertyformgui":
128                $form = $this->initForm($this->creation_mode ? self::MODE_CREATE : self::MODE_EDIT);
129                $this->ctrl->forwardCommand($form);
130                break;
131
132            case 'ilpermissiongui':
133                $ilTabs->setTabActive('perm_settings');
134                include_once("Services/AccessControl/classes/class.ilPermissionGUI.php");
135                $ilCtrl->forwardCommand(new ilPermissionGUI($this));
136                break;
137
138            default:
139                if (!$cmd || $cmd == 'view') {
140                    $cmd = "edit";
141                }
142                $cmd .= "Object";
143                $this->$cmd();
144                break;
145        }
146        return true;
147    }
148
149    /**
150     * Add locator item
151     */
152    protected function addLocatorItems()
153    {
154        $ilLocator = $this->locator;
155
156        if ($this->object instanceof ilObject) {
157            $ilLocator->addItem($this->object->getPresentationTitle(), $this->ctrl->getLinkTarget($this));
158        }
159    }
160
161    /**
162     * redirect to target
163     * @param
164     */
165    public function redirectObject()
166    {
167        $ilCtrl = $this->ctrl;
168
169        $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", $this->object->getTargetRefId());
170        $ilCtrl->redirectByClass("ilrepositorygui", "");
171    }
172
173    /**
174     * Create object
175     *
176     * @return void
177     */
178    public function createObject()
179    {
180        $ilUser = $this->user;
181        $ilAccess = $this->access;
182        $ilErr = $this->error;
183        $ilSetting = $this->settings;
184
185        $new_type = $_REQUEST["new_type"];
186        if (!$ilAccess->checkAccess("create_" . $this->getReferenceType(), '', $_GET["ref_id"], $new_type)) {
187            $ilErr->raiseError($this->lng->txt("permission_denied"), $ilErr->MESSAGE);
188        }
189        $form = $this->initForm(self::MODE_CREATE);
190        $this->tpl->setContent($form->getHTML());
191    }
192
193
194    /**
195     * save object
196     *
197     * @access public
198     * @param
199     * @return
200     */
201    public function saveObject()
202    {
203        $ilAccess = $this->access;
204
205        if (!(int) $_REQUEST['target_id']) {
206            ilUtil::sendFailure($this->lng->txt('select_one'));
207            $this->createObject();
208            return false;
209        }
210        if (!$ilAccess->checkAccess('visible', '', (int) $_REQUEST['target_id'])) {
211            ilUtil::sendFailure($this->lng->txt('permission_denied'));
212            $this->createObject();
213            return false;
214        }
215
216        parent::saveObject();
217    }
218
219    protected function initCreateForm($a_new_type)
220    {
221        return $this->initForm(self::MODE_CREATE);
222    }
223
224    /**
225     * @param ilObject $a_new_object
226     */
227    protected function afterSave(ilObject $a_new_object)
228    {
229        $target_obj_id = ilObject::_lookupObjId((int) $this->form->getInput('target_id'));
230        $a_new_object->setTargetId($target_obj_id);
231
232        $a_new_object->setTitleType($this->form->getInput('title_type'));
233        if ($this->form->getInput('title_type') == ilContainerReference::TITLE_TYPE_CUSTOM) {
234            $a_new_object->setTitle($this->form->getInput('title'));
235        }
236
237        $a_new_object->update();
238
239        ilUtil::sendSuccess($this->lng->txt("object_added"), true);
240        $this->ctrl->setParameter($this, 'ref_id', $a_new_object->getRefId());
241        $this->ctrl->setParameter($this, 'creation_mode', 0);
242        $this->ctrl->redirect($this, 'firstEdit');
243    }
244
245    /**
246     * show edit screen without info message
247     */
248    protected function firstEditObject()
249    {
250        $this->editObject();
251    }
252
253    public function editReferenceObject()
254    {
255        $this->editObject();
256    }
257
258    /**
259     * edit title
260     *
261     * @param ilPropertyFormGUI $form
262     */
263    public function editObject(ilPropertyFormGUI $form = null)
264    {
265        global $DIC;
266
267        $main_tpl = $DIC->ui()->mainTemplate();
268
269        $ilTabs = $this->tabs;
270
271        $ilTabs->setTabActive('settings');
272
273        if (!$form instanceof ilPropertyFormGUI) {
274            $form = $this->initForm();
275        }
276        $main_tpl->setContent($form->getHTML());
277    }
278
279    /**
280     * Init title form
281     * @param int $a_mode
282     * @return ilPropertyFormGUI
283     */
284    protected function initForm($a_mode = self::MODE_EDIT)
285    {
286        include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
287        include_once './Services/ContainerReference/classes/class.ilContainerReference.php';
288        $form = new ilPropertyFormGUI();
289
290        if ($a_mode == self::MODE_CREATE) {
291            $form->setTitle($this->lng->txt($this->getReferenceType() . '_new'));
292
293            $this->ctrl->setParameter($this, 'creation_mode', $a_mode);
294            $this->ctrl->setParameter($this, 'new_type', $_REQUEST['new_type']);
295        } else {
296            $form->setTitle($this->lng->txt('edit'));
297        }
298
299        $form->setFormAction($this->ctrl->getFormAction($this));
300        if ($a_mode == self::MODE_CREATE) {
301            $form->addCommandButton('save', $this->lng->txt('create'));
302            $form->addCommandButton('cancel', $this->lng->txt('cancel'));
303        } else {
304            $form->addCommandButton('update', $this->lng->txt('save'));
305        }
306
307        // title type
308        $ttype = new ilRadioGroupInputGUI($this->lng->txt('title'), 'title_type');
309        if ($a_mode == self::MODE_EDIT) {
310            $ttype->setValue($this->object->getTitleType());
311        } else {
312            $ttype->setValue(ilContainerReference::TITLE_TYPE_REUSE);
313        }
314
315        $reuse = new ilRadioOption($this->lng->txt('objref_reuse_title'));
316        $reuse->setValue(ilContainerReference::TITLE_TYPE_REUSE);
317        $ttype->addOption($reuse);
318
319        $custom = new ilRadioOption($this->lng->txt('objref_custom_title'));
320        $custom->setValue(ilContainerReference::TITLE_TYPE_CUSTOM);
321
322        // title
323        $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
324        $title->setSize(min(40, ilObject::TITLE_LENGTH));
325        $title->setMaxLength(ilObject::TITLE_LENGTH);
326        $title->setRequired(true);
327
328        if ($a_mode == self::MODE_EDIT) {
329            $title->setValue($this->object->getTitle());
330        }
331
332        $custom->addSubItem($title);
333        $ttype->addOption($custom);
334        $form->addItem($ttype);
335
336        include_once("./Services/Form/classes/class.ilRepositorySelector2InputGUI.php");
337        $repo = new ilRepositorySelector2InputGUI($this->lng->txt("objref_edit_ref"), "target_id");
338        //$repo->setParent($this);
339        $repo->setRequired(true);
340        $repo->getExplorerGUI()->setSelectableTypes(array($this->getTargetType()));
341        $repo->getExplorerGUI()->setTypeWhiteList(
342            array_merge(
343                array($this->getTargetType()),
344                array("root", "cat", "grp", "fold", "crs")
345            )
346        );
347        $repo->setInfo($this->lng->txt($this->getReferenceType() . '_edit_info'));
348
349        if ($a_mode == self::MODE_EDIT) {
350            $repo->getExplorerGUI()->setPathOpen($this->object->getTargetRefId());
351            $repo->setValue($this->object->getTargetRefId());
352        }
353
354        $form->addItem($repo);
355        $this->form = $form;
356        return $form;
357    }
358
359    /**
360     * update title
361     */
362    public function updateObject()
363    {
364        $ilAccess = $this->access;
365        $form = $this->initForm();
366        if ($form->checkInput()) {
367            $this->object->setTitleType($form->getInput('title_type'));
368            if ($form->getInput('title_type') == ilContainerReference::TITLE_TYPE_CUSTOM) {
369                $this->object->setTitle($form->getInput('title'));
370            }
371
372            if (!$ilAccess->checkAccess('visible', '', (int) $form->getInput('target_id')) ||
373                ilObject::_lookupType($form->getInput('target_id'), true) != $this->target_type) {
374                ilUtil::sendFailure($this->lng->txt('permission_denied'));
375                $this->editObject();
376                return false;
377            }
378            $this->checkPermission('write');
379
380            $target_obj_id = ilObject::_lookupObjId((int) $form->getInput('target_id'));
381            $this->object->setTargetId($target_obj_id);
382
383
384            $this->object->update();
385            ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
386            $this->ctrl->redirect($this, 'edit');
387        }
388        $form->setValuesByPost();
389        ilUtil::sendFailure($this->lng->txt('err_check_input'));
390        $this->editObject($form);
391        return true;
392    }
393
394    /**
395     * get target type
396     *
397     * @access public
398     * @return string
399     */
400    public function getTargetType()
401    {
402        return $this->target_type;
403    }
404
405    /**
406     * get reference type
407     *
408     * @access public
409     * @return string
410     */
411    public function getReferenceType()
412    {
413        return $this->reference_type;
414    }
415
416    /**
417     * @return int
418     */
419    public function getId()
420    {
421        return $this->obj_id;
422    }
423}
424