document.class.php 4.1KB

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