ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMMSubitemFormGUI.php
Go to the documentation of this file.
1 <?php
2 
9 
15 {
16  use Hasher;
17 
18  const F_TITLE = "title";
19  const F_TYPE = "type";
20  const F_PARENT = "parent";
21  const F_ACTIVE = "active";
22  const F_ICON = "icon";
23  const F_ROLE_BASED_VISIBILITY = "role_based_visibility";
27  private $repository;
31  private $form;
35  protected $lng;
39  protected $ctrl;
43  protected $ui_fa;
47  protected $ui_re;
51  private $item_facade;
52 
63  {
64  $this->ctrl = $ctrl;
65  $this->ui_fa = $ui_fa;
66  $this->ui_re = $ui_re;
67  $this->lng = $lng;
68  $this->item_facade = $item;
69  $this->repository = $repository;
70  if (!$this->item_facade->isEmpty()) {
71  $this->ctrl->saveParameterByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::IDENTIFIER);
72  }
73 
74  $this->initForm();
75  }
76 
77  private function initForm() : void
78  {
79  // TITLE
80  $txt = function ($id) : string {
81  return $this->lng->txt($id);
82  };
83  $f = function () : InputFactory {
84  return $this->ui_fa->input();
85  };
86 
87  $title = $f()->field()->text($txt('sub_title_default'), $txt('sub_title_default_byline'));
88  if (!$this->item_facade->isEmpty()) {
89  $title = $title->withValue($this->item_facade->getDefaultTitle());
90  }
91  $items[self::F_TITLE] = $title;
92 
93  // TYPE
94  if (($this->item_facade->isEmpty() || $this->item_facade->isCustom())) {
95  $type_groups = $this->getTypeGroups($f);
96  $type = $f()->field()->switchableGroup($type_groups, $txt('sub_type'), $txt('sub_type_byline'))->withRequired(true);
97  if (!$this->item_facade->isEmpty()) {
98  $string = $this->item_facade->getType() === '' ? Link::class : $this->item_facade->getType();
99  $type = $type->withValue($this->hash($string));
100  } else {
101  $type = $type->withValue($this->hash(Link::class));
102  }
103  $items[self::F_TYPE] = $type;
104  }
105 
106  // ICON
107  if ($this->item_facade->supportsCustomIcon()) {
108  // ICON
109  $icon = $f()->field()->file(new ilMMUploadHandlerGUI(), $txt('sub_icon'))
110  ->withByline($txt('sub_icon_byline'))
111  ->withAcceptedMimeTypes([ilMimeTypeUtil::IMAGE__SVG_XML])
112  ->withMaxFileSize(ilMMUploadHandlerGUI::MAX_FILE_SIZE);
113  if ($this->item_facade->getIconID() !== null) {
114  $icon = $icon->withValue([$this->item_facade->getIconID()]);
115  }
116 
117  $items[self::F_ICON] = $icon;
118  }
119 
120  // PARENT
121  $parent = $f()->field()->select($txt('sub_parent'), $this->repository->getPossibleParentsForFormAndTable())
122  ->withRequired(true);
123  if (!$this->item_facade->isEmpty() && !$this->item_facade->isInLostItem()) {
124  $parent = $parent->withValue($this->item_facade->getParentIdentificationString());
125  } else {
126  $array = array_keys($this->repository->getPossibleParentsForFormAndTable());
127  $parent = $parent->withValue(reset($array));
128  }
129  $items[self::F_PARENT] = $parent;
130 
131  // ACTIVE
132  $active = $f()->field()->checkbox($txt('sub_active'), $txt('sub_active_byline'));
133  $active = $active->withValue($this->item_facade->isActivated());
134  $items[self::F_ACTIVE] = $active;
135 
136  // ROLE BASED VISIBILITY
137  if($this->item_facade->supportsRoleBasedVisibility()) {
138  $access = new ilObjMainMenuAccess();
139  $value_role_based_visibility = NULL;
140  $global_roles = $access->getGlobalRoles();
141  $global_role_ids = $this->item_facade->getGlobalRoleIDs();
142  if($this->item_facade->hasRoleBasedVisibility() && !empty($global_role_ids)) {
143  // remove deleted roles, see https://mantis.ilias.de/view.php?id=34936
144  $value_role_based_visibility[0] = array_intersect(
145  $global_role_ids,
146  array_keys($global_roles)
147  );
148  }
149  $role_based_visibility = $f()->field()->optionalGroup(
150  [
151  $f()->field()->multiSelect(
152  $txt('sub_global_roles'),
153  $global_roles
154  )->withRequired(true)
155  ],
156  $txt('sub_role_based_visibility'),
157  $txt('sub_role_based_visibility_byline')
158  )->withValue($value_role_based_visibility);
159  $items[self::F_ROLE_BASED_VISIBILITY] = $role_based_visibility;
160  }
161 
162  // RETURN FORM
163  if ($this->item_facade->isEmpty()) {
164  $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_ADD), "");
165  $this->form = $f()->container()->form()
166  ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_CREATE), [$section]);
167  } else {
168  $section = $f()->field()->section($items, $txt(ilMMSubItemGUI::CMD_EDIT), "");
169  $this->form = $f()->container()->form()
170  ->standard($this->ctrl->getLinkTargetByClass(ilMMSubItemGUI::class, ilMMSubItemGUI::CMD_UPDATE), [$section]);
171  }
172  }
173 
174  public function save() : bool
175  {
176  global $DIC;
177  $r = new ilMMItemRepository();
178  $this->form = $this->form->withRequest($DIC->http()->request());
179  $data = $this->form->getData();
180 
181  if (is_null($data)) {
182  return false;
183  }
184 
185  $this->item_facade->setAction((string) $data[0]['action']);
186  $this->item_facade->setDefaultTitle((string) $data[0][self::F_TITLE]);
187  $this->item_facade->setActiveStatus((bool) $data[0][self::F_ACTIVE]);
188  $this->item_facade->setRoleBasedVisibility((bool) $data[0][self::F_ROLE_BASED_VISIBILITY]);
189  if((bool) $data[0][self::F_ROLE_BASED_VISIBILITY] AND (bool) !empty($data[0][self::F_ROLE_BASED_VISIBILITY])) {
190  $this->item_facade->setGlobalRoleIDs((array) $data[0][self::F_ROLE_BASED_VISIBILITY][0]);
191  }
192  if ((string) $data[0][self::F_PARENT]) {
193  $this->item_facade->setParent((string) $data[0][self::F_PARENT]);
194  }
195  $this->item_facade->setIsTopItm(false);
196 
197  if ($this->item_facade->isEmpty()) {
198  $type = $this->unhash((string) ($data[0][self::F_TYPE][0]));
199  $this->item_facade->setType($type);
200  $r->createItem($this->item_facade);
201  }
202 
203  if ($this->item_facade->supportsCustomIcon()) {
204  $icon = (string) $data[0][self::F_ICON][0];
205  $this->item_facade->setIconID($icon);
206  }
207 
208  if ($this->item_facade->isCustom()) {
209  $type = $this->item_facade->getType();
210  $type_specific_data = (array) $data[0][self::F_TYPE][1];
211  $type_handler = $this->repository->getTypeHandlerForType($type);
212  $type_handler->saveFormFields($this->item_facade->identification(), $type_specific_data);
213  }
214 
215  $r->updateItem($this->item_facade);
216 
217  return true;
218  }
219 
220  public function getHTML()
221  {
222  return $this->ui_re->render([$this->form]);
223  }
224 
229  private function getTypeGroups(Closure $f) : array
230  {
231  $type_groups = [];
232  $type_informations = $this->repository->getPossibleSubItemTypesWithInformation();
233  foreach ($type_informations as $classname => $information) {
234  if ($this->item_facade->isEmpty()
235  || (!$this->item_facade->isEmpty() && $classname === $this->item_facade->getType() && $this->item_facade->isCustom())
236  ) { // https://mantis.ilias.de/view.php?id=24152
237  $inputs = $this->repository->getTypeHandlerForType($classname)->getAdditionalFieldsForSubForm($this->item_facade->identification());
238  $type_groups[$this->hash($classname)] = $f()->field()->group($inputs, $information->getTypeNameForPresentation());
239  }
240  }
241 
242  return $type_groups;
243  }
244 }
An entity that renders components to a string output.
Definition: Renderer.php:14
This class provides processing control methods.
$data
Definition: storeScorm.php:23
$type
Class ilMMItemRepository.
Class ilMMUploadHandlerGUI.
$section
Definition: Utf8Test.php:83
repository()
Definition: repository.php:5
This is how the factory for UI elements looks.
Definition: Factory.php:17
global $DIC
Definition: goto.php:24
Class ilMMSubitemFormGUI.
$txt
Definition: error.php:13
__construct(ilCtrl $ctrl, Factory $ui_fa, Renderer $ui_re, ilLanguage $lng, ilMMItemFacadeInterface $item, ilMMItemRepository $repository)
ilMMSubitemFormGUI constructor.
Interface ilMMItemFacadeInterface.