ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPCSourceCodeGUI.php
Go to the documentation of this file.
1 <?php
2 
24 
33 {
34  protected ilTabsGUI $tabs;
38  protected string $requested_par_downloadtitle;
39  protected string $requested_par_content;
40  protected ilObjUser $user;
41 
42  public function __construct(
43  ilPageObject $a_pg_obj,
44  ?ilPageContent $a_content_obj,
45  string $a_hier_id,
46  string $a_pc_id = ""
47  ) {
48  global $DIC;
49 
50  $this->user = $DIC->user();
51  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
52  $this->tabs = $DIC->tabs();
53  }
54 
55  public function executeCommand(): void
56  {
57  // get next class that processes or forwards current command
58  $next_class = $this->ctrl->getNextClass($this);
59 
60  // get current command
61  $cmd = $this->ctrl->getCmd();
62 
63  switch ($next_class) {
64 
65  case strtolower(ilRepoStandardUploadHandlerGUI::class):
66  $form = $this->getImportFormAdapter();
67  $gui = $form->getRepoStandardUploadHandlerGUI("input_file");
68  $this->ctrl->forwardCommand($gui);
69  break;
70 
71  default:
72  $this->$cmd();
73  break;
74  }
75  }
76 
77  public function edit(): void
78  {
79  $form = $this->initPropertyForm($this->lng->txt("cont_edit_src"), "update", "cancelCreate");
80 
81  $this->displayValidationError();
82 
83  $this->initEditor();
84  $this->tabs->setBackTarget("", "");
85 
86  $cmd = $this->ctrl->getCmd();
87  if ($cmd == "update") {
88  $form->setValuesByPost();
89  } else {
90  /*
91  $form->getItemByPostVar("par_language")->setValue($this->content_obj->getLanguage());
92  $form->getItemByPostVar("par_subcharacteristic")->setValue($this->content_obj->getSubCharacteristic());
93  $form->getItemByPostVar("par_downloadtitle")->setValue($this->content_obj->getDownloadTitle());
94  $form->getItemByPostVar("par_showlinenumbers")->setChecked(
95  $this->content_obj->getShowLineNumbers() == "y"
96  );
97  // $form->getItemByPostVar("par_autoindent")->setChecked(
98  // $this->content_obj->getAutoIndent()=="y"?true:false);
99  */
100  $par_content = $this->content_obj->xml2output($this->content_obj->getText());
101 
102  $par_content = str_replace("&#123;", "[curlybegin ", $par_content);
103  $par_content = str_replace("&#125;", " curlyend]", $par_content);
104 
105  $form->getItemByPostVar("par_content")->setValue($par_content);
106  }
107 
108  $f = $this->gui
109  ->ui()
110  ->factory()->input()->field()
111  ->textarea(
112  $this->lng->txt("cont_pc_code")
113  )->withValue($par_content);
114  $t = $this->gui->ui()->renderer()->render($f);
115  $t = str_replace("<textarea", "<textarea name='code' rows='20' form='copg-src-form' ", $t);
116  $t = str_replace("[curlybegin ", "&#123;", $t);
117  $t = str_replace(" curlyend]", "&#125;", $t);
118  $this->tpl->setContent($t . $this->getEditorScriptTag($this->pc_id, "SourceCode"));
119  }
120 
121  public function insert(): void
122  {
123  $ilUser = $this->user;
124 
125  $form = $this->initPropertyForm($this->lng->txt("cont_insert_src"), "create_src", "cancelCreate");
126 
127  if ($this->pg_obj->getParentType() == "lm") {
128  $this->tpl->setVariable(
129  "LINK_ILINK",
130  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp")
131  );
132  $this->tpl->setVariable("TXT_ILINK", "[" . $this->lng->txt("cont_internal_link") . "]");
133  }
134 
135  $this->displayValidationError();
136 
137  $cmd = $this->ctrl->getCmd();
138  if ($cmd == "create_src") {
139  $form->setValuesByPost();
140  } else {
141  if ($this->getCurrentTextLang() != "") {
142  $form->getItemByPostVar("par_language")->setValue($this->getCurrentTextLang());
143  } else {
144  $form->getItemByPostVar("par_language")->setValue($ilUser->getLanguage());
145  }
146 
147  $form->getItemByPostVar("par_showlinenumbers")->setChecked(true);
148  // $form->getItemByPostVar("par_autoindent")->setChecked(true);
149  $form->getItemByPostVar("par_subcharacteristic")->setValue("");
150  $form->getItemByPostVar("par_content")->setValue("");
151  }
152 
153  $this->tpl->setContent($form->getHTML());
154  }
155 
156  public function update(): void
157  {
158  $this->requested_par_content = $this->request->getRaw("par_content");
159  $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
160 
161  // $this->upload_source();
162 
163  // set language and characteristic
164 
165  /*
166  $this->content_obj->setLanguage(
167  $this->request->getString("par_language")
168  );
169  $this->content_obj->setCharacteristic($this->request->getString("par_characteristic"));*/
170 
171  // set language and characteristic
172  /*$this->content_obj->setLanguage($this->request->getString("par_language"));
173  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
174  $this->content_obj->setDownloadTitle(
175  str_replace('"', '', $this->requested_par_downloadtitle)
176  );
177  $this->content_obj->setShowLineNumbers(
178  $this->request->getString("par_showlinenumbers") ? "y" : "n"
179  );
180  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
181  $this->content_obj->setCharacteristic("Code");
182 
183  */
184 
185  $this->updated = $this->content_obj->setText(
186  $this->content_obj->input2xml($this->requested_par_content, 0, false)
187  );
188 
189  if ($this->updated !== true) {
190  //echo "Did not update!";
191  $this->edit();
192  return;
193  }
194 
195  $this->updated = $this->pg_obj->update();
196 
197  if ($this->updated === true && $this->ctrl->getCmd() != "upload") {
198  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
199  } else {
200  $this->edit();
201  }
202  }
203 
204  public function cancelUpdate(): void
205  {
206  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
207  }
208 
209  public function create(): void
210  {
211  $this->content_obj = new ilPCSourceCode($this->getPage());
212  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
213  $this->content_obj->setLanguage($this->request->getString("par_language"));
214 
215  $this->setCurrentTextLang($this->request->getString("par_language"));
216 
217  $this->requested_par_content = $this->request->getRaw("par_content");
218  $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
219 
220  $uploaded = $this->upload_source();
221 
222  $this->content_obj->setCharacteristic(
223  $this->request->getString("par_characteristic")
224  );
225  $this->content_obj->setSubCharacteristic(
226  $this->request->getString("par_subcharacteristic")
227  );
228  $this->content_obj->setDownloadTitle(str_replace('"', '', $this->requested_par_downloadtitle));
229  $this->content_obj->setShowLineNumbers(
230  $this->request->getString("par_showlinenumbers") ? 'y' : 'n'
231  );
232  $this->content_obj->setCharacteristic('Code');
233 
234  if ($uploaded) {
235  $this->insert();
236  return;
237  }
238 
239  $this->updated = $this->content_obj->setText(
240  $this->content_obj->input2xml($this->requested_par_content, 0, false)
241  );
242 
243  if ($this->updated !== true) {
244  $this->insert();
245  return;
246  }
247 
248  $this->updated = $this->pg_obj->update();
249 
250  if ($this->updated === true) {
251  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
252  } else {
253  $this->insert();
254  }
255  }
256 
257  public function cancelCreate(): void
258  {
259  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
260  }
261 
262  public function upload_source(): bool
263  {
264  if (isset($_FILES['userfile']['name'])) {
265  $userfile = $_FILES['userfile']['tmp_name'];
266 
267  if ($userfile == "" || !is_uploaded_file($userfile)) {
268  $error_str = "<strong>Error(s):</strong><br>Upload error: file name must not be empty!";
269  $this->tpl->setVariable("MESSAGE", $error_str);
270  $this->content_obj->setText(
271  $this->content_obj->input2xml(
272  $this->request->getRaw("par_content"),
273  0,
274  false
275  )
276  );
277  return false;
278  }
279 
280  $this->requested_par_content = file_get_contents($userfile);
281  $this->requested_par_downloadtitle = $_FILES['userfile']['name'];
282  return true;
283  }
284 
285  return false;
286  }
287 
288 
293  public function getProgLangOptions(): array
294  {
295  $prog_langs = array(
296  "" => "other");
297  foreach (ilSyntaxHighlighter::getSupportedLanguagesV51() as $k => $v) {
298  $prog_langs[$k] = $v;
299  }
300  return $prog_langs;
301  }
302 
303  public function initPropertyForm(
304  string $a_title,
305  string $a_cmd,
306  string $a_cmd_cancel
307  ): ilPropertyFormGUI {
308  $form = new ilPropertyFormGUI();
309  $form->setTitle($a_title);
310  $form->setFormAction($this->ctrl->getFormAction($this, $a_cmd));
311  $form->addCommandButton($a_cmd, $this->lng->txt("save"));
312  $form->addCommandButton($a_cmd_cancel, $this->lng->txt("cancel"));
313 
314  /*
315  $lang_var = ilMDLanguageItem::_getLanguages();
316  $lang = new ilSelectInputGUI($this->lng->txt("language"), "par_language");
317  $lang->setOptions($lang_var);
318  $form->addItem($lang);
319 
320  $prog_langs = $this->getProgLangOptions();
321  $code_style = new ilSelectInputGUI($this->lng->txt("cont_src"), "par_subcharacteristic");
322  $code_style->setOptions($prog_langs);
323  $form->addItem($code_style);
324  $line_number = new ilCheckboxInputGUI($this->lng->txt("cont_show_line_numbers"), "par_showlinenumbers");
325  $form->addItem($line_number);
326 */
327  $code = new ilTextAreaInputGUI("", "par_content");
328  $code->setRows(12);
329  $form->addItem($code);
330 
331  /*
332  $downlaod_title = new ilTextInputGUI($this->lng->txt("cont_download_title"), "par_downloadtitle");
333  $downlaod_title->setSize(40);
334  $form->addItem($downlaod_title);
335 
336  $file = new ilFileInputGUI($this->lng->txt("import_file"), "userfile");
337  $form->addItem($file);*/
338 
339  return $form;
340  }
341 
342  public function getImportFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
343  {
344  $this->ctrl->setParameter($this, "cname", "SourceCode");
345  $form = $this->gui->form([self::class], "#")
346  ->async()
347  ->hidden("mode", "import")
348  ->file(
349  "input_file",
350  $this->lng->txt("import_file"),
351  \Closure::fromCallable([$this, 'handleUploadResult']),
352  "filename",
353  "",
354  1,
355  [],
356  [self::class],
357  "copg"
358  )
359  ->text(
360  "title",
361  $this->lng->txt("cont_download_title")
362  )
363  ->select(
364  "subchar",
365  $this->lng->txt("cont_src"),
366  $this->getProgLangOptions()
367  )
368  ->checkbox(
369  "linenumbers",
370  $this->lng->txt("cont_show_line_numbers")
371  );
372  return $form;
373  }
374 
375  public function getManualFormAdapter(
376  ?string $download_title = null,
377  ?string $subchar = null,
378  ?bool $line_numbers = null
379  ): \ILIAS\Repository\Form\FormAdapterGUI {
380  $this->ctrl->setParameter($this, "cname", "SourceCode");
381  $form = $this->gui->form([self::class], "#")
382  ->async()
383  ->hidden("mode", "manual")
384  ->text(
385  "title",
386  $this->lng->txt("cont_download_title"),
387  "",
388  $download_title
389  )
390  ->select(
391  "subchar",
392  $this->lng->txt("cont_src"),
393  $this->getProgLangOptions(),
394  "",
395  $subchar
396  )
397  ->checkbox(
398  "linenumbers",
399  $this->lng->txt("cont_show_line_numbers"),
400  "",
401  $line_numbers
402  );
403  return $form;
404  }
405 
406  public function getEditingFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
407  {
408  return $this->getManualFormAdapter(
409  $this->content_obj->getDownloadTitle(),
410  $this->content_obj->getSubCharacteristic(),
411  ($this->content_obj->getShowLineNumbers() == "y")
412  );
413  }
414 
415  public function handleUploadResult(
416  FileUpload $upload,
417  UploadResult $result
418  ): BasicHandlerResult {
419  $fac = new ILIAS\Data\UUID\Factory();
420  $uuid = $fac->uuid4AsString();
421  $name = $uuid . ".txt";
422  $upload->moveOneFileTo(
423  $result,
424  "",
425  Location::TEMPORARY,
426  $name,
427  true
428  );
429 
430  return new BasicHandlerResult(
431  "filename",
432  HandlerResult::STATUS_OK,
433  $name,
434  ''
435  );
436  }
437 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
Class ChatMainBarProvider .
Class ilPCSourcecodeGUI.
Content object of ilPageObject (see ILIAS DTD).
global $DIC
Definition: feed.php:28
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handleUploadResult(FileUpload $upload, UploadResult $result)
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
moveOneFileTo(UploadResult $uploadResult, string $destination, int $location=Location::STORAGE, string $file_name='', bool $override_existing=false)
Moves a single File (the attributes, metadata and upload-status of which are contained in UploadResul...
setCurrentTextLang(string $lang_key)
Class FileUpload.
Definition: FileUpload.php:34
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
getEditorScriptTag(string $form_pc_id="", string $form_cname="")
static getSupportedLanguagesV51()
Get supported languages (keys are ILIAS <= 5.1 internal values, values are for representation) ...
This class represents a text area property in a property form.
getProgLangOptions()
Get selectable programming languages.
getManualFormAdapter(?string $download_title=null, ?string $subchar=null, ?bool $line_numbers=null)
initPropertyForm(string $a_title, string $a_cmd, string $a_cmd_cancel)
ILIAS COPage InternalGUIService $gui