* @copyright Deakin University 2007,2008 * @package queXS * @subpackage functions * @link http://www.deakin.edu.au/dcarf/ queXS was writen for DCARF - Deakin Computer Assisted Research Facility * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License (GPL) Version 2 * */ /** * Configuration file */ include_once(dirname(__FILE__).'/../config.inc.php'); /** * Database file */ include_once(dirname(__FILE__).'/../db.inc.php'); /** * Return only numbers from a string * * @param string $str String containing any character * @return int A number * */ function only_numbers($str) { return ereg_replace('[^0-9]*','',$str); } /** * Verify fields in a CSV file * confirm that there are no duplicate names * confirm that there is one and only one primary number selected * * @param array $fields an array of field information i_ if selected, n_ name, t_ type code * @return string An empty string if valid, otherwise invalid with error message * */ function verify_fields($fields) { //i_ is if selected //n_ is name of field //t_ is type of field $selected = array(); foreach($fields as $key => $val) { if (strncmp($key, "i_", 2) == 0) { $selected[] = substr($key,2); } } $names = array(); //check for duplicate names foreach($selected as $val) { if (array_key_exists($fields["n_$val"], $names)) { return T_("Duplicate name"); } else { //set name to type $names[$fields["n_$val"]] = $fields["t_$val"]; } } //check that there is one and one only primary phone selected $count = 0; foreach($names as $val) { if ($val == 3) $count++; } if ($count == 1) { return ""; } else { return T_("You must select one and one only Primary Phone number"); } } /** * Display an XHTML table of the CSV data header * * @param array $data Header data from a CSV file * * @see get_first_row() * */ function display_table($data) { print ""; print ""; $row = 1; global $db; $sql = "SELECT description,type FROM sample_var_type"; $rs = $db->Execute($sql); foreach ($data as $key => $value) { $val = str_replace(" ", "_", $value); $checked = "checked"; if (empty($val)) $val = "samp_$row"; print ""; $row++; $rs->MoveFirst(); } print "
" . T_("Import?") . "" . T_("Name") . "" . T_("Type") . "
$value" . $rs->GetMenu("t_$row","1",false) . "
"; } /** * Return the first row of a CSV file as an array * * @param string $file File name to open * */ function get_first_row($file) { $handle = fopen($file, "r"); $data = fgetcsv($handle); fclose($handle); return $data; } /** * Import a CSV file to the sample database * * @param string $file File name to open * @param string $description A description of the sample * @param array $fields Which fields to import and what type they are assigned to * * @see verify_fields() * @see display_table() * */ function import_file($file, $description, $fields, $firstrow = 2) { $row = 1; $handle = fopen($file, "r"); //import into database global $db; $db->StartTrans(); $sql = "INSERT INTO sample_import (sample_import_id, description) VALUES (NULL, '$description')"; //print("$sql
"); //if ($db->HasFailedTrans()) { print "FAILED"; exit(); } $rs = $db->Execute($sql); $id = $db->Insert_ID(); $selected_type = array(); $selected_name = array(); foreach($fields as $key => $val) { if (strncmp($key, "i_", 2) == 0) { $selected_type[substr($key,2)] = $fields["t_" . substr($key,2)]; $selected_name[substr($key,2)] = $fields["n_" . substr($key,2)]; } } /** * create an ordered index of columns that contain data for obtaining the timezone * type of 5,4,3,2 preferred */ arsort($selected_type); $imported = 0; while (($data = fgetcsv($handle)) !== FALSE) { //data contains an array of elements in the csv //selected contains an indexed array of elements to import with the type attached if ($row >= $firstrow) //don't import the header row { //determine if there is a phone number - if not - do not import $numberavail = 0; foreach($selected_type as $key => $val) { if ($val == 2 || $val == 3) { $dkey = only_numbers($data[$key - 1]); if (!empty($dkey)) $numberavail = 1; } } if ($numberavail == 1) { //insert into sample field //first find the timezone $tzone = DEFAULT_TIME_ZONE; //set this to default /** * Determine time zone from all possible sources in sample_var_type table * */ foreach($selected_type as $key => $val) { $sql = "SELECT `table` FROM sample_var_type WHERE type = '$val'"; $tname = $db->GetRow($sql); if (!empty($tname)) { $tname = $tname['table']; if (!empty($tname)) { $value = $db->Quote($data[$key - 1]); $sql = "SELECT Time_zone_name as tz FROM `$tname` WHERE val = SUBSTR($value, 1, CHAR_LENGTH( val ) )"; $tz = $db->GetRow($sql); //print("$sql
"); //if ($db->HasFailedTrans()) { print "FAILED"; exit(); } if (!empty($tz)) { $tzone = $tz['tz']; break; } } } } /** * insert using primary phone number (3) */ $ppid = array_search('3', $selected_type); $dppid = only_numbers($data[$ppid - 1]); $sql = "INSERT INTO sample (sample_id,import_id,Time_zone_name,phone) VALUES (NULL,'$id','$tzone','$dppid')"; $db->Execute($sql); $sid = $db->Insert_Id(); /** * insert into sample_var field */ foreach($selected_name as $key => $val) { $dkey = $db->Quote($data[$key - 1]); $sql = "INSERT INTO sample_var (sample_id,var,val,type) VALUES ('$sid','$val',{$dkey},'{$selected_type[$key]}')"; $db->Execute($sql); } $imported++; } } $row++; } fclose($handle); //cleanup unlink($file); return $db->CompleteTrans(); } ?>