ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPCTable.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.ilPageContent.php");
25 
36 class ilPCTable extends ilPageContent
37 {
38  var $dom;
39  var $tab_node;
40 
41 
45  function init()
46  {
47  $this->setType("tab");
48  }
49 
50  function setNode(&$a_node)
51  {
52  parent::setNode($a_node); // this is the PageContent node
53  $this->tab_node =& $a_node->first_child(); // this is the Table node
54  }
55 
56  function create(&$a_pg_obj, $a_hier_id, $a_pc_id = "")
57  {
58  $this->node = $this->createPageContentNode();
59  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
60  $this->tab_node =& $this->dom->create_element("Table");
61  $this->tab_node =& $this->node->append_child($this->tab_node);
62  $this->tab_node->set_attribute("Language", "");
63  }
64 
65  function &addRow () {
66  $new_tr =& $this->dom->create_element("TableRow");
67  $new_tr = &$this->tab_node->append_child($new_tr);
68  return $new_tr;
69  }
70 
71  function &addCell (&$aRow, $a_data = "", $a_lang = "")
72  {
73  $new_td =& $this->dom->create_element("TableData");
74  $new_td =& $aRow->append_child($new_td);
75 
76  // insert data if given
77  if ($a_data != "")
78  {
79  $new_pg =& $this->createPageContentNode(false);
80  $new_par =& $this->dom->create_element("Paragraph");
81  $new_par =& $new_pg->append_child($new_par);
82  $new_par->set_attribute("Language", $a_lang);
83  $new_par->set_attribute("Characteristic", "TableContent");
84  $new_par->set_content($a_data);
85  $new_td->append_child ($new_pg);
86  }
87 
88  return $new_td;
89  }
90 
94  function addRows($a_nr_rows, $a_nr_cols)
95  {
96  for ($i=1; $i<=$a_nr_rows; $i++)
97  {
98  $aRow = $this->addRow();
99  for ($j=1; $j<=$a_nr_cols; $j++)
100  {
101  $this->addCell($aRow);
102  }
103  }
104  }
105 
109  function importSpreadsheet($a_lang, $a_data)
110  {
111  str_replace($a_data, "\r", "\n");
112  str_replace($a_data, "\n\n", "\n");
113  $target_rows = array();
114  $rows = explode("\n", $a_data);
115 
116  // get maximum of cols in a row and
117  // put data in target_row arrays
118  foreach($rows as $row)
119  {
120  $cells = explode("\t", $row);
121  $max_cols = ($max_cols > count($cells))
122  ? $max_cols
123  : count($cells);
124  $target_rows[] = $cells;
125  }
126 
127  // iterate target row arrays and insert data
128  foreach($target_rows as $row)
129  {
130  $aRow = $this->addRow();
131  for ($j=0; $j<$max_cols; $j++)
132  {
133  // mask html
134  $data = str_replace("&","&amp;", $row[$j]);
135  $data = str_replace("<","&lt;", $data);
136  $data = str_replace(">","&gt;", $data);
137 
138  $this->addCell($aRow, $data, $a_lang);
139  }
140  }
141  }
142 
146  function getLanguage()
147  {
148  return $this->tab_node->get_attribute("Language");
149  }
150 
156  function setLanguage($a_lang)
157  {
158  if($a_lang != "")
159  {
160  $this->tab_node->set_attribute("Language", $a_lang);
161  }
162  }
163 
167  function getWidth()
168  {
169  return $this->tab_node->get_attribute("Width");
170  }
171 
177  function setWidth($a_width)
178  {
179  if($a_width != "")
180  {
181  $this->tab_node->set_attribute("Width", $a_width);
182  }
183  else
184  {
185  if ($this->tab_node->has_attribute("Width"))
186  {
187  $this->tab_node->remove_attribute("Width");
188  }
189  }
190  }
191 
192 
193 
197  function getBorder()
198  {
199  return $this->tab_node->get_attribute("Border");
200  }
201 
207  function setBorder($a_border)
208  {
209  if($a_border != "")
210  {
211  $this->tab_node->set_attribute("Border", $a_border);
212  }
213  else
214  {
215  if ($this->tab_node->has_attribute("Border"))
216  {
217  $this->tab_node->remove_attribute("Border");
218  }
219  }
220  }
221 
225  function getCellSpacing()
226  {
227  return $this->tab_node->get_attribute("CellSpacing");
228  }
229 
235  function setCellSpacing($a_spacing)
236  {
237  if($a_spacing != "")
238  {
239  $this->tab_node->set_attribute("CellSpacing", $a_spacing);
240  }
241  else
242  {
243  if ($this->tab_node->has_attribute("CellSpacing"))
244  {
245  $this->tab_node->remove_attribute("CellSpacing");
246  }
247  }
248  }
249 
253  function getCellPadding()
254  {
255  return $this->tab_node->get_attribute("CellPadding");
256  }
257 
263  function setCellPadding($a_padding)
264  {
265  if($a_padding != "")
266  {
267  $this->tab_node->set_attribute("CellPadding", $a_padding);
268  }
269  else
270  {
271  if ($this->tab_node->has_attribute("CellPadding"))
272  {
273  $this->tab_node->remove_attribute("CellPadding");
274  }
275  }
276  }
277 
281  function setHorizontalAlign($a_halign)
282  {
283  $this->tab_node->set_attribute("HorizontalAlign", $a_halign);
284  }
285 
290  {
291  return $this->tab_node->get_attribute("HorizontalAlign");
292  }
293 
297  function setTDWidth($a_hier_id, $a_width, $a_pc_id = "")
298  {
299  $xpc = xpath_new_context($this->dom);
300 
301  if ($a_pc_id == "")
302  {
303  $path = "//TableData[@HierId = '".$a_hier_id."']";
304  }
305  else
306  {
307  $path = "//TableData[@PCID = '".$a_pc_id."']";
308  }
309  $res =& xpath_eval($xpc, $path);
310 
311  if (count($res->nodeset) == 1)
312  {
313  if($a_width != "")
314  {
315  $res->nodeset[0]->set_attribute("Width", $a_width);
316  }
317  else
318  {
319  if ($res->nodeset[0]->has_attribute("Width"))
320  {
321  $res->nodeset[0]->remove_attribute("Width");
322  }
323  }
324  }
325  }
326 
330  function setTDClass($a_hier_id, $a_class, $a_pc_id = "")
331  {
332  $xpc = xpath_new_context($this->dom);
333  if ($a_pc_id == "")
334  {
335  $path = "//TableData[@HierId = '".$a_hier_id."']";
336  }
337  else
338  {
339  $path = "//TableData[@PCID = '".$a_pc_id."']";
340  }
341  $res =& xpath_eval($xpc, $path);
342  if (count($res->nodeset) == 1)
343  {
344  if($a_class != "")
345  {
346  $res->nodeset[0]->set_attribute("Class", $a_class);
347  }
348  else
349  {
350  if ($res->nodeset[0]->has_attribute("Class"))
351  {
352  $res->nodeset[0]->remove_attribute("Class");
353  }
354  }
355  }
356  }
357 
361  function getCaption()
362  {
363  $hier_id = $this->getHierId();
364  if(!empty($hier_id))
365  {
366  $xpc = xpath_new_context($this->dom);
367  $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
368  $res =& xpath_eval($xpc, $path);
369 
370  if (count($res->nodeset) == 1)
371  {
372  return $res->nodeset[0]->get_content();
373  }
374  }
375  }
376 
380  function getCaptionAlign()
381  {
382  $hier_id = $this->getHierId();
383  if(!empty($hier_id))
384  {
385  $xpc = xpath_new_context($this->dom);
386  $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
387  $res =& xpath_eval($xpc, $path);
388  if (count($res->nodeset) == 1)
389  {
390  return $res->nodeset[0]->get_attribute("Align");
391  }
392  }
393  }
394 
398  function setCaption($a_content, $a_align)
399  {
400  if ($a_content != "")
401  {
402  ilDOMUtil::setFirstOptionalElement($this->dom, $this->tab_node, "Caption",
403  array("Summary", "TableRow"), $a_content,
404  array("Align" => $a_align));
405  }
406  else
407  {
408  ilDOMUtil::deleteAllChildsByName($this->tab_node, array("Caption"));
409  }
410  }
411 
412 
414  /*echo "importing table attributes";
415  var_dump($tableNode);*/
416  if ($node->has_attributes ())
417  {
418  foreach($node->attributes() as $n)
419  {
420 
421  switch (strtolower($n->node_name ())) {
422  case "border":
423  $this->setBorder ($this->extractText($n));
424  break;
425  case "align":
426  $this->setHorizontalAlign(ucfirst(strtolower($this->extractText($n))));
427  break;
428  case "cellspacing":
429  $this->setCellSpacing($this->extractText($n));
430  break;
431  case "cellpadding":
432  $this->setCellPadding($this->extractText($n));
433  break;
434  case "width":
435  $this->setWidth($this->extractText($n));
436  break;
437 
438  }
439 
440  }
441  }
442  }
443 
444 
445  function importCellAttributes (&$node, &$par) {
446  /*echo "importing table attributes";
447  var_dump($tableNode);*/
448  if ($node->has_attributes ())
449  {
450  foreach($node->attributes() as $n)
451  {
452 
453  switch (strtolower($n->node_name ())) {
454  case "class":
455  $par->set_attribute("Class", $this->extractText($n));
456  break;
457  case "width":
458  $par->set_attribute("Width", $this->extractText($n));
459  break;
460  }
461 
462  }
463  }
464  }
465 
466 
467  function importRow ($lng, &$node) {
468  /*echo "add Row";
469  var_dump($node);*/
470 
471  $aRow = $this->addRow();
472 
473  if ($node->has_child_nodes())
474  {
475  foreach($node->child_nodes() as $n)
476  {
477  if ($n->node_type() == XML_ELEMENT_NODE &&
478  strcasecmp($n->node_name (), "td") == 0)
479  {
480  $this->importCell ($lng, $n, $aRow);
481  }
482  }
483  }
484  }
485 
486  function importCell ($lng, &$cellNode, &$aRow) {
487  /*echo "add Cell";
488  var_dump($cellNode);*/
489  $aCell = $this->addCell($aRow);
490  $par = new ilPCParagraph($this->dom);
491  $par->createAtNode($aCell);
492  $par->setText($par->input2xml($this->extractText ($cellNode)));
493  $par->setCharacteristic("TableContent");
494  $par->setLanguage($lng);
495  $this->importCellAttributes($cellNode, $aCell);
496  }
497 
498  function extractText (&$node) {
499  $owner_document = $node->owner_document ();
500  $children = $node->child_nodes();
501  $total_children = count($children);
502  for ($i = 0; $i < $total_children; $i++){
503  $cur_child_node = $children[$i];
504  $output .= $owner_document->dump_node($cur_child_node);
505  }
506  return $output;
507  }
508 
509  function importHtml ($lng, $htmlTable) {
510  $dummy = ilUtil::stripSlashes($htmlTable, false);
511  //echo htmlentities($dummy);
512  $dom = @domxml_open_mem($dummy,DOMXML_LOAD_PARSING, $error);
513 
514  if ($dom)
515  {
516  $xpc = @xpath_new_context($dom);
517  // extract first table object
518  $path = "//table[1] | //Table[1]";
519  $res = @xpath_eval($xpc, $path);
520 
521  if (count($res->nodeset) == 0)
522  {
523  $error = "Could not find a table root node";
524  }
525 
526  if (empty ($error))
527  {
528  for($i = 0; $i < count($res->nodeset); $i++)
529  {
530  $node = $res->nodeset[$i];
531 
532  $this->importTableAttributes ($node);
533 
534  if ($node->has_child_nodes())
535  {
536  foreach($node->child_nodes() as $n)
537  {
538  if ($n->node_type() == XML_ELEMENT_NODE &&
539  strcasecmp($n->node_name (), "tr") == 0)
540  {
541 
542  $this->importRow ($lng, $n);
543  }
544  }
545  }
546  }
547  }
548  $dom->free ();
549  }
550  if (is_array($error)) {
551  $errmsg = "";
552  foreach ($error as $errorline) { # Loop through all errors
553  $errmsg .= "[" . $errorline['line'] . ", " . $errorline['col'] . "]: ".$errorline['errormessage']." at Node '". $errorline['nodename'] . "'<br />";
554  }
555  }else
556  {
557  $errmsg = $error;
558  }
559 
560  if (empty ($errmsg)) {
561  return true;
562  }
563 
564  $_SESSION["message"] = $errmsg;
565  return false;
566  }
567 
571  function setFirstRowStyle($a_class)
572  {
573  $childs = $this->tab_node->child_nodes();
574  foreach($childs as $child)
575  {
576  if ($child->node_name() == "TableRow")
577  {
578  $gchilds = $child->child_nodes();
579  foreach($gchilds as $gchild)
580  {
581  if ($gchild->node_name() == "TableData")
582  {
583  $gchild->set_attribute("Class", $a_class);
584  }
585  }
586  return;
587  }
588  }
589  }
590 }
591 
592 ?>