From 353835d402a159958ff48e6a41d0041e28b8cae1 Mon Sep 17 00:00:00 2001 From: james Date: Wed, 23 Feb 2005 14:29:28 +0000 Subject: [PATCH] add a class to export the reports to CSV - modelled after the PDF class so the two will be easy to use together update the checkin reports to export to CSV as well --- admin/index.php | 2 +- admin/reports.php | 10 +++- admin/reports_checkin.php | 35 ++++++++---- lcsv.php | 111 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 11 deletions(-) create mode 100644 lcsv.php diff --git a/admin/index.php b/admin/index.php index b0a933bc..6fb53081 100644 --- a/admin/index.php +++ b/admin/index.php @@ -27,7 +27,7 @@ send_header("Administration"); echo "Participant Registration
"; - echo "Printable Reports
"; + echo "Print / Export Reports
"; echo "Committee Management
"; send_footer(); diff --git a/admin/reports.php b/admin/reports.php index 05a9a24d..1a472264 100644 --- a/admin/reports.php +++ b/admin/reports.php @@ -27,13 +27,21 @@ send_header("Administration - Reports"); echo "<< ".i18n("Back to Administration")."
"; echo "
"; + echo ""; //lets split this up by age category, $catq=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' ORDER BY id"); while($catr=mysql_fetch_object($catq)) { - echo "id\">$catr->category   "; + echo ""; } +echo ""; +echo "
"; echo i18n("Day of Fair Registration/Checkin Forms").": "; + echo ""; + echo "id\">$catr->category (PDF)   "; + echo "
"; + echo "id\">$catr->category (CSV)   "; + echo "
"; send_footer(); diff --git a/admin/reports_checkin.php b/admin/reports_checkin.php index 282c605d..801180c3 100644 --- a/admin/reports_checkin.php +++ b/admin/reports_checkin.php @@ -25,18 +25,28 @@ require("../common.inc.php"); auth_required('admin'); require("../lpdf.php"); + require("../lcsv.php"); + + $type=$_GET['type']; $catq=mysql_query("SELECT * FROM projectcategories WHERE year='".$config['FAIRYEAR']."' AND id='".$_GET['cat']."'"); if($catr=mysql_fetch_object($catq)) { + if($type=="pdf") + { - $pdf=new lpdf( i18n($config['fairname']), - i18n("Checkin List")." - ".i18n($catr->category), - $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif" - ); + $pdf=new lpdf( i18n($config['fairname']), + i18n("Checkin List")." - ".i18n($catr->category), + $_SERVER['DOCUMENT_ROOT'].$config['SFIABDIRECTORY']."/data/logo-200.gif" + ); - $pdf->newPage(); - $pdf->setFontSize(11); + $pdf->newPage(); + $pdf->setFontSize(11); + } + else if($type=="csv") + { + $csv=new lcsv(i18n("Checkin List")." - ".i18n($catr->category)); + } $q=mysql_query("SELECT registrations.id AS reg_id, registrations.num AS reg_num, registrations.status, @@ -110,8 +120,15 @@ if($catr=mysql_fetch_object($catq)) } - $pdf->addTable($table); - - $pdf->output(); + if($type=="pdf") + { + $pdf->addTable($table); + $pdf->output(); + } + else if($type=="csv") + { + $csv->addTable($table); + $csv->output(); + } } ?> diff --git a/lcsv.php b/lcsv.php new file mode 100644 index 00000000..0c8a74f2 --- /dev/null +++ b/lcsv.php @@ -0,0 +1,111 @@ + + Copyright (C) 2005 James Grant + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public + License as published by the Free Software Foundation, version 2. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +?> +str_separator; + } + function setSeparator($s) + { + $this->str_separator=$s; + } + + + function newline() + { + return $this->str_newline; + } + function setNewline($s) + { + $this->str_newline=$s; + } + + function addTable($table) + { + if($table['header']) + { + $table_cols=count($table['header']); + for($c=0;$c<$table_cols;$c++) + { + $head=$table['header'][$c]; + $this->csvdata.=$head; + if($c<$table_cols-1) + $this->csvdata.=$this->separator(); + } + $this->csvdata.=$this->newline(); + } + else + { + //is this right ? + $table_cols=count($table['data'][0]); + } + + //now do the data in the table + if($table['data']) + { + foreach($table['data'] AS $dataline) + { + for($c=0;$c<$table_cols;$c++) + { + //if the data contains the separator, we need to puti the data inside ""'s + if(strstr($dataline[$c],$this->separator())) + $this->csvdata.="\"".$dataline[$c]."\""; + else + $this->csvdata.=$dataline[$c]; + + if($c<$table_cols-1) + $this->csvdata.=$this->separator(); + } + $this->csvdata.=$this->newline(); + } + } + } + + + function output() + { + if($this->csvdata) + { + //header("Content-type: application/csv"); + header("Content-type: application/csv"); + header("Content-disposition: inline; filename=sfiab_".$this->page_subtitle.".csv"); + header("Content-length: ".strlen($this->csvdata)); + echo $this->csvdata; + } + } + + function lcsv($subtitle,$sep=",",$nl="\r\n") + { + $this->page_subtitle=$subtitle; + $this->setSeparator($sep); + $this->setNewline($nl); + + } +}