ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCSourcecodeGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/COPage/classes/class.ilPCParagraph.php");
25 require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
26 
38 {
39 
44  function ilPCSourcecodeGUI(&$a_pg_obj, &$a_content_obj, $a_hier_id, $a_pc_id = "")
45  {
46  parent::ilPageContentGUI($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
47  }
48 
49 
53  function &executeCommand()
54  {
55  // get next class that processes or forwards current command
56  $next_class = $this->ctrl->getNextClass($this);
57 
58  // get current command
59  $cmd = $this->ctrl->getCmd();
60 
61  switch($next_class)
62  {
63  default:
64  $ret =& $this->$cmd();
65  break;
66  }
67 
68  return $ret;
69  }
70 
74  function edit()
75  {
76  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.sourcecode_edit.html", "Services/COPage");
77  //$content = $this->pg_obj->getContent();
78  //$cnt = 1;
79  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_edit_src"));
80 
81  if ($this->pg_obj->getParentType() == "lm" ||
82  $this->pg_obj->getParentType() == "dbk")
83  {
84  $this->tpl->setVariable("LINK_ILINK",
85  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp"));
86  $this->tpl->setVariable("TXT_ILINK", "[".$this->lng->txt("cont_internal_link")."]");
87  }
88 
89  $this->displayValidationError();
90 
91  // language and characteristic selection
92  if (key($_POST["cmd"]) == "update")
93  {
94  $s_lang = $_POST["par_language"];
95  $s_char = $_POST["par_characteristic"];
96  $s_subchar = $_POST["par_subcharacteristic"];
97  $s_downloadtitle = str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"]));
98  $s_showlinenumbers = ($_POST["par_showlinenumbers"]=="on")?'y':'n';
99  $s_autoindent = ($_POST["par_autoindent"]=="on")?'y':'n';
100  }
101  else
102  {
103  $s_lang = $this->content_obj->getLanguage();
104  $s_char = $this->content_obj->getCharacteristic();
105  $s_subchar = $this->content_obj->getSubCharacteristic();
106  $s_downloadtitle = $this->content_obj->getDownloadTitle();
107  $s_showlinenumbers = $this->content_obj->getShowLineNumbers();
108  $s_autoindent = $this->content_obj->getAutoIndent ();
109  }
110 
111  $this->setTemplateText($s_lang, $s_subchar);
112 
113  if (key($_POST["cmd"]) == "update")
114  {
115  $s_text = stripslashes($_POST["par_content"]);
116  }
117  else
118  {
119  $s_text = $this->content_obj->xml2output($this->content_obj->getText());
120  }
121 
122  $this->tpl->setVariable("PAR_TA_NAME", "par_content");
123  $this->tpl->setVariable("PAR_TA_CONTENT", ilUtil::prepareFormOutput($s_text));
124 //var_dump($this->tpl);
125 // $this->tpl->parseCurrentBlock();
126 
127  if (strcmp($s_showlinenumbers,"y")==0)
128  {
129  $this->tpl->setVariable("SHOWLINENUMBERS", "checked=\"checked\"");
130  }
131 
132  if (strcmp($s_autoindent,"y") == 0)
133  {
134  $this->tpl->setVariable("AUTOINDENT", "checked=\"checked\"");
135  }
136 
137 
138  $this->tpl->setVariable("DOWNLOAD_TITLE_VALUE", ilUtil::prepareFormOutput($s_downloadtitle));
139 
140  // operations
141  $this->tpl->setCurrentBlock("commands");
142  $this->tpl->setVariable("BTN_NAME", "update");
143  $this->tpl->setVariable("UPLOAD_BTN_NAME", "upload");
144  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
145  $this->tpl->setVariable("BTN_CANCEL", "cancelUpdate");
146  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
147  $this->tpl->parseCurrentBlock();
148  }
149 
150 
151  function setTemplateText ($s_lang, $s_proglang) {
152  $this->tpl->setVariable ("TXT_CREATEFILE", $this->lng->txt("create_download_link"));
153  $this->tpl->setVariable ("TXT_DOWNLOADTITLE", $this->lng->txt("cont_download_title"));
154  $this->tpl->setVariable ("TXT_IMPORTFILE", $this->lng->txt("import_file"));
155  $this->tpl->setVariable ("TXT_UPLOAD_BTN", $this->lng->txt("import"));
156  $this->tpl->setVariable ("TXT_SUBCHARACTERISTIC", $this->lng->txt("cont_src"));
157  $this->tpl->setVariable ("TXT_LANGUAGE", $this->lng->txt("language"));
158  $this->tpl->setVariable ("TXT_SHOWLINENUMBERS", $this->lng->txt("cont_show_line_numbers"));
159  $this->tpl->setVariable ("TXT_AUTOINDENT", $this->lng->txt("cont_autoindent"));
160 
161 
162  $this->tpl->setVariable ("FORMACTION", $this->ctrl->getFormAction($this));
163 
164  require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
166  $select_lang = ilUtil::formSelect ($s_lang,"par_language",$lang,false,true);
167  $this->tpl->setVariable ("SELECT_LANGUAGE", $select_lang);
168 
169  $prog_langs = $this->readProgLangs ();
170 
171  $select_subchar = ilUtil::formSelect ($s_proglang, "par_subcharacteristic",$prog_langs,false,true);
172  $this->tpl->setVariable ("SELECT_SUBCHARACTERISTIC", $select_subchar);
173 
174  }
175 
179  function insert()
180  {
181  global $ilUser;
182 
183  // add paragraph edit template
184  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.sourcecode_edit.html", "Services/COPage");
185  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_insert_src"));
186 
187  if ($this->pg_obj->getParentType() == "lm" ||
188  $this->pg_obj->getParentType() == "dbk")
189  {
190  $this->tpl->setVariable("LINK_ILINK",
191  $this->ctrl->getLinkTargetByClass("ilInternalLinkGUI", "showLinkHelp"));
192  $this->tpl->setVariable("TXT_ILINK", "[".$this->lng->txt("cont_internal_link")."]");
193  }
194 
195  $this->displayValidationError();
196 
197  // get values from new object (repeated form display on error)
198 
199  //echo key ($_POST["cmd"]);
200 
201  if (key($_POST["cmd"]) == "create_src")
202  {
203  $s_lang = $_POST["par_language"];
204  $s_subchar = $_POST["par_subcharacteristic"];
205  $s_downloadtitle = str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"]));
206  $s_showlinenumbers = strcmp($_POST["par_showlinenumbers"],'on')==0?'checked=\"true\"':'';
207  $s_autoindent = strcmp($_POST["par_autoindent"],'on')==0?'checked=\"true\"':'';
208  $s_isexample = strcmp($_POST["par_isexample"],"on")==0?'checked=\"true\"':'';
209  }
210  else
211  {
212  if ($_SESSION["il_text_lang_".$_GET["ref_id"]] != "")
213  {
214  $s_lang = $_SESSION["il_text_lang_".$_GET["ref_id"]];
215  }
216  else
217  {
218  $s_lang = $ilUser->getLanguage();
219  }
220 
221  $s_showlinenumbers = 'CHECKED';
222  $s_autoindent = 'CHECKED';
223  $s_isexample = '';
224  $s_subchar = '';
225  }
226 
227  $this->setTemplateText($s_lang, $s_subchar);
228 
229  $this->tpl->setVariable("SHOWLINENUMBERS", $s_showlinenumbers);
230  $this->tpl->setVariable("AUTOINDENT", $s_autoindent);
231  $this->tpl->setVariable("DOWNLOAD_TITLE_VALUE", $s_downloadtitle);
232  $this->tpl->setVariable("ISEXAMPLE", $s_isexample);
233 
234 
235  // content is in utf-8, todo: set globally
236  // header('Content-type: text/html; charset=UTF-8');
237 
238  // input text area
239  $this->tpl->setVariable("PAR_TA_NAME", "par_content");
240 
241  if (key($_POST["cmd"]) == "create_src")
242  {
243  $this->tpl->setVariable("PAR_TA_CONTENT", ilUtil::prepareFormOutput(stripslashes($_POST["par_content"])));
244  }
245  else
246  {
247  $this->tpl->setVariable("PAR_TA_CONTENT", "");
248  }
249 // $this->tpl->parseCurrentBlock();
250 
251  // operations
252  $this->tpl->setCurrentBlock("commands");
253  $this->tpl->setVariable("BTN_NAME", "create_src"); //--
254  $this->tpl->setVariable("UPLOAD_BTN_NAME", "create_src");
255  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
256  $this->tpl->setVariable("BTN_CANCEL", "cancelCreate");
257  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
258  $this->tpl->parseCurrentBlock();
259 
260  }
261 
262 
266  function update()
267  {
268  global $ilBench;
269 
270  $ilBench->start("Editor","Paragraph_update");
271  // set language and characteristic
272 
273  $this->content_obj->setLanguage($_POST["par_language"]);
274  $this->content_obj->setCharacteristic($_POST["par_characteristic"]);
275 
276  //echo "PARupdate:".htmlentities($this->content_obj->input2xml($_POST["par_content"])).":<br>"; exit;
277 
278 
279  // set language and characteristic
280  $this->content_obj->setLanguage($_POST["par_language"]);
281  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
282  $this->content_obj->setDownloadTitle(str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"])));
283  $this->content_obj->setShowLineNumbers(($_POST["par_showlinenumbers"]=="on")?"y":"n");
284  $this->content_obj->setAutoIndent(($_POST["par_autoindent"]=="on")?"y":"n");
285  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
286  $this->content_obj->setCharacteristic("Code");
287 
288  $this->updated = $this->content_obj->setText(
289  $this->content_obj->input2xml($_POST["par_content"], 0, false));
290 
291  if ($this->updated !== true)
292  {
293  //echo "Did not update!";
294  $ilBench->stop("Editor","Paragraph_update");
295  $this->edit();
296  return;
297  }
298 
299  $this->updated = $this->pg_obj->update();
300 
301  $ilBench->stop("Editor","Paragraph_update");
302 
303  if ($this->updated === true && $this->ctrl->getCmd () != "upload" )
304  {
305  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
306  }
307  else
308  {
309  $this->edit();
310  }
311  }
312 
316  function cancelUpdate()
317  {
318  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
319  }
320 
324  function create()
325  {
326  $this->content_obj =& new ilPCParagraph($this->dom);
327  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
328  $this->content_obj->setLanguage($_POST["par_language"]);
329 
330  $_SESSION["il_text_lang_".$_GET["ref_id"]] = $_POST["par_language"];
331 
332  $uploaded = $this->upload_source();
333 
334  $this->content_obj->setCharacteristic ($_POST["par_characteristic"]);
335  $this->content_obj->setSubCharacteristic($_POST["par_subcharacteristic"]);
336  $this->content_obj->setDownloadTitle (str_replace('"', '', ilUtil::stripSlashes($_POST["par_downloadtitle"])));
337  $this->content_obj->setShowLineNumbers (($_POST["par_showlinenumbers"]=='on')?'y':'n');
338  $this->content_obj->setCharacteristic ('Code');
339  $this->content_obj->setAutoIndent (($_POST["par_indent"]=='on')?'y':'n');
340 
341  if ($uploaded) {
342  $this->insert ();
343  return;
344  }
345 
346  $this->updated = $this->content_obj->setText(
347  $this->content_obj->input2xml($_POST["par_content"], 0, false));
348 
349  if ($this->updated !== true)
350  {
351  $this->insert();
352  return;
353  }
354 
355  $this->updated = $this->pg_obj->update();
356 
357  if ($this->updated === true && !$uploaded)
358  {
359  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
360  }
361  else
362  {
363  $this->insert ();
364  }
365  }
366 
370  function cancelCreate()
371  {
372  $this->ctrl->returnToParent($this, "jump".$this->hier_id);
373  }
374 
375  function upload () {
376  $this->upload_source();
377  $this->update ();
378  }
379 
380  function upload_source () {
381  if (isset($_FILES['userfile']['name']))
382  {
383  $userfile = $_FILES['userfile']['tmp_name'];
384 
385  if ($userfile == "" || !is_uploaded_file($userfile))
386  {
387  $error_str = "<b>Error(s):</b><br>Upload error: file name must not be empty!";
388  $this->tpl->setVariable("MESSAGE", $error_str);
389  $this->content_obj->setText($this->content_obj->input2xml(stripslashes($_POST["par_content"]), 0, false));
390  return false;
391  }
392 
393  $_POST["par_content"] = file_get_contents($userfile);
394  $_POST["par_downloadtitle"] = $_FILES['userfile']['name'];
395  return true;
396  }
397 
398  return false;
399  }
400 
401 
402  function readProgLangs () {
403  $prog_langs_ini = file ("Services/COPage/syntax_highlight/php/admin/prog_langs.ini");
404  $prog_langs = array ("" => $this->lng->txt("cont_src_other"));
405  foreach ($prog_langs_ini as $prog_lang) {
406  $prog_lang_prop = split (":", $prog_lang);
407  if ($prog_lang_prop[2] == 1) {
408  $prog_langs[$prog_lang_prop[0]] = $prog_lang_prop[1];
409  }
410  }
411 
412  return $prog_langs;
413  }
414 }
415 ?>