Browse Source

File::addDocument now properly uploads the file

Brendan Abolivier 9 years ago
parent
commit
3ab363e328
1 changed files with 32 additions and 9 deletions
  1. 32
    9
      models/file.class.php

+ 32
- 9
models/file.class.php View File

@@ -33,22 +33,45 @@ class File
33 33
         $this->fichier = $document["fichier"];
34 34
     }
35 35
 
36
-    public static function addDocument($document)
36
+    public static function addDocument($document, $options)
37 37
     {
38
-        foreach($document as $key=>$value)
38
+        $filename = $document["name"];
39
+
40
+        // Check for upload error
41
+        if($document["error"])
42
+        {
43
+            throw new InvalidArgumentException("Une erreur s'est produite lors de l'envoi du fichier (".$document["error"].")");
44
+        }
45
+
46
+        // Determining the folder to put the document in
47
+        if(strstr($filename, "A1") || strstr($filename, "A2"))
48
+        {
49
+            $destination = "A12/".$filename;
50
+        }
51
+        elseif(strstr($filename, "A3") || strstr($filename, "A4") || strstr($filename, "A5"))
52
+        {
53
+            $destination = "A345/".$filename;
54
+        }
55
+        else
56
+        {
57
+            $destination = $filename;
58
+        }
59
+
60
+        move_uploaded_file($document["tmp_name"], __DIR__."../../pdf/".$destination);
61
+
62
+        foreach($options as $key=>$value)
39 63
         {
40
-            if(empty($value) && $key != "promo" && $key != "id")
64
+            if(empty($value) && $key != "promo")
41 65
             {
42
-                throw InvalidArgumentException("La colonne `".$key."` doit être définie");
66
+                throw new InvalidArgumentException("La colonne `".$key."` doit être définie");
43 67
             }
44 68
         }
45 69
         $bdd = new Connector();
46 70
         $bdd->Insert("document", array(
47
-            "id" => $document["id"],
48
-            "rang" => $document["rang"],
49
-            "promo" => $document["promo"],
50
-            "libelle" => $document["libelle"],
51
-            "fichier" => $document["fichier"]
71
+            "rang" => $options["rang"],
72
+            "promo" => $options["promo"],
73
+            "libelle" => $options["libelle"],
74
+            "fichier" => $destination
52 75
         ));
53 76
     }
54 77