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