promo.class.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. require_once("connector.class.php");
  3. class Promo
  4. {
  5. private $id_promo;
  6. private $libelle;
  7. function __construct($id)
  8. {
  9. $bdd = new Connector();
  10. $promo = $bdd->Select("*", "promo", array(
  11. "where" => array(
  12. array("id_promo", "=", $id)
  13. )
  14. ));
  15. if($promo == NULL)
  16. {
  17. throw new LengthException("La promo n'existe pas");
  18. }
  19. $promo = $promo[0];
  20. $this->id_promo = $promo["id_promo"];
  21. $this->libelle = $promo["libelle"];
  22. }
  23. public static function getAll()
  24. {
  25. $bdd = new Connector();
  26. $promos = $bdd->Select("*", "promo");
  27. $toReturn = array();
  28. foreach ($promos as $promo) {
  29. $doc = new Promo($promo["id_promo"]);
  30. array_push($toReturn, self::toArray($doc));
  31. }
  32. return $toReturn;
  33. }
  34. /**
  35. * @return mixed
  36. */
  37. public function getIdPromo()
  38. {
  39. return $this->id_promo;
  40. }
  41. /**
  42. * @return mixed
  43. */
  44. public function getLibelle()
  45. {
  46. return $this->libelle;
  47. }
  48. /**
  49. * @param mixed $id_promo
  50. */
  51. public function setIdPromo($id_promo)
  52. {
  53. $this->id_promo = $id_promo;
  54. }
  55. /**
  56. * @param mixed $libelle
  57. */
  58. public function setLibelle($libelle)
  59. {
  60. $this->libelle = $libelle;
  61. }
  62. function write()
  63. {
  64. $bdd = new Connector();
  65. $promo = $bdd->Select("*", "promo", array(
  66. "where" => array(
  67. array("id_promo", "=", $this->id_promo)
  68. )
  69. ));
  70. if(!$promo)
  71. {
  72. throw new UnexpectedValueException("La promo n'existe plus en base de données");
  73. }
  74. $promo = $promo[0];
  75. $attrs = get_object_vars($this);
  76. $toUpdate = array();
  77. foreach ($attrs as $key => $value) {
  78. if ($value != $promo[$key]) {
  79. $toUpdate[$key] = $value;
  80. }
  81. }
  82. $bdd->Update("promo", array(
  83. "set" => $toUpdate,
  84. "where" => array(array("id_promo", "=", $this->id_promo))
  85. ));
  86. }
  87. function erase()
  88. {
  89. $bdd = new Connector();
  90. $bdd->Delete("promo", array(array("id_promo", "=", $this->id_promo)));
  91. }
  92. public static function addPromo($promo)
  93. {
  94. $bdd = new Connector();
  95. $bdd->Insert("promo", array(
  96. "id_promo" => $promo["id"],
  97. "libelle" => $promo["libelle"]
  98. ));
  99. }
  100. public static function toArray($promo)
  101. {
  102. return array(
  103. "id" => $promo->id_promo,
  104. "Identifiant" => $promo->id_promo,
  105. "Libellé" => $promo->libelle
  106. );
  107. }
  108. }