* @version 1.1 * @package tableeditor */ //Version 2006-04-21 //Version 2006-05-04 //Version 2006-05-07 //Version 2006-06-08 - add filtering capabilities to the display //Version 2006-06-16 - add file uploads setUploadPath() and fieldname='filename' //Version 2006-07-21 - add ability to autodetect options for enum() fields, and show them as a "; //check for any defaults if(count($this->fieldDefaults)) { foreach($this->fieldDefaults AS $f=>$n) $editdata->$f=$n; } } else if($action=="edit") { echo "

".i18n("Edit %1",array($this->recordType))."

"; echo ""; echo ""; $query="SELECT {$this->primaryKey}"; foreach($this->editfields AS $f=>$n) $query.=", `$f`"; $query.=" FROM `{$this->table}`"; $query.=" WHERE {$this->primaryKey}='{$_GET['edit']}'"; if($this->DEBUG) echo $query; $editquery=mysql_query($query); $editdata=mysql_fetch_object($editquery); } echo ""; foreach($this->editfields AS $f=>$n) { echo ""; } echo ""; echo "
".i18n($n).""; //figure out what kind of input this should be $q=mysql_query("SHOW COLUMNS FROM `{$this->table}` LIKE '$f'"); $r=mysql_fetch_object($q); if(ereg("([a-z]*)\(([0-9,]*)\)",$r->Type,$regs)) { switch($regs[1]) { case "varchar": $inputtype="text"; $inputmaxlen=$regs[2]; if($regs[2]>50) $inputsize=50; else $inputsize=$regs[2]; break; case "int": $inputtype="text"; $inputmaxlen=10; $inputsize=10; break; case "decimal": $inputtype="text"; $inputmaxlen=10; $inputsize=10; break; case "tinyint": $inputtype="text"; $inputmaxlen=5; $inputsize=4; break; default: $inputtype="text"; $inputmaxlen=$regs[2]; if($regs[2]>50) $inputsize=50; else $inputsize=$regs[2]; break; } } else if(ereg("([a-z]*)",$r->Type,$regs)) { switch($regs[1]) { case "text": $inputtype="textarea"; break; case "date": $inputtype="date"; break; case "time": $inputtype="time"; break; case "enum": //an enum is a select box, but we already know what the options should be //so rip out the options right now and add them $inputtype="select"; $enums=substr(ereg_replace("'","",$r->Type),5,-1); $toks=split(",",$enums); foreach($toks as $tok) { $this->fieldOptions[$f][]=$tok; } break; } } if(substr($f,0,4)=="sel_") { $inputtype="select_or_text"; } if(substr($f,0,8)=="filename" && $this->uploadPath) { $inputtype="file"; } if(array_key_exists($f,$this->fieldOptions)) { //only change to select if the type is not select_or_Text //if we are already select or text, then the options will appear //first in the list, then any options that arent there by default //will appear under them in the dropdown if($inputtype!="select_or_text") $inputtype="select"; } switch($inputtype) { case "text": if($this->fieldInputOptions[$f]) echo "fieldInputOptions[$f]." id=\"$f\" name=\"$f\" value=\"".htmlspecialchars($editdata->$f)."\"/>"; else echo "$f)."\"/>"; break; case "textarea": if($this->fieldInputOptions[$f]) echo ""; else echo ""; break; case "select": if($this->fieldInputOptions[$f]) echo ""; echo "\n"; foreach($this->fieldOptions[$f] AS $opt) { if(is_array($opt)) { if($opt['key'] == $editdata->$f) $sel="selected=\"selected\""; else $sel=""; echo "\n"; } else { if($opt == $editdata->$f) $sel="selected=\"selected\""; else $sel=""; echo "\n"; } } echo ""; break; case "enum": break; case "select_or_text": $optq=mysql_query("SELECT DISTINCT($f) AS $f FROM `{$this->table}` ORDER BY $f"); if($this->fieldInputOptions[$f]) echo ""; echo "\n"; if($this->fieldOptions[$f]) { foreach($this->fieldOptions[$f] AS $opt) { if($opt == $editdata->$f) $sel="selected=\"selected\""; else $sel=""; echo "\n"; } echo ""; } print_r($this->fieldOptions[$f]); while($opt=mysql_fetch_object($optq)) { if(is_array($this->fieldOptions[$f]) && in_array($opt->$f,$this->fieldOptions[$f])) continue; if($opt->$f == $editdata->$f) $sel="selected=\"selected\""; else $sel=""; echo "\n"; } echo ""; echo " or "; //only show the input half-sized because its beside the select box which is already taking up space. $inputsize=round($inputsize/2); //input always starts emptpy as well, because we already have it selected in the list if($this->fieldInputOptions[$f]) echo "fieldInputOptions[$f]." id=\"".$f."_text\" name=\"".$f."_text\" value=\"\" />"; else echo ""; break; case "date": list($yy,$mm,$dd)=split("-",$editdata->$f); //if we put a small width here, then it prevents it from expanding to whatever width it feels like. echo ""; echo ""; echo "
"; $this->month_selector($f."_month",$mm); echo ""; $this->day_selector($f."_day",$dd); echo ""; $this->year_selector($f."_year",$yy); echo "
"; break; case "time": list($hh,$mm,$ss)=split(":",$editdata->$f); echo ""; echo ""; echo "
"; $this->hour_selector($f."_hour",$hh,false,"12hr"); echo ""; $this->minute_selector($f."_minute",$mm); echo "
"; break; case "file": if($editdata->$f) { if(file_exists($this->uploadPath."/".$editdata->$f)) { //only show a link to the file if the upload path is inside the document root if(strstr(realpath($this->uploadPath),$_SERVER['DOCUMENT_ROOT'])) { echo "uploadPath}/{$editdata->$f}\">{$editdata->$f}"; } else { echo $editdata->$f; } echo " (".filesize($this->uploadPath."/".$editdata->$f)." bytes) delete
"; } else { echo $editdata->$f." (does not exist)
"; } } echo "fieldInputOptions[$f]." id=\"$f\" name=\"$f\" />"; break; default: echo "$f)."\"/>"; } echo "
"; echo "
"; if($action=="add") echo "recordType))."\">"; else if($action=="edit") echo "recordType))."\">"; echo ""; echo ""; echo "
"; echo ""; echo "
"; echo "
"; echo "
"; } else if($_GET['TableEditorAction']=="export") { //fixme: how to do an export? we cant send headers because its possible that output has already started! } else { $this->displayTable(); } } function displayTable() { global $icon_path; global $icon_extension; $query="SELECT SQL_CALC_FOUND_ROWS {$this->primaryKey}"; foreach($this->listfields AS $f=>$n) $query.=", `$f`"; $query.=" FROM `{$this->table}`"; if(count($this->fieldFilterList)) { $query.=" WHERE 1 "; foreach($this->fieldFilterList AS $k=>$v) { $query.=" AND `$k`='$v'"; } } if($this->sortField()) $query.=" ORDER BY ".$this->sortField().""; if($this->rowsPerPage>0) { //first, we treat page 1 as the first page, but really, "row 0" is the first page, so we need //to be careful. //LIMIT offset,number $offset=($this->activePage-1)*$this->rowsPerPage; //just to make sure nothing funky is goin on. if($offset<0) $offset=0; $query.=" LIMIT $offset,$this->rowsPerPage"; } if($this->allowAdding) { echo "".i18n("Add new %1",array($this->recordType))."

