ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPCSourceCodeGUI.php
Go to the documentation of this file.
1 <?php
2 
27 {
31  protected string $requested_par_downloadtitle;
32  protected string $requested_par_content;
33  protected ilObjUser $user;
34 
35  public function __construct(
36  ilPageObject $a_pg_obj,
37  ?ilPageContent $a_content_obj,
38  string $a_hier_id,
39  string $a_pc_id = ""
40  ) {
41  global $DIC;
42 
43  $this->user = $DIC->user();
44  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
45  }
46 
47  public function executeCommand(): void
48  {
49  // get next class that processes or forwards current command
50  $next_class = $this->ctrl->getNextClass($this);
51 
52  // get current command
53  $cmd = $this->ctrl->getCmd();
54 
55  switch ($next_class) {
56  default:
57  $this->$cmd();
58  break;
59  }
60  }
61 
62  public function edit(): void
63  {
64  $form = $this->initPropertyForm($this->lng->txt("cont_edit_src"), "update", "cancelCreate");
65 
66  if ($this->pg_obj->getParentType() == "lm") {
67  $this->tpl->setVariable(
68  "LINK_ILINK",
69  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp")
70  );
71  $this->tpl->setVariable("TXT_ILINK", "[" . $this->lng->txt("cont_internal_link") . "]");
72  }
73 
74  $this->displayValidationError();
75 
76  $cmd = $this->ctrl->getCmd();
77  if ($cmd == "update") {
78  $form->setValuesByPost();
79  } else {
80  $form->getItemByPostVar("par_language")->setValue($this->content_obj->getLanguage());
81  $form->getItemByPostVar("par_subcharacteristic")->setValue($this->content_obj->getSubCharacteristic());
82  $form->getItemByPostVar("par_downloadtitle")->setValue($this->content_obj->getDownloadTitle());
83  $form->getItemByPostVar("par_showlinenumbers")->setChecked(
84  $this->content_obj->getShowLineNumbers() == "y"
85  );
86  // $form->getItemByPostVar("par_autoindent")->setChecked(
87  // $this->content_obj->getAutoIndent()=="y"?true:false);
88 
89  $par_content = $this->content_obj->xml2output($this->content_obj->getText());
90 
91  //TODO: Find a better way to convert back curly brackets
92  $par_content = str_replace("&#123;", "{", $par_content);
93  $par_content = str_replace("&#125;", "}", $par_content);
94 
95  $form->getItemByPostVar("par_content")->setValue($par_content);
96  }
97 
98 
99  $this->tpl->setContent($form->getHTML());
100  }
101 
102  public function insert(): void
103  {
105 
106  $form = $this->initPropertyForm($this->lng->txt("cont_insert_src"), "create_src", "cancelCreate");
107 
108  if ($this->pg_obj->getParentType() == "lm") {
109  $this->tpl->setVariable(
110  "LINK_ILINK",
111  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp")
112  );
113  $this->tpl->setVariable("TXT_ILINK", "[" . $this->lng->txt("cont_internal_link") . "]");
114  }
115 
116  $this->displayValidationError();
117 
118  $cmd = $this->ctrl->getCmd();
119  if ($cmd == "create_src") {
120  $form->setValuesByPost();
121  } else {
122  if ($this->getCurrentTextLang() != "") {
123  $form->getItemByPostVar("par_language")->setValue($this->getCurrentTextLang());
124  } else {
125  $form->getItemByPostVar("par_language")->setValue($ilUser->getLanguage());
126  }
127 
128  $form->getItemByPostVar("par_showlinenumbers")->setChecked(true);
129  // $form->getItemByPostVar("par_autoindent")->setChecked(true);
130  $form->getItemByPostVar("par_subcharacteristic")->setValue("");
131  $form->getItemByPostVar("par_content")->setValue("");
132  }
133 
134  $this->tpl->setContent($form->getHTML());
135  }
136 
137  public function update(): void
138  {
139  $this->requested_par_content = $this->request->getRaw("par_content");
140  $this->requested_par_downloadtitle = str_replace('"', '', $this->request->getString("par_downloadtitle"));
141 
142  $this->upload_source();
143 
144  // set language and characteristic
145 
146  $this->content_obj->setLanguage(
147  $this->request->getString("par_language")
148  );
149  $this->content_obj->setCharacteristic($this->request->getString("par_characteristic"));
150 
151  // set language and characteristic
152  $this->content_obj->setLanguage($this->request->getString("par_language"));
153  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
154  $this->content_obj->setDownloadTitle(
155  str_replace('"', '', $this->requested_par_downloadtitle)
156  );
157  $this->content_obj->setShowLineNumbers(
158  $this->request->getString("par_showlinenumbers") ? "y" : "n"
159  );
160  $this->content_obj->setSubCharacteristic($this->request->getString("par_subcharacteristic"));
161  $this->content_obj->setCharacteristic("Code");
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 = "<b>Error(s):</b><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 (ilSyntaxHighlighter::getSupportedLanguagesV51() 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
285  ): ilPropertyFormGUI {
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  $lang_var = ilMDLanguageItem::_getLanguages();
293  $lang = new ilSelectInputGUI($this->lng->txt("language"), "par_language");
294  $lang->setOptions($lang_var);
295  $form->addItem($lang);
296 
297  $prog_langs = $this->getProgLangOptions();
298  $code_style = new ilSelectInputGUI($this->lng->txt("cont_src"), "par_subcharacteristic");
299  $code_style->setOptions($prog_langs);
300  $form->addItem($code_style);
301  $line_number = new ilCheckboxInputGUI($this->lng->txt("cont_show_line_numbers"), "par_showlinenumbers");
302  $form->addItem($line_number);
303 
304  $code = new ilTextAreaInputGUI("", "par_content");
305  $code->setRows(12);
306  $form->addItem($code);
307 
308  $downlaod_title = new ilTextInputGUI($this->lng->txt("cont_download_title"), "par_downloadtitle");
309  $downlaod_title->setSize(40);
310  $form->addItem($downlaod_title);
311 
312  $file = new ilFileInputGUI($this->lng->txt("import_file"), "userfile");
313  $form->addItem($file);
314 
315  return $form;
316  }
317 }
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
This class represents a file property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
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...
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
setCurrentTextLang(string $lang_key)
static getSupportedLanguagesV51()
Get supported languages (keys are ILIAS <= 5.1 internal values, values are for representation) ...
$lang
Definition: xapiexit.php:26
__construct(Container $dic, ilPlugin $plugin)
This class represents a text area property in a property form.
getProgLangOptions()
Get selectable programming languages.
$ilUser
Definition: imgupload.php:34
initPropertyForm(string $a_title, string $a_cmd, string $a_cmd_cancel)