ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitPositionFormGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
21
27{
28 public const F_AUTHORITIES = "authorities";
29 public const F_TITLE = 'title';
30 public const F_DESCRIPTION = 'description';
31 protected \ilOrgUnitPositionDBRepository $positionRepo;
32
33 protected \ILIAS\DI\Container $DIC;
34 protected \ilLanguage $lng;
35 protected \ilCtrl $ctrl;
36
37 public function __construct(
38 protected ?BaseCommands $parent_gui,
39 protected \ilOrgUnitPosition $object
40 ) {
41 global $DIC;
42
44 $this->positionRepo = $dic["repo.Positions"];
45
46 $this->parent_gui = $parent_gui;
47 $this->object = $object;
48 $this->lng = $DIC->language();
49 $this->ctrl = $DIC->ctrl();
50
51 $action_cmd = $object->getId() > 0 ? BaseCommands::CMD_UPDATE : BaseCommands::CMD_CREATE;
52 $form_action = $parent_gui->getSinglePosLinkTarget($action_cmd, $object->getId());
53 $this->setFormAction($form_action);
54
55 $this->initFormElements();
56 $this->initButtons();
57 $this->setTarget('_top');
58
60 }
61
62 protected function initFormElements(): void
63 {
64 global $DIC;
65 $lng = $DIC->language();
66
67 $te = new ilTextInputGUI($lng->txt(self::F_TITLE), self::F_TITLE);
68 $te->setRequired(true);
69 $this->addItem($te);
70
71 $te = new ilTextAreaInputGUI($lng->txt(self::F_DESCRIPTION), self::F_DESCRIPTION);
72 $this->addItem($te);
73
74 $m = new ilOrgUnitGenericMultiInputGUI($lng->txt(self::F_AUTHORITIES), self::F_AUTHORITIES);
75 $m->setShowLabel(true);
76 $m->setRenderOneForEmptyValue(false);
77 $m->setMulti(true);
78
80 $m->addInput($id);
81
83 $over_options = [];
84 $over_options[ilOrgUnitAuthority::OVER_EVERYONE] = $lng->txt('over_'
86 $over_options += $this->positionRepo->getArray('id', 'title');
87 $over->setOptions($over_options);
88 $m->addInput($over);
89
90 $available_scopes = [];
92 $txt = $lng->txt('scope_' . $scope);
93 $available_scopes[$scope] = $txt;
94 }
95
97 $scopes->setOptions($available_scopes);
98 $m->addInput($scopes);
99
100 $this->addItem($m);
101 }
102
103 private function initButtons(): void
104 {
105 if (!$this->object->getId()) {
106 $this->setTitle($this->txt('create'));
107 $this->addCommandButton(BaseCommands::CMD_CREATE, $this->txt(BaseCommands::CMD_CREATE));
108 $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
109 } else {
110 $this->setTitle($this->txt('update'));
111 $this->addCommandButton(BaseCommands::CMD_UPDATE, $this->txt(BaseCommands::CMD_UPDATE));
112 $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
113 }
114 }
115
116 public function fillForm(): void
117 {
118 $array = [
119 self::F_TITLE => $this->object->getTitle(),
120 self::F_DESCRIPTION => $this->object->getDescription(),
121 self::F_AUTHORITIES => $this->object->getAuthoritiesAsArray()
122 ];
123 $this->setValuesByArray($array);
124 }
125
126 public function fillObject(): bool
127 {
128 if (!$this->checkInput()) {
129 return false;
130 }
131
132 $authorities = ($this->getInput(self::F_AUTHORITIES) != '')
133 ? (array) $this->getInput(self::F_AUTHORITIES)
134 : [];
135
136 if (count($authorities) == 0) {
137 $this->object = $this->object
138 ->withTitle($this->getInput(self::F_TITLE))
139 ->withDescription($this->getInput(self::F_DESCRIPTION))
140 ->withAuthorities([]);
141 return true;
142 }
143
147 $new_authorities = [];
148 foreach ($authorities as $authority) {
149 $id = ($authority["id"] == '') ? null : (int) $authority["id"];
150
151 $new_authorities[] = $this->positionRepo->getAuthority($id)
152 ->withPositionId($this->object->getId())
153 ->withScope((int) $authority["scope"])
154 ->withOver((int) $authority["over"]);
155 }
156 $this->object = $this->object
157 ->withTitle($this->getInput(self::F_TITLE))
158 ->withDescription($this->getInput(self::F_DESCRIPTION))
159 ->withAuthorities($new_authorities);
160 return true;
161 }
162
163 public function saveObject(): bool
164 {
165 if ($this->fillObject() === false) {
166 return false;
167 }
168
169 $this->object = $this->positionRepo->store($this->object);
170 return true;
171 }
172
173 private function txt(string $key): string
174 {
175 return $this->lng->txt($key);
176 }
177
178 private function infoTxt(string $key): string
179 {
180 return $this->lng->txt($key . '_info');
181 }
182}
setFormAction(string $a_formaction)
setTarget(string $a_target)
This class represents a hidden form property in a property form.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilOrgUnitPositionFormGUI.
ilOrgUnitPositionDBRepository $positionRepo
__construct(protected ?BaseCommands $parent_gui, protected \ilOrgUnitPosition $object)
Class ilOrgUnitPosition.
This class represents a property form user interface.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
This class represents a selection list property in a property form.
This class represents a text area property in a property form.
This class represents a text property in a property form.
$txt
Definition: error.php:31
$scope
Definition: ltiregstart.php:51
$dic
Definition: ltiresult.php:33
$scopes
Definition: ltitoken.php:96
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc