file.class.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. require_once("connector.class.php");
  3. class File
  4. {
  5. private $id;
  6. private $rang;
  7. private $promo;
  8. private $libelle;
  9. private $fichier;
  10. function __construct($id)
  11. {
  12. $bdd = new Connector();
  13. $document = $bdd->Select("*", "document", array(
  14. "where" => array(
  15. array("id", "=", $id)
  16. )
  17. ));
  18. if(!$document)
  19. {
  20. throw new LengthException("Le fichier n'existe pas");
  21. }
  22. $document = $document[0];
  23. $this->id = $document["id"];
  24. $this->rang = $document["rang"];
  25. $this->promo = $document["promo"];
  26. $this->libelle = $document["libelle"];
  27. $this->fichier = $document["fichier"];
  28. }
  29. public static function addDocument($document, $options)
  30. {
  31. $filename = $document["name"];
  32. // Check for upload error
  33. if($document["error"])
  34. {
  35. throw new InvalidArgumentException("Une erreur s'est produite lors de l'envoi du fichier (".$document["error"].")");
  36. }
  37. // Determining the folder to put the document in
  38. if(strstr($filename, "A1") || strstr($filename, "A2"))
  39. {
  40. $destination = "A12/".$filename;
  41. }
  42. elseif(strstr($filename, "A3") || strstr($filename, "A4") || strstr($filename, "A5"))
  43. {
  44. $destination = "A345/".$filename;
  45. }
  46. else
  47. {
  48. $destination = $filename;
  49. }
  50. move_uploaded_file($document["tmp_name"], __DIR__."../../pdf/".$destination);
  51. foreach($options as $key=>$value)
  52. {
  53. if(empty($value) && $key != "promo")
  54. {
  55. throw new InvalidArgumentException("La colonne `".$key."` doit être définie");
  56. }
  57. }
  58. $bdd = new Connector();
  59. $bdd->Insert("document", array(
  60. "rang" => $options["rang"],
  61. "promo" => $options["promo"],
  62. "libelle" => $options["libelle"],
  63. "fichier" => $destination
  64. ));
  65. }
  66. function erase()
  67. {
  68. $bdd = new Connector();
  69. $bdd->Delete("document", array(array("id", "=", $this->id)));
  70. unlink(__DIR__."/../../pdf/".$this->fichier);
  71. }
  72. function changePromo($newPromo)
  73. {
  74. $bdd = new Connector();
  75. // Check if promo exists
  76. $promo = $bdd->Select("*", "promo", array(
  77. "where" => array(
  78. array("promo_id", "=", $newPromo)
  79. )
  80. ));
  81. if(!$promo)
  82. {
  83. throw new LengthException("La promo n'existe pas");
  84. }
  85. // Change promo in both object and BDD
  86. $this->promo = $newPromo;
  87. $bdd->Update("document", array(
  88. "promo" => $this->promo
  89. ));
  90. }
  91. function changeRank($newRank)
  92. {
  93. $bdd = new Connector();
  94. // Change promo in both object and BDD
  95. $this->rang = $newRank;
  96. $bdd->Update("document", array(
  97. "rang" => $this->rang
  98. ));
  99. }
  100. }