1<?php
2
3/**
4 * Class ilObjOrgUnitSettingsFormGUI
5 *
6 * @author Stefan Wanzenried <sw@studer-raimann.ch>
7 */
8class ilObjOrgUnitSettingsFormGUI extends ilPropertyFormGUI
9{
10
11    /**
12     * @var ilObjOrgUnit
13     */
14    protected $obj_orgu;
15
16    /**
17     * @var ilTemplate
18     */
19    protected $tpl;
20
21    /**
22     * @var ilLanguage
23     */
24    protected $lng;
25
26    /**
27     * @var ilCtrl
28     */
29    protected $ctrl;
30
31    /**
32     * @var ilObjUser
33     */
34    protected $user;
35
36    /**
37     * @var
38     */
39    protected $parent_gui;
40
41
42    public function __construct($parent_gui, ilObjOrgUnit $obj_orgu)
43    {
44        global $DIC;
45        $tpl = $DIC['tpl'];
46        $ilCtrl = $DIC['ilCtrl'];
47        $lng = $DIC['lng'];
48        $ilUser = $DIC['ilUser'];
49        $this->parent_gui = $parent_gui;
50        $this->obj_orgu = $obj_orgu;
51        $this->tpl = $tpl;
52        $this->ctrl = $ilCtrl;
53        $this->lng = $lng;
54        $this->user = $ilUser;
55        $this->initForm();
56    }
57
58
59    /**
60     * Update object
61     *
62     * @return bool
63     */
64    public function saveObject()
65    {
66        if (!$this->fillObject()) {
67            return false;
68        }
69        $this->obj_orgu->update();
70        $this->updateTranslation();
71        return true;
72    }
73
74    /**
75     * Add all fields to the form
76     */
77    protected function initForm()
78    {
79        $this->setFormAction($this->ctrl->getFormAction($this->parent_gui));
80        $this->setTitle($this->lng->txt('orgu_settings'));
81
82        $item = new ilTextInputGUI($this->lng->txt('title'), 'title');
83        $item->setRequired(true);
84        $item->setValue($this->obj_orgu->getTitle());
85        $this->addItem($item);
86
87        $item = new ilTextAreaInputGUI($this->lng->txt('description'), 'description');
88        $item->setValue($this->obj_orgu->getDescription());
89        $this->addItem($item);
90
91        $item = new ilFormSectionHeaderGUI();
92        $item->setTitle($this->lng->txt('orgu_type'));
93        $this->addItem($item);
94        $types = ilOrgUnitType::getAllTypes();
95        $options = array(0 => '');
96        /** @var ilOrgUnitType $type */
97        foreach ($types as $type) {
98            $options[$type->getId()] = $type->getTitle();
99        }
100        asort($options);
101        $item = new ilSelectInputGUI($this->lng->txt('orgu_type'), 'orgu_type');
102        $item->setOptions($options);
103        $item->setValue($this->obj_orgu->getOrgUnitTypeId());
104        $this->addItem($item);
105
106        $item = new ilFormSectionHeaderGUI();
107        $item->setTitle($this->lng->txt('ext_id'));
108        $this->addItem($item);
109
110        $item = new ilTextInputGUI($this->lng->txt('ext_id'), 'ext_id');
111        $item->setValue($this->obj_orgu->getImportId());
112        $this->addItem($item);
113
114        $this->addCommandButton('updateSettings', $this->lng->txt('save'));
115    }
116
117    /**
118     * Check validity of form and pass values from form to object
119     *
120     * @return bool
121     */
122    protected function fillObject()
123    {
124        $this->setValuesByPost();
125        if (!$this->checkInput()) {
126            return false;
127        }
128        $this->obj_orgu->setOrgUnitTypeId($this->getInput('orgu_type'));
129        $this->obj_orgu->setImportId($this->getInput('ext_id'));
130        $this->obj_orgu->setTitle($this->getInput('title'));
131        $this->obj_orgu->setDescription($this->getInput('description'));
132        return true;
133    }
134
135    /**
136     * Update title and description for the default language of translation
137     */
138    protected function updateTranslation()
139    {
140        $translations = $this->obj_orgu->getTranslations();
141        $lang_code_default = '';
142        $lang_codes = array();
143        foreach ($translations['Fobject'] as $translation) {
144            if ($translation['lang_default']) {
145                $lang_code_default = $translation['lang'];
146            }
147            $lang_codes[] = $translation['lang'];
148        }
149        $lang_code = (in_array($this->user->getLanguage(), $lang_codes)) ? $this->user->getLanguage() : $lang_code_default;
150        $this->obj_orgu->updateTranslation($this->getInput('title'), $this->getInput('description'), $lang_code, 0);
151    }
152}
153