From 162c01911e2dcf66b8d3b9088c2aba6019add20d Mon Sep 17 00:00:00 2001
From: nicrausaz <n.crausaz99@gmail.com>
Date: Tue, 5 Mar 2019 15:29:49 +0100
Subject: [PATCH] file update working stable

---
 canapEPFL/cible.php                          |  2 +-
 canapEPFL/helpers.php                        | 10 ++++------
 canapEPFL/templates/confirmationTextEdit.php |  7 +++++++
 canapEPFL/templates/errorText.php            |  1 -
 canapEPFL/templates/errorTextEdit.php        | 16 ++++++++++++++++
 5 files changed, 28 insertions(+), 8 deletions(-)
 create mode 100644 canapEPFL/templates/confirmationTextEdit.php
 create mode 100644 canapEPFL/templates/errorTextEdit.php

diff --git a/canapEPFL/cible.php b/canapEPFL/cible.php
index 92ee6ac..55e1112 100644
--- a/canapEPFL/cible.php
+++ b/canapEPFL/cible.php
@@ -22,13 +22,13 @@
         if (isset($_GET['edit'])) {
           // update data
           updatePostulation($pdo, $candidateData, $_FILES);
+          include("templates/confirmationText.php");
         } else {
           // write infos in DB
           $candidateId = insertDataIntoDB($pdo, $candidateData);
 
           // create dir + move files in it
           $filesPath = createFilesFolder($candidateData->rootpath, $candidateId);
-          // TODO: check if i can pass only candidateData
           $candidateData = uploadAllFiles($filesPath, $candidateData->fichiers, $candidateData);
 
           // insert files in DB
diff --git a/canapEPFL/helpers.php b/canapEPFL/helpers.php
index 73989d1..8e6093b 100644
--- a/canapEPFL/helpers.php
+++ b/canapEPFL/helpers.php
@@ -476,23 +476,21 @@ function updatePostulation ($pdo, $posted_data) {
   insertTrainings($pdo, $to_add, $posted_data->id);
 
   // Update files
-  $sqlreq = "UPDATE file SET file_path=:file_path
+  $sqlreq = "UPDATE file SET file_path=:file_path, file_name=:file_name
   WHERE fk_applicant_id=:applicant_id AND file_id=:file_id";
 
   foreach ($posted_files as $key => $file) {
     $file_data = getOneFile($pdo, $file['id'], $posted_data->id);
     $extension = strtolower("." . pathinfo($file['name'])['extension']);
+    $filename = $key . $extension;
     $path = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_data['file_path']) . $extension;
 
-    if (!move_uploaded_file($file['tmp_name'], $path)) {
-      // Erreur: Le fichier est ouvert
-      echo "err";
-      $_SESSION['edit_error']['files'] = "Erreur lors du remplacement d'un fichier, réessayez plus tard.";
-    } else {
+    if (move_uploaded_file($file['tmp_name'], $path)) {
       // success, update db
       $query = $pdo->prepare($sqlreq);
       $query->bindParam(':applicant_id', $posted_data->id, PDO::PARAM_INT);
       $query->bindParam(':file_path', $path, PDO::PARAM_STR);
+      $query->bindParam(':file_name', $filename, PDO::PARAM_STR);
       $query->bindParam(':file_id', $file_data['file_id'], PDO::PARAM_STR);
       $query->execute();
       // TODO: old delete file ?
diff --git a/canapEPFL/templates/confirmationTextEdit.php b/canapEPFL/templates/confirmationTextEdit.php
new file mode 100644
index 0000000..a04f096
--- /dev/null
+++ b/canapEPFL/templates/confirmationTextEdit.php
@@ -0,0 +1,7 @@
+<main id="main" role="main" class="content container-grid">
+  <p>Vos modifications ont bien été enregistrées.</p>
+  <p>Vous avez la possibilité de consulter votre postulation depuis la page d'accueil de ce formulaire.</p>
+  <button type="button" class="btn btn-primary btn-sm mt-3 mb-2" id="retourHome">Terminer</button>
+  <br>
+  <button type="button" class="btn btn-primary btn-block btn-sm mt-3 mb-2" id="viewPostB">Consulter ma postulation</button>
+</main>
\ No newline at end of file
diff --git a/canapEPFL/templates/errorText.php b/canapEPFL/templates/errorText.php
index 000100d..7c45fa8 100644
--- a/canapEPFL/templates/errorText.php
+++ b/canapEPFL/templates/errorText.php
@@ -5,7 +5,6 @@
       if (!$_POST && !$_FILES) {
         echo "<li>Fichiers fournis trop volumineux ! <small>(Taille recommandée env. 2Mo)</small></li>";
       } else {
-        echo isset($_SESSION['edit_error']['files']) ? $_SESSION['edit_error']['files'] : '';
         foreach($validator->errors() as $error => $errorVal) {
           echo "<li>$errorVal</li>";
         }
diff --git a/canapEPFL/templates/errorTextEdit.php b/canapEPFL/templates/errorTextEdit.php
new file mode 100644
index 0000000..4f93b7e
--- /dev/null
+++ b/canapEPFL/templates/errorTextEdit.php
@@ -0,0 +1,16 @@
+<main id="main" role="main" class="content container-grid">
+  <h5 style="color: red;">Des erreurs se sont produites, merci de remplir tous les champs obligatoires!</h5>
+  <div id="formErrorsDiv" style="padding-bottom: 20px;">
+    <?php
+      if (!$_POST && !$_FILES) {
+        echo "<li>Fichiers fournis trop volumineux ! <small>(Taille recommandée env. 2Mo)</small></li>";
+      } else {
+        echo "<li>" . $_SESSION['edit_error'] . "</li>";
+        foreach($validator->errors() as $error => $errorVal) {
+          echo "<li>$errorVal</li>";
+        }
+      }
+    ?>
+  </div>
+  <button type="button" class="btn btn-primary btn-sm mt-3 mb-2" id="retourFormulaire">Retour au formulaire</button>
+</main>
\ No newline at end of file
-- 
GitLab