"; } if($this->DEBUG) echo $query; $q=mysql_query($query); //put in some paganation stuff here. $foundrowsq=mysql_query("SELECT FOUND_ROWS() AS f"); $foundrowsr=mysql_fetch_object($foundrowsq); $foundrows=$foundrowsr->f; if($foundrows>$this->rowsPerPage) { $numpages=ceil($foundrows/$this->rowsPerPage); if($this->activePage>1) { $prevpage=$this->activePage-1; echo "< Prev"; } else echo "< Prev"; echo " "; if($numpages<10) { //if there's less than 10 pages, lets show links for all the pages for($x=1;$x<=$numpages;$x++) { echo " "; if($x==$this->activePage) echo "$x"; else echo "$x"; echo " "; } } else { //if we have more than 10 pages, lets show the previous 5 ..currentpage.. next 5 //if there's less than 10 pages, lets show links for all the pages $start=$this->activePage-4; $end=$this->activePage+4; if($start<1) { $end+=abs(1-$start); $start=1; } if($end>$numpages) { $start-=abs($numpages-$end); $end=$numpages; } if($start>1) { echo " "; echo "1"; echo " "; echo "..."; } for($x=$start;$x<=$end;$x++) { echo " "; if($x==$this->activePage) echo "$x"; else echo "$x"; echo " "; } if($end<$numpages) { echo "..."; echo " "; echo "$numpages"; echo " "; } } echo " "; if($this->activePage<$numpages) { $nextpage=$this->activePage+1; echo "Next >"; } else echo "Next >"; } // else // echo "no need to paganate, foundrows=$foundrows, rowsPerPage=".$this->rowsPerPage; if(mysql_error()){ echo error(mysql_error()); return;} if(mysql_num_rows($q)) { echo ""; echo ""; foreach($this->listfields AS $f=>$n) { if($this->sortField()==$f) echo ""; else echo ""; } echo ""; echo ""; while($r=mysql_fetch_object($q)) { echo ""; foreach($this->listfields AS $f=>$n) { //figure out what kind of input this should be $typeq=mysql_query("SHOW COLUMNS FROM `{$this->table}` LIKE '$f'"); $typer=mysql_fetch_object($typeq); if($typer->Type=="time") echo ""; else if($typer->Type=="date") echo ""; else if(substr($f,0,8)=="filename" && $this->uploadPath) { echo ""; } else { if($this->fieldOptions[$f]) { if(is_array($this->fieldOptions[$f][0])) { //if it is an aray, then we have a key=> and val=> so we need //to lookup the key and display the val $TE_Found_Field=false; foreach($this->fieldOptions[$f] AS $i=>$o) { if($o['key']==$r->$f) { echo ""; $TE_Found_Field=true; break; } } if(!$TE_Found_Field) { echo ""; } } else { //if its not an array, then each element is simply a data value anyways, so we can just show the value echo ""; } } else { echo ""; } } } echo ""; echo ""; } echo "
".i18n($n)."".i18n($n)."".i18n("Actions")."
".$this->format_time($r->$f)."".$this->format_date($r->$f).""; //only show a link to the file if the upload path is inside the document root if(strstr(realpath($this->uploadPath),$_SERVER['DOCUMENT_ROOT']) && file_exists($this->uploadPath."/".$editdata->$f)) { echo "uploadPath}/{$r->$f}\">{$r->$f}"; } else { echo $r->$f; } echo "{$o['val']}{$r->$f}{$r->$f}"; $pk=$this->primaryKey; //custom action buttons go first if(count($this->actionButtons)) { foreach($this->actionButtons AS $button) { echo "$pk."\">"; echo "  "; } } //now the default action buttons that you cant get rid of :) echo "recordType."\" href=\"{$_SERVER['PHP_SELF']}?TableEditorAction=edit&edit=".$r->$pk."\">"; echo "  "; echo "recordType."\" onclick=\"return confirmClick('".i18n("Are you sure you want to delete this %1?",array($this->recordType))."')\" href=\"{$_SERVER['PHP_SELF']}?TableEditorAction=delete&delete=".$r->$pk."\">"; echo "
"; } echo "
"; } function month_selector($name,$selected="") { echo "\n"; } function year_selector($name,$selected="") { $now=date("Y"); if($this->yearSelectRangeMin) $start=$this->yearSelectRangeMin; else $start=$now-2; if($this->yearSelectRangeMax) $end=$this->yearSelectRangeMax; else $end=$now+3; echo "\n"; } function day_selector($name,$selected="") { echo "\n"; } function hour_selector($name,$selected="", $disabled=false) { echo "\n"; } function minute_selector($name,$selected="",$disabled=false) { if($selected=="") $selected=-1; for($x=0;$x<60;$x++) $mins[]=sprintf("%02d",$x); // $mins=array("00","15","30","45"); echo "\n"; } function format_date($d) { if(!$d) return; list($y,$m,$d)=split("-",$d); $t=mktime(0,0,0,$m,$d,$y); return date($this->dateformat,$t); } function format_time($t) { if(!$t) return; if($this->timeformat=="12hrs") { list($hh,$mm,$ss)=split(":",$t); //hack to get rid of leading "0" and turn it into a number $hh++; $hh--; if($hh==0) $ret="12:$mm AM"; else if($hh<12) $ret="$hh:$mm AM"; else if($hh==12) $ret="12:$mm PM"; else $ret=($hh-12).":$mm PM"; } else if($this->timeformat=="24hrs") { $ret=substr($t,0,-3); } return $ret; } } ?>