• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

content/classes/Pages/class.ilPCTable.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 +-----------------------------------------------------------------------------+
00004 | ILIAS open source                                                           |
00005 +-----------------------------------------------------------------------------+
00006 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007 |                                                                             |
00008 | This program is free software; you can redistribute it and/or               |
00009 | modify it under the terms of the GNU General Public License                 |
00010 | as published by the Free Software Foundation; either version 2              |
00011 | of the License, or (at your option) any later version.                      |
00012 |                                                                             |
00013 | This program is distributed in the hope that it will be useful,             |
00014 | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016 | GNU General Public License for more details.                                |
00017 |                                                                             |
00018 | You should have received a copy of the GNU General Public License           |
00019 | along with this program; if not, write to the Free Software                 |
00020 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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);               // this is the PageContent node
00057                 $this->tab_node =& $a_node->first_child();              // this is the Table node
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, $a_data = "", $a_lang = "")
00076         {
00077                 $new_td =& $this->dom->create_element("TableData");
00078                 $new_td =& $aRow->append_child($new_td);
00079                 
00080                 // insert data if given
00081                 if ($a_data != "")
00082                 {
00083                         $new_pg =& $this->dom->create_element("PageContent");
00084                         $new_par =& $this->dom->create_element("Paragraph");
00085                         $new_par =& $new_pg->append_child($new_par);
00086                         $new_par->set_attribute("Language", $a_lang);
00087                         $new_par->set_attribute("Characteristic", "TableContent");
00088                         $new_par->set_content($a_data);
00089                         $new_td->append_child ($new_pg);
00090                 }
00091                 
00092                 return $new_td;
00093         }
00094 
00098         function addRows($a_nr_rows, $a_nr_cols)
00099         {
00100                 for ($i=1; $i<=$a_nr_rows; $i++)
00101                 {
00102                         $aRow = $this->addRow();
00103                         for ($j=1; $j<=$a_nr_cols; $j++)
00104                         {
00105                                 $this->addCell($aRow);
00106                         }
00107                 }
00108         }
00109         
00113         function importSpreadsheet($a_lang, $a_data)
00114         {
00115                 str_replace($a_data, "\r", "\n");
00116                 str_replace($a_data, "\n\n", "\n");
00117                 $target_rows = array();
00118                 $rows = explode("\n", $a_data);
00119 
00120                 // get maximum of cols in a row and
00121                 // put data in target_row arrays
00122                 foreach($rows as $row)
00123                 {
00124                         $cells = explode("\t", $row);
00125                         $max_cols = ($max_cols > count($cells))
00126                                 ? $max_cols
00127                                 : count($cells);
00128                         $target_rows[] = $cells;
00129                 }
00130 
00131                 // iterate target row arrays and insert data
00132                 foreach($target_rows as $row)
00133                 {
00134                         $aRow = $this->addRow();
00135                         for ($j=0; $j<$max_cols; $j++)
00136                         {
00137                                 // mask html
00138                                 $data = str_replace("&","&amp;", $row[$j]);
00139                                 $data = str_replace("<","&lt;", $data);
00140                                 $data = str_replace(">","&gt;", $data);
00141 
00142                                 $this->addCell($aRow, $data, $a_lang);
00143                         }
00144                 }
00145         }
00146 
00150         function getLanguage()
00151         {
00152                 return $this->tab_node->get_attribute("Language");
00153         }
00154 
00160         function setLanguage($a_lang)
00161         {
00162                 if($a_lang != "")
00163                 {
00164                         $this->tab_node->set_attribute("Language", $a_lang);
00165                 }
00166         }
00167 
00171         function getWidth()
00172         {
00173                 return $this->tab_node->get_attribute("Width");
00174         }
00175 
00181         function setWidth($a_width)
00182         {
00183                 if($a_width != "")
00184                 {
00185                         $this->tab_node->set_attribute("Width", $a_width);
00186                 }
00187                 else
00188                 {
00189                         if ($this->tab_node->has_attribute("Width"))
00190                         {
00191                                 $this->tab_node->remove_attribute("Width");
00192                         }
00193                 }
00194         }
00195 
00196 
00197 
00201         function getBorder()
00202         {
00203                 return $this->tab_node->get_attribute("Border");
00204         }
00205 
00211         function setBorder($a_border)
00212         {
00213                 if($a_border != "")
00214                 {
00215                         $this->tab_node->set_attribute("Border", $a_border);
00216                 }
00217                 else
00218                 {
00219                         if ($this->tab_node->has_attribute("Border"))
00220                         {
00221                                 $this->tab_node->remove_attribute("Border");
00222                         }
00223                 }
00224         }
00225 
00229         function getCellSpacing()
00230         {
00231                 return $this->tab_node->get_attribute("CellSpacing");
00232         }
00233 
00239         function setCellSpacing($a_spacing)
00240         {
00241                 if($a_spacing != "")
00242                 {
00243                         $this->tab_node->set_attribute("CellSpacing", $a_spacing);
00244                 }
00245                 else
00246                 {
00247                         if ($this->tab_node->has_attribute("CellSpacing"))
00248                         {
00249                                 $this->tab_node->remove_attribute("CellSpacing");
00250                         }
00251                 }
00252         }
00253 
00257         function getCellPadding()
00258         {
00259                 return $this->tab_node->get_attribute("CellPadding");
00260         }
00261 
00267         function setCellPadding($a_padding)
00268         {
00269                 if($a_padding != "")
00270                 {
00271                         $this->tab_node->set_attribute("CellPadding", $a_padding);
00272                 }
00273                 else
00274                 {
00275                         if ($this->tab_node->has_attribute("CellPadding"))
00276                         {
00277                                 $this->tab_node->remove_attribute("CellPadding");
00278                         }
00279                 }
00280         }
00281 
00285         function setHorizontalAlign($a_halign)
00286         {
00287                 $this->tab_node->set_attribute("HorizontalAlign", $a_halign);
00288         }
00289 
00293         function getHorizontalAlign()
00294         {
00295                 return $this->tab_node->get_attribute("HorizontalAlign");
00296         }
00297 
00301         function setTDWidth($a_hier_id, $a_width)
00302         {
00303                 $xpc = xpath_new_context($this->dom);
00304                 $path = "//TableData[@HierId = '".$a_hier_id."']";
00305                 $res =& xpath_eval($xpc, $path);
00306                 if (count($res->nodeset) == 1)
00307                 {
00308                         if($a_width != "")
00309                         {
00310                                 $res->nodeset[0]->set_attribute("Width", $a_width);
00311                         }
00312                         else
00313                         {
00314                                 if ($res->nodeset[0]->has_attribute("Width"))
00315                                 {
00316                                         $res->nodeset[0]->remove_attribute("Width");
00317                                 }
00318                         }
00319                 }
00320         }
00321 
00325         function setTDClass($a_hier_id, $a_class)
00326         {
00327                 $xpc = xpath_new_context($this->dom);
00328                 $path = "//TableData[@HierId = '".$a_hier_id."']";
00329                 $res =& xpath_eval($xpc, $path);
00330                 if (count($res->nodeset) == 1)
00331                 {
00332                         if($a_class != "")
00333                         {
00334                                 $res->nodeset[0]->set_attribute("Class", $a_class);
00335                         }
00336                         else
00337                         {
00338                                 if ($res->nodeset[0]->has_attribute("Class"))
00339                                 {
00340                                         $res->nodeset[0]->remove_attribute("Class");
00341                                 }
00342                         }
00343                 }
00344         }
00345 
00349         function getCaption()
00350         {
00351                 $hier_id = $this->getHierId();
00352                 if(!empty($hier_id))
00353                 {
00354                         $xpc = xpath_new_context($this->dom);
00355                         $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
00356                         $res =& xpath_eval($xpc, $path);
00357 
00358                         if (count($res->nodeset) == 1)
00359                         {
00360                                 return $res->nodeset[0]->get_content();
00361                         }
00362                 }
00363         }
00364 
00368         function getCaptionAlign()
00369         {
00370                 $hier_id = $this->getHierId();
00371                 if(!empty($hier_id))
00372                 {
00373                         $xpc = xpath_new_context($this->dom);
00374                         $path = "//PageContent[@HierId = '".$hier_id."']/Table/Caption";
00375                         $res =& xpath_eval($xpc, $path);
00376                         if (count($res->nodeset) == 1)
00377                         {
00378                                 return $res->nodeset[0]->get_attribute("Align");
00379                         }
00380                 }
00381         }
00382 
00386         function setCaption($a_content, $a_align)
00387         {
00388                 if ($a_content != "")
00389                 {
00390                         ilDOMUtil::setFirstOptionalElement($this->dom, $this->tab_node, "Caption",
00391                         array("Summary", "TableRow"), $a_content,
00392                         array("Align" => $a_align));
00393                 }
00394                 else
00395                 {
00396                         ilDOMUtil::deleteAllChildsByName($this->tab_node, array("Caption"));
00397                 }
00398         }
00399 
00400 
00401         function importTableAttributes (&$node) {
00402                 /*echo "importing table attributes";
00403                 var_dump($tableNode);*/
00404                 if ($node->has_attributes ())
00405                 {
00406                         foreach($node->attributes() as $n)
00407                         {
00408 
00409                                 switch (strtolower($n->node_name ())) {
00410                                         case "border":
00411                                         $this->setBorder ($this->extractText($n));
00412                                         break;
00413                                         case "align":
00414                                         $this->setHorizontalAlign(ucfirst(strtolower($this->extractText($n))));
00415                                         break;
00416                                         case "cellspacing":
00417                                         $this->setCellSpacing($this->extractText($n));
00418                                         break;
00419                                         case "cellpadding":
00420                                         $this->setCellPadding($this->extractText($n));
00421                                         break;
00422                                         case "width":
00423                                         $this->setWidth($this->extractText($n));
00424                                         break;
00425 
00426                                 }
00427 
00428                         }
00429                 }
00430         }
00431 
00432 
00433         function importCellAttributes (&$node, &$par) {
00434                 /*echo "importing table attributes";
00435                 var_dump($tableNode);*/
00436                 if ($node->has_attributes ())
00437                 {
00438                         foreach($node->attributes() as $n)
00439                         {
00440 
00441                                 switch (strtolower($n->node_name ())) {
00442                                         case "class":
00443                                         $par->set_attribute("Class", $this->extractText($n));
00444                                         break;
00445                                         case "width":
00446                                         $par->set_attribute("Width", $this->extractText($n));
00447                                         break;
00448                                 }
00449 
00450                         }
00451                 }
00452         }
00453 
00454 
00455         function importRow ($lng, &$node) {
00456                 /*echo "add Row";
00457                 var_dump($node);*/
00458 
00459                 $aRow = $this->addRow();
00460 
00461                 if ($node->has_child_nodes())
00462                 {
00463                         foreach($node->child_nodes() as $n)
00464                         {
00465                                 if ($n->node_type() == XML_ELEMENT_NODE &&
00466                                 strcasecmp($n->node_name (), "td") == 0)
00467                                 {
00468                                         $this->importCell ($lng, $n, $aRow);
00469                                 }
00470                         }
00471                 }
00472         }
00473 
00474         function importCell ($lng, &$cellNode, &$aRow) {
00475                 /*echo "add Cell";
00476                 var_dump($cellNode);*/
00477                 $aCell = $this->addCell($aRow);
00478                 $par = new ilPCParagraph($this->dom);
00479                 $par->createAtNode($aCell);
00480                 $par->setText($par->input2xml($this->extractText ($cellNode)));
00481                 $par->setCharacteristic("TableContent");
00482                 $par->setLanguage($lng);
00483                 $this->importCellAttributes($cellNode, $aCell);
00484         }
00485 
00486         function extractText (&$node) {
00487                 $owner_document = $node->owner_document ();
00488                 $children = $node->child_nodes();
00489                 $total_children = count($children);
00490                 for ($i = 0; $i < $total_children; $i++){
00491                         $cur_child_node = $children[$i];
00492                         $output .= $owner_document->dump_node($cur_child_node);
00493                 }
00494                 return $output;
00495         }
00496 
00497         function importHtml ($lng, $htmlTable) {
00498                 $dummy = ilUtil::stripSlashes($htmlTable, false);
00499                 //echo htmlentities($dummy);
00500                 $dom = @domxml_open_mem($dummy,DOMXML_LOAD_PARSING, $error);
00501 
00502                 if ($dom)
00503                 {
00504                         $xpc = @xpath_new_context($dom);
00505                         // extract first table object
00506                         $path = "//table[1] | //Table[1]";
00507                         $res = @xpath_eval($xpc, $path);
00508 
00509                         if (count($res->nodeset) == 0)
00510                         {
00511                                 $error = "Could not find a table root node";
00512                         }
00513 
00514                         if (empty ($error)) 
00515                         {
00516                                 for($i = 0; $i < count($res->nodeset); $i++)
00517                                 {
00518                                         $node = $res->nodeset[$i];
00519 
00520                                         $this->importTableAttributes ($node);
00521 
00522                                         if ($node->has_child_nodes())
00523                                         {
00524                                                 foreach($node->child_nodes() as $n)
00525                                                 {
00526                                                         if ($n->node_type() == XML_ELEMENT_NODE &&
00527                                                         strcasecmp($n->node_name (), "tr") == 0)
00528                                                         {
00529 
00530                                                                 $this->importRow ($lng, $n);
00531                                                         }
00532                                                 }
00533                                         }
00534                                 }                               
00535                         }
00536                         $dom->free ();
00537                 }
00538                 if (is_array($error)) {
00539                         $errmsg = "";
00540                         foreach ($error as $errorline) {    # Loop through all errors
00541                                 $errmsg .=  "[" . $errorline['line'] . ", " . $errorline['col'] . "]: ".$errorline['errormessage']." at Node '". $errorline['nodename'] . "'<br />";
00542                         }
00543                 }else
00544                 {
00545                         $errmsg = $error;
00546                 }
00547                 
00548                 if (empty ($errmsg)) {
00549                         return true;
00550                 }
00551                 
00552                 $_SESSION["message"] = $errmsg;
00553                 return false;
00554         }
00555 }
00556 
00557 ?>

Generated on Fri Dec 13 2013 09:06:36 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1