<?php
// Open Source Library Name: vt.php
// Author: Md. Azizur Rahman
// Release Type: Beta
// Version: 0.10
// website: phpvirtualtable.sf.net
// First Release: May 29, 2012
// Revised: May 30, 2012
// Revised: June 03, 2012
// Revised: June 06, 2012
// Revised: Nov 01. 2012 // changes: function link_element() added. function show() now supports external form elements
interface VTglobals
{
public function link_to_form();
public function is_new();
public function get_num_of_columns();
public function get_num_of_rows();
public function hide_column($col_num);
public function unhide_all_column();
public function disable_row($index);
public function show();
public function add_row();
public function usage();
public function sum($col);
public function fetch_rows_as_mysql_data();
public function save_as_csv($filename);
public function fetch_record($row, $col);
public function set_font_family($s);
public function set_font_size($s);
public function set_font_weight($s);
public function set_position($s);
public function set_border_width($s);
public function set_border_type($s);
public function set_border_color($s);
public function set_valign($s);
public function set_width($s);
public function set_style($s);
public function get_style();
public function set_header_bgcolor($s);
public function set_even_row_color($s);
public function set_odd_row_color($s);
}
abstract class VirtualTable
{
protected $column_number; // number of columns in the virtual table
protected $rec, // this pointer will point to the data array
$rec_header; // points to the header information
protected $styleData;
}
class VirTable extends VirtualTable implements VTglobals
{
private $func_list=array(
"link_to_form()",
"is_new()",
"get_num_of_columns()",
"get_num_of_rows()",
"hide_column(\$col_num)",
"unhide_all_column()",
"disable_row(\$index)",
"link_element(\$elemName)",
"show()",
"add_row()",
"usage()",
"sum(\$col)",
"fetch_rows_as_mysql_data()",
"save_as_csv(\$filename)",
"fetch_record(\$row, \$col)",
"set_font_family(\$s)",
"set_font_size(\$s)",
"set_font_weight(\$s)",
"set_position(\$s)",
"set_border_width(\$s)",
"set_border_type(\$s)",
"set_border_color(\$s)",
"set_valign(\$s)",
"set_width(\$s)",
"set_style(\$s)",
"get_style()",
"set_header_bgcolor(\$s)",
"set_even_row_color(\$s)",
"set_odd_row_color(\$s)"
);
function __construct(){
if($this->is_new())
{
$num_args=func_num_args();
if($num_args<1)return -1;
$this->rec_header=array(array(0));
$this->column_number=$num_args;
$args=func_get_args();
for($i=0;$i<$num_args;$i++)
{
$this->rec_header[$i][0]=1;//1 means visible
$this->rec_header[$i][1]=$args[$i];
}
$this->rec=array_fill(0,$num_args,0);
$this->styleData=array( "header-bgcolor"=>"#297147","even-row-color"=>"#DDDDDD","odd-row-color"=>"#FFFFFF","width"=>"400px", "font-family"=>"courier", "font-size"=>"14px",
"font-weight"=>"normal", "position"=>"relative", "border"=>"1px",
"border-type"=>"solid", "border-color"=>"#d0d0d0", "valign"=>"middle",
"style"=>"style=\"width:400px;font-family:courier;font-size:14px;font-weight:normal;position:relative;border:1px solid #d0d0d0; valign:middle;\"");
}
else $this->restore_table();
}
//---------------Form Link Function-------------------------------------
public function link_to_form()
{
echo "<input type=\"hidden\" name=\"vT_link\" value=\"1\">";
echo "<input type=\"hidden\" name=\"vT_array_data\" value=\"".VirTable::get_array_pointer()."\">";
echo "<input type=\"hidden\" name=\"vT_array_header_data\" value=\"".VirTable::get_array_header_pointer()."\">";
echo "<input type=\"hidden\" name=\"vT_num_of_cols\" value=\"".VirTable::get_num_of_columns()."\">";
echo "<input type=\"hidden\" name=\"vT_array_style_data\" value=\"".VirTable::get_array_style_pointer()."\">";
}
//---------------Form Link Function-------------------------------------
//---------------Form Link Verification---------------------------------
public function is_new()
{
if(isset($_POST["vT_id"]))return 0;
if(isset($_POST["vT_link"]))return 0;
return 1;
}
//---------------Form Link Verification---------------------------------
private function set_num_of_columns($n)
{
if($n>0)$this->column_number=$n;
else return -1;
}
public function get_num_of_columns()
{
return $this->column_number;
}
public function get_num_of_rows()
{
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number; //actual number of rows
$visual_row_index=0; //initialized
for($t=0;$t<$num_of_rows;$t++)
{
if($this->rec[$t*$this->column_number]=='0')continue;
else $visual_row_index=$visual_row_index + 1;
}
return $visual_row_index;
}
//---------------------Hiding/Unhiding Rows and Columns-----------------
public function hide_column($col_num)
{
if($col_num==0 || $col_num>=($this->column_number))
{echo "Error: Invalid column no (either 0 or above ".($this->column_number-1).")<br/>";return -1;}
for($i=0;$i<$this->column_number;$i++)if($i==$col_num)$this->rec_header[$i][0]=0;
}
public function unhide_all_column()
{
for($i=0;$i<$this->column_number;$i++){$this->rec_header[$i][0]=1;}
}
public function disable_row($index)
//first row is 0, second row is 1 etc.
{
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
$this->rec[$index * $this->column_number]='0';
}
//---------------------Hiding/Unhiding Rows and Columns-----------------
//----------------------------Memory Functions--------------------------
private function restore_array_pointer()
{
$this->rec=unserialize(base64_decode($_POST["vT_array_data"]));
}
private function restore_array_header_pointer()
{
$this->rec_header=unserialize(base64_decode($_POST["vT_array_header_data"]));
}
private function get_array_style_pointer()
{
return base64_encode(serialize($this->styleData));
}
private function restore_array_style_pointer()
{
$this->styleData=unserialize(base64_decode($_POST["vT_array_style_data"]));
}
private function get_array_pointer()
{
return base64_encode(serialize($this->rec));
}
private function get_array_header_pointer()
{
return base64_encode(serialize($this->rec_header));
}
private function restore_table()
{
if(isset($_POST["vT_id"]) && $_POST["vT_id"]!="")
{
$this->set_num_of_columns($_POST["vT_num_of_cols"]);
$this->restore_array_header_pointer();
$this->restore_array_pointer();
$this->restore_array_style_pointer();
$this->disable_row($_POST["vT_id"]);
}
if(isset($_POST["vT_link"]) && $_POST["vT_link"]!="")
{
$this->set_num_of_columns($_POST["vT_num_of_cols"]);
$this->restore_array_header_pointer();
$this->restore_array_pointer();
$this->restore_array_style_pointer();
}
}
private function link_element($elemName)
{
echo "<input type=\"hidden\" name=\"".$elemName."\" value=".$_POST[$elemName].">";
}
//----------------------------Memory Functions--------------------------
public function show()
{
//added on beta 0.10
$num_of_arguments=func_num_args();
$args=func_get_args();
//
$color_flag=0;
$num_of_elem = count($this->rec);
if($this->column_number>0)$num_of_rows = $num_of_elem/$this->column_number;
else return -1;
echo "<form method=\"POST\" name=\"frm_virTable\" action=\"\">";
echo "<input type=\"hidden\" name=\"vT_id\" value=\"\">";
echo "<input type=\"hidden\" name=\"vT_array_data\" value=\"".VirTable::get_array_pointer()."\">";
echo "<input type=\"hidden\" name=\"vT_array_header_data\" value=\"".VirTable::get_array_header_pointer()."\">";
echo "<input type=\"hidden\" name=\"vT_num_of_cols\" value=\"".VirTable::get_num_of_columns()."\">";
echo "<input type=\"hidden\" name=\"vT_array_style_data\" value=\"".VirTable::get_array_style_pointer()."\">";
//added on beta 0.10
for($z=0;$z<$num_of_arguments;$z++)
{
$this->link_element($args[$z]);
}
//
echo "<table ".$this->styleData["style"]." cellpadding=5 cellspacing=1>";
// to remember first row contains column header names
// header part starts here
$bgColor=$this->styleData["header-bgcolor"];
echo "<tr style=\"height:32px;background-color:".$bgColor.";color:#FFFFFF;\" >";
for($i=0;$i<$this->column_number;$i++)
{
if($this->rec_header[$i][0]==1)
{
if($i==0)echo "<td width=40 align=\"right\">".$this->rec_header[$i][1]."</td>";
else echo "<td align=\"right\"><strong>".$this->rec_header[$i][1]."</strong></td>";
}
}
echo "</tr>";
// header part ends here
// data part starts here
for($j=1;$j<$num_of_rows;$j++)
{
if($this->rec[($j * $this->column_number)]=='0')continue;
if($color_flag){$bgColor=$this->styleData["even-row-color"];}else {$bgColor=$this->styleData["odd-row-color"];}
$color_flag=!$color_flag;
echo "<tr style=\"background-color:".$bgColor.";\">";
echo "<td align=\"center\"><a href=\"#\"
onclick=\"javascript:document.forms['frm_virTable']['vT_id'].value='".$j."';
document.forms['frm_virTable'].submit();\"><img src=\"editcut.png\" width=32 height=24></a></td>";
for($i=1;$i<$this->column_number;$i++)
{
if($this->rec_header[$i][0]==1)
echo "<td align=\"right\">".$this->rec[($j * $this->column_number) + $i]."</td>";
}
echo "</tr>";
}
// data part ends here
echo "</table>";
echo "</form>";
}
public function add_row()
{
$num_of_arguments=func_num_args();
//echo $this->column_number;
if($num_of_arguments!=$this->column_number)
{
echo "Error:Insufficient number of arguments. The row was not added<br/>";return -1;
}
$args=func_get_args();
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
for($i=0;$i<$num_of_arguments;$i++)
{
$this->rec[$num_of_elem+$i]=$args[$i];
}
}
public function usage()
{
echo "<table border=1><tr><td colspan=2><strong>Function List</strong></td></tr><tr><td>";
for($i=0;$i<count($this->func_list);$i++)
{
echo ($i+1).")</td><td> <i>".$this->func_list[$i]."</i></td></tr><tr><td>";
}
echo "</td></tr></table>";
}
//---------------Math Functions-----------------------------------------
public function sum($col)
{
if($col>=$this->column_number)return "??";
$sum=0;
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
for($j=0;$j<$num_of_rows;$j++)
{
if($this->rec[$j * $this->column_number]==0)continue;
$sum=$sum + $this->rec[$j * $this->column_number +$col];
}
return $sum;
}
//.........We have to add some functions here
//
//
//---------------Math Functions-----------------------------------------
//--------------------------Fetching Data/ Saving Data -----------------
public function fetch_rows_as_mysql_data()
{
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
$z="(";
for($j=0;$j<$num_of_rows;$j++)
{
if($this->rec[$j * $this->column_number]==0)continue;
if(strlen($z)>1)$z=$z."),(";
for($i=1;$i<($this->column_number-1);$i++)
{
$z=$z."'".$this->rec[$j * $this->column_number+$i]."',";
}
$z=$z."'".$this->rec[$j * $this->column_number+$i]."'";
}
//if strlen($z) still equals to 1 then there is no data. return a NULL
if(strlen($z)==1) return "";
$z=$z.")";
return $z;
}
public function save_as_csv($filename)
{
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
$z="";
for($i=1;$i<($this->column_number-1);$i++)
{
$z=$z."'".$this->rec_header[$i][1]."',";
}
$z=$z."'".$this->rec_header[$i][1]."'\n";
if(!file_exists($filename))file_put_contents($filename,$z,LOCK_EX);
else if(filesize($filename)==0)file_put_contents($filename,$z,LOCK_EX);
for($j=0;$j<$num_of_rows;$j++)
{
$z="";
if($this->rec[$j * $this->column_number]==0)continue;
for($i=1;$i<($this->column_number-1);$i++)
{
$z=$z."'".$this->rec[$j * $this->column_number+$i]."',";
}
$z=$z."'".$this->rec[$j * $this->column_number+$i]."'\n";
file_put_contents($filename,$z,FILE_APPEND|LOCK_EX);
}
if(strlen($z)==0) return "";
return $z;
}
public function fetch_record($row, $col)
{
if($row<0){echo "Error(fetch_record()):Minimum row number is 0<br/>";return -1;}
if($row>=($this->get_num_of_rows())){echo "Error(fetch_record()):Maximum row number is ".($this->get_num_of_rows()-1)."<br/>";return -2;}
if($col<1){echo "Error(fetch_record()):Minimum column number is 1<br/>";return -3;}
if($col>($this->column_number)){echo "Error(fetch_record()):Maximum column number is ".($this->column_number)."<br/>";return -4;}
$num_of_elem = count($this->rec);
$num_of_rows = $num_of_elem/$this->column_number;
//$numb_of_rows are greater if you delete one or more row(s) from table;
$z="";
$visual_row_index=0; //initialized
for($t=0;$t<$num_of_rows;$t++)
{
if($this->rec[$t*$this->column_number]=='0')continue;
else
{
if($row==$visual_row_index)
{
$z=$this->rec[$t * $this->column_number+$col];
return $z;
}
$visual_row_index=$visual_row_index + 1;
}
}
return $z;
}
//--------------------------Fetching Data/ Saving Data -----------------
//------------------------Style Functions-------------------------------
public function set_font_family($s)
{
$this->styleData["font-family"]=$s;
$this->_set_style_raw();
}
public function set_font_size($s)
{
$this->styleData["font-size"]=$s;
$this->_set_style_raw();
}
public function set_font_weight($s)
{
$this->styleData["font-weight"]=$s;
$this->_set_style_raw();
}
public function set_position($s)
{
$this->styleData["position"]=$s;
$this->_set_style_raw();
}
public function set_border_width($s)
{
$this->styleData["border"]=$s;
$this->_set_style_raw();
}
public function set_border_type($s)
{
$this->styleData["border-type"]=$s;
$this->_set_style_raw();
}
public function set_border_color($s)
{
$this->styleData["border-color"]=$s;
$this->_set_style_raw();
}
public function set_valign($s)
{
$this->styleData["valign"]=$s;
$this->_set_style_raw();
}
public function set_width($s)
{
$this->styleData["width"]=$s;
$this->_set_style_raw();
}
private function _set_style_raw()
{
$this->styleData["style"]="style=\""."font-family:".$this->styleData["font-family"].";".
"font-size:".$this->styleData["font-size"].";".
"font-weight:".$this->styleData["font-weight"].";".
"position:".$this->styleData["position"].";".
"width:".$this->styleData["width"].";".
"border:".$this->styleData["border"]." ".$this->styleData["border-type"]." ".$this->styleData["border-color"].";".
"valign:".$this->styleData["valign"].";\"";
}
public function set_style($s)
{
$this->styleData["style"]=$s;
}
public function get_style()
{
return $this->styleData["style"];
}
public function set_header_bgcolor($s)
{
$this->styleData["header-bgcolor"]=$s;
}
public function set_even_row_color($s)
{
$this->styleData["even-row-color"]=$s;
}
public function set_odd_row_color($s)
{
$this->styleData["odd-row-color"]=$s;
}
//------------------------Style Functions-------------------------------
}
?>