ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilMMTopItemFormGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 
35 {
36  use Hasher;
37 
41  private const F_ICON = 'icon';
42 
43  private Standard $form;
44 
52  public const F_ACTIVE = 'active';
53  public const F_TITLE = 'title';
54  public const F_TYPE = 'type';
55  public const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
56 
57  public function __construct(
58  protected ilCtrl $ctrl,
59  protected ILIAS\UI\Factory $ui_fa,
60  protected Renderer $ui_re,
61  protected ilLanguage $lng,
62  private Services $http,
63  private ilMMItemFacadeInterface $item_facade,
64  private ilMMItemRepository $repository
65  ) {
66  $this->access = new ilObjMainMenuAccess();
67  if (!$this->item_facade->isEmpty()) {
68  $this->ctrl->saveParameterByClass(ilMMTopItemGUI::class, ilMMAbstractItemGUI::IDENTIFIER);
69  }
70 
71  $this->initForm();
72  }
73 
74  private function initForm(): void
75  {
76  $txt = (fn($key): string => $this->lng->txt($key));
77  $f = (fn(): InputFactory => $this->ui_fa->input());
78 
79  // TITLE
80  $title = $f()->field()->text($txt('topitem_title_default'), $txt('topitem_title_default_byline'))
81  ->withRequired(true);
82  if (!$this->item_facade->isEmpty()) {
83  $title = $title->withValue($this->item_facade->getDefaultTitle());
84  }
85 
86  $items[self::F_TITLE] = $title;
87 
88  if ($this->item_facade->supportsCustomIcon()) {
89  // ICON
90  $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('topitem_icon'))
91  ->withByline($txt('topitem_icon_byline'))
92  ->withAcceptedMimeTypes([MimeType::IMAGE__SVG_XML]);
93  if ($this->item_facade->getIconID() !== null) {
94  $icon = $icon->withValue([$this->item_facade->getIconID()]);
95  }
96 
97  $items[self::F_ICON] = $icon;
98  }
99 
100  // TYPE
101  if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
102  $type_groups = $this->getTypeGroups($f, $this->item_facade->isEmpty());
103  $type = $f()->field()->switchableGroup(
104  $type_groups,
105  $txt('topitem_type'),
106  $txt('topitem_type_byline')
107  )->withRequired(true);
108  if (!$this->item_facade->isEmpty()) {
109  $string = $this->item_facade->getType() === '' ? TopParentItem::class : $this->item_facade->getType();
110  $type = $type->withValue($this->hash($string));
111  } else {
112  $type = $type->withValue($this->hash(TopParentItem::class));
113  }
114  $items[self::F_TYPE] = $type;
115  }
116 
117  // ACTIVE
118  $active = $f()->field()->checkbox($txt('topitem_active'), $txt('topitem_active_byline'));
119  $active = $active->withValue($this->item_facade->isActivated());
120  $items[self::F_ACTIVE] = $active;
121 
122  // ROLE BASED VISIBILITY
123  if ($this->item_facade->supportsRoleBasedVisibility()) {
124  $value_role_based_visibility = null;
125  $global_roles = $this->access->getGlobalRoles();
126  $global_role_ids = $this->item_facade->getGlobalRoleIDs();
127  if ($this->item_facade->hasRoleBasedVisibility() && !empty($global_role_ids)) {
128  // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
129  $value_role_based_visibility[0] = array_intersect(
130  $global_role_ids,
131  array_keys($global_roles)
132  );
133  }
134  $role_based_visibility = $f()->field()->optionalGroup(
135  [
136  $f()->field()->multiSelect(
137  $txt('sub_global_roles'),
138  $global_roles
139  )->withRequired(false)
140  ],
141  $txt('sub_role_based_visibility'),
142  $txt('sub_role_based_visibility_byline')
143  )->withValue($value_role_based_visibility);
144  $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
145  }
146 
147  // RETURN FORM
148  if ($this->item_facade->isEmpty()) {
149  $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_ADD), "");
150  $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(
151  ilMMTopItemGUI::class,
153  ), [$section]);
154  } else {
155  $section = $f()->field()->section($items, $txt(ilMMTopItemGUI::CMD_EDIT), "");
156  $this->form = $f()->container()->form()->standard($this->ctrl->getLinkTargetByClass(
157  ilMMTopItemGUI::class,
159  ), [$section]);
160  }
161  }
162 
163  public function save(): bool
164  {
165  $this->form = $this->form->withRequest($this->http->request());
166  $data = $this->form->getData();
167  if (is_null($data)) {
168  return false;
169  }
170 
171  $this->item_facade->setAction((string) ($data[0]['action'] ?? ''));
172  $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
173  $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
174  if ($this->item_facade->supportsRoleBasedVisibility()) {
175  $this->item_facade->setRoleBasedVisibility((bool) $data[0][self::F_ROLE_BASED_VISIBILITY]);
176  if ($data[0][self::F_ROLE_BASED_VISIBILITY] && !empty($data[0][self::F_ROLE_BASED_VISIBILITY])) {
177  $this->item_facade->setGlobalRoleIDs((array) $data[0][self::F_ROLE_BASED_VISIBILITY][0]);
178  }
179  }
180 
181  $this->item_facade->setIsTopItm(true);
182 
183  if ($this->item_facade->isEmpty()) {
184  $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
185  $this->item_facade->setType($type);
186  $this->repository->createItem($this->item_facade);
187  }
188 
189  if ($this->item_facade->supportsCustomIcon()) {
190  $icon = (string) ($data[0][self::F_ICON][0] ?? '');
191  $this->item_facade->setIconID($icon);
192  }
193 
194  if ($this->item_facade->isCustom()) {
195  $type = $this->item_facade->getType();
196  $type_specific_data = (array) $data[0][self::F_TYPE][1];
197  $type_handler = $this->repository->getTypeHandlerForType($type);
198  $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
199  }
200 
201  $this->repository->updateItem($this->item_facade);
202 
203  return true;
204  }
205 
209  public function getHTML(): string
210  {
211  return $this->ui_re->render([$this->form]);
212  }
213 
218  private function getTypeGroups(Closure $f, bool $new): array
219  {
220  $type_groups = [];
221  $type_informations = $this->repository->getPossibleTopItemTypesWithInformation($new);
222  foreach ($type_informations as $classname => $information) {
223  if ($this->item_facade->isEmpty()
224  || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
225  ) { // https://mantis.ilias.de/view.php?id=24152
226  $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
227  $type_groups[$this->hash($classname)] = $f()->field()->group(
228  $inputs,
229  $information->getTypeNameForPresentation()
230  );
231  }
232  }
233 
234  return $type_groups;
235  }
236 }
const F_ACTIVE
ilMMTopItemFormGUI constructor.
Interface Observer Contains several chained tasks and infos about them.
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
$http
Definition: deliver.php:30
Class ilMMItemRepository.
Class ilMMUploadHandlerGUI.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
getTypeGroups(Closure $f, bool $new)
static http()
Fetches the global http state from ILIAS.
This is how the factory for UI elements looks.
Definition: Factory.php:37
__construct(protected ilCtrl $ctrl, protected ILIAS\UI\Factory $ui_fa, protected Renderer $ui_re, protected ilLanguage $lng, private Services $http, private ilMMItemFacadeInterface $item_facade, private ilMMItemRepository $repository)
$txt
Definition: error.php:31
This describes a standard form.
Definition: Standard.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
form( $class_path, string $cmd, string $submit_caption="")
global $lng
Definition: privfeed.php:31
Class ilMMTopItemFormGUI.
ilObjMainMenuAccess $access
Interface ilMMItemFacadeInterface.