<?php

include_once('_dirinfo.php');
include_once(PATH_TO_ROOT . 'init.php');
include(PATH_TO_ROOT . '_class/class.parsecsv.php');

$filename = $_FILES['csvfilepath']['tmp_name'];
$csv = new parseCSV();
$csv->auto($filename);

$projectId = intval($_POST['project_id']);

$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
$dataTable = $_POST['exportSelect'] == 8 ? '_DatamapAscii' : '_DatamapBinary';
$sql = "DELETE FROM $dataTable WHERE id_project = $projectId";
//$db->SQLexecute($sql);

if(!$db->SQLexecute($sql)) { echo $db->GetError(); exit;}

$first = true;

$fields = array('id_project');
$values = array();
foreach($csv->data as $row)
{
    $vals = array($projectId);
    foreach($row as $key => $val)
    {
        if($first)
            $fields[] = $key;
    
        $vals[] = "'" . $val . "'";
    }
    $first = false;
    $values[] = "(" . implode(',', $vals) . ")";
}


$sql = "INSERT INTO " . $dataTable . "(" . implode(',', $fields) . ") VALUES " . implode(',', $values);
if(!$db->SQLexecute($sql)) { echo $db->GetError(); exit;}
echo '{success:true}';

?>
