document.class.php 3.1KB

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