mirror of
https://github.com/ACSPRI/queXS
synced 2024-04-02 12:12:16 +00:00
New feature: Add records to an existing sample
This commit is contained in:
@@ -53,6 +53,46 @@ function only_numbers($str)
|
||||
return preg_replace("/[^0-9]/", "",$str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a provided file matches an existing sample file
|
||||
*/
|
||||
function verify_file($file,$import_id)
|
||||
{
|
||||
$row = get_first_row($file);
|
||||
|
||||
global $db;
|
||||
|
||||
$sql = "SELECT var,type,var_id
|
||||
FROM sample_import_var_restrict
|
||||
WHERE sample_import_id = $import_id
|
||||
ORDER BY var_id ASC";
|
||||
|
||||
$sivr = $db->GetAll($sql);
|
||||
|
||||
$count = 0;
|
||||
|
||||
$nrow = array();
|
||||
|
||||
foreach($row as $key => $val) {
|
||||
$nrow[$key] = strtolower(str_replace(array(" ,",", ",","," "," "),"_", $val));
|
||||
|
||||
}
|
||||
|
||||
foreach($sivr as $r) {
|
||||
$var = strtolower($r['var']);
|
||||
if ($var != $nrow[$count]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$count++;
|
||||
}
|
||||
|
||||
//column headings match database
|
||||
if ($count == count($row)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Verify fields in a CSV file
|
||||
@@ -188,6 +228,38 @@ function get_first_row($file)
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add records to an existing sample
|
||||
*/
|
||||
function update_file($file,$import_id)
|
||||
{
|
||||
global $db;
|
||||
|
||||
$db->StartTrans();
|
||||
|
||||
$selected_type = array();
|
||||
$selected_name = array();
|
||||
$sirv_id = array();
|
||||
|
||||
$sql = "SELECT var_id,var,type
|
||||
FROM sample_import_var_restrict
|
||||
WHERE sample_import_id = $import_id
|
||||
ORDER BY var_id ASC";
|
||||
|
||||
$sivr = $db->GetAll($sql);
|
||||
|
||||
$count = 1;
|
||||
foreach($sivr as $s) {
|
||||
$selected_type[$count] = $s['type'];
|
||||
$selected_name[$count] = $s['var'];
|
||||
$sirv_id[$count] = $s['var_id'];
|
||||
$count++;
|
||||
}
|
||||
|
||||
add_to_sample($file,$import_id,$selected_type,$selected_name,$sirv_id,2);
|
||||
|
||||
return $db->CompleteTrans();
|
||||
}
|
||||
|
||||
/**
|
||||
* Import a CSV file to the sample database
|
||||
@@ -202,10 +274,6 @@ function get_first_row($file)
|
||||
*/
|
||||
function import_file($file, $description, $fields, $firstrow = 2)
|
||||
{
|
||||
|
||||
$row = 1;
|
||||
$handle = fopen($file, "r");
|
||||
|
||||
//import into database
|
||||
|
||||
global $db;
|
||||
@@ -246,6 +314,23 @@ function import_file($file, $description, $fields, $firstrow = 2)
|
||||
}
|
||||
}
|
||||
|
||||
add_to_sample($file,$id,$selected_type,$selected_name,$sirv_id,$firstrow);
|
||||
|
||||
return $db->CompleteTrans();
|
||||
}
|
||||
|
||||
/**
|
||||
* add_to_sample
|
||||
*
|
||||
* Add records to the sample file
|
||||
*
|
||||
*/
|
||||
function add_to_sample($file,$id,$selected_type,$selected_name,$sirv_id,$firstrow)
|
||||
{
|
||||
global $db;
|
||||
|
||||
$db->StartTrans();
|
||||
|
||||
/**
|
||||
* create an ordered index of columns that contain data for obtaining the timezone
|
||||
* type of 5,4,3,2 preferred
|
||||
@@ -253,6 +338,9 @@ function import_file($file, $description, $fields, $firstrow = 2)
|
||||
|
||||
arsort($selected_type);
|
||||
|
||||
$row = 1;
|
||||
$handle = fopen($file, "r");
|
||||
|
||||
$imported = 0;
|
||||
|
||||
while (($data = fgetcsv($handle)) !== FALSE)
|
||||
@@ -346,9 +434,9 @@ function import_file($file, $description, $fields, $firstrow = 2)
|
||||
unlink($file);
|
||||
|
||||
return $db->CompleteTrans();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the timezone given the sample value and type
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user