file.class.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. require_once("connector.class.php");
  3. class File
  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. ))[0];
  18. if(!$document)
  19. {
  20. throw new LengthException("Le fichier n'existe pas");
  21. }
  22. $this->id = $document["id"];
  23. $this->rang = $document["rang"];
  24. $this->promo = $document["promo"];
  25. $this->libelle = $document["libelle"];
  26. $this->fichier = $document["fichier"];
  27. }
  28. public static function addDocument($document)
  29. {
  30. foreach($document as $key=>$value)
  31. {
  32. if(empty($value) && $key != "promo" && $key != "id")
  33. {
  34. throw InvalidArgumentException("La colonne `".$key."` doit être définie");
  35. }
  36. }
  37. $bdd = new Connector();
  38. $bdd->Insert("document", array(
  39. "id" => $document["id"],
  40. "rang" => $document["rang"],
  41. "promo" => $document["promo"],
  42. "libelle" => $document["libelle"],
  43. "fichier" => $document["fichier"]
  44. ));
  45. }
  46. function erase()
  47. {
  48. $bdd = new Connector();
  49. $bdd->Delete("document", array(array("id", "=", $this->id)));
  50. unlink(__DIR__."/../../pdf/".$this->fichier);
  51. }
  52. function changePromo($newPromo)
  53. {
  54. $bdd = new Connector();
  55. // Check if promo exists
  56. $promo = $bdd->Select("*", "promo", array(
  57. "where" => array(
  58. array("promo_id", "=", $newPromo)
  59. )
  60. ))[0];
  61. if(!$promo)
  62. {
  63. throw new LengthException("La promo n'existe pas");
  64. }
  65. // Change promo in both object and BDD
  66. $this->promo = $newPromo;
  67. $bdd->Update("document", array(
  68. "promo" => $this->promo
  69. ));
  70. }
  71. function changeRank($newRank)
  72. {
  73. $bdd = new Connector();
  74. // Change promo in both object and BDD
  75. $this->rang = $newRank;
  76. $bdd->Update("document", array(
  77. "rang" => $this->rang
  78. ));
  79. }
  80. }