1<?php
2
3/**
4 * Class ilOrgUnitPermissionTableGUI
5 *
6 * @author            Fabian Schmid <fs@studer-raimann.ch>
7 */
8class ilOrgUnitPermissionTableGUI extends ilTable2GUI
9{
10
11    /**
12     * @var null|int
13     */
14    private $ref_id = null;
15
16
17    /**
18     * ilOrgUnitPermissionTableGUI constructor.
19     *
20     * @param \ILIAS\Modules\OrgUnit\ARHelper\BaseCommands $a_parent_obj
21     * @param string                                       $a_parent_cmd
22     * @param string                                       $a_ref_id
23     */
24    public function __construct($a_parent_obj, $a_parent_cmd, $a_ref_id)
25    {
26        global $ilCtrl, $tpl;
27
28        parent::__construct($a_parent_obj, $a_parent_cmd);
29
30        $this->lng->loadLanguageModule('rbac');
31        $this->lng->loadLanguageModule("orgu");
32
33        $this->ref_id = $a_ref_id;
34
35        $this->setId('objpositionperm_' . $this->ref_id);
36
37        $tpl->addJavaScript('./Services/AccessControl/js/ilPermSelect.js');
38
39        $this->setTitle($this->lng->txt('org_permission_settings'));
40        $this->setEnableHeader(true);
41        $this->disable('sort');
42        $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
43        $this->disable('numinfo');
44        $this->setRowTemplate("tpl.obj_position_perm_row.html", "Modules/OrgUnit");
45        $this->setShowRowsSelector(false);
46        $this->setDisableFilterHiding(true);
47        $this->setNoEntriesText($this->lng->txt('msg_no_roles_of_type'));
48
49        $this->addCommandButton(ilPermissionGUI::CMD_SAVE_POSITIONS_PERMISSIONS, $this->lng->txt('save'));
50    }
51
52
53    /**
54     * @return int
55     */
56    public function getRefId()
57    {
58        return (int) $this->ref_id;
59    }
60
61
62    /**
63     * @return int Object-ID of current object
64     */
65    public function getObjId()
66    {
67        return (int) ilObject::_lookupObjId($this->getRefId());
68    }
69
70
71    /**
72     * @return string
73     */
74    public function getObjType()
75    {
76        return (string) ilObject::_lookupType($this->getObjId());
77    }
78
79
80    /**
81     * @param array $row
82     *
83     * @return bool
84     */
85    public function fillRow($row)
86    {
87        // Select all
88        if (isset($row['show_select_all'])) {
89            $this->fillSelectAll($row);
90
91            return true;
92        }
93        if (isset($row['header_command'])) {
94            $this->fillHeaderCommand($row);
95
96            return true;
97        }
98
99        $objdefinition = $this->dic()['objDefinition'];
100        $is_plugin = $objdefinition->isPlugin($this->getObjType());
101
102        foreach ($row as $permission) {
103            /**
104             * @var $operation \ilOrgUnitOperation
105             * @var $position  \ilOrgUnitPosition
106             */
107            $position = $permission["position"];
108            $op_id = $permission["op_id"];
109            $operation = $permission["operation"];
110            $this->tpl->setCurrentBlock('position_td');
111            $this->tpl->setVariable('POSITION_ID', $position->getId());
112            $this->tpl->setVariable('PERM_ID', $op_id);
113
114            if ($is_plugin) {
115                $label = ilObjectPlugin::lookupTxtById($this->getObjType(), $operation->getOperationString());
116            } else {
117                $label = $this->dic()->language()->txt('org_op_' . $operation->getOperationString());
118            }
119
120            $this->tpl->setVariable('TXT_PERM', $label);
121            $this->tpl->setVariable('PERM_LONG', $op_id);
122
123            if ($permission['permission_set']) {
124                $this->tpl->setVariable('PERM_CHECKED', 'checked="checked"');
125            }
126            if ($permission['from_template']) {
127                $this->tpl->setVariable('PERM_DISABLED', 'disabled="disabled"');
128            }
129
130            $this->tpl->parseCurrentBlock();
131        }
132    }
133
134
135    public function collectData()
136    {
137        $positions = ilOrgUnitPosition::get();
138
139        $this->initColumns($positions);
140
141        $perms = [];
142
143        $operations = ilOrgUnitOperationQueries::getOperationsForContextName($this->getObjType());
144        $ops_ids = [];
145        $from_templates = [];
146        foreach ($operations as $op) {
147            $ops_ids[] = $op->getOperationId();
148
149            $ops = [];
150            foreach ($positions as $position) {
151                $ilOrgUnitPermission = ilOrgUnitPermissionQueries::getSetForRefId($this->getRefId(), $position->getId());
152
153                $is_template = $ilOrgUnitPermission->isTemplate();
154                $from_templates[$position->getId()] = $is_template;
155
156                $ops[] = [
157                    "op_id" => $op->getOperationId(),
158                    "operation" => $op,
159                    "position" => $position,
160                    "permission" => $ilOrgUnitPermission,
161                    "permission_set" => $ilOrgUnitPermission->isOperationIdSelected($op->getOperationId()),
162                    "from_template" => $is_template,
163                ];
164            }
165            $perms[] = $ops;
166        }
167
168        $perms[] = [
169            "show_select_all" => true,
170            "positions" => $positions,
171            "ops" => $ops_ids,
172            "template" => $from_templates,
173        ];
174        if (ilOrgUnitGlobalSettings::getInstance()
175            ->isPositionAccessActiveForObject($this->getObjId())
176        ) {
177            $perms[] = [
178                "header_command" => true,
179                "positions" => $positions,
180                "template" => $from_templates,
181            ];
182        }
183
184        $this->setData($perms);
185
186        return;
187    }
188
189
190    /**
191     * @param array $positions
192     *
193     * @return bool
194     */
195    protected function initColumns(array $positions)
196    {
197        foreach ($positions as $position) {
198            $this->addColumn($position->getTitle(), '', '', '', false, $position->getDescription());
199        }
200
201        return true;
202    }
203
204
205    /**
206     * @return \ILIAS\DI\Container
207     */
208    private function dic()
209    {
210        return $GLOBALS['DIC'];
211    }
212
213
214    /**
215     * @param $row
216     */
217    protected function fillSelectAll($row)
218    {
219        /**
220         * @var $position \ilOrgUnitPosition
221         */
222        foreach ($row["positions"] as $position) {
223            $this->tpl->setCurrentBlock('position_select_all');
224            $id = $position->getId();
225            $this->tpl->setVariable('JS_ROLE_ID', $id);
226            $this->tpl->setVariable('JS_SUBID', 0);
227            $this->tpl->setVariable('JS_ALL_PERMS', "['" . implode("','", $row['ops']) . "']");
228            $this->tpl->setVariable('JS_FORM_NAME', $this->getFormName());
229            $this->tpl->setVariable('TXT_SEL_ALL', $this->lng->txt('select_all'));
230            if ($row["template"][$id]) {
231                $this->tpl->setVariable('ALL_DISABLED', "disabled='disabled'");
232            }
233            $this->tpl->parseCurrentBlock();
234        }
235    }
236
237
238    /**
239     * @param $row
240     */
241    protected function fillHeaderCommand($row)
242    {
243        /**
244         * @var $position \ilOrgUnitPosition
245         */
246        foreach ($row["positions"] as $position) {
247            $this->tpl->setCurrentBlock('header_command');
248            $this->tpl->setVariable('POSITION_ID', $position->getId());
249            $this->tpl->setVariable('HEADER_COMMAND_TXT', $this->dic()
250                ->language()
251                ->txt('positions_override_operations'));
252            if (ilOrgUnitPermissionQueries::hasLocalSet($this->getRefId(), $position->getId())) {
253                $this->tpl->setVariable('HEADER_CHECKED', "checked='checked'");
254            }
255
256            $this->tpl->parseCurrentBlock();
257        }
258    }
259}
260