ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCSourceCodeGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once("./Services/COPage/classes/class.ilPCSourceCode.php");
6 require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
7 
19 {
20 
25  function ilPCSourceCodeGUI($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id = "")
26  {
27  parent::ilPageContentGUI($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
28  }
29 
30 
34  function &executeCommand()
35  {
36  // get next class that processes or forwards current command
37  $next_class = $this->ctrl->getNextClass($this);
38 
39  // get current command
40  $cmd = $this->ctrl->getCmd();
41 
42  switch($next_class)
43  {
44  default:
45  $ret =& $this->$cmd();
46  break;
47  }
48 
49  return $ret;
50  }
51 
55  function edit()
56  {
57  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.sourcecode_edit.html", "Services/COPage");
58  //$content = $this->pg_obj->getContent();
59  //$cnt = 1;
60  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_edit_src"));
61 
62  if ($this->pg_obj->getParentType() == "lm" ||
63  $this->pg_obj->getParentType() == "dbk")
64  {
65  $this->tpl->setVariable("LINK_ILINK",
66  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp"));
67  $this->tpl->setVariable("TXT_ILINK", "[".$this->lng->txt("cont_internal_link")."]");
68  }
69 
70  $this->displayValidationError();
71 
72  // language and characteristic selection
73  if (key($_POST["cmd"]) == "update")
74  {
75  $s_lang = $_POST["par_language"];
76  $s_char = $_POST["par_characteristic"];
77  $s_subchar = $_POST["par_subcharacteristic"];
78  $s_downloadtitle = str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"]));
79  $s_showlinenumbers = ($_POST["par_showlinenumbers"]=="on")?'y':'n';
80  $s_autoindent = ($_POST["par_autoindent"]=="on")?'y':'n';
81  }
82  else
83  {
84  $s_lang = $this->content_obj->getLanguage();
85  $s_char = $this->content_obj->getCharacteristic();
86  $s_subchar = $this->content_obj->getSubCharacteristic();
87  $s_downloadtitle = $this->content_obj->getDownloadTitle();
88  $s_showlinenumbers = $this->content_obj->getShowLineNumbers();
89  $s_autoindent = $this->content_obj->getAutoIndent ();
90  }
91 
92  $this->setTemplateText($s_lang, $s_subchar);
93 
94  if (key($_POST["cmd"]) == "update")
95  {
96  $s_text = stripslashes($_POST["par_content"]);
97  }
98  else
99  {
100  $s_text = $this->content_obj->xml2output($this->content_obj->getText());
101  }
102 
103  $this->tpl->setVariable("PAR_TA_NAME", "par_content");
104  $this->tpl->setVariable("PAR_TA_CONTENT", ilUtil::prepareFormOutput($s_text));
105 //var_dump($this->tpl);
106 // $this->tpl->parseCurrentBlock();
107 
108  if (strcmp($s_showlinenumbers,"y")==0)
109  {
110  $this->tpl->setVariable("SHOWLINENUMBERS", "checked=\"checked\"");
111  }
112 
113  if (strcmp($s_autoindent,"y") == 0)
114  {
115  $this->tpl->setVariable("AUTOINDENT", "checked=\"checked\"");
116  }
117 
118 
119  $this->tpl->setVariable("DOWNLOAD_TITLE_VALUE", $s_downloadtitle);
120 
121  // operations
122  $this->tpl->setCurrentBlock("commands");
123  $this->tpl->setVariable("BTN_NAME", "update");
124  $this->tpl->setVariable("UPLOAD_BTN_NAME", "upload");
125  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
126  $this->tpl->setVariable("BTN_CANCEL", "cancelUpdate");
127  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
128  $this->tpl->parseCurrentBlock();
129  }
130 
131 
132  function setTemplateText ($s_lang, $s_proglang) {
133  $this->tpl->setVariable ("TXT_CREATEFILE", $this->lng->txt("create_download_link"));
134  $this->tpl->setVariable ("TXT_DOWNLOADTITLE", $this->lng->txt("cont_download_title"));
135  $this->tpl->setVariable ("TXT_IMPORTFILE", $this->lng->txt("import_file"));
136  $this->tpl->setVariable ("TXT_UPLOAD_BTN", $this->lng->txt("import"));
137  $this->tpl->setVariable ("TXT_SUBCHARACTERISTIC", $this->lng->txt("cont_src"));
138  $this->tpl->setVariable ("TXT_LANGUAGE", $this->lng->txt("language"));
139  $this->tpl->setVariable ("TXT_SHOWLINENUMBERS", $this->lng->txt("cont_show_line_numbers"));
140  $this->tpl->setVariable ("TXT_AUTOINDENT", $this->lng->txt("cont_autoindent"));
141 
142 
143  $this->tpl->setVariable ("FORMACTION", $this->ctrl->getFormAction($this));
144 
145  require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
147  $select_lang = ilUtil::formSelect ($s_lang,"par_language",$lang,false,true);
148  $this->tpl->setVariable ("SELECT_LANGUAGE", $select_lang);
149 
150  $prog_langs = $this->readProgLangs ();
151 
152  $select_subchar = ilUtil::formSelect ($s_proglang, "par_subcharacteristic",$prog_langs,false,true);
153  $this->tpl->setVariable ("SELECT_SUBCHARACTERISTIC", $select_subchar);
154 
155  }
156 
160  function insert()
161  {
162  global $ilUser;
163 
164  // add paragraph edit template
165  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.sourcecode_edit.html", "Services/COPage");
166  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_insert_src"));
167 
168  if ($this->pg_obj->getParentType() == "lm" ||
169  $this->pg_obj->getParentType() == "dbk")
170  {
171  $this->tpl->setVariable("LINK_ILINK",
172  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp"));
173  $this->tpl->setVariable("TXT_ILINK", "[".$this->lng->txt("cont_internal_link")."]");
174  }
175 
176  $this->displayValidationError();
177 
178  // get values from new object (repeated form display on error)
179 
180  //echo key ($_POST["cmd"]);
181 
182  if (key($_POST["cmd"]) == "create_src")
183  {
184  $s_lang = $_POST["par_language"];
185  $s_subchar = $_POST["par_subcharacteristic"];
186  $s_downloadtitle = str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"]));
187  $s_showlinenumbers = strcmp($_POST["par_showlinenumbers"],'on')==0?'checked=\"true\"':'';
188  $s_autoindent = strcmp($_POST["par_autoindent"],'on')==0?'checked=\"true\"':'';
189  $s_isexample = strcmp($_POST["par_isexample"],"on")==0?'checked=\"true\"':'';
190  }
191  else
192  {
193  if ($_SESSION["il_text_lang_".$_GET["ref_id"]] != "")
194  {
195  $s_lang = $_SESSION["il_text_lang_".$_GET["ref_id"]];
196  }
197  else
198  {
199  $s_lang = $ilUser->getLanguage();
200  }
201 
202  $s_showlinenumbers = 'CHECKED';
203  $s_autoindent = 'CHECKED';
204  $s_isexample = '';
205  $s_subchar = '';
206  }
207 
208  $this->setTemplateText($s_lang, $s_subchar);
209 
210  $this->tpl->setVariable("SHOWLINENUMBERS", $s_showlinenumbers);
211  $this->tpl->setVariable("AUTOINDENT", $s_autoindent);
212  $this->tpl->setVariable("DOWNLOAD_TITLE_VALUE", $s_downloadtitle);
213  $this->tpl->setVariable("ISEXAMPLE", $s_isexample);
214 
215 
216  // content is in utf-8, todo: set globally
217  // header('Content-type: text/html; charset=UTF-8');
218 
219  // input text area
220  $this->tpl->setVariable("PAR_TA_NAME", "par_content");
221 
222  if (key($_POST["cmd"]) == "create_src")
223  {
224  $this->tpl->setVariable("PAR_TA_CONTENT", ilUtil::prepareFormOutput(stripslashes($_POST["par_content"])));
225  }
226  else
227  {
228  $this->tpl->setVariable("PAR_TA_CONTENT", "");
229  }
230 // $this->tpl->parseCurrentBlock();
231 
232  // operations
233  $this->tpl->setCurrentBlock("commands");
234  $this->tpl->setVariable("BTN_NAME", "create_src"); //--
235  $this->tpl->setVariable("UPLOAD_BTN_NAME", "create_src");
236  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
237  $this->tpl->setVariable("BTN_CANCEL", "cancelCreate");
238  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
239  $this->tpl->parseCurrentBlock();
240 
241  }
242 
243 
247  function update()
248  {
249  global $ilBench;
250 
251  $ilBench->start("Editor","Paragraph_update");
252  // set language and characteristic
253 
254  $this->content_obj->setLanguage($_POST["par_language"]);
255  $this->content_obj->setCharacteristic($_POST["par_characteristic"]);
256 
257  //echo "PARupdate:".htmlentities($this->content_obj->input2xml($_POST["par_content"])).":<br>"; exit;
258 
259 
260  // set language and characteristic
261  $this->content_obj->setLanguage($_POST["par_language"]);
262  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
263  $this->content_obj->setDownloadTitle(str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"])));
264  $this->content_obj->setShowLineNumbers(($_POST["par_showlinenumbers"]=="on")?"y":"n");
265  $this->content_obj->setAutoIndent(($_POST["par_autoindent"]=="on")?"y":"n");
266  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
267  $this->content_obj->setCharacteristic("Code");
268 
269  $this->updated = $this->content_obj->setText(
270  $this->content_obj->input2xml($_POST["par_content"], 0, false));
271 
272  if ($this->updated !== true)
273  {
274  //echo "Did not update!";
275  $ilBench->stop("Editor","Paragraph_update");
276  $this->edit();
277  return;
278  }
279 
280  $this->updated = $this->pg_obj->update();
281 
282  $ilBench->stop("Editor","Paragraph_update");
283 
284  if ($this->updated === true && $this->ctrl->getCmd () != "upload" )
285  {
286  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
287  }
288  else
289  {
290  $this->edit();
291  }
292  }
293 
297  function cancelUpdate()
298  {
299  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
300  }
301 
305  function create()
306  {
307  $this->content_obj = new ilPCSourceCode($this->getPage());
308  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
309  $this->content_obj->setLanguage($_POST["par_language"]);
310 
311  $_SESSION["il_text_lang_".$_GET["ref_id"]] = $_POST["par_language"];
312 
313  $uploaded = $this->upload_source();
314 
315  $this->content_obj->setCharacteristic ($_POST["par_characteristic"]);
316  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
317  $this->content_obj->setDownloadTitle (str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"])));
318  $this->content_obj->setShowLineNumbers (($_POST["par_showlinenumbers"]=='on')?'y':'n');
319  $this->content_obj->setCharacteristic ('Code');
320  $this->content_obj->setAutoIndent (($_POST["par_indent"]=='on')?'y':'n');
321 
322  if ($uploaded) {
323  $this->insert ();
324  return;
325  }
326 
327  $this->updated = $this->content_obj->setText(
328  $this->content_obj->input2xml($_POST["par_content"], 0, false));
329 
330  if ($this->updated !== true)
331  {
332  $this->insert();
333  return;
334  }
335 
336  $this->updated = $this->pg_obj->update();
337 
338  if ($this->updated === true && !$uploaded)
339  {
340  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
341  }
342  else
343  {
344  $this->insert ();
345  }
346  }
347 
351  function cancelCreate()
352  {
353  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
354  }
355 
356  function upload () {
357  $this->upload_source();
358  $this->update ();
359  }
360 
361  function upload_source () {
362  if (isset($_FILES['userfile']['name']))
363  {
364  $userfile = $_FILES['userfile']['tmp_name'];
365 
366  if ($userfile == "" || !is_uploaded_file($userfile))
367  {
368  $error_str = "<b>Error(s):</b><br>Upload error: file name must not be empty!";
369  $this->tpl->setVariable("MESSAGE", $error_str);
370  $this->content_obj->setText($this->content_obj->input2xml(stripslashes($_POST["par_content"]), 0, false));
371  return false;
372  }
373 
374  $_POST["par_content"] = file_get_contents($userfile);
375  $_POST["par_downloadtitle"] = $_FILES['userfile']['name'];
376  return true;
377  }
378 
379  return false;
380  }
381 
382 
383  function readProgLangs () {
384  $prog_langs_ini = file ("Services/COPage/syntax_highlight/php/admin/prog_langs.ini");
385  $prog_langs = array ("" => $this->lng->txt("cont_src_other"));
386  foreach ($prog_langs_ini as $prog_lang) {
387  $prog_lang_prop = split (":", $prog_lang);
388  if ($prog_lang_prop[2] == 1) {
389  $prog_langs[$prog_lang_prop[0]] = $prog_lang_prop[1];
390  }
391  }
392 
393  return $prog_langs;
394  }
395 }
396 ?>