blob: c8f940259fd9187db6e1b2b8fb2e4b16c4047ccf [file] [log] [blame]
<?php
/*
* hores
* Copyright (c) 2023 Adrià Vilanova Martínez
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program.
* If not, see http://www.gnu.org/licenses/.
*/
require_once("core.php");
security::checkType(security::ADMIN);
if (!isset($_FILES["file"]) || $_FILES["file"]["error"] == UPLOAD_ERR_NO_FILE) {
security::go("users.php?msg=empty");
}
if ($_FILES["file"]["error"] !== UPLOAD_ERR_OK) {
security::go("users.php?msg=unexpected");
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $conf["appName"]; ?></title>
<?php visual::includeHead(); ?>
<link rel="stylesheet" href="css/dashboard.css">
</head>
<?php visual::printBodyTag(); ?>
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-drawer">
<?php visual::includeNav(); ?>
<main class="mdl-layout__content">
<div class="page-content">
<div class="main mdl-shadow--4dp">
<h2>Resultado de la importación de usuarios</h2>
<?php
$workers = csv::csv2array($_FILES["file"]["tmp_name"]);
if ($workers === false) {
echo "<p class='mdl-color-text--red'>El formato del documento no es correcto (la cabecera debería ser: <code>".security::htmlsafe(implode(";", csv::$fields))."</code>).</p>";
} else {
foreach ($workers as $worker) {
$username = explode("@", $worker["email"])[0];
$passwordHash = password_hash($worker["dni"], PASSWORD_DEFAULT);
$status = people::add($username, $worker["name"], $worker["dni"], $worker["email"], $worker["category"], $passwordHash, security::WORKER, 0);
echo "<p class='mdl-color-text--".($status ? "green" : "red")."'>&ldquo;".security::htmlsafe($worker["name"])."&rdquo; ".($status ? "se ha importado correctamente" : " no se ha podido importar correctamente").".</p>";
if ($status) {
$personid = mysqli_insert_id($con);
$companies = explode(",", $worker["companies"]);
foreach ($companies as $company) {
$status2 = people::addToCompany($personid, $company);
echo "<p class='mdl-color-text--".($status2 ? "green" : "red")."'>".($status2 ? "Se ha" : "No se ha")." podido añadir &ldquo;".security::htmlsafe($worker["name"])."&rdquo; a la empresa con número de identificación ".(int)$company.".</p>";
}
}
}
}
?>
<a href="users.php" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--accent">Ir al listado de personas</a>
</div>
</div>
</main>
</div>
</body>
</html>