ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilPCAMDFormGUI.php
Go to the documentation of this file.
1 <?php
2 
22 
30 {
32  protected \ILIAS\DI\UIServices $ui;
35 
36  public function __construct(
37  ilPageObject $a_pg_obj,
38  ?ilPageContent $a_content_obj,
39  string $a_hier_id,
40  string $a_pc_id = ""
41  ) {
42  global $DIC;
43  $this->ctrl = $DIC->ctrl();
44  $this->ui = $DIC->ui();
45  $this->http_request = $DIC->http()->request();
46  $this->port_request = $DIC->portfolio()
47  ->internal()
48  ->gui()
49  ->standardRequest();
50  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
51 
52  $this->lng->loadLanguageModule("prtt");
53  $this->lng->loadLanguageModule("prtf");
54  }
55 
56  public function executeCommand(): void
57  {
58  // get next class that processes or forwards current command
59  $next_class = $this->ctrl->getNextClass($this);
60 
61  // get current command
62  $cmd = $this->ctrl->getCmd();
63 
64  switch ($next_class) {
65  case "ilpropertyformgui":
66  $form = $this->getPortfolioForm(true);
67  $this->ctrl->forwardCommand($form);
68  break;
69 
70  default:
71  $ret = $this->$cmd();
72  break;
73  }
74  }
75 
76  protected function isTemplate(): bool
77  {
78  return ($this->getPage()->getParentType() === "prtt");
79  }
80 
81  public function insert(?Form\Standard $form = null): void
82  {
83  $tpl = $this->tpl;
84 
85  $this->displayValidationError();
86 
87  if (!$form) {
88  $form = $this->getTemplateForm();
89  }
90  $tpl->setContent($this->ui->renderer()->render($form));
91  }
92 
93  public function edit(): void
94  {
95  if ($this->isTemplate()) {
96  $this->editTemplate();
97  return;
98  }
99  $this->editPortfolio();
100  }
101 
102  public function editTemplate(?Form\Standard $form = null): void
103  {
104  $tpl = $this->tpl;
105 
106  $this->displayValidationError();
107 
108  if (!$form) {
109  $form = $this->getTemplateForm(true);
110  }
111  $tpl->setContent($this->ui->renderer()->render($form));
112  }
113 
114  public function getTemplateForm(bool $edit = false): Form\Standard
115  {
116  $ui = $this->ui;
117  $f = $ui->factory();
118  $ctrl = $this->ctrl;
119 
120  $selected = [];
121  if ($edit) {
122  $selected = $this->content_obj->getRecordIds();
123  }
124  $recs = $this->getAdvRecords();
125  $fields = array();
126  foreach ($recs as $r) {
127  $val = (in_array($r->getRecordId(), $selected));
128  $fields["rec" . $r->getRecordId()] =
129  $f->input()->field()->checkbox($r->getTitle(), $r->getDescription())
130  ->withValue($val);
131  }
132 
133  // section
134  $section1 = $f->input()->field()->section($fields, $this->lng->txt("prtt_select_datasets"));
135 
136  if ($edit) {
137  $form_action = $ctrl->getLinkTarget($this, "update");
138  } else {
139  $form_action = $ctrl->getLinkTarget($this, "create_amdfrm");
140  }
141  return $f->input()->container()->form()->standard($form_action, ["sec" => $section1]);
142  }
143 
144  public function create_amdfrm(): void
145  {
146  $this->create();
147  }
148 
149  public function create(): void
150  {
152  $form = $this->getTemplateForm();
153  $lng = $this->lng;
154  $tpl = $this->tpl;
155 
156  if ($request->getMethod() === "POST") {
157  $form = $form->withRequest($request);
158  $data = $form->getData();
159  if (is_null($data)) {
160  $tpl->setContent($this->ui->renderer()->render($form));
161  return;
162  }
163  if (is_array($data["sec"])) {
164  $this->content_obj = new ilPCAMDForm($this->getPage());
165  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
166  $this->content_obj->setRecordIds($this->getRecordIdsFromForm($form));
167  $this->updated = $this->pg_obj->update();
168  if (!$this->updated) {
169  $tpl->setContent($this->ui->renderer()->render($form));
170  return;
171  }
172  $this->tpl->setOnScreenMessage('info', $lng->txt("msg_obj_modified"), true);
173  }
174  }
175  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
176  }
177 
178  protected function getRecordIdsFromForm(Form\Standard $form): array
179  {
180  $data = $form->getData();
181  $ids = [];
182  if (!is_null($data) && is_array($data["sec"])) {
183  $recs = $this->getAdvRecords();
184  $ids = [];
185  foreach ($recs as $r) {
186  $rec_id = $data["sec"]["rec" . $r->getRecordId()];
187  if (isset($rec_id) && $rec_id) {
188  $ids[] = $r->getRecordId();
189  }
190  }
191  }
192  return $ids;
193  }
194 
195  protected function getAdvRecords(): array
196  {
197  if ($this->isTemplate()) {
199  $is_ref_id = true;
200  } else {
201  $id = $this->getPage()->getPortfolioId();
202  $is_ref_id = false;
203  }
204 
205  $recs = \ilAdvancedMDRecord::_getSelectedRecordsByObject($this->getPage()->getParentType(), $id, "pfpg", $is_ref_id);
206  return $recs;
207  }
208 
209  public function update(): void
210  {
212  $form = $this->getTemplateForm(true);
213  $lng = $this->lng;
214  $tpl = $this->tpl;
215 
216  if ($request->getMethod() === "POST") {
217  $form = $form->withRequest($request);
218  $data = $form->getData();
219  if (is_null($data)) {
220  $tpl->setContent($this->ui->renderer()->render($form));
221  return;
222  }
223  if (is_array($data["sec"])) {
224  $this->content_obj->setRecordIds($this->getRecordIdsFromForm($form));
225  $this->updated = $this->pg_obj->update();
226  if (!$this->updated) {
227  $tpl->setContent($this->ui->renderer()->render($form));
228  return;
229  }
230  $this->tpl->setOnScreenMessage('info', $lng->txt("msg_obj_modified"), true);
231  }
232  }
233  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
234  }
235 
239 
240 
244  public function editPortfolio(?ilPropertyFormGUI $form = null): void
245  {
246  $tpl = $this->tpl;
247 
248  $this->displayValidationError();
249 
250  if (!$form) {
251  $form = $this->getPortfolioForm(true);
252  }
253  $tpl->setContent($form->getHTML());
254  }
255 
256  public function getPortfolioForm(bool $edit = false): ilPropertyFormGUI
257  {
259  if (is_null($content_obj)) {
260  $page = new ilPortfolioPage($this->port_request->getPortfolioPageId());
261  $page->buildDom();
262  $content_obj = $page->getContentObjectForPcId($this->request->getPCId());
263  }
264 
265  $lng = $this->lng;
266  $ctrl = $this->ctrl;
267 
268  $selected = [];
269  if ($edit) {
270  $selected = $content_obj->getRecordIds();
271  }
272  $recs = $this->getAdvRecords();
273  foreach ($recs as $r) {
274  $val = (in_array($r->getRecordId(), $selected));
275  }
276 
277  $form = new ilPropertyFormGUI();
278  $form->setFormAction($ctrl->getFormAction($this, "updateAdvancedMetaData"));
279 
280  $form->setTitle($lng->txt("prtf_edit_data"));
281 
282  $this->record_gui = new ilAdvancedMDRecordGUI(
284  'prtf',
285  $this->getPage()->getPortfolioId(),
286  'pfpg',
287  $this->getPage()->getId(),
288  false
289  );
290  $this->record_gui->setRecordFilter($selected);
291  $this->record_gui->setPropertyForm($form);
292  $this->record_gui->parse();
293 
294  $form->addCommandButton("updateAdvancedMetaData", $lng->txt("save"));
295  $form->addCommandButton("cancel", $lng->txt("cancel"));
296 
297  return $form;
298  }
299 
300  public function updateAdvancedMetaData(): void
301  {
302  $lng = $this->lng;
303 
304  $form = $this->getPortfolioForm(true);
305 
306  // needed for proper advanced MD validation
307  $form->checkInput();
308  if (!$this->record_gui->importEditFormPostValues()) {
309  $this->editPortfolio($form); // #16470
310  return;
311  }
312 
313  if ($this->record_gui->writeEditForm()) {
314  $this->tpl->setOnScreenMessage('success', $lng->txt("settings_saved"), true);
315  }
316  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
317  }
318 }
getPortfolioForm(bool $edit=false)
getFormAction(object $a_gui_obj, ?string $a_fallback_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
This describes commonalities between all forms.
Definition: Form.php:32
StandardGUIRequest $port_request
buildDom(bool $a_force=false)
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...
ilAdvancedMDRecordGUI $record_gui
ServerRequestInterface $http_request
editTemplate(?Form\Standard $form=null)
getLinkTarget(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
getRecordIdsFromForm(Form\Standard $form)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
setContent(string $a_html)
Sets content for standard template.
ILIAS DI UIServices $ui
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
Content object of ilPageObject (see ILIAS DTD).
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
AMD Form Page UI.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
global $DIC
Definition: shib_login.php:26
ilGlobalTemplateInterface $tpl
getTemplateForm(bool $edit=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
This describes a standard form.
Definition: Standard.php:28
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
__construct(Container $dic, ilPlugin $plugin)
insert(?Form\Standard $form=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
editPortfolio(?ilPropertyFormGUI $form=null)
Edit courses form.
$r