%PDF- %PDF-
| Direktori : /www/varak.net/_losik.varak.net/app/ |
| Current File : //www/varak.net/_losik.varak.net/app/csv-prepare-process.php |
<?php
/**
* Created by IntelliJ IDEA.
* User: mvarak
* Date: 2/19/19
* Time: 10:34 AM
*/
$target_dir = __DIR__ . '/../tmp/';
$target_file = $target_dir . uniqid("zavodnici", true) . ".txt";
$target_squad = str_replace("zavodnici", "squady", $target_file);
$files_ok = true;
$replaces = array(
"Malá pistole" => "MPi",
"Malý revolver" => "MRe",
"Revolver" => "Re",
"Pistole" => "Pi",
"Osobní obranná zbraň" => "PDW",
"Puška samonabíjecí" => "PSa",
"Puška opakovací" => "POp",
"Brokovnice samonabíjecí" => "BSa",
"Brokovnice opakovac" => "BOp",
"Brokovnice ostatní" => "BOs"
);
if (!move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
$smarty->assign("errorMessage", "Sorry, there was an error uploading your file.");
echo "Sorry, there was an error uploading your file.";
$files_ok = false;
//break;
}
elseif (!move_uploaded_file($_FILES["squads"]["tmp_name"], $target_squad))
{
$smarty->assign("errorMessage", "Sorry, there was an error uploading your file.");
echo "Sorry, there was an error uploading your file.";
$files_ok = false;
}
if($files_ok)
{
header("Content-type: text/plain");
$f = fopen($target_file, "r");
$first = true;
$shooters = array();
while($line = fgets($f))
{
if(trim($line) == "") continue;
if($first)
{
$first = false;
continue;
}
foreach ($replaces as $orig => $replace)
{
$line = str_ireplace($orig, $replace, $line);
}
$data = explode("\t", $line);
$shooter = array();
$shooter['mid'] = $data[0];
list($shooter['lastName'], $shooter['firstName']) = explode(' ', $data[1]);
$shooter['division'] = $data[2];
$shooter['email'] = $data[3];
$shooter['zp'] = $data[4];
$shooter['lex'] = $data[9];
$shooters[$data[0]] = $shooter;
}
fclose($f);
$f = fopen($target_squad, "r");
$squad = "";
$squads = 0;
while($line = fgets($f))
{
$line = trim($line);
if($line == "") continue;
if(substr($line, 0, 5) === "Squad")
{
if(substr($line, 6, 3) === "č.")
{
$sq = substr($line, 10);
}
else
$sq = 999;
$squads++;
}
else
{
list($mid) = explode("\t", $line);
$shooters[$mid]['squad'] = $sq;
}
}
//print_r($shooters);
$match_guid = trim(file_get_contents('/proc/sys/kernel/random/uuid'));
$matchArr = array(
"shooters" => $shooters,
"guid" => $match_guid,
"squads" => $squads,
"name" => $_POST['name'],
);
$data = array(
"guid" => $match_guid,
"user" => $userId,
"data" => json_encode($matchArr, JSON_PRETTY_PRINT)
);
dibi::query("INSERT INTO [registration]", $data);
header("Location: index.php?action=csv-prepare");
}
die;