ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCDataTableGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 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.ilPCDataTable.php");
25 require_once("./Services/COPage/classes/class.ilPCTableGUI.php");
26 require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
27 
39 {
40 
45  function ilPCDataTableGUI(&$a_pg_obj, &$a_content_obj, $a_hier_id, $a_pc_id = "")
46  {
47  parent::ilPageContentGUI($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
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 
71 
75  function edit()
76  {
77 return parent::edit();
78  }
79 
83  function editData()
84  {
85  global $lng, $ilCtrl;
86 //var_dump($_GET);
87 //var_dump($_POST);
88 
89  $this->setTabs();
90 
91  $this->displayValidationError();
92 
93  include_once("./Services/COPage/classes/class.ilPCParagraph.php");
94 
95  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.tabledata.html", "Services/COPage");
96  $dtpl = $this->tpl;
97  //$dtpl = new ilTemplate("tpl.tabledata.html", true, true, "Services/COPage");
98  $dtpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this, "tableAction"));
99  $dtpl->setVariable("BB_MENU", $this->getBBMenu());
100 
101  $this->tpl->addJavascript("./Services/COPage/phpBB/3_0_0/editor.js");
102  $this->tpl->addJavascript("./Services/COPage/js/page_editing.js");
103 
104  // get all rows
105  $xpc = xpath_new_context($this->dom);
106  $path = "//PageContent[@HierId='".$this->getHierId()."']".
107  "/Table/TableRow";
108  $res =& xpath_eval($xpc, $path);
109 
110  for($i = 0; $i < count($res->nodeset); $i++)
111  {
112 
113  $xpc2 = xpath_new_context($this->dom);
114  $path2 = "//PageContent[@HierId='".$this->getHierId()."']".
115  "/Table/TableRow[$i+1]/TableData";
116  $res2 =& xpath_eval($xpc2, $path2);
117 
118  // if this is the first row -> col icons
119  if ($i == 0)
120  {
121  for($j = 0; $j < count($res2->nodeset); $j++)
122  {
123  if ($j == 0)
124  {
125  $dtpl->touchBlock("empty_td");
126  }
127 
128  if ($j == 0)
129  {
130  if (count($res2->nodeset) == 1)
131  {
132  $move_type = "none";
133  }
134  else
135  {
136  $move_type = "forward";
137  }
138  }
139  else if ($j == (count($res2->nodeset) - 1))
140  {
141  $move_type = "backward";
142  }
143  else
144  {
145  $move_type = "both";
146  }
147  $dtpl->setCurrentBlock("col_icon");
148  $dtpl->setVariable("COL_ICON_ALT", $lng->txt("content_column"));
149  $dtpl->setVariable("COL_ICON", ilUtil::getImagePath("col.gif"));
150  $dtpl->setVariable("COL_ONCLICK", "COL_".$move_type);
151  $dtpl->setVariable("NR", $j);
152  $dtpl->parseCurrentBlock();
153  }
154  $dtpl->setCurrentBlock("row");
155  $dtpl->parseCurrentBlock();
156  }
157 
158 
159  for($j = 0; $j < count($res2->nodeset); $j++)
160  {
161  // first col: row icons
162  if ($j == 0)
163  {
164  if ($i == 0)
165  {
166  if (count($res->nodeset) == 1)
167  {
168  $move_type = "none";
169  }
170  else
171  {
172  $move_type = "forward";
173  }
174  }
175  else if ($i == (count($res->nodeset) - 1))
176  {
177  $move_type = "backward";
178  }
179  else
180  {
181  $move_type = "both";
182  }
183  $dtpl->setCurrentBlock("row_icon");
184  $dtpl->setVariable("ROW_ICON_ALT", $lng->txt("content_row"));
185  $dtpl->setVariable("ROW_ICON", ilUtil::getImagePath("row.gif"));
186  $dtpl->setVariable("ROW_ONCLICK", "ROW_".$move_type);
187  $dtpl->setVariable("NR", $i);
188  $dtpl->parseCurrentBlock();
189  }
190 
191  // cell
192  $dtpl->setCurrentBlock("cell");
193 
194  if (is_array($_POST["cmd"]) && key($_POST["cmd"]) == "update")
195  {
196  $s_text = ilUtil::stripSlashes("cell_".$i."_".$j, false);
197  }
198  else
199  {
200  $s_text = ilPCParagraph::xml2output($this->content_obj->getCellText($i, $j));
201  }
202 
203  $dtpl->setVariable("PAR_TA_NAME", "cell[".$i."][".$j."]");
204  $dtpl->setVariable("PAR_TA_CONTENT", $s_text);
205  $dtpl->parseCurrentBlock();
206  }
207  $dtpl->setCurrentBlock("row");
208  $dtpl->parseCurrentBlock();
209  }
210 
211  // init menues
212  $types = array("row", "col");
213  $moves = array("none", "backward", "both", "forward");
214  $commands = array(
215  "row" => array( "newRowAfter" => "cont_ed_new_row_after",
216  "newRowBefore" => "cont_ed_new_row_before",
217  "moveRowUp" => "cont_ed_row_up",
218  "moveRowDown" => "cont_ed_row_down",
219  "deleteRow" => "cont_ed_delete_row"),
220  "col" => array( "newColAfter" => "cont_ed_new_col_after",
221  "newColBefore" => "cont_ed_new_col_before",
222  "moveColLeft" => "cont_ed_col_left",
223  "moveColRight" => "cont_ed_col_right",
224  "deleteCol" => "cont_ed_delete_col")
225  );
226 
227  foreach($types as $type)
228  {
229  foreach($moves as $move)
230  {
231  foreach($commands[$type] as $command => $lang_var)
232  {
233  if ($move == "none" && (substr($command, 0, 4) == "move"))
234  {
235  continue;
236  }
237  if (($move == "backward" && (in_array($command, array("movedown", "moveright")))) ||
238  ($move == "forward" && (in_array($command, array("moveup", "moveleft")))))
239  {
240  continue;
241  }
242  $this->tpl->setCurrentBlock("menu_item");
243  $this->tpl->setVariable("MENU_ITEM_TITLE", $lng->txt($lang_var));
244  $this->tpl->setVariable("CMD", $command);
245  $this->tpl->setVariable("TYPE", $type);
246  $this->tpl->parseCurrentBlock();
247  }
248  $this->tpl->setCurrentBlock("menu");
249  $this->tpl->setVariable("TYPE", $type);
250  $this->tpl->setVariable("MOVE", $move);
251  $this->tpl->parseCurrentBlock();
252  }
253  }
254 
255  // update/cancel
256  $this->tpl->setCurrentBlock("commands");
257  $this->tpl->setVariable("BTN_NAME", "update");
258  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
259  $this->tpl->parseCurrentBlock();
260 
261  $this->tpl->setVariable("FORMACTION2",
262  $ilCtrl->getFormAction($this, "tableAction"));
263  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_table"));
264 
265  }
266 
270  function update($a_redirect = true)
271  {
272  global $ilBench, $lng;
273 
274  $ilBench->start("Editor","Data_Table_update");
275 
276  // handle input data
277  include_once("./Services/COPage/classes/class.ilPCParagraph.php");
278  $data = array();
279 //var_dump($_POST);
280 //var_dump($_GET);
281  if (is_array($_POST["cell"]))
282  {
283  foreach ($_POST["cell"] as $i => $row)
284  {
285  if (is_array($row))
286  {
287  foreach ($row as $j => $cell)
288  {
289  $data[$i][$j] =
291  $this->content_obj->getLanguage());
292  }
293  }
294  }
295  }
296 
297  $this->updated = $this->content_obj->setData($data);
298 
299  if ($this->updated !== true)
300  {
301  $ilBench->stop("Editor","Data_Table_update");
302  $this->editData();
303  return;
304  }
305 
306  $this->updated = $this->pg_obj->update();
307  $ilBench->stop("Editor","Data_Table_update");
308 
309  if ($a_redirect)
310  {
311  ilUtil::sendInfo($lng->txt("msg_obj_modified", true));
312  $this->ctrl->redirect($this, "editData");
313  }
314  }
315 
319  function setWidth()
320  {
321  if (is_array($_POST["target"]))
322  {
323  foreach ($_POST["target"] as $hier_id)
324  {
325  $this->content_obj->setTDWidth($hier_id, $_POST["td_width"]);
326  }
327  }
328  $this->setProperties();
329  $this->updated = $this->pg_obj->update();
330  $this->pg_obj->addHierIDs();
331  $this->edit();
332  }
333 
337  function setClass()
338  {
339  if (is_array($_POST["target"]))
340  {
341  foreach ($_POST["target"] as $hier_id)
342  {
343  $this->content_obj->setTDClass($hier_id, $_POST["td_class"]);
344  }
345  }
346  $this->setProperties();
347  $this->updated = $this->pg_obj->update();
348  $this->pg_obj->addHierIDs();
349  $this->edit();
350  }
351 
352 
356  function insert()
357  {
358  global $ilUser, $ilCtrl, $tpl, $lng;
359 
360  $this->displayValidationError();
361 
362  // edit form
363  include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
364  $form = new ilPropertyFormGUI();
365  $form->setFormAction($ilCtrl->getFormAction($this));
366  $form->setTitle($this->lng->txt("cont_insert_table"));
367 
368  $nr = array();
369  for($i=1; $i<=20; $i++)
370  {
371  $nr[$i] = $i;
372  }
373 
374  // cols
375  $cols = new ilSelectInputGUI($this->lng->txt("cont_nr_cols"), "nr_cols");
376  $cols->setOptions($nr);
377  $cols->setValue(2);
378  $form->addItem($cols);
379 
380  // rows
381  $rows = new ilSelectInputGUI($this->lng->txt("cont_nr_rows"), "nr_rows");
382  $rows->setOptions($nr);
383  $rows->setValue(2);
384  $form->addItem($rows);
385 
386  // width
387  $width = new ilTextInputGUI($this->lng->txt("cont_table_width"), "width");
388  $width->setValue("");
389  $width->setSize(6);
390  $width->setMaxLength(6);
391  $form->addItem($width);
392 
393  // border
394  $border = new ilTextInputGUI($this->lng->txt("cont_table_border"), "border");
395  $border->setValue("1px");
396  $border->setSize(6);
397  $border->setMaxLength(6);
398  $form->addItem($border);
399 
400  // padding
401  $padding = new ilTextInputGUI($this->lng->txt("cont_table_cellpadding"), "padding");
402  $padding->setValue("2px");
403  $padding->setSize(6);
404  $padding->setMaxLength(6);
405  $form->addItem($padding);
406 
407  // spacing
408  $spacing = new ilTextInputGUI($this->lng->txt("cont_table_cellspacing"), "spacing");
409  $spacing->setValue("0px");
410  $spacing->setSize(6);
411  $spacing->setMaxLength(6);
412  $form->addItem($spacing);
413 
414  // first row style
415  require_once("./Services/Form/classes/class.ilRadioMatrixInputGUI.php");
416  $fr_style = new ilRadioMatrixInputGUI($this->lng->txt("cont_first_row_style"), "first_row_style");
417  $options = array("" => $this->lng->txt("none"), "ilc_Cell1" => "Cell1", "ilc_Cell2" => "Cell2",
418  "ilc_Cell3" => "Cell3", "ilc_Cell4" => "Cell4");
419  foreach($options as $k => $option)
420  {
421  $options[$k] = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="'.$k.'">'.
422  $option.'</td></tr></table>';
423  }
424 
425  // first row style
426  require_once("./Services/Form/classes/class.ilRadioMatrixInputGUI.php");
427  $fr_style = new ilRadioMatrixInputGUI($this->lng->txt("cont_first_row_style"), "first_row_style");
428  $options = array("" => $this->lng->txt("none"), "ilc_Cell1" => "Cell1", "ilc_Cell2" => "Cell2",
429  "ilc_Cell3" => "Cell3", "ilc_Cell4" => "Cell4");
430  foreach($options as $k => $option)
431  {
432  $options[$k] = '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="'.$k.'">'.
433  $option.'</td></tr></table>';
434  }
435 
436  $fr_style->setValue("");
437  $fr_style->setOptions($options);
438  $form->addItem($fr_style);
439 
440  // alignment
441  $align_opts = array("Left" => $lng->txt("cont_left"),
442  "Right" => $lng->txt("cont_right"), "Center" => $lng->txt("cont_center"),
443  "LeftFloat" => $lng->txt("cont_left_float"),
444  "RightFloat" => $lng->txt("cont_right_float"));
445  $align = new ilSelectInputGUI($this->lng->txt("cont_align"), "align");
446  $align->setOptions($align_opts);
447  $align->setValue("Center");
448  $form->addItem($align);
449 
450  // import table
451  $import = new ilRadioGroupInputGUI($this->lng->txt("cont_paste_table"), "import_type");
452  $op = new ilRadioOption($this->lng->txt("cont_html_table"), "html");
453  $import->addOption($op);
454  $op2 = new ilRadioOption($this->lng->txt("cont_spreadsheet_table"), "spreadsheet");
455 
456  $import_data = new ilTextAreaInputGUI("", "import_table");
457  $import_data->setRows(8);
458  $import_data->setCols(50);
459  $op2->addSubItem($import_data);
460 
461  $import->addOption($op2);
462  $import->setValue("html");
463  $form->addItem($import);
464 
465  // language
466  if ($_SESSION["il_text_lang_".$_GET["ref_id"]] != "")
467  {
468  $s_lang = $_SESSION["il_text_lang_".$_GET["ref_id"]];
469  }
470  else
471  {
472  $s_lang = $ilUser->getLanguage();
473  }
474  require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
476  //$select_language = ilUtil::formSelect ($s_lang, "tab_language", $lang, false, true);
477  $language = new ilSelectInputGUI($this->lng->txt("language"), "tab_language");
478  $language->setOptions($lang);
479  $language->setValue($s_lang);
480  $form->addItem($language);
481 
482  $form->addCommandButton("create_tab", $lng->txt("save"));
483  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
484 
485  $html = $form->getHTML();
486  $tpl->setContent($html);
487 return;
488 
489 
490  // new table form (input of rows and columns)
491  $this->tpl->addBlockFile("ADM_CONTENT", "adm_content", "tpl.table_new.html", "Services/COPage");
492  $this->tpl->setVariable("TXT_ACTION", $this->lng->txt("cont_insert_table"));
493  $this->tpl->setVariable("FORMACTION", $this->ctrl->getFormAction($this));
494 
495  $this->displayValidationError();
496 
497  for($i=1; $i<=10; $i++)
498  {
499  $nr[$i] = $i;
500  }
501 
502  if ($_SESSION["il_text_lang_".$_GET["ref_id"]] != "")
503  {
504  $s_lang = $_SESSION["il_text_lang_".$_GET["ref_id"]];
505  }
506  else
507  {
508  $s_lang = $ilUser->getLanguage();
509  }
510 
511  // select fields for number of columns
512  $this->tpl->setVariable("TXT_LANGUAGE", $this->lng->txt("language"));
513  require_once("Services/MetaData/classes/class.ilMDLanguageItem.php");
515  $select_language = ilUtil::formSelect ($s_lang, "tab_language", $lang, false, true);
516  $this->tpl->setVariable("SELECT_LANGUAGE", $select_language);
517  $this->tpl->setVariable("TXT_COLS", $this->lng->txt("cont_nr_cols"));
518  $select_cols = ilUtil::formSelect ("2","nr_cols",$nr,false,true);
519  $this->tpl->setVariable("SELECT_COLS", $select_cols);
520  $this->tpl->setVariable("TXT_ROWS", $this->lng->txt("cont_nr_rows"));
521  $select_rows = ilUtil::formSelect ("2","nr_rows",$nr,false,true);
522  $this->tpl->setVariable("SELECT_ROWS", $select_rows);
523 
524  //import html table
525  $this->tpl->setVariable("TXT_HTML_IMPORT", $this->lng->txt("cont_table_html_import"));
526  $this->tpl->setVariable("TXT_SPREADSHEET", $this->lng->txt("cont_table_spreadsheet_import"));
527  $this->tpl->setVariable("TXT_BTN_HTML_IMPORT", $this->lng->txt("import"));
528  $this->tpl->setVariable("TXT_HTML_IMPORT_INFO", $this->lng->txt("cont_table_html_import_info"));
529  $this->tpl->setVariable("TXT_SPREADSHEET_IMPORT_INFO", $this->lng->txt("cont_table_spreadsheet_import_info"));
530  $this->tpl->setVariable("CMD_HTML_IMPORT", "create_tab");
531  $this->tpl->setVariable("SELECT_ROWS", $select_rows);
532 
533 // $this->tpl->parseCurrentBlock();
534 
535  // operations
536  $this->tpl->setCurrentBlock("commands");
537  $this->tpl->setVariable("BTN_NAME", "create_tab");
538  $this->tpl->setVariable("BTN_TEXT", $this->lng->txt("save"));
539  $this->tpl->setVariable("BTN_CANCEL", "cancelCreate");
540  $this->tpl->setVariable("TXT_CANCEL", $this->lng->txt("cancel"));
541  $this->tpl->parseCurrentBlock();
542 
543  }
544 
545 
549  function create()
550  {
551  global $lng, $ilCtrl;
552 
553  $this->content_obj = new ilPCDataTable($this->dom);
554  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
555  $this->content_obj->setLanguage(ilUtil::stripSlashes($_POST["tab_language"]));
556  $import_table = trim($_POST["import_table"]);
557 
558  // import xhtml or spreadsheet table
559  if (!empty ($import_table))
560  {
561  switch($_POST["import_type"])
562  {
563  // xhtml import
564  case "html":
565  if (!$this->content_obj->importHtml ($_POST["tab_language"], $import_table))
566  {
567  $this->insert();
568  return;
569  }
570  break;
571 
572  // spreadsheet
573  case "spreadsheet":
574  $this->content_obj->importSpreadsheet($_POST["tab_language"], $import_table);
575  break;
576  }
577  }
578  else
579  {
580  $this->content_obj->addRows(ilUtil::stripSlashes($_POST["nr_rows"]),
581  ilUtil::stripSlashes($_POST["nr_cols"]));
582  }
583  $this->content_obj->setWidth(ilUtil::stripSlashes($_POST["width"]));
584  $this->content_obj->setBorder(ilUtil::stripSlashes($_POST["border"]));
585  $this->content_obj->setCellPadding(ilUtil::stripSlashes($_POST["padding"]));
586  $this->content_obj->setCellSpacing(ilUtil::stripSlashes($_POST["spacing"]));
587  $this->content_obj->setHorizontalAlign(ilUtil::stripSlashes($_POST["align"]));
588 
589  $frtype = ilUtil::stripSlashes($_POST["first_row_style"]);
590  if ($frtype != "")
591  {
592  $this->content_obj->setFirstRowStyle($frtype);
593  }
594 
595  $this->updated = $this->pg_obj->update();
596 
597  if ($this->updated === true)
598  {
599  $this->pg_obj->stripHierIDs();
600  $this->pg_obj->addHierIDs();
601  $ilCtrl->setParameter($this, "hier_id", $this->content_obj->readHierId());
602  $ilCtrl->setParameter($this, "pc_id", $this->content_obj->readPCId());
603  $this->content_obj->setHierId($this->content_obj->readHierId());
604  $this->setHierId($this->content_obj->readHierId());
605  $this->content_obj->setPCId($this->content_obj->readPCId());
606 //echo $this->content_obj->readHierId().":".$this->content_obj->readPCId();
607  $this->editData();
608  //$ilCtrl->redirect($this, "editData");
609 
610 // $this->ctrl->returnToParent($this, "jump".$this->hier_id);
611  }
612  else
613  {
614  $this->insert();
615  }
616  }
617 
621  function tableAction()
622  {
623  global $ilCtrl;
624 
625  $this->update(false);
626  $this->pg_obj->addHierIDs();
627 
628  $cell_hier_id = ($_POST["type"] == "col")
629  ? $this->hier_id."_1_".($_POST["id"] + 1)
630  : $this->hier_id."_".($_POST["id"] + 1)."_1";
631  $cell_obj = $this->pg_obj->getContentObject($cell_hier_id);
632  if (is_object($cell_obj))
633  {
634  $cell_obj->$_POST["action"]();
635  $_SESSION["il_pg_error"] = $this->pg_obj->update();
636  }
637  $ilCtrl->redirect($this, "editData");
638  }
639 
643  function setTabs()
644  {
645  global $ilCtrl, $ilTabs;
646 
647  parent::setTabs();
648 
649  $ilTabs->addTarget("cont_ed_edit_data",
650  $ilCtrl->getLinkTarget($this, "editData"), "editData",
651  get_class($this));
652 
653  }
654 
655 }
656 ?>