* @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 ""; $data = new $this->classname($_GET['edit']); if(method_exists($data, 'tableEditorLoad')) { $editdata = $data->tableEditorLoad(); } else { $editdata = $this->defaultLoad(); } } echo ""; foreach($this->editfields AS $f=>$n) { $pos = strpos($n, "|"); $n2 = ""; if($pos != false) { $n2 = i18n(substr($n, $pos + 1)).' '; $n = substr($n, 0, $pos); } echo ""; } echo ""; echo "
".i18n($n)."$n2"; /* If we know the input type, assume the user knows what they are doing, else, * try to query it from the databse */ if(isset($this->fieldInputType[$f])) { $inputtype = $this->fieldInputType[$f]; $inputmaxlen = 0; // FIXME $inputsize = 0; // FIXME } else { list($inputtype, $inputmaxlen, $inputsize) = $this->getFieldType($f); } switch($inputtype) { case "text": if($this->fieldInputOptions[$f]) echo "fieldInputOptions[$f]." id=\"$f\" name=\"$f\" value=\"".htmlspecialchars($editdata[$f])."\"/>"; else echo ""; 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 "multicheck": $ks = array_keys($this->fieldOptions[$f]); foreach($ks as $k) { $ch = ''; if(is_array($editdata[$f])) { if(array_key_exists($k, $editdata[$f])) { $ch = ' checked=checked '; } } echo " {$this->fieldOptions[$f][$k]}
"; } 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 "
"; 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 "
"; 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 ""; } 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; } } ?>