blob: c8f940259fd9187db6e1b2b8fb2e4b16c4047ccf [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
Adrià Vilanova Martínez5af86512023-12-02 20:44:16 +01002/*
3 * hores
4 * Copyright (c) 2023 Adrià Vilanova Martínez
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public
17 * License along with this program.
18 * If not, see http://www.gnu.org/licenses/.
19 */
20
Copybara botbe50d492023-11-30 00:16:42 +010021require_once("core.php");
22security::checkType(security::ADMIN);
23
24if (!isset($_FILES["file"]) || $_FILES["file"]["error"] == UPLOAD_ERR_NO_FILE) {
25 security::go("users.php?msg=empty");
26}
27
28if ($_FILES["file"]["error"] !== UPLOAD_ERR_OK) {
29 security::go("users.php?msg=unexpected");
30}
31?>
32<!DOCTYPE html>
33<html>
34<head>
35 <title><?php echo $conf["appName"]; ?></title>
36 <?php visual::includeHead(); ?>
37 <link rel="stylesheet" href="css/dashboard.css">
38</head>
39<?php visual::printBodyTag(); ?>
40 <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-drawer">
41 <?php visual::includeNav(); ?>
42
43 <main class="mdl-layout__content">
44 <div class="page-content">
45 <div class="main mdl-shadow--4dp">
46 <h2>Resultado de la importación de usuarios</h2>
47
48 <?php
49 $workers = csv::csv2array($_FILES["file"]["tmp_name"]);
50
51 if ($workers === false) {
52 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>";
53 } else {
54 foreach ($workers as $worker) {
55 $username = explode("@", $worker["email"])[0];
56 $passwordHash = password_hash($worker["dni"], PASSWORD_DEFAULT);
57
58 $status = people::add($username, $worker["name"], $worker["dni"], $worker["email"], $worker["category"], $passwordHash, security::WORKER, 0);
59
60 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>";
61
62 if ($status) {
63 $personid = mysqli_insert_id($con);
64
65 $companies = explode(",", $worker["companies"]);
66
67 foreach ($companies as $company) {
68 $status2 = people::addToCompany($personid, $company);
69
70 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>";
71 }
72 }
73 }
74 }
75 ?>
76
77 <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>
78 </div>
79 </div>
80 </main>
81 </div>
82</body>
83</html>