document.class.php 3.9KB

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