00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 require_once("content/classes/Pages/class.ilPageContent.php");
00025
00036 class ilPCTable extends ilPageContent
00037 {
00038 var $dom;
00039 var $tab_node;
00040
00041
00046 function ilPCTable(&$a_dom)
00047 {
00048 parent::ilPageContent();
00049 $this->setType("tab");
00050
00051 $this->dom =& $a_dom;
00052 }
00053
00054 function setNode(&$a_node)
00055 {
00056 parent::setNode($a_node);
00057 $this->tab_node =& $a_node->first_child();
00058 }
00059
00060 function create(&$a_pg_obj, $a_hier_id)
00061 {
00062 $this->node =& $this->dom->create_element("PageContent");
00063 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER);
00064 $this->tab_node =& $this->dom->create_element("Table");
00065 $this->tab_node =& $this->node->append_child($this->tab_node);
00066 $this->tab_node->set_attribute("Language", "");
00067 }
00068
00069 function &addRow () {
00070 $new_tr =& $this->dom->create_element("TableRow");
00071 $new_tr = &$this->tab_node->append_child($new_tr);
00072 return $new_tr;
00073 }
00074
00075 function &addCell (&$aRow) {
00076 $new_td =& $this->dom->create_element("TableData");
00077 $new_td =& $aRow->append_child($new_td);
00078 return $new_td;
00079 }
00080
00081 function addRows($a_nr_rows, $a_nr_cols)
00082 {
00083 for ($i=1; $i<=$a_nr_rows; $i++)
00084 {
00085 $aRow = $this->addRow();
00086 for ($j=1; $j<=$a_nr_cols; $j++)
00087 {
00088 $this->addCell($aRow);
00089 }
00090 }
00091 }
00092
00096 function getLanguage()
00097 {
00098 return $this->tab_node->get_attribute("Language");
00099 }
00100
00106 function setLanguage($a_lang)
00107 {
00108 if($a_lang != "")
00109 {
00110 $this->tab_node->set_attribute("Language", $a_lang);
00111 }
00112 }
00113
00117 function getWidth()
00118 {
00119 return $this->tab_node->get_attribute("Width");
00120 }
00121
00127 function setWidth($a_width)
00128 {
00129 if($a_width != "")
00130 {
00131 $this->tab_node->set_attribute("Width", $a_width);
00132 }
00133 else
00134 {
00135 if ($this->tab_node->has_attribute("Width"))
00136 {
00137 $this->tab_node->remove_attribute("Width");
00138 }
00139 }
00140 }
00141
00142
00143
00147 function getBorder()
00148 {
00149 return $this->tab_node->get_attribute("Border");
00150 }
00151
00157 function setBorder($a_border)
00158 {
00159 if($a_border != "")
00160 {
00161 $this->tab_node->set_attribute("Border", $a_border);
00162 }
00163 else
00164 {
00165 if ($this->tab_node->has_attribute("Border"))
00166 {
00167 $this->tab_node->remove_attribute("Border");
00168 }
00169 }
00170 }
00171
00175 function getCellSpacing()
00176 {
00177 return $this->tab_node->get_attribute("CellSpacing");
00178 }
00179
00185 function setCellSpacing($a_spacing)
00186 {
00187 if($a_spacing != "")
00188 {
00189 $this->tab_node->set_attribute("CellSpacing", $a_spacing);
00190 }
00191 else
00192 {
00193 if ($this->tab_node->has_attribute("CellSpacing"))
00194 {
00195 $this->tab_node->remove_attribute("CellSpacing");
00196 }
00197 }
00198 }
00199
00203 function getCellPadding()
00204 {
00205 return $this->tab_node->get_attribute("CellPadding");
00206 }
00207
00213 function setCellPadding($a_padding)
00214 {
00215 if($a_padding != "")
00216 {
00217 $this->tab_node->set_attribute("CellPadding", $a_padding);
00218 }
00219 else
00220 {
00221 if ($this->tab_node->has_attribute("CellPadding"))
00222 {
00223 $this->tab_node->remove_attribute("CellPadding");
00224 }
00225 }
00226 }
00227
00231 function setHorizontalAlign($a_halign)
00232 {
00233 $this->tab_node->set_attribute("HorizontalAlign", $a_halign);
00234 }
00235
00239 function getHorizontalAlign()
00240 {
00241 return $this->tab_node->get_attribute("HorizontalAlign");
00242 }
00243
00247 function setTDWidth($a_hier_id, $a_width)
00248 {
00249 $xpc = xpath_new_context($this->dom);
00250 $path = "//TableData[@HierId = '".$a_hier_id."']";
00251 $res =& xpath_eval($xpc, $path);
00252 if (count($res->nodeset) == 1)
00253 {
00254 if($a_width != "")
00255 {
00256 $res->nodeset[0]->set_attribute("Width", $a_width);
00257 }
00258 else
00259 {
00260 if ($res->nodeset[0]->has_attribute("Width"))
00261 {
00262 $res->nodeset[0]->remove_attribute("Width");
00263 }
00264 }
00265 }
00266 }
00267
00271 function setTDClass($a_hier_id, $a_class)
00272 {
00273 $xpc = xpath_new_context($this->dom);
00274 $path = "//TableData[@HierId = '".$a_hier_id."']";
00275 $res =& xpath_eval($xpc, $path);
00276 if (count($res->nodeset) == 1)
00277 {
00278 if($a_class != "")
00279 {
00280 $res->nodeset[0]->set_attribute("Class", $a_class);
00281 }
00282 else
00283 {
00284 if ($res->nodeset[0]->has_attribute("Class"))
00285 {
00286 $res->nodeset[0]->remove_attribute("Class");
00287 }
00288 }
00289 }
00290 }
00291
00295 function getCaption()
00296 {
00297 $hier_id = $this->getHierId();
00298 if(!empty($hier_id))
00299 {
00300 $xpc = xpath_new_context($this->dom);
00301 $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
00302 $res =& xpath_eval($xpc, $path);
00303
00304 if (count($res->nodeset) == 1)
00305 {
00306 return $res->nodeset[0]->get_content();
00307 }
00308 }
00309 }
00310
00314 function getCaptionAlign()
00315 {
00316 $hier_id = $this->getHierId();
00317 if(!empty($hier_id))
00318 {
00319 $xpc = xpath_new_context($this->dom);
00320 $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
00321 $res =& xpath_eval($xpc, $path);
00322 if (count($res->nodeset) == 1)
00323 {
00324 return $res->nodeset[0]->get_attribute("Align");
00325 }
00326 }
00327 }
00328
00329 function setCaption($a_content, $a_align)
00330 {
00331 if ($a_content != "")
00332 {
00333 ilDOMUtil::setFirstOptionalElement($this->dom, $this->tab_node, "Caption",
00334 array("Summary", "TableRow"), $a_content,
00335 array("Align" => $a_align));
00336 }
00337 else
00338 {
00339 ilDOMUtil::deleteAllChildsByName($this->tab_node, array("Caption"));
00340 }
00341 }
00342
00343
00344 function importTableAttributes (&$node) {
00345
00346
00347 if ($node->has_attributes ())
00348 {
00349 foreach($node->attributes() as $n)
00350 {
00351
00352 switch (strtolower($n->node_name ())) {
00353 case "border":
00354 $this->setBorder ($this->extractText($n));
00355 break;
00356 case "align":
00357 $this->setHorizontalAlign(ucfirst(strtolower($this->extractText($n))));
00358 break;
00359 case "cellspacing":
00360 $this->setCellSpacing($this->extractText($n));
00361 break;
00362 case "cellpadding":
00363 $this->setCellPadding($this->extractText($n));
00364 break;
00365 case "width":
00366 $this->setWidth($this->extractText($n));
00367 break;
00368
00369 }
00370
00371 }
00372 }
00373 }
00374
00375
00376 function importCellAttributes (&$node, &$par) {
00377
00378
00379 if ($node->has_attributes ())
00380 {
00381 foreach($node->attributes() as $n)
00382 {
00383
00384 switch (strtolower($n->node_name ())) {
00385 case "class":
00386 $par->set_attribute("Class", $this->extractText($n));
00387 break;
00388 case "width":
00389 $par->set_attribute("Width", $this->extractText($n));
00390 break;
00391 }
00392
00393 }
00394 }
00395 }
00396
00397
00398 function importRow ($lng, &$node) {
00399
00400
00401
00402 $aRow = $this->addRow();
00403
00404 if ($node->has_child_nodes())
00405 {
00406 foreach($node->child_nodes() as $n)
00407 {
00408 if ($n->node_type() == XML_ELEMENT_NODE &&
00409 strcasecmp($n->node_name (), "td") == 0)
00410 {
00411 $this->importCell ($lng, $n, $aRow);
00412 }
00413 }
00414 }
00415 }
00416
00417 function importCell ($lng, &$cellNode, &$aRow) {
00418
00419
00420 $aCell = $this->addCell($aRow);
00421 $par = new ilPCParagraph($this->dom);
00422 $par->createAtNode($aCell);
00423 $par->setText($par->input2xml($this->extractText ($cellNode)));
00424 $par->setCharacteristic("TableContent");
00425 $par->setLanguage($lng);
00426 $this->importCellAttributes($cellNode, $aCell);
00427 }
00428
00429 function extractText (&$node) {
00430 $owner_document = $node->owner_document ();
00431 $children = $node->child_nodes();
00432 $total_children = count($children);
00433 for ($i = 0; $i < $total_children; $i++){
00434 $cur_child_node = $children[$i];
00435 $output .= $owner_document->dump_node($cur_child_node);
00436 }
00437 return $output;
00438 }
00439
00440 function importHtml ($lng, $htmlTable) {
00441 $dummy = ilUtil::stripSlashes($htmlTable, false);
00442
00443 $dom = @domxml_open_mem($dummy,DOMXML_LOAD_PARSING, $error);
00444
00445 if ($dom)
00446 {
00447 $xpc = @xpath_new_context($dom);
00448
00449 $path = "//table[1] | //Table[1]";
00450 $res = @xpath_eval($xpc, $path);
00451
00452 if (count($res->nodeset) == 0)
00453 {
00454 $error = "Could not find a table root node";
00455 }
00456
00457 if (empty ($error))
00458 {
00459 for($i = 0; $i < count($res->nodeset); $i++)
00460 {
00461 $node = $res->nodeset[$i];
00462
00463 $this->importTableAttributes ($node);
00464
00465 if ($node->has_child_nodes())
00466 {
00467 foreach($node->child_nodes() as $n)
00468 {
00469 if ($n->node_type() == XML_ELEMENT_NODE &&
00470 strcasecmp($n->node_name (), "tr") == 0)
00471 {
00472
00473 $this->importRow ($lng, $n);
00474 }
00475 }
00476 }
00477 }
00478 }
00479 $dom->free ();
00480 }
00481 if (is_array($error)) {
00482 $errmsg = "";
00483 foreach ($error as $errorline) { # Loop through all errors
00484 $errmsg .= "[" . $errorline['line'] . ", " . $errorline['col'] . "]: ".$errorline['errormessage']." at Node '". $errorline['nodename'] . "'<br />";
00485 }
00486 }else
00487 {
00488 $errmsg = $error;
00489 }
00490
00491 if (empty ($errmsg)) {
00492 return true;
00493 }
00494
00495 $_SESSION["message"] = $errmsg;
00496 return false;
00497 }
00498 }
00499
00500 ?>