ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
class.ilPCSourceCodeGUI.php
Go to the documentation of this file.
1<?php
2
24
33{
34 protected ilTabsGUI $tabs;
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
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->updated = $this->content_obj->setText(
164 $this->content_obj->input2xml($this->requested_par_content, 0, false)
165 );
166
167 if ($this->updated !== true) {
168 //echo "Did not update!";
169 $this->edit();
170 return;
171 }
172
173 $this->updated = $this->pg_obj->update();
174
175 if ($this->updated === true && $this->ctrl->getCmd() != "upload") {
176 $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
177 } else {
178 $this->edit();
179 }
180 }
181
182 public function cancelUpdate(): void
183 {
184 $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
185 }
186
187 public function create(): void
188 {
189 $this->content_obj = new ilPCSourceCode($this->getPage());
190 $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
191 $this->content_obj->setLanguage($this->request->getString("par_language"));
192
193 $this->setCurrentTextLang($this->request->getString("par_language"));
194
195 $this->requested_par_content = $this->request->getRaw("par_content");
196 $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
197
198 $uploaded = $this->upload_source();
199
200 $this->content_obj->setCharacteristic(
201 $this->request->getString("par_characteristic")
202 );
203 $this->content_obj->setSubCharacteristic(
204 $this->request->getString("par_subcharacteristic")
205 );
206 $this->content_obj->setDownloadTitle(str_replace('"', '', $this->requested_par_downloadtitle));
207 $this->content_obj->setShowLineNumbers(
208 $this->request->getString("par_showlinenumbers") ? 'y' : 'n'
209 );
210 $this->content_obj->setCharacteristic('Code');
211
212 if ($uploaded) {
213 $this->insert();
214 return;
215 }
216
217 $this->updated = $this->content_obj->setText(
218 $this->content_obj->input2xml($this->requested_par_content, 0, false)
219 );
220
221 if ($this->updated !== true) {
222 $this->insert();
223 return;
224 }
225
226 $this->updated = $this->pg_obj->update();
227
228 if ($this->updated === true) {
229 $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
230 } else {
231 $this->insert();
232 }
233 }
234
235 public function cancelCreate(): void
236 {
237 $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
238 }
239
240 public function upload_source(): bool
241 {
242 if (isset($_FILES['userfile']['name'])) {
243 $userfile = $_FILES['userfile']['tmp_name'];
244
245 if ($userfile == "" || !is_uploaded_file($userfile)) {
246 $error_str = "<strong>Error(s):</strong><br>Upload error: file name must not be empty!";
247 $this->tpl->setVariable("MESSAGE", $error_str);
248 $this->content_obj->setText(
249 $this->content_obj->input2xml(
250 $this->request->getRaw("par_content"),
251 0,
252 false
253 )
254 );
255 return false;
256 }
257
258 $this->requested_par_content = file_get_contents($userfile);
259 $this->requested_par_downloadtitle = $_FILES['userfile']['name'];
260 return true;
261 }
262
263 return false;
264 }
265
266
271 public function getProgLangOptions(): array
272 {
273 $prog_langs = array(
274 "" => "other");
275 foreach (ilPCSourceCode::getSupportedLanguages() as $k => $v) {
276 $prog_langs[$k] = $v;
277 }
278 return $prog_langs;
279 }
280
281 public function initPropertyForm(
282 string $a_title,
283 string $a_cmd,
284 string $a_cmd_cancel
286 $form = new ilPropertyFormGUI();
287 $form->setTitle($a_title);
288 $form->setFormAction($this->ctrl->getFormAction($this, $a_cmd));
289 $form->addCommandButton($a_cmd, $this->lng->txt("save"));
290 $form->addCommandButton($a_cmd_cancel, $this->lng->txt("cancel"));
291
292 $code = new ilTextAreaInputGUI("", "par_content");
293 $code->setRows(12);
294 $form->addItem($code);
295
296 return $form;
297 }
298
299 public function getImportFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
300 {
301 $this->ctrl->setParameter($this, "cname", "SourceCode");
302 $form = $this->gui->form([self::class], "#")
303 ->async()
304 ->hidden("mode", "import")
305 ->file(
306 "input_file",
307 $this->lng->txt("import_file"),
308 \Closure::fromCallable([$this, 'handleUploadResult']),
309 "filename",
310 "",
311 1,
312 [],
313 [self::class],
314 "copg"
315 )
316 ->text(
317 "title",
318 $this->lng->txt("cont_download_title")
319 )
320 ->select(
321 "subchar",
322 $this->lng->txt("cont_src"),
323 $this->getProgLangOptions()
324 )
325 ->checkbox(
326 "linenumbers",
327 $this->lng->txt("cont_show_line_numbers")
328 );
329 return $form;
330 }
331
332 public function getManualFormAdapter(
333 ?string $download_title = null,
334 ?string $subchar = null,
335 ?bool $line_numbers = null
336 ): \ILIAS\Repository\Form\FormAdapterGUI {
337 $this->ctrl->setParameter($this, "cname", "SourceCode");
338 $form = $this->gui->form([self::class], "#")
339 ->async()
340 ->hidden("mode", "manual")
341 ->text(
342 "title",
343 $this->lng->txt("cont_download_title"),
344 "",
345 $download_title
346 )
347 ->select(
348 "subchar",
349 $this->lng->txt("cont_src"),
350 $this->getProgLangOptions(),
351 "",
352 $subchar
353 )
354 ->checkbox(
355 "linenumbers",
356 $this->lng->txt("cont_show_line_numbers"),
357 "",
358 $line_numbers
359 );
360 return $form;
361 }
362
363 public function getEditingFormAdapter(): \ILIAS\Repository\Form\FormAdapterGUI
364 {
365 return $this->getManualFormAdapter(
366 $this->content_obj->getDownloadTitle(),
367 $this->content_obj->getSubCharacteristic(),
368 ($this->content_obj->getShowLineNumbers() == "y")
369 );
370 }
371
372 public function handleUploadResult(
373 FileUpload $upload,
374 UploadResult $result
376 $fac = new ILIAS\Data\UUID\Factory();
377 $uuid = $fac->uuid4AsString();
378 $name = $uuid . ".txt";
379 $upload->moveOneFileTo(
380 $result,
381 "",
382 Location::TEMPORARY,
383 $name,
384 true
385 );
386
387 return new BasicHandlerResult(
388 "filename",
389 HandlerResult::STATUS_OK,
390 $name,
391 ''
392 );
393 }
394}
User class.
Class ilPCSourcecodeGUI.
handleUploadResult(FileUpload $upload, UploadResult $result)
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
initPropertyForm(string $a_title, string $a_cmd, string $a_cmd_cancel)
getProgLangOptions()
Get selectable programming languages.
getManualFormAdapter(?string $download_title=null, ?string $subchar=null, ?bool $line_numbers=null)
cancelCreate()
cancel creating page content
static getSupportedLanguages()
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
ILIAS COPage InternalGUIService $gui
setCurrentTextLang(string $lang_key)
getEditorScriptTag(string $form_pc_id="", string $form_cname="")
Content object of ilPageObject (see ILIAS DTD).
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a text area property in a property form.
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...
Interface Location.
Definition: Location.php:33
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26