ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitPositionFormGUI.php
Go to the documentation of this file.
1 <?php
19 declare(strict_types=1);
21 
27 {
28  public const F_AUTHORITIES = "authorities";
29  protected \ilOrgUnitPosition $object;
30  public const F_TITLE = 'title';
31  public const F_DESCRIPTION = 'description';
32  protected \ilOrgUnitPositionDBRepository $positionRepo;
33 
35  protected \ILIAS\DI\Container $DIC;
36  protected \ilLanguage $lng;
37  protected \ilCtrl $ctrl;
38 
39  public function __construct(BaseCommands $parent_gui, \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 
50  $this->ctrl = $DIC->ctrl();
51  $this->ctrl->saveParameter($parent_gui, 'arid');
52  $this->setFormAction($this->ctrl->getFormAction($this->parent_gui));
53  $this->initFormElements();
54  $this->initButtons();
55  $this->setTarget('_top');
56 
58  }
59 
60  protected function initFormElements(): void
61  {
62  global $DIC;
63  $lng = $DIC->language();
64 
65  $te = new ilTextInputGUI($lng->txt(self::F_TITLE), self::F_TITLE);
66  $te->setRequired(true);
67  $this->addItem($te);
68 
69  $te = new ilTextAreaInputGUI($lng->txt(self::F_DESCRIPTION), self::F_DESCRIPTION);
70  $this->addItem($te);
71 
72  $m = new ilOrgUnitGenericMultiInputGUI($lng->txt(self::F_AUTHORITIES), self::F_AUTHORITIES);
73  $m->setShowLabel(true);
74  $m->setRenderOneForEmptyValue(false);
75  $m->setMulti(true);
76 
78  $m->addInput($id);
79 
81  $over_options = [];
82  $over_options[ilOrgUnitAuthority::OVER_EVERYONE] = $lng->txt('over_'
84  $over_options += $this->positionRepo->getArray('id', 'title');
85  $over->setOptions($over_options);
86  $m->addInput($over);
87 
88  $available_scopes = [];
89  foreach (ilOrgUnitAuthority::getScopes() as $scope) {
90  $txt = $lng->txt('scope_' . $scope);
91  $available_scopes[$scope] = $txt;
92  }
93 
95  $scopes->setOptions($available_scopes);
96  $m->addInput($scopes);
97 
98  $this->addItem($m);
99  }
100 
101  private function initButtons(): void
102  {
103  if (!$this->object->getId()) {
104  $this->setTitle($this->txt('create'));
105  $this->addCommandButton(BaseCommands::CMD_CREATE, $this->txt(BaseCommands::CMD_CREATE));
106  $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
107  } else {
108  $this->setTitle($this->txt('update'));
109  $this->addCommandButton(BaseCommands::CMD_UPDATE, $this->txt(BaseCommands::CMD_UPDATE));
110  $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
111  }
112  }
113 
114  public function fillForm(): void
115  {
116  $array = [
117  self::F_TITLE => $this->object->getTitle(),
118  self::F_DESCRIPTION => $this->object->getDescription(),
119  self::F_AUTHORITIES => $this->object->getAuthoritiesAsArray()
120  ];
121  $this->setValuesByArray($array);
122  }
123 
124  public function fillObject(): bool
125  {
126  if (!$this->checkInput()) {
127  return false;
128  }
129 
130  $authorities = ($this->getInput(self::F_AUTHORITIES) != '')
131  ? (array) $this->getInput(self::F_AUTHORITIES)
132  : [];
133 
134  if (count($authorities) == 0) {
135  $this->object = $this->object
136  ->withTitle($this->getInput(self::F_TITLE))
137  ->withDescription($this->getInput(self::F_DESCRIPTION))
138  ->withAuthorities([]);
139  return true;
140  }
141 
145  $new_authorities = [];
146  foreach ($authorities as $authority) {
147  $id = ($authority["id"] == '') ? null : (int) $authority["id"];
148 
149  $new_authorities[] = $this->positionRepo->getAuthority($id)
150  ->withPositionId($this->object->getId())
151  ->withScope((int) $authority["scope"])
152  ->withOver((int) $authority["over"]);
153  }
154  $this->object = $this->object
155  ->withTitle($this->getInput(self::F_TITLE))
156  ->withDescription($this->getInput(self::F_DESCRIPTION))
157  ->withAuthorities($new_authorities);
158  return true;
159  }
160 
161  public function saveObject(): bool
162  {
163  if ($this->fillObject() === false) {
164  return false;
165  }
166 
167  $this->object = $this->positionRepo->store($this->object);
168  return true;
169  }
170 
171  private function txt(string $key): string
172  {
173  return $this->lng->txt($key);
174  }
175 
176  private function infoTxt(string $key): string
177  {
178  return $this->lng->txt($key . '_info');
179  }
180 }
$scope
Definition: ltiregstart.php:53
This class represents a selection list property in a property form.
This class represents a property form user interface.
$scopes
Definition: ltitoken.php:99
setTarget(string $a_target)
setOptions(array $a_options)
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-...
Class ilOrgUnitPositionFormGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(VocabulariesInterface $vocabularies)
setFormAction(string $a_formaction)
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(BaseCommands $parent_gui, \ilOrgUnitPosition $object)
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
$txt
Definition: error.php:14
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
This class represents a text area property in a property form.
$dic
Definition: result.php:32
ilOrgUnitPositionDBRepository $positionRepo