2
0
Эх сурвалжийг харах

Améliore la lisibilité du code et ajoute la gestion des erreurs lors de l'importation de fichiers Excel

stany.ferer 3 долоо хоног өмнө
parent
commit
a9a77b64fe

+ 8 - 8
core/class/salaries.class.php

@@ -219,8 +219,8 @@ class salaries
     {
         // Récupération des données de l'excel au format Json
         db::query("SELECT 
-        " .DB_T_EXCEL. ".md5, 
-        " .DB_T_FILES. ".name 
+        " . DB_T_EXCEL . ".md5, 
+        " . DB_T_FILES . ".name 
         FROM " . DB_T_EXCEL . " 
         INNER JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL . ".md5 = " . DB_T_FILES . ".id 
         WHERE " . DB_T_EXCEL . ".id = :id");
@@ -277,9 +277,9 @@ class salaries
     public static function excelGetInProgress()
     {
         db::query("SELECT 
-        " .DB_T_EXCEL. ".id, 
-        " .DB_T_EXCEL. ".md5, 
-        " .DB_T_FILES. ".name 
+        " . DB_T_EXCEL . ".id, 
+        " . DB_T_EXCEL . ".md5, 
+        " . DB_T_FILES . ".name 
         FROM " . DB_T_EXCEL . " 
         INNER JOIN " . DB_T_FILES . " ON " . DB_T_EXCEL . ".md5 = " . DB_T_FILES . ".id 
         WHERE " . DB_T_EXCEL . ".inProgress = 1");
@@ -335,7 +335,7 @@ class salaries
         if (!empty($row)) {
             foreach ($row as $salaries) {
                 db::query("INSERT INTO " . DB_T_TEMP_SALARIES . " (idLocal, loginId, nom, prenom, sexe, contrat, jourEntree, lieu, actif, new, excel) "
-                . "VALUES (:idLocal, :loginId, :nom, :prenom, :sexe, :contrat, :jourEntree, :lieu, :actif, :new, :excel)");
+                    . "VALUES (:idLocal, :loginId, :nom, :prenom, :sexe, :contrat, :jourEntree, :lieu, :actif, :new, :excel)");
                 db::bind(':idLocal', $salaries["idLocal"]);
                 db::bind(':loginId', $salaries["loginId"]);
                 db::bind(':nom', $salaries["nom"]);
@@ -413,8 +413,8 @@ class salaries
             $sql = $change = NULL;
             if ($key > 0) {
                 $tmp = self::excelToMysql($value);
-                if(!is_int($tmp["idLocal"])) {
-                    alert::recError("Une valeur dans le fichie n'est pas conforme à l'attendu (Ligne " . $key . ")");
+                if (!is_int($tmp["idLocal"])) {
+                    alert::recError("Une valeur dans le fichier n'est pas conforme à l'attendu (Ligne " . $key . ")");
                     self::cleanTmp();
                     self::excelUpdateInProgress(self::lastExcel(), 0);
                     return FALSE;

+ 25 - 17
core/submit/cms.rh-import-excel.php

@@ -1,8 +1,8 @@
 <?php
 
-if (core::ifPost("from") AND core::getPost("from") == "rh-import-excel") {
+if (core::ifPost("from") and core::getPost("from") == "rh-import-excel") {
 
-    if (isset($_FILES[core::getPost("from")]['error']) AND $_FILES[core::getPost("from")]['error'] > 0) {
+    if (isset($_FILES[core::getPost("from")]['error']) and $_FILES[core::getPost("from")]['error'] > 0) {
 
         switch ($_FILES[core::getPost("from")]['error']) {
             case 1: // UPLOAD_ERR_INI_SIZE
@@ -30,19 +30,28 @@ if (core::ifPost("from") AND core::getPost("from") == "rh-import-excel") {
 
             $simpleXLSX = new simpleXLSX();
             $xlsx = $simpleXLSX->parse($_FILES[core::getPost("from")]['tmp_name']);
+
+            if ($xlsx === false) {
+                alert::recError("Erreur lors de la lecture du fichier Excel : " . $_FILES[core::getPost("from")]['name']);
+                header("Location: /rh-upload-excel.html");
+                exit();
+            }
+
             $returnXlsx = $xlsx->rows();
 
-            if(     $returnXlsx[0][0] != "ID LOCAL" 
-                    OR $returnXlsx[0][1] != "NOM" 
-                    OR $returnXlsx[0][2] != "PRENOM" 
-                    OR $returnXlsx[0][3] != "SEXE" 
-                    OR $returnXlsx[0][4] != "STATUT ACTIVITE" 
-                    OR $returnXlsx[0][5] != "PREMIERE EMBAUCHE" 
-                    OR $returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE"
-                    OR $returnXlsx[0][7] != "LOGIN ID"){
+            if (
+                $returnXlsx[0][0] != "ID LOCAL"
+                or $returnXlsx[0][1] != "NOM"
+                or $returnXlsx[0][2] != "PRENOM"
+                or $returnXlsx[0][3] != "SEXE"
+                or $returnXlsx[0][4] != "STATUT ACTIVITE"
+                or $returnXlsx[0][5] != "PREMIERE EMBAUCHE"
+                or $returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE"
+                or $returnXlsx[0][7] != "LOGIN ID"
+            ) {
 
                 alert::recError("Le fichier " . $_FILES[core::getPost("from")]['name'] . " n'est pas un fichier RH.");
-                
+
                 ($returnXlsx[0][0] != "ID LOCAL") ? alert::recError("La 1er colonne doit se nommer ID LOCAL") : "";
                 ($returnXlsx[0][1] != "NOM") ? alert::recError("La 2em colonne doit se nommer NOM") : "";
                 ($returnXlsx[0][2] != "PRENOM") ? alert::recError("La 3em colonne doit se nommer PRENOM") : "";
@@ -51,13 +60,13 @@ if (core::ifPost("from") AND core::getPost("from") == "rh-import-excel") {
                 ($returnXlsx[0][5] != "PREMIERE EMBAUCHE") ? alert::recError("La 6em colonne doit se nommer PREMIERE EMBAUCHE") : "";
                 ($returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE") ? alert::recError("La 7em colonne doit se nommer SOUS-DOMAINE DU PERSONNEL - TE") : "";
                 ($returnXlsx[0][7] != "LOGIN ID") ? alert::recError("La 8em colonne doit se nommer LOGIN ID") : "";
-                        
+
                 header("Location: /rh-upload-excel.html");
                 exit();
-            } 
-            
+            }
+
             $data["md5File"] = file::record($_FILES[core::getPost("from")]);
-            if($data["md5File"] == FALSE){
+            if ($data["md5File"] == FALSE) {
                 header("Location: /rh-upload-excel.html");
                 exit();
             }
@@ -66,7 +75,7 @@ if (core::ifPost("from") AND core::getPost("from") == "rh-import-excel") {
             $data["json"] = json_encode($returnXlsx);
             $data["date"] = core::getPost("date");
 
-            if(salaries::insertExcel($data) == FALSE){
+            if (salaries::insertExcel($data) == FALSE) {
                 alert::recError("Erreur #submit sur l'import du fichier " . $_FILES[core::getPost("from")]['name']);
                 header("Location: /rh-upload-excel.html");
                 exit();
@@ -96,4 +105,3 @@ if (core::ifPost("from") AND core::getPost("from") == "rh-import-excel") {
     header('HTTP/1.0 401 Unauthorized');
     exit();
 }
-