ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  $this->tpl->addCss(ilObjStyleSheet::getBaseContentStylePath());
83 
84  $this->initEditor();
85  $this->tabs->setBackTarget("", "");
86 
87  $cmd = $this->ctrl->getCmd();
88  if ($cmd == "update") {
89  $form->setValuesByPost();
90  } else {
91  /*
92  $form->getItemByPostVar("par_language")->setValue($this->content_obj->getLanguage());
93  $form->getItemByPostVar("par_subcharacteristic")->setValue($this->content_obj->getSubCharacteristic());
94  $form->getItemByPostVar("par_downloadtitle")->setValue($this->content_obj->getDownloadTitle());
95  $form->getItemByPostVar("par_showlinenumbers")->setChecked(
96  $this->content_obj->getShowLineNumbers() == "y"
97  );
98  // $form->getItemByPostVar("par_autoindent")->setChecked(
99  // $this->content_obj->getAutoIndent()=="y"?true:false);
100  */
101  $par_content = $this->content_obj->xml2output($this->content_obj->getText());
102 
103  $par_content = str_replace("&#123;", "[curlybegin ", $par_content);
104  $par_content = str_replace("&#125;", " curlyend]", $par_content);
105 
106  $form->getItemByPostVar("par_content")->setValue($par_content);
107  }
108 
109  $f = $this->gui
110  ->ui()
111  ->factory()->input()->field()
112  ->textarea(
113  $this->lng->txt("cont_pc_code")
114  )->withValue($par_content);
115  $t = $this->gui->ui()->renderer()->render($f);
116  $t = str_replace("<textarea", "<textarea name='code' rows='20' form='copg-src-form' ", $t);
117  $t = str_replace("[curlybegin ", "&#123;", $t);
118  $t = str_replace(" curlyend]", "&#125;", $t);
119  $t = "<div class='c-form copg-src-form'>$t</div>";
120  $this->tpl->setContent($t . $this->getEditorScriptTag($this->pc_id, "SourceCode"));
121  }
122 
123  public function insert(): void
124  {
125  $ilUser = $this->user;
126 
127  $form = $this->initPropertyForm($this->lng->txt("cont_insert_src"), "create", "cancelCreate");
128 
129  if ($this->pg_obj->getParentType() == "lm") {
130  $this->tpl->setVariable(
131  "LINK_ILINK",
132  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp")
133  );
134  $this->tpl->setVariable("TXT_ILINK", "[" . $this->lng->txt("cont_internal_link") . "]");
135  }
136 
137  $this->displayValidationError();
138 
139  $cmd = $this->ctrl->getCmd();
140  if ($cmd == "create_src") {
141  $form->setValuesByPost();
142  } else {
143  if ($this->getCurrentTextLang() != "") {
144  $form->getItemByPostVar("par_language")->setValue($this->getCurrentTextLang());
145  } else {
146  $form->getItemByPostVar("par_language")->setValue($ilUser->getLanguage());
147  }
148 
149  $form->getItemByPostVar("par_showlinenumbers")->setChecked(true);
150  // $form->getItemByPostVar("par_autoindent")->setChecked(true);
151  $form->getItemByPostVar("par_subcharacteristic")->setValue("");
152  $form->getItemByPostVar("par_content")->setValue("");
153  }
154 
155  $this->tpl->setContent($form->getHTML());
156  }
157 
158  public function update(): void
159  {
160  $this->requested_par_content = $this->request->getRaw("par_content");
161  $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
162 
163  // $this->upload_source();
164 
165  // set language and characteristic
166 
167  /*
168  $this->content_obj->setLanguage(
169  $this->request->getString("par_language")
170  );
171  $this->content_obj->setCharacteristic($this->request->getString("par_characteristic"));*/
172 
173  // set language and characteristic
174  /*$this->content_obj->setLanguage($this->request->getString("par_language"));
175  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
176  $this->content_obj->setDownloadTitle(
177  str_replace('"', '', $this->requested_par_downloadtitle)
178  );
179  $this->content_obj->setShowLineNumbers(
180  $this->request->getString("par_showlinenumbers") ? "y" : "n"
181  );
182  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
183  $this->content_obj->setCharacteristic("Code");
184 
185  */
186 
187  $this->updated = $this->content_obj->setText(
188  $this->content_obj->input2xml($this->requested_par_content, 0, false)
189  );
190 
191  if ($this->updated !== true) {
192  //echo "Did not update!";
193  $this->edit();
194  return;
195  }
196 
197  $this->updated = $this->pg_obj->update();
198 
199  if ($this->updated === true && $this->ctrl->getCmd() != "upload") {
200  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
201  } else {
202  $this->edit();
203  }
204  }
205 
206  public function cancelUpdate(): void
207  {
208  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
209  }
210 
211  public function create(): void
212  {
213  $this->content_obj = new ilPCSourceCode($this->getPage());
214  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
215  $this->content_obj->setLanguage($this->request->getString("par_language"));
216 
217  $this->setCurrentTextLang($this->request->getString("par_language"));
218 
219  $this->requested_par_content = $this->request->getRaw("par_content");
220  $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
221 
222  $uploaded = $this->upload_source();
223 
224  $this->content_obj->setCharacteristic(
225  $this->request->getString("par_characteristic")
226  );
227  $this->content_obj->setSubCharacteristic(
228  $this->request->getString("par_subcharacteristic")
229  );
230  $this->content_obj->setDownloadTitle(str_replace('"', '', $this->requested_par_downloadtitle));
231  $this->content_obj->setShowLineNumbers(
232  $this->request->getString("par_showlinenumbers") ? 'y' : 'n'
233  );
234  $this->content_obj->setCharacteristic('Code');
235 
236  if ($uploaded) {
237  $this->insert();
238  return;
239  }
240 
241  $this->updated = $this->content_obj->setText(
242  $this->content_obj->input2xml($this->requested_par_content, 0, false)
243  );
244 
245  if ($this->updated !== true) {
246  $this->insert();
247  return;
248  }
249 
250  $this->updated = $this->pg_obj->update();
251 
252  if ($this->updated === true) {
253  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
254  } else {
255  $this->insert();
256  }
257  }
258 
259  public function cancelCreate(): void
260  {
261  $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
262  }
263 
264  public function upload_source(): bool
265  {
266  if (isset($_FILES['userfile']['name'])) {
267  $userfile = $_FILES['userfile']['tmp_name'];
268 
269  if ($userfile == "" || !is_uploaded_file($userfile)) {
270  $error_str = "<strong>Error(s):</strong><br>Upload error: file name must not be empty!";
271  $this->tpl->setVariable("MESSAGE", $error_str);
272  $this->content_obj->setText(
273  $this->content_obj->input2xml(
274  $this->request->getRaw("par_content"),
275  0,
276  false
277  )
278  );
279  return false;
280  }
281 
282  $this->requested_par_content = file_get_contents($userfile);
283  $this->requested_par_downloadtitle = $_FILES['userfile']['name'];
284  return true;
285  }
286 
287  return false;
288  }
289 
290 
295  public function getProgLangOptions(): array
296  {
297  $prog_langs = array(
298  "" => "other");
299  foreach (ilSyntaxHighlighter::getSupportedLanguagesV51() as $k => $v) {
300  $prog_langs[$k] = $v;
301  }
302  return $prog_langs;
303  }
304 
305  public function initPropertyForm(
306  string $a_title,
307  string $a_cmd,
308  string $a_cmd_cancel
309  ): ilPropertyFormGUI {
310  $form = new ilPropertyFormGUI();
311  $form->setTitle($a_title);
312  $form->setFormAction($this->ctrl->getFormAction($this, $a_cmd));
313  $form->addCommandButton($a_cmd, $this->lng->txt("save"));
314  $form->addCommandButton($a_cmd_cancel, $this->lng->txt("cancel"));
315 
316  /*
317  $lang_var = ilMDLanguageItem::_getLanguages();
318  $lang = new ilSelectInputGUI($this->lng->txt("language"), "par_language");
319  $lang->setOptions($lang_var);
320  $form->addItem($lang);
321 
322  $prog_langs = $this->getProgLangOptions();
323  $code_style = new ilSelectInputGUI($this->lng->txt("cont_src"), "par_subcharacteristic");
324  $code_style->setOptions($prog_langs);
325  $form->addItem($code_style);
326  $line_number = new ilCheckboxInputGUI($this->lng->txt("cont_show_line_numbers"), "par_showlinenumbers");
327  $form->addItem($line_number);
328 */
329  $code = new ilTextAreaInputGUI("", "par_content");
330  $code->setRows(12);
331  $form->addItem($code);
332 
333  /*
334  $downlaod_title = new ilTextInputGUI($this->lng->txt("cont_download_title"), "par_downloadtitle");
335  $downlaod_title->setSize(40);
336  $form->addItem($downlaod_title);
337 
338  $file = new ilFileInputGUI($this->lng->txt("import_file"), "userfile");
339  $form->addItem($file);*/
340 
341  return $form;
342  }
343 
344  public function getImportFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
345  {
346  $this->ctrl->setParameter($this, "cname", "SourceCode");
347  $form = $this->gui->form([self::class], "#")
348  ->async()
349  ->hidden("mode", "import")
350  ->file(
351  "input_file",
352  $this->lng->txt("import_file"),
353  \Closure::fromCallable([$this, 'handleUploadResult']),
354  "filename",
355  "",
356  1,
357  [],
358  [self::class],
359  "copg"
360  )
361  ->text(
362  "title",
363  $this->lng->txt("cont_download_title")
364  )
365  ->select(
366  "subchar",
367  $this->lng->txt("cont_src"),
368  $this->getProgLangOptions()
369  )
370  ->checkbox(
371  "linenumbers",
372  $this->lng->txt("cont_show_line_numbers")
373  );
374  return $form;
375  }
376 
377  public function getManualFormAdapter(
378  ?string $download_title = null,
379  ?string $subchar = null,
380  ?bool $line_numbers = null
381  ): \ILIAS\Repository\Form\FormAdapterGUI {
382  $this->ctrl->setParameter($this, "cname", "SourceCode");
383  $form = $this->gui->form([self::class], "#")
384  ->async()
385  ->hidden("mode", "manual")
386  ->text(
387  "title",
388  $this->lng->txt("cont_download_title"),
389  "",
390  $download_title
391  )
392  ->select(
393  "subchar",
394  $this->lng->txt("cont_src"),
395  $this->getProgLangOptions(),
396  "",
397  $subchar
398  )
399  ->checkbox(
400  "linenumbers",
401  $this->lng->txt("cont_show_line_numbers"),
402  "",
403  $line_numbers
404  );
405  return $form;
406  }
407 
408  public function getEditingFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
409  {
410  return $this->getManualFormAdapter(
411  $this->content_obj->getDownloadTitle(),
412  $this->content_obj->getSubCharacteristic(),
413  ($this->content_obj->getShowLineNumbers() == "y")
414  );
415  }
416 
417  public function handleUploadResult(
418  FileUpload $upload,
419  UploadResult $result
420  ): BasicHandlerResult {
421  $fac = new ILIAS\Data\UUID\Factory();
422  $uuid = $fac->uuid4AsString();
423  $name = $uuid . ".txt";
424  $upload->moveOneFileTo(
425  $result,
426  "",
427  Location::TEMPORARY,
428  $name,
429  true
430  );
431 
432  return new BasicHandlerResult(
433  "filename",
434  HandlerResult::STATUS_OK,
435  $name,
436  ''
437  );
438  }
439 }
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="")
Interface Observer Contains several chained tasks and infos about them.
Class ilPCSourcecodeGUI.
Content object of ilPageObject (see ILIAS DTD).
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
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...
global $DIC
Definition: shib_login.php:25
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:59
getEditorScriptTag(string $form_pc_id="", string $form_cname="")
static getSupportedLanguagesV51()
Get supported languages (keys are ILIAS <= 5.1 internal values, values are for representation) ...
__construct(Container $dic, ilPlugin $plugin)
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