document.class.php 4.5KB

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