blob: a34d1908384c6deb9ebbff5ffdc6558d6027176c [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<?php
2require_once("core.php");
3security::checkType(security::ADMIN);
4
5if (!isset($_FILES["file"]) || $_FILES["file"]["error"] == UPLOAD_ERR_NO_FILE) {
6 security::go("users.php?msg=empty");
7}
8
9if ($_FILES["file"]["error"] !== UPLOAD_ERR_OK) {
10 security::go("users.php?msg=unexpected");
11}
12?>
13<!DOCTYPE html>
14<html>
15<head>
16 <title><?php echo $conf["appName"]; ?></title>
17 <?php visual::includeHead(); ?>
18 <link rel="stylesheet" href="css/dashboard.css">
19</head>
20<?php visual::printBodyTag(); ?>
21 <div class="mdl-layout mdl-js-layout mdl-layout--fixed-header mdl-layout--fixed-drawer">
22 <?php visual::includeNav(); ?>
23
24 <main class="mdl-layout__content">
25 <div class="page-content">
26 <div class="main mdl-shadow--4dp">
27 <h2>Resultado de la importación de usuarios</h2>
28
29 <?php
30 $workers = csv::csv2array($_FILES["file"]["tmp_name"]);
31
32 if ($workers === false) {
33 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>";
34 } else {
35 foreach ($workers as $worker) {
36 $username = explode("@", $worker["email"])[0];
37 $passwordHash = password_hash($worker["dni"], PASSWORD_DEFAULT);
38
39 $status = people::add($username, $worker["name"], $worker["dni"], $worker["email"], $worker["category"], $passwordHash, security::WORKER, 0);
40
41 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>";
42
43 if ($status) {
44 $personid = mysqli_insert_id($con);
45
46 $companies = explode(",", $worker["companies"]);
47
48 foreach ($companies as $company) {
49 $status2 = people::addToCompany($personid, $company);
50
51 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>";
52 }
53 }
54 }
55 }
56 ?>
57
58 <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>
59 </div>
60 </div>
61 </main>
62 </div>
63</body>
64</html>