From 07e888eb8d1a811547581770eaa82f7c96338683 Mon Sep 17 00:00:00 2001
From: nicrausaz <n.crausaz99@gmail.com>
Date: Wed, 31 Oct 2018 11:32:39 +0100
Subject: [PATCH] fix files errors

---
 canapEPFL/cible.php                         |  9 ++++++---
 canapEPFL/helpers.php                       |  3 ++-
 canapEPFL/models/PersonnalData.php          | 18 +++++++++---------
 canapEPFL/models/PersonnalDataValidator.php | 20 ++++++++++----------
 4 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/canapEPFL/cible.php b/canapEPFL/cible.php
index f213b4c..b0b108f 100644
--- a/canapEPFL/cible.php
+++ b/canapEPFL/cible.php
@@ -18,18 +18,21 @@
       // Init dataValidator
       $validator = new PersonnalDataValidator($candidateData);
 
-      if($validator->isValid()){
+      if($validator->isValid()) {
+        $candidateData->fichiers = $candidateData->tmp_fichiers;
         // Create folders
         createCandidateFolders($candidateData);
         // Upload files
-        $candidateData = uploadAllFiles($candidateData->getPaths()["pathAnnexes"], $candidateData->tmp_fichiers, $candidateData);
+        $candidateData = uploadAllFiles($candidateData->getPaths()["pathAnnexes"], $candidateData->fichiers, $candidateData);
+
         // Create JSON file and upload it
         $encodedJson = (json_encode($candidateData,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
         file_put_contents($candidateData->getPaths()["pathInfos"].'/informations.json', $encodedJson);
-        // Send mails
 
+        // Send mails
         mailToResp($candidateData->prenomApprenti, $candidateData->nomApprenti, $candidateData->formation);
         mailToApprenti($candidateData->mailApprenti, $candidateData->formation);
+
         // kill session
         $_SESSION['formError'] = false;
         unset($_SESSION);
diff --git a/canapEPFL/helpers.php b/canapEPFL/helpers.php
index 3d6341e..4d5e7e3 100644
--- a/canapEPFL/helpers.php
+++ b/canapEPFL/helpers.php
@@ -22,7 +22,7 @@ function uploadFile(&$candidateData, $pathAnnexes, $file, $name){
     $extension = strtolower(strrchr($file['name'], '.'));
     $file['name'] = $name . $extension;
     move_uploaded_file($file['tmp_name'], $pathAnnexes . $file['name']);
-    $candidateData->fichiers[$name] = $file['name'];
+    $candidateData->fichiersAnnexes[$name] = $file['name'];
 }
 
 //Crée le dossier principal est ses 2 sous-dossiers
@@ -58,6 +58,7 @@ function uploadAllFiles($pathAnnexes, $postedFiles, $candidateData){
     }
 
     unset($candidateData->tmp_fichiers);
+    unset($candidateData->fichiers);
     return $candidateData;
 }
 ?>
\ No newline at end of file
diff --git a/canapEPFL/models/PersonnalData.php b/canapEPFL/models/PersonnalData.php
index d1ccfd1..efbc49e 100644
--- a/canapEPFL/models/PersonnalData.php
+++ b/canapEPFL/models/PersonnalData.php
@@ -48,7 +48,7 @@ class PersonnalData {
     public $fichiers = [];
     public $tmp_fichiers = [];
 
-    public function __construct($postedData, $postedFiles) {
+    public function __construct ($postedData, $postedFiles) {
         $this->dateNow = date('Y-m-d_H-i-s');
         // Rempli les infos
         $this->postedData = $postedData;
@@ -85,7 +85,7 @@ class PersonnalData {
         $this->tmp_fichiers = $this->setFiles($postedFiles);
     }
 
-    private function setRepresentants(){
+    private function setRepresentants () {
         $rep1 = array("genre"=>$this->postedData['genreRep1'],"nom"=>$this->postedData['nameRep1'],"prenom"=>$this->postedData['surnameRep1'],"addresse"=> array("rue"=>$this->postedData['adrRep1'],"NPA"=>$this->postedData['NPARep1']),"telephone"=>$this->postedData['telRep1']);
         $rep2 = array("genre"=>$this->postedData['genreRep2'],"nom"=>$this->postedData['nameRep2'],"prenom"=>$this->postedData['surnameRep2'],"addresse"=> array("rue"=>$this->postedData['adrRep2'],"NPA"=>$this->postedData['NPARep2']),"telephone"=>$this->postedData['telRep2']);
 
@@ -96,34 +96,34 @@ class PersonnalData {
             array_push($this->representants, $rep2);
         }
     }
-    private function setScolarite(){
+    private function setScolarite () {
         for ($i = 1; $i <= 5; $i++) {
             if(array_key_exists('ecole'.$i, $this->postedData)){
                 array_push($this->scolarite, array("ecole"=>$this->postedData['ecole'.$i],"lieu"=>$this->postedData['lieuEcole'.$i],"niveau"=>$this->postedData['niveauEcole'.$i],"annees"=>$this->postedData['anneesEcole'.$i]));
             }
         }
     }
-    private function setActivitesPro(){
+    private function setActivitesPro () {
         for ($i = 1; $i <= 3; $i++) {
             if(array_key_exists('employeurPro'.$i, $this->postedData)){
                 array_push($this->activitesProfessionnelles,array("employeur"=>$this->postedData['employeurPro'.$i],"lieu"=>$this->postedData['lieuPro'.$i],"activite"=>$this->postedData['activitePro'.$i],"annees"=>$this->postedData['anneesPro'.$i]));
             }
         }
     }
-    private function setStages(){
+    private function setStages () {
         for ($i = 1; $i <= 4; $i++) {
             if(array_key_exists('activiteStage'.$i, $this->postedData)){
                 array_push($this->stages,array("metier"=>$this->postedData['activiteStage'.$i],"employeur"=>$this->postedData['entrepriseStage'.$i]));
             }
         }
     }
-    private function setDejacand(){
+    private function setDejacand () {
         $this->dejaCandidat = $this->postedData['dejaCand'];
         if($this->postedData['dejaCand'] == "true"){
             $this->anneeCandidature = $this->postedData['dejaCandAnnee'];
         }
     }
-    public function setLanguages($languages){
+    public function setLanguages ($languages) {
         if(isset($languages) && is_array($languages)){
             $this->connaissancesLinguistiques = $languages;
         } else {
@@ -141,14 +141,14 @@ class PersonnalData {
         return $postedFiles;
     }
 
-    public function getPaths(){
+    public function getPaths () {
         $folderName = $this->dateNow."_".$this->mailApprenti;
         $path = $this->rootpath.$this->formations[$this->formation].'/'.$folderName.'/';
         $pathInfos = $path."informations/";
         $pathAnnexes = $path."annexes/";
         return ["pathInfos"=>$pathInfos, "pathAnnexes"=>$pathAnnexes, "path"=> $path];
     }
-    public function getFormations(){
+    public function getFormations () {
         return $this->formations;
     }
 }
diff --git a/canapEPFL/models/PersonnalDataValidator.php b/canapEPFL/models/PersonnalDataValidator.php
index 17b4274..76070ac 100644
--- a/canapEPFL/models/PersonnalDataValidator.php
+++ b/canapEPFL/models/PersonnalDataValidator.php
@@ -10,7 +10,7 @@ class PersonnalDataValidator {
         $this->personnalData = $personnalData;
     }
 
-    public function errors(){
+    public function errors () {
         return $this->errors;
     }
 
@@ -128,24 +128,24 @@ class PersonnalDataValidator {
         }
     }
     private function filesValid () {
-        $filesToCheck = $this->personnalData->fichiers;
+        $filesToCheck = $this->personnalData->tmp_fichiers;
         $validExt = ['.pdf', '.jpeg', '.png', '.jpg'];
         $requiredKeys = ['photo', 'idCard', 'cv', 'lettre', 'certifs1'];
 
         foreach($filesToCheck as $key => $file) {
-            // Pour les fichiers obligatoire
-            if (in_array($file['name'], $requiredKeys)) {
-                // si fichier non fourni
-                if (!$file['error'] == 0) {
-                    $this->errors['fichiers'] = 'Fichier(s) non fourni(s)';
-                }
-            }
-
             // Check les formats
             if ($file['name'] != "") {
                 $extension = strtolower(strrchr($file['name'], '.'));
                 if(!in_array($extension, $validExt)){
                     $this->errors['fichiers'] = 'Format de fichier(s) non valable(s)';
+                } else {
+                    // Pour les fichiers obligatoire
+                    if (in_array($file['name'], $requiredKeys)) {
+                        // si fichier non fourni
+                        if (!$file['error'] == 0) {
+                            $this->errors['fichiers'] = 'Fichier(s) non fourni(s)';
+                        }
+                    }
                 }
             }
         }
-- 
GitLab