Go to the documentation of this file.00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 {
00004 header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005 echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006 echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007 exit(1);
00008 }
00016 class tbman
00017 {
00018 protected $tablename;
00019 protected $result;
00020 public $editable;
00021 public $formaction;
00022 protected $querystring;
00023 private $imagePath;
00024 private $scriptPath;
00025
00026 function tbman($querystring)
00027 {
00028 $this->tablename=$this->get_tablename_from_query($querystring);
00029 $this->querystring=$querystring;
00030
00031 @ $result=mysql_query($querystring);
00032 if(!$result) {
00033 displayerror("Error(tbman_renderer.lib.php): ".mysql_error());
00034 return;
00035 }
00036 else
00037 $this->result=$result;
00038 if(stristr($querystring,"select"))
00039 $this->editable="yes";
00040 global $urlRequestRoot;
00041 global $cmsFolder,$sourceFolder;
00042 global $templateFolder;
00043 $this->imagePath = "$urlRequestRoot/$cmsFolder/$templateFolder/common/images";
00044 $this->scriptPath = "$urlRequestRoot/$cmsFolder/$templateFolder/common/scripts";
00045 }
00046
00047 function make_table()
00048 {
00049 $result=$this->result;
00050
00051 $str =<<<STR
00052 <style type="text/css">
00053 .first { background-color:#d3dce3; }
00054 .even { background-color:#e5e5e5; }
00055 .odd { background-color:#d5d5d5; }
00056 </style>
00057 <script type="text/javascript" src="$this->scriptPath/tbman.js"></script>
00058 STR;
00059 $str.="\n<form id='f1' name='f1' method='post' action='".$this->formaction."'>";
00060 $str.="\n<table id=resultTable cellpadding=2 cellspacing=1>";
00061
00062 for($i=0;$i<mysql_num_rows($result);$i++)
00063 {
00064 $a_rows=mysql_fetch_assoc($result);
00065
00066 #Field name row and add row-
00067 if($i==0)
00068 {
00069 $str.="\n<tr bgcolor=#d3dce3>\n";
00070 if($this->editable=="yes")
00071 {
00072 $str.='<td><input type="checkbox" onClick="challtoggle(this)"></td>';
00073 $str.="<td colspan=2></td>";
00074 $str_addRow="\n<tr bgcolor=#d3dce3 id=\"addRow\" style=\"display:none\">\n";
00075 $str_addRow.="<td><input type=\"checkbox\" id=\"checkiaddRow\" onClick=\"if(this.checked==false) {document.getElementById('addRow').style.display='none';document.getElementById('addRowLink').style.display='';statusOfUpdateButton();}\"></td>";
00076 $str_addRow.="<td colspan=2></td>";
00077 foreach($a_rows as $key=>$value)
00078 {
00079 $str_addRow.="\n\t<td id=".str_replace(" ","_",$key)."addRow".">";
00080
00081 if(($input_box_size=$this->get_size_of_field($key))!="text_type")
00082 $str_addRow.=('<input type="text" value="" size="'.$input_box_size.
00083 '" name='.str_replace(" ","_",$key)."addRow".' /></td>');
00084 else
00085 $str_addRow.=('<textarea rows="3" cols="25" name='.str_replace(" ","_",$key)."addRow".' /></textarea></td>');
00086
00087 $str_addRow.='</td>';
00088 }
00089 $str_addRow.="\n</tr>";
00090 }
00091 $fields = "";
00092 foreach($a_rows as $key=>$value)
00093 {
00094 $str.="<td>".$key."</td>";
00095 #to get the fields array:
00096 $fields.=$key."|";
00097 }
00098 $fields=substr($fields,0,-1);
00099 $str.="\n</tr>";
00100 }
00101
00102 #data rows-
00103 $dat_str="\n<tr id=\"dat".$i."\" bgcolor=".(($i%2==0)?"#e5e5e5":"#d5d5d5").">\n";
00104 $dat_str.="\n\t<input id=\"chan".$i."\" type=\"hidden\" value=\"notchanged\">";
00105 $inp_str="\n<tr id=\"inp".$i."\" style=\"display:none\" bgcolor=".(($i%2==0)?"#e5e5e5":"#d5d5d5").">";
00106
00107 #edit columns-
00108 ###$dat_str- code for initially shown rows
00109 ###$dat_str- code for initially hidden rows
00110 if($this->editable=="yes")
00111 {
00112 $dat_str.="\n\t<td><input onClick=\"statusOfDeleteSelected()\" id=checkd".$i." type=checkbox /></td>";
00113
00114
00115 $dat_str.="\n\t<td><a href=\"javascript:toggle(".$i.")\"><img src=\"$this->imagePath/b_edit.png\" border=0 alt=\"edit\"></a></td>";
00116 $dat_str.="\n\t<td><a href=\"javascript:deleteRow(".$i.")\"><img src=\"$this->imagePath/b_drop.png\" border=0 alt=\"delete\"></a></td>";
00117
00118 $inp_str.="\n\t<td><input onClick=\"toggle(".$i.")\" id=checki".$i." type=checkbox /></td>";
00119 $inp_str.="\n\t<td><a href=\"javascript:toggle(".$i.")\"><img src=\"$this->imagePath/b_edit.png\" border=0 alt=\"edit\"></a></td>";
00120 $inp_str.="\n\t<td><a href=\"javascript:deleteRow(".$i.")\"><img src=\"$this->imagePath/b_drop.png\" border=0 alt=\"delete\"></a></td>";
00121 }
00122 #data columns-
00123
00124 $inp3_str="";
00125 foreach($a_rows as $key=>$value)
00126 {
00127 $dat_str.="\n\t<td>".$value."</td>";
00128 $inp_str.=("\n\t<td id=".str_replace(" ","_",$key).$i.">");
00129 if(($input_box_size=$this->get_size_of_field($key))!="text_type")
00130 $inp_str.=('<input type="text" value="'.$value.'" size="'.$input_box_size.
00131 '" name='.str_replace(" ","_",$key).$i.' /></td>');
00132 else
00133 $inp_str.=('<textarea rows="3" cols="25" name='.str_replace(" ","_",$key).$i.' />'.$value.'</textarea></td>');
00134 }
00135 $dat_str.="\n</tr>";
00136 $inp_str.="\n</tr>";
00137 if($this->editable=="yes")
00138 $str.=$dat_str.$inp_str;
00139 else
00140 $str.=$dat_str;
00141 }
00142 if($this->editable=="yes")
00143 $str.=$str_addRow;
00144
00145 $str.="\n</table>";
00146 #add row, update and delete all button-
00147 if($this->editable=="yes")
00148 {
00149 $str.="\n<table>";
00150 $str.="\n<tr><td><input id=\"noOfRows\" name=\"noOfRows\" type=\"hidden\" value=\"".$i."\"></td>";
00151 $str.="\n<td id=\"addRowLink\" onClick=\"document.getElementById('addRow').style.display='';document.getElementById('checkiaddRow').checked=true;this.style.display='none';statusOfUpdateButton();\"><small>Add record</small></td>";
00152 $str.="\n<td id=\"deleteSelectedText\"><small>Delete selected</small></td>";
00153 $str.="\n<td id=\"deleteSelectedLink\" style=\"display:none\">
00154 <a href=\"javascript:deleteRow('selected')\"><small>Delete selected</small></a></td>\n";
00155 $str.='<td id="updateButton" style="display:none">
00156 <input onClick=\'update()\' type="submit" value="Update"></input></td></tr>';
00157
00158 $str.="\n<input type=\"hidden\" id=\"fields\" name=\"fields\" value=\"".$fields."\" />";
00159 $str.="\n<input type=\"hidden\" id=\"querystring\" name=\"querystring\" value=\"".$this->querystring."\" />";
00160 $str.="\n<input type=\"hidden\" id=\"buttonpressed\" name=\"buttonpressed\" value=\"\" />";
00161 $str.="\n<input type=\"hidden\" id=\"tablename\" name=\"tablename\" value=\"".$this->tablename."\" />";
00162 $str.="\n</table>";
00163 }
00164 $str.="\n</form>";
00165
00166 return $str;
00167 }
00168
00169 function get_tablename_from_query($query)
00170 {
00171
00172 $a=strtok($query,' ');
00173 while(is_string($a))
00174 {
00175 if($a)
00176 {
00177 if (strtolower($a)!='from')
00178 {
00179 $a=strtok(' ');
00180 continue;
00181 }
00182 else
00183 {
00184 $a=strtok(' ');
00185 break;
00186 }
00187 }
00188 }
00189 return $a;
00190 }
00191
00192 function get_size_of_field($fieldname)
00193 {
00194 if(!is_string($this->tablename)) return "text_type";
00195
00196
00197
00198 $b=mysql_fetch_assoc(mysql_query("explain ".$this->tablename." '".$fieldname."'"));
00199
00200 $a=$b['Type'];
00201 if($a=="text") return "text_type";
00202 $a=substr($a,strpos($a,'(')+1,strpos($a,')')-strpos($a,'(')-1);
00203
00204 if(!is_string($a)) return 15;
00205 else if ($a>15) return 15;
00206 return $a;
00207 }
00208
00209 }