1<?php
2
3/**
4 * Class ilOrgUnitTypeFormGUI
5 *
6 * @author Stefan Wanzenried <sw@studer-raimann.ch>
7 */
8class ilOrgUnitTypeFormGUI extends ilPropertyFormGUI
9{
10
11    /**
12     * @var ilOrgUnitType
13     */
14    protected $type;
15    /**
16     * @var ilTemplate
17     */
18    protected $tpl;
19    /**
20     * @var
21     */
22    protected $lng;
23    /**
24     * @var ilCtrl
25     */
26    protected $ctrl;
27    /**
28     * @var
29     */
30    protected $parent_gui;
31
32
33    public function __construct($parent_gui, ilOrgUnitType $type)
34    {
35        global $DIC;
36        $tpl = $DIC['tpl'];
37        $ilCtrl = $DIC['ilCtrl'];
38        $lng = $DIC['lng'];
39        $this->parent_gui = $parent_gui;
40        $this->type = $type;
41        $this->tpl = $tpl;
42        $this->ctrl = $ilCtrl;
43        $this->lng = $lng;
44        $this->lng->loadLanguageModule('meta');
45        $this->initForm();
46    }
47
48
49    /**
50     * Save object (create or update)
51     *
52     * @return bool
53     */
54    public function saveObject()
55    {
56        if (!$this->fillObject()) {
57            return false;
58        }
59        try {
60            $this->type->save();
61
62            return true;
63        } catch (ilException $e) {
64            ilUtil::sendFailure($e->getMessage());
65
66            return false;
67        }
68    }
69
70
71    /**
72     * Add all fields to the form
73     */
74    protected function initForm()
75    {
76        $this->setFormAction($this->ctrl->getFormAction($this->parent_gui));
77        $title = $this->type->getId() ? $this->lng->txt('orgu_type_edit') : $this->lng->txt('orgu_type_add');
78        $this->setTitle($title);
79        $item = new ilSelectInputGUI($this->lng->txt('default_language'), 'default_lang');
80        $item->setValue($this->type->getDefaultLang());
81        $languages = $this->lng->getInstalledLanguages();
82        $options = array();
83        foreach ($languages as $lang_code) {
84            $options[$lang_code] = $this->lng->txt("meta_l_{$lang_code}");
85        }
86        $item->setOptions($options);
87        $item->setRequired(true);
88        $this->addItem($item);
89
90        foreach ($languages as $lang_code) {
91            $this->addTranslationInputs($lang_code);
92        }
93
94        if ($this->type->getId()) {
95            $this->addCommandButton('update', $this->lng->txt('save'));
96        } else {
97            $this->addCommandButton('create', $this->lng->txt('create'));
98        }
99    }
100
101
102    /**
103     * Check validity of form and pass values from form to object
104     *
105     * @return bool
106     */
107    protected function fillObject()
108    {
109        $this->setValuesByPost();
110        if (!$this->checkInput()) {
111            return false;
112        }
113
114        $success = true;
115        try {
116            $this->type->setDefaultLang($this->getInput('default_lang'));
117            foreach ($this->lng->getInstalledLanguages() as $lang_code) {
118                $title = $this->getInput("title_{$lang_code}");
119                $description = $this->getInput("description_{$lang_code}");
120                $this->type->setTitle($title, $lang_code);
121                $this->type->setDescription($description, $lang_code);
122            }
123        } catch (ilOrgUnitTypePluginException $e) {
124            ilUtil::sendFailure($e->getMessage());
125            $success = false;
126        }
127
128        return $success;
129    }
130
131
132    /**
133     * Add a text and textarea input per language
134     *
135     * @param $a_lang_code
136     */
137    protected function addTranslationInputs($a_lang_code)
138    {
139        $section = new ilFormSectionHeaderGUI();
140        $section->setTitle($this->lng->txt("meta_l_{$a_lang_code}"));
141        $this->addItem($section);
142        $item = new ilTextInputGUI($this->lng->txt('title'), "title_{$a_lang_code}");
143        $item->setValue($this->type->getTitle($a_lang_code));
144        $this->addItem($item);
145        $item = new ilTextAreaInputGUI($this->lng->txt('description'), "description_{$a_lang_code}");
146        $item->setValue($this->type->getDescription($a_lang_code));
147        $this->addItem($item);
148    }
149}
150