1<?php
2
3/**
4 * Class ilOrgUnitTypeAdvancedMetaDataFormGUI
5 *
6 * @author Stefan Wanzenried <sw@studer-raimann.ch>
7 */
8class ilOrgUnitTypeAdvancedMetaDataFormGUI extends ilPropertyFormGUI
9{
10
11    /**
12     * @var ilOrgUnitType
13     */
14    protected $type;
15
16    /**
17     * @var ilTemplate
18     */
19    protected $tpl;
20
21    /**
22     * @var
23     */
24    protected $lng;
25
26    /**
27     * @var ilCtrl
28     */
29    protected $ctrl;
30
31    /**
32     * @var
33     */
34    protected $parent_gui;
35
36
37    public function __construct($parent_gui, ilOrgUnitType $type)
38    {
39        global $DIC;
40        $tpl = $DIC['tpl'];
41        $ilCtrl = $DIC['ilCtrl'];
42        $lng = $DIC['lng'];
43        $this->parent_gui = $parent_gui;
44        $this->type = $type;
45        $this->tpl = $tpl;
46        $this->ctrl = $ilCtrl;
47        $this->lng = $lng;
48        $this->lng->loadLanguageModule('meta');
49        $this->initForm();
50    }
51
52
53    /**
54     * Save object (create or update)
55     *
56     * @return bool
57     */
58    public function saveObject()
59    {
60        if (!$this->fillObject()) {
61            return false;
62        }
63        return true;
64    }
65
66    /**
67     * Add all fields to the form
68     */
69    protected function initForm()
70    {
71        $this->setFormAction($this->ctrl->getFormAction($this->parent_gui));
72        $this->setTitle($this->lng->txt('orgu_type_assign_amd_sets'));
73        $options = array();
74        $records = ilOrgUnitType::getAvailableAdvancedMDRecords();
75        /** @var ilAdvancedMDRecord $record */
76        foreach ($records as $record) {
77            $options[$record->getRecordId()] = $record->getTitle();
78        }
79        $selected = array();
80        $records_selected = $this->type->getAssignedAdvancedMDRecordIds();
81        foreach ($records_selected as $record_id) {
82            $selected[] = $record_id;
83        }
84        $item = new ilMultiSelectInputGUI($this->lng->txt('orgu_type_available_amd_sets'), 'amd_records');
85        $item->setOptions($options);
86        $item->setValue($selected);
87        $this->addItem($item);
88        $this->addCommandButton('updateAMD', $this->lng->txt('save'));
89    }
90
91    /**
92     * Check validity of form and pass values from form to object
93     *
94     * @return bool
95     */
96    protected function fillObject()
97    {
98        $this->setValuesByPost();
99        if (!$this->checkInput()) {
100            return false;
101        }
102        try {
103            // Assign and deassign amd records. A plugin could prevent those actions.
104            $record_ids_selected = (array) $this->getInput('amd_records');
105            $record_ids = $this->type->getAssignedAdvancedMDRecordIds(true);
106            $record_ids_removed = array_diff($record_ids, $record_ids_selected);
107            $record_ids_added = array_diff($record_ids_selected, $record_ids);
108            foreach ($record_ids_added as $record_id) {
109                $this->type->assignAdvancedMDRecord($record_id);
110            }
111            foreach ($record_ids_removed as $record_id) {
112                $this->type->deassignAdvancedMdRecord($record_id);
113            }
114            return true;
115        } catch (ilException $e) {
116            ilUtil::sendFailure($e->getMessage());
117            return false;
118        }
119    }
120}
121