document.class.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. error_log("A1\n");
  55. $destination = "A12/" . $filename;
  56. } elseif (strstr($filename, "A3") || strstr($filename, "A4") || strstr($filename, "A5")) {
  57. error_log("A3\n");
  58. $destination = "A345/" . $filename;
  59. } else {
  60. error_log("meh\n");
  61. $destination = $filename;
  62. }
  63. error_log($destination);
  64. move_uploaded_file($document["tmp_name"], __DIR__ . "../../pdf/" . $destination);
  65. foreach ($options as $key => $value) {
  66. if (empty($value) && $key != "promo") {
  67. throw new InvalidArgumentException("La colonne `" . $key . "` doit être définie");
  68. }
  69. }
  70. $bdd = new Connector();
  71. $bdd->Insert("document", array(
  72. "rang" => $options["rang"],
  73. "promo" => $options["promo"],
  74. "libelle" => $options["libelle"],
  75. "fichier" => $destination
  76. ));
  77. }
  78. function erase()
  79. {
  80. $bdd = new Connector();
  81. $bdd->Delete("document", array(array("id", "=", $this->id)));
  82. unlink(__DIR__ . "/../../pdf/" . $this->fichier);
  83. }
  84. function changePromo($newPromo)
  85. {
  86. $bdd = new Connector();
  87. // Check if promo exists
  88. $promo = $bdd->Select("*", "promo", array(
  89. "where" => array(
  90. array("promo_id", "=", $newPromo)
  91. )
  92. ));
  93. if (!$promo) {
  94. throw new LengthException("La promo n'existe pas");
  95. }
  96. // Change promo in both object and BDD
  97. $this->promo = $newPromo;
  98. $this->libelle_promo = $promo[0]["libelle"];
  99. $bdd->Update("document", array(
  100. "promo" => $this->promo
  101. ));
  102. }
  103. function changeRank($newRank)
  104. {
  105. $bdd = new Connector();
  106. // Change promo in both object and BDD
  107. $this->rang = $newRank;
  108. $bdd->Update("document", array(
  109. "rang" => $this->rang
  110. ));
  111. }
  112. public static function toArray($document)
  113. {
  114. return array(
  115. "Rang" => $document->rang,
  116. "Promotion" => array(
  117. "id" => $document->promo,
  118. "libelle" => $document->libelle_promo
  119. ),
  120. "Libellé" => $document->libelle,
  121. "Nom du fichier" => $document->fichier,
  122. );
  123. }
  124. }