forked from science-ation/science-ation
Add a generic CSV reader that properly handles multi-line fields
Add a school importer to import school list from CSV file
This commit is contained in:
parent
050f2093fb
commit
6ab58a1b8e
@ -161,6 +161,8 @@
|
||||
echo "<br />";
|
||||
echo "<a href=\"schools.php?action=add\">Add New School</a>\n";
|
||||
echo "<br />";
|
||||
echo "<a href=\"schoolsimport.php?action=add\">Import Schools from CSV</a>\n";
|
||||
echo "<br />";
|
||||
echo "<table class=\"summarytable\">";
|
||||
echo "<tr>";
|
||||
echo " <th>School</th>";
|
||||
|
114
admin/schoolsimport.php
Normal file
114
admin/schoolsimport.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?
|
||||
/*
|
||||
This file is part of the 'Science Fair In A Box' project
|
||||
SFIAB Website: http://www.sfiab.ca
|
||||
|
||||
Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
|
||||
Copyright (C) 2005 James Grant <james@lightbox.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
?>
|
||||
<?
|
||||
require("../common.inc.php");
|
||||
require("../csvimport.inc.php");
|
||||
|
||||
auth_required('admin');
|
||||
|
||||
send_header("Administration - Schools Import");
|
||||
|
||||
echo "<a href=\"index.php\"><< ".i18n("Back to Administration")."</a>\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 "<a href=\"schools.php\">".i18n("School Management")."</a> <br />";
|
||||
}
|
||||
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 "<br />";
|
||||
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 "<br />";
|
||||
echo "<br />";
|
||||
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 "<br />";
|
||||
echo "<br />";
|
||||
echo "<form enctype=\"multipart/form-data\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">";
|
||||
echo "<input type=\"hidden\" name=\"action\" value=\"import\">";
|
||||
echo "<input type=\"checkbox\" name=\"emptycurrent\" value=\"1\">".i18n("Empty all current school information before importing?")."<br />";
|
||||
echo "<input type=\"file\" name=\"schools\">";
|
||||
echo "<input type=\"submit\" value=\"".i18n("Upload School CSV")."\">\n";
|
||||
echo "</form>";
|
||||
|
||||
}
|
||||
|
||||
send_footer();
|
||||
|
||||
?>
|
88
csvimport.inc.php
Normal file
88
csvimport.inc.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?
|
||||
/*
|
||||
This file is part of the 'Science Fair In A Box' project
|
||||
SFIAB Website: http://www.sfiab.ca
|
||||
|
||||
Copyright (C) 2006 Sci-Tech Ontario Inc <info@scitechontario.org>
|
||||
Copyright (C) 2006 James Grant <james@lightbox.org>
|
||||
|
||||
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.
|
||||
*/
|
||||
?>
|
||||
<?
|
||||
class CSVParser
|
||||
{
|
||||
var $delimiter; // Field delimiter
|
||||
var $enclosure; // Field enclosure character
|
||||
var $data; // CSV data
|
||||
|
||||
function CSVParser()
|
||||
{
|
||||
$this->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;$k<count($row);$k++ )
|
||||
{
|
||||
if($row[$k]!="" && $row[$k][0]==$this->enclosure )
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user