cms.rh-import-excel.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. if (core::ifPost("from") and core::getPost("from") == "rh-import-excel") {
  3. if (isset($_FILES[core::getPost("from")]['error']) and $_FILES[core::getPost("from")]['error'] > 0) {
  4. switch ($_FILES[core::getPost("from")]['error']) {
  5. case 1: // UPLOAD_ERR_INI_SIZE
  6. alert::recError("Le fichier dépasse la limite autorisée par le serveur (fichier php.ini) !");
  7. break;
  8. case 2: // UPLOAD_ERR_FORM_SIZE
  9. alert::recError("Le fichier dépasse la limite autorisée dans le formulaire HTML !");
  10. break;
  11. case 3: // UPLOAD_ERR_PARTIAL
  12. alert::recError("L'envoi du fichier a été interrompu pendant le transfert !");
  13. break;
  14. case 4: // UPLOAD_ERR_NO_FILE
  15. alert::recError("Vous n'avez pas chargé de fichier");
  16. break;
  17. }
  18. } elseif ($_FILES[core::getPost("from")]['type'] != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
  19. alert::recError("Seuls les fichiers Excel au format xlsx sont acceptés");
  20. header("Location: /rh-upload-excel.html");
  21. exit();
  22. } else {
  23. if (file_exists($_FILES[core::getPost("from")]['tmp_name'])) {
  24. $data = array();
  25. // Vérification si le fichier est protégé par mot de passe
  26. $fileContent = file_get_contents($_FILES[core::getPost("from")]['tmp_name']);
  27. if (strpos($fileContent, 'EncryptedPackage') !== false || strpos($fileContent, 'encryption') !== false) {
  28. alert::recError("Le fichier Excel est protégé par un mot de passe. Veuillez retirer la protection avant de l'importer.");
  29. header("Location: /rh-upload-excel.html");
  30. exit();
  31. }
  32. $simpleXLSX = new simpleXLSX();
  33. $xlsx = $simpleXLSX->parse($_FILES[core::getPost("from")]['tmp_name']);
  34. if ($xlsx === false) {
  35. $errorMsg = simpleXLSX::parseError();
  36. // Détection spécifique des fichiers protégés par mot de passe
  37. if (stripos($errorMsg, 'encrypt') !== false || stripos($errorMsg, 'password') !== false) {
  38. alert::recError("Le fichier Excel est protégé par un mot de passe. Veuillez retirer la protection avant de l'importer.");
  39. } elseif ($errorMsg) {
  40. alert::recError("Erreur lors de la lecture du fichier Excel : " . $errorMsg);
  41. } else {
  42. alert::recError("Erreur lors de la lecture du fichier Excel : " . $_FILES[core::getPost("from")]['name']);
  43. }
  44. header("Location: /rh-upload-excel.html");
  45. exit();
  46. }
  47. $returnXlsx = $xlsx->rows();
  48. if (
  49. $returnXlsx[0][0] != "ID LOCAL"
  50. or $returnXlsx[0][1] != "NOM"
  51. or $returnXlsx[0][2] != "PRENOM"
  52. or $returnXlsx[0][3] != "SEXE"
  53. or $returnXlsx[0][4] != "STATUT ACTIVITE"
  54. or $returnXlsx[0][5] != "PREMIERE EMBAUCHE"
  55. or $returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE"
  56. or $returnXlsx[0][7] != "LOGIN ID"
  57. ) {
  58. alert::recError("Le fichier " . $_FILES[core::getPost("from")]['name'] . " n'est pas un fichier RH.");
  59. ($returnXlsx[0][0] != "ID LOCAL") ? alert::recError("La 1er colonne doit se nommer ID LOCAL") : "";
  60. ($returnXlsx[0][1] != "NOM") ? alert::recError("La 2em colonne doit se nommer NOM") : "";
  61. ($returnXlsx[0][2] != "PRENOM") ? alert::recError("La 3em colonne doit se nommer PRENOM") : "";
  62. ($returnXlsx[0][3] != "SEXE") ? alert::recError("La 4em colonne doit se nommer SEXE") : "";
  63. ($returnXlsx[0][4] != "STATUT ACTIVITE") ? alert::recError("La 5em colonne doit se nommer STATUT ACTIVITE") : "";
  64. ($returnXlsx[0][5] != "PREMIERE EMBAUCHE") ? alert::recError("La 6em colonne doit se nommer PREMIERE EMBAUCHE") : "";
  65. ($returnXlsx[0][6] != "SOUS-DOMAINE DU PERSONNEL - TE") ? alert::recError("La 7em colonne doit se nommer SOUS-DOMAINE DU PERSONNEL - TE") : "";
  66. ($returnXlsx[0][7] != "LOGIN ID") ? alert::recError("La 8em colonne doit se nommer LOGIN ID") : "";
  67. header("Location: /rh-upload-excel.html");
  68. exit();
  69. }
  70. $data["md5File"] = file::record($_FILES[core::getPost("from")]);
  71. if ($data["md5File"] == FALSE) {
  72. header("Location: /rh-upload-excel.html");
  73. exit();
  74. }
  75. $data["nbSalaries"] = count($returnXlsx) - 1;
  76. $data["json"] = json_encode($returnXlsx);
  77. $data["date"] = core::getPost("date");
  78. if (salaries::insertExcel($data) == FALSE) {
  79. alert::recError("Erreur #submit sur l'import du fichier " . $_FILES[core::getPost("from")]['name']);
  80. header("Location: /rh-upload-excel.html");
  81. exit();
  82. }
  83. json::create("excel");
  84. historique::recRef("/rh-historique-excel.html");
  85. historique::add(array(
  86. "idType" => historique::getIdRef("ACTION"),
  87. "idUser" => session::getId(),
  88. "idPage" => historique::getIdRef("/rh-historique-excel.html"),
  89. "log" => "Import du fichier " . $_FILES[core::getPost("from")]['name']
  90. ));
  91. alert::recSuccess("Le fichier " . $_FILES[core::getPost("from")]['name'] . " a été chargé avec succès");
  92. header("Location: /rh-historique-excel.html");
  93. exit();
  94. } else {
  95. alert::recError("Erreur lors du chargement du fichier : " . $_FILES[core::getPost("from")]['name']);
  96. }
  97. }
  98. header("Location: /rh-upload-excel.html");
  99. exit();
  100. } else {
  101. header('HTTP/1.0 401 Unauthorized');
  102. exit();
  103. }