From 6ab58a1b8ee68fcdda3256f4a5b23d77dbf6043a Mon Sep 17 00:00:00 2001 From: james Date: Mon, 11 Sep 2006 17:32:02 +0000 Subject: [PATCH] Add a generic CSV reader that properly handles multi-line fields Add a school importer to import school list from CSV file --- admin/schools.php | 2 + admin/schoolsimport.php | 114 ++++++++++++++++++++++++++++++++++++++++ csvimport.inc.php | 88 +++++++++++++++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 admin/schoolsimport.php create mode 100644 csvimport.inc.php diff --git a/admin/schools.php b/admin/schools.php index 0f824f3b..cbae550d 100644 --- a/admin/schools.php +++ b/admin/schools.php @@ -161,6 +161,8 @@ echo "
"; echo "Add New School\n"; echo "
"; + echo "Import Schools from CSV\n"; + echo "
"; echo ""; echo ""; echo " "; diff --git a/admin/schoolsimport.php b/admin/schoolsimport.php new file mode 100644 index 00000000..4e2eb3fc --- /dev/null +++ b/admin/schoolsimport.php @@ -0,0 +1,114 @@ + + 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. +*/ +?> +<< ".i18n("Back to Administration")."\n"; + $showform=true; + + if($_POST['action']=="import") + { + if(!$_FILES['schools']['error'] && $_FILES['schools']['size']>0) + { + $showform=false; + $CSVP=new CSVParser(); + $CSVP->parseFile($_FILES['schools']['tmp_name']); + if(count($CSVP->data)>0) + { + //okay it looks like we have something.. lets dump the current stuff + if($_POST['emptycurrent']==1) + { + echo happy(i18n("Old school data erased")); + mysql_query("DELETE FROM schools WHERE year='".$config['FAIRYEAR']."'"); + } + + $loaded=0; + foreach($CSVP->data AS $row) + { + mysql_query("INSERT INTO schools (school,board,district,phone,fax,address,city,province_code,postalcode,sciencehead,scienceheademail,scienceheadphone,accesscode,registration_password,projectlimit,projectlimitper,year) VALUES ( + '".mysql_escape_string(stripslashes($row[0]))."', + '".mysql_escape_string(stripslashes($row[1]))."', + '".mysql_escape_string(stripslashes($row[2]))."', + '".mysql_escape_string(stripslashes($row[3]))."', + '".mysql_escape_string(stripslashes($row[4]))."', + '".mysql_escape_string(stripslashes($row[5]))."', + '".mysql_escape_string(stripslashes($row[6]))."', + '".mysql_escape_string(stripslashes($row[7]))."', + '".mysql_escape_string(stripslashes($row[8]))."', + '".mysql_escape_string(stripslashes($row[9]))."', + '".mysql_escape_string(stripslashes($row[10]))."', + '".mysql_escape_string(stripslashes($row[11]))."', + '".mysql_escape_string(stripslashes($row[12]))."', + '".mysql_escape_string(stripslashes($row[13]))."', + '".mysql_escape_string(stripslashes($row[14]))."', + '".mysql_escape_string(stripslashes($row[15]))."', + '".$config['FAIRYEAR']."')"); + if(!mysql_Error()) + $loaded++; + else + echo mysql_error(); + } + echo happy(i18n("Successfully loaded %1 schools",array($loaded))); + echo "".i18n("School Management")."
"; + } + else + { + echo error(i18n("Found no CSV data in the uploaded file")); + } + print_r($data); + } + else + { + echo error(i18n("Please choose a valid CSV file to upload")); + $showform=true; + } + } + + if($showform) + { + echo "
"; + echo i18n("Choose the CSV file containing the school information. The COLUMNS of the file must contain the following information, in this exact order, separated by comma's (,) with fields optionally enclosed by quotes (\"):"); + echo "
"; + echo "
"; + echo i18n("School Name, Board, District, Phone, Fax, Address, City, Province, Postal Code, Science Head, Science Head Email, Science Head Phone, Access Code, Registration Password, Project Limit, Project Limit Per(total or agecategory)"); + + echo "
"; + echo "
"; + echo "
"; + echo ""; + echo "".i18n("Empty all current school information before importing?")."
"; + echo ""; + echo "\n"; + echo ""; + + } + + send_footer(); + +?> diff --git a/csvimport.inc.php b/csvimport.inc.php new file mode 100644 index 00000000..25e678cd --- /dev/null +++ b/csvimport.inc.php @@ -0,0 +1,88 @@ + + Copyright (C) 2006 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. +*/ +?> +delimiter = ","; + $this->enclosure = '"'; + $this->data = array(); +} + +function parseFile($filename) +{ + $content = file_get_contents($filename); + $content = str_replace( "\r\n", "\n", $content ); + $content = str_replace( "\r", "\n", $content ); + if($content[strlen($content)-1] != "\n") // Make sure it always end with a newline + $content .= "\n"; + + // Parse the content character by character + $row = array( "" ); + $idx = 0; + $quoted = false; + for ( $i = 0; $i < strlen($content); $i++ ) + { + $ch = $content[$i]; + if ($ch == $this->enclosure) + $quoted = !$quoted; + + // End of line + if ($ch=="\n" && !$quoted) + { + // Remove enclosure delimiters + for ($k=0;$kenclosure ) + { + $row[$k] = substr($row[$k], 1, strlen($row[$k]) - 2); + } + $row[$k] = str_replace(str_repeat($this->enclosure, 2), $this->enclosure, $row[$k]); + } + + // Append row into table + $this->data[] = $row; + $row = array( "" ); + $idx = 0; + } + + // End of field + else if ($ch==$this->delimiter && !$quoted) + $row[++$idx]=""; + + // Inside the field + else + $row[$idx].=$ch; + } + return true; +} + +} + +?>
School