file.class.php 2.3KB

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