ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilPCDataTable.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 require_once("./Services/COPage/classes/class.ilPCTable.php");
26 
38 class ilPCDataTable extends ilPCTable
39 {
40  public $dom;
41  public $tab_node;
42 
46  public function init()
47  {
48  $this->setType("dtab");
49  }
50 
51  public function setNode($a_node)
52  {
53  parent::setNode($a_node); // this is the PageContent node
54  $this->tab_node = $a_node->first_child(); // this is the Table node
55  }
56 
57  public function create(&$a_pg_obj, $a_hier_id, $a_pc_id = "")
58  {
59  $this->node = $this->createPageContentNode();
60  $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
61  $this->tab_node = $this->dom->create_element("Table");
62  $this->tab_node = $this->node->append_child($this->tab_node);
63  $this->tab_node->set_attribute("Language", "");
64  $this->tab_node->set_attribute("DataTable", "y");
65  }
66 
70  public function getCellNode($i, $j)
71  {
72  $xpc = xpath_new_context($this->dom);
73  $path = "//PageContent[@HierId='" . $this->getHierId() . "']" .
74  "/Table/TableRow[$i+1]/TableData[$j+1]/PageContent[1]/Paragraph[1]";
75  //echo "<br>++".$path;
76  //]--//PageContent[@HierId='3']/Table/TableRow[+1]/TableData[0 style=+1]/PageContent[1]/Paragraph[1]
77  $res = xpath_eval($xpc, $path);
78 
79  if (is_object($res->nodeset[0])) {
80  return $res->nodeset[0];
81  } else { // no node -> delete all childs and create paragraph
82  $xpc2 = xpath_new_context($this->dom);
83  $path2 = "//PageContent[@HierId='" . $this->getHierId() . "']" .
84  "/Table/TableRow[" . ($i+1) . "]/TableData[" . ($j+1) . "]";
85  //$path2 = "//PageContent";
86 
87  $res2 = xpath_eval($xpc2, $path2);
88 
89  $td_node = $res2->nodeset[0];
90 
91  if (is_object($td_node)) {
92  // delete children of paragraph node
93  $children = $td_node->child_nodes();
94  for ($i=0; $i<count($children); $i++) {
95  $td_node->remove_child($children[$i]);
96  }
97 
98  // create page content and paragraph node here.
99  $pc_node = $this->createPageContentNode(false);
100  $pc_node = $td_node->append_child($pc_node);
101  $par_node = $this->dom->create_element("Paragraph");
102  $par_node = $pc_node->append_child($par_node);
103  $par_node->set_attribute("Characteristic", "TableContent");
104  $par_node->set_attribute(
105  "Language",
106  $this->getLanguage()
107  );
108 
109  return $par_node;
110  }
111  }
112 
113  return "";
114  }
115 
119  public function makeEmptyCell($td_node)
120  {
121  // delete children of paragraph node
122  $children = $td_node->child_nodes();
123  for ($i=0; $i<count($children); $i++) {
124  $td_node->remove_child($children[$i]);
125  }
126 
127  // create page content and paragraph node here.
128  $pc_node = $this->createPageContentNode(false);
129  $pc_node = $td_node->append_child($pc_node);
130  $par_node = $this->dom->create_element("Paragraph");
131  $par_node = $pc_node->append_child($par_node);
132  $par_node->set_attribute("Characteristic", "TableContent");
133  $par_node->set_attribute(
134  "Language",
135  $this->getLanguage()
136  );
137  }
138 
142  public function getCellText($i, $j)
143  {
144  $cell_par = $this->getCellNode($i, $j);
145 
146  if (is_object($cell_par)) {
147  $content = "";
148  $childs = $cell_par->child_nodes();
149  for ($i=0; $i<count($childs); $i++) {
150  $content.= $this->dom->dump_node($childs[$i]);
151  }
152  return $content;
153  } else {
154  return "";
155  }
156  }
157 
161  public function setData($a_data)
162  {
163  $ok = true;
164  //var_dump($a_data);
165  if (is_array($a_data)) {
166  foreach ($a_data as $i => $row) {
167  if (is_array($row)) {
168  foreach ($row as $j => $cell) {
169  //echo "<br><br>=".$cell."=";
170  $temp_dom = @domxml_open_mem(
171  '<?xml version="1.0" encoding="UTF-8"?><Paragraph>' . $cell . '</Paragraph>',
173  $error
174  );
175 
176  $par_node = $this->getCellNode($i, $j);
177  //echo "<br>=".htmlentities($this->dom->dump_node($par_node))."=";
178  //echo "<br>-$i-$j-$cell-";
179  // remove all childs
180  if (empty($error) && is_object($par_node)) {
181  // delete children of paragraph node
182  $children = $par_node->child_nodes();
183  for ($k=0; $k < count($children); $k++) {
184  $par_node->remove_child($children[$k]);
185  }
186 
187  // copy new content children in paragraph node
188  $xpc = xpath_new_context($temp_dom);
189  $path = "//Paragraph";
190  $res = xpath_eval($xpc, $path);
191 
192  if (count($res->nodeset) == 1) {
193  $new_par_node = $res->nodeset[0];
194  $new_childs = $new_par_node->child_nodes();
195  for ($l = 0; $l < count($new_childs); $l++) {
196  $cloned_child = $new_childs[$l]->clone_node(true);
197  $par_node->append_child($cloned_child);
198  //echo "<br>=".htmlentities($this->dom->dump_node($cloned_child))."=";
199  }
200  }
201  } else {
202  if (!empty($error)) {
203  return $error;
204  }
205  }
206  }
207  }
208  }
209  }
210  //exit;
211  return true;
212  }
213 }
setData($a_data)
Set data of cells.
xpath_new_context($dom_document)
Class ilPCTable.
init()
Init page content component.
domxml_open_mem($str, $mode=0, &$error=null)
xpath_eval($xpath_context, $eval_str, $contextnode=null)
setType($a_type)
Set Type.
create(&$a_pg_obj, $a_hier_id, $a_pc_id="")
$error
Definition: Error.php:17
foreach($_POST as $key=> $value) $res
getCellText($i, $j)
Get cell text of row $i and cell $j.
const IL_INSERT_AFTER
getHierId()
Get hierarchical id.
createPageContentNode($a_set_this_node=true)
Create page content node (always use this method first when adding a new element) ...
makeEmptyCell($td_node)
Make cell empty.
global $l
Definition: afr.php:30
const DOMXML_LOAD_PARSING
$i
Definition: disco.tpl.php:19
getCellNode($i, $j)
Get cell paragraph node of row $i and cell $j.
getLanguage()
get table language
Class ilPCDataTable.