contexts = array();
$this->current_context = "";
$this->current_row_index = 0;
$this->current_column_count = 0;
$this->max_column_count = 0;
$this->set_table_attributes($table_attributes);
$this->set_body_attributes($body_attributes);
}
function begin_body_row($attributes = "")
{
$this->internal_begin_row($attributes, "body");
}
function begin_header_row($attributes = "")
{
$this->internal_begin_row($attributes, "header");
}
function begin_footer_row($attributes = "")
{
$this->internal_begin_row($attributes, "footer");
}
function add_cell($value = "", $attributes = "")
{
$this->internal_add_cell($value, $attributes);
}
function add_body_row($value = "", $cell_attributes = "", $row_attributes = "")
{
$this->internal_add_row($value, $cell_attributes, $row_attributes, "body");
}
function add_header_row($value = "", $cell_attributes = "", $row_attributes = "")
{
$this->internal_add_row($value, $cell_attributes, $row_attributes, "header");
}
function add_footer_row($value = "", $cell_attributes = "", $row_attributes = "")
{
$this->internal_add_row($value, $cell_attributes, $row_attributes, "footer");
}
function set_table_attributes($attributes = "")
{
$this->table_attributes = $attributes;
}
function set_body_attributes($attributes = "")
{
$this->internal_set_context_attributes($attributes, "body");
}
function set_header_attributes($attributes = "")
{
$this->internal_set_context_attributes($attributes, "header");
}
function set_footer_attributes($attributes = "")
{
$this->internal_set_context_attributes($attributes, "footer");
}
// private
function internal_add_row($value, $attributes, $row_attributes, $context)
{
$this->internal_begin_row($row_attributes, $context);
// MAXCOLSPAN gets replaced with number in to_html
$attributes .= " colspan='" . MAXCOLSPAN . "'";
$this->internal_add_cell($value, $attributes);
}
// private, helper function for begin_body_row, begin_header_row, begin_footer_row
function internal_begin_row($attributes, $context)
{
$this->current_context = $context;
$this->column_count = 0;
// assign row attributes
$this->contexts[$this->current_context]["rows"][]["attributes"] = $attributes;
// get key for new row
end($this->contexts[$this->current_context]["rows"]);
$this->current_row_index = key($this->contexts[$this->current_context]["rows"]);
}
// private: abstract table storage implementation from rest of object
function internal_add_cell($value, $attributes)
{
$this->contexts[$this->current_context]["rows"][$this->current_row_index]["cells"][] = array("value" => $value,
"attributes" => $attributes);
$this->column_count++;
if ($this->column_count > $this->max_column_count)
{
$this->max_column_count = $this->column_count;
}
}
//private
function internal_set_context_attributes($attributes, $context)
{
$this->contexts[$context]["attributes"] = $attributes;
}
//private: utility function
function start_tag($tag, $attributes = "", $prefix = "", $suffix = "")
{
if ($attributes != "")
{
$attributes = " " . $attributes;
}
return $prefix . "<" . $tag . $attributes . ">" . $suffix;
}
//private: utility function
function end_tag($tag)
{
return "" . $tag . ">\n";
}
// for debugging
function to_string()
{
print_r($this->contexts);
}
//private function
function generate_table_context($context)
{
// initialize variables
$result = "";
$row_tag = "TR";
$cell_tag = "TD";
// figure out context tag and special header cell tag
switch ($context) {
case "body":
$context_tag = "TBODY";
break;
case "header":
$cell_tag = "TH";
$context_tag = "THEAD";
break;
case "footer":
$context_tag = "TFOOT";
break;
}
$result .= $this->start_tag($context_tag, $this->contexts[$context]["attributes"], "", "\n");
foreach ($this->contexts[$context]["rows"] as $row)
{
$result .= $this->start_tag($row_tag, $row["attributes"], "", "\n");
foreach ($row["cells"] as $cell)
{
$result .= $this->start_tag($cell_tag, $cell["attributes"], "\t");
$result .= $cell["value"];
$result .= $this->end_tag($cell_tag);
}
$result .= $this->end_tag($row_tag);
}
$result .= $this->end_tag($context_tag);
return $result;
}
function to_html()
{
$result = $this->start_tag("TABLE", $this->table_attributes, "\n", "\n");
if (count($this->contexts["header"]["rows"]) != 0)
{
$result .= $this->generate_table_context("header");
}
if (count($this->contexts["footer"]["rows"]) != 0)
{
$result .= $this->generate_table_context("footer");
}
if (count($this->contexts["body"]["rows"]) != 0)
{
$result .= $this->generate_table_context("body");
}
$result .= $this->end_tag("TABLE");
$result = str_replace(MAXCOLSPAN, $this->max_column_count, $result);
return $result;
}
function generate_csv_context($context)
{
$result = "";
foreach ($this->contexts[$context]["rows"] as $row)
{
foreach ($row["cells"] as $cell)
{
$result .= "\"";
$result .= str_replace("\"","\"\"",$cell["value"]); //replace single double quotes with two double quotes
$result .= "\",";
}
$result .= chr(10); // end row with line feed
}
return $result;
}
function generate_tab_context($context)
{
$result = "";
foreach ($this->contexts[$context]["rows"] as $row)
{
foreach ($row["cells"] as $cell)
{
// remove all tabs and then end cell with a tab
$result .= html2text(str_replace(chr(9), "", $cell["value"])) . chr(9);
}
$result .= chr(10); // end row with line feed
}
return $result;
}
function to_csv()
{
$result = "";
if (count($this->contexts["header"]["rows"]) != 0)
{
$result .= $this->generate_csv_context("header");
}
if (count($this->contexts["body"]["rows"]) != 0)
{
$result .= $this->generate_csv_context("body");
}
if (count($this->contexts["footer"]["rows"]) != 0)
{
$result .= $this->generate_csv_context("footer");
}
return $result;
}
function to_tab()
{
$result = "";
if (count($this->contexts["header"]["rows"]) != 0)
{
$result .= $this->generate_tab_context("header");
}
if (count($this->contexts["body"]["rows"]) != 0)
{
$result .= $this->generate_tab_context("body");
}
if (count($this->contexts["footer"]["rows"]) != 0)
{
$result .= $this->generate_tab_context("footer");
}
return $result;
}
}
?>