From cfd43a29e66b679c6a556bc331756f72bc18c8ec Mon Sep 17 00:00:00 2001
From: nicrausaz <n.crausaz99@gmail.com>
Date: Wed, 12 Dec 2018 09:24:32 +0100
Subject: [PATCH] delete files on server

---
 canapEPFL/helpers.php | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/canapEPFL/helpers.php b/canapEPFL/helpers.php
index afef05d..bf18dc9 100644
--- a/canapEPFL/helpers.php
+++ b/canapEPFL/helpers.php
@@ -1,4 +1,5 @@
 <?php
+#region [Mail Sendings]
 function mailToResp($surname, $name, $job){
   require_once("templates/mailToResp.php");
   mail($to, $subject, $message, $headers);
@@ -8,7 +9,9 @@ function mailToApprenti($to, $job){
   require_once("templates/mailToApp.php");
   mail($to, $subject, $message, $headers);
 }
+#endregion
 
+#region [Folder & Files]
 // Crée le dossier annexes
 function createFilesFolder ($rootpath, $applicantId) {
   $path = $rootpath . $applicantId . "/";
@@ -26,7 +29,6 @@ function uploadFile(&$candidateData, $pathAnnexes, $file, $key, $name){
   unset($candidateData->fichiers[$key]);
 }
 
-
 function uploadAllFiles($pathAnnexes, $postedFiles, $candidateData){
   uploadFile($candidateData, $pathAnnexes, $postedFiles['photo'], 'photo', "photo-passeport");
   uploadFile($candidateData, $pathAnnexes, $postedFiles['idCard'], 'idCard', "carte-identite");
@@ -51,6 +53,11 @@ function uploadAllFiles($pathAnnexes, $postedFiles, $candidateData){
   return $candidateData;
 }
 
+function deleteApplicantFiles ($files) {
+  
+}
+#endregion
+
 #region [DB Interactions]
 function insertDataIntoDB ($pdo, $candidateData) {
 
@@ -264,8 +271,6 @@ function validFileAccess ($pdo, $id, $sciper) {
 }
 
 function deletePostulation ($pdo, $id, $sciper, $rep1, $rep2) {
-  // TODO: delete related responsibles, files etc...
-
   // delete scolarities
   $sqlreq = "DELETE FROM scolarity WHERE applicant_applicant_id=:id";
   $query = $pdo->prepare($sqlreq);
@@ -284,6 +289,11 @@ function deletePostulation ($pdo, $id, $sciper, $rep1, $rep2) {
   $query->bindParam(':id', $id, PDO::PARAM_STR);
   $query->execute();
 
+  // delete files from server
+  $filesToRemove = getFilesById($pdo, $id);
+  foreach ($filesToRemove as $key => $file) {
+    unlink($file['file_path']);
+  }
   // delete files (DB only)
   $sqlreq = "DELETE FROM file WHERE applicant_applicant_id=:id";
   $query = $pdo->prepare($sqlreq);
@@ -336,7 +346,6 @@ function deletePostulation ($pdo, $id, $sciper, $rep1, $rep2) {
   $query->bindParam(':id2', $rep2, PDO::PARAM_STR);
   $query->execute();
 
-  // TODO: Delete files on server ?
   header('Location: viewpostulation.php');
 }
 #endregion
-- 
GitLab