Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dao.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. class Dao
  3. {
  4. private $conn;
  5. public function __construct()
  6. {
  7. $username = 'proximo';
  8. $password = $this->read_password();
  9. $dsn = 'mysql:dbname=proximo;host=127.0.0.1';
  10. try {
  11. $this->conn = new PDO($dsn, $username, $password, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
  12. } catch (PDOException $e) {
  13. echo "error";
  14. echo $e;
  15. }
  16. }
  17. private function read_password()
  18. {
  19. $real_path = __DIR__ . DIRECTORY_SEPARATOR . ".htpassdb";
  20. $file = fopen($real_path, "r") or die("Unable to open DB password file!");
  21. $password = fgets($file);
  22. fclose($file);
  23. return trim($password);
  24. }
  25. public function get_articles()
  26. {
  27. $sql = 'SELECT * FROM articles';
  28. $cursor = $this->conn->prepare($sql);
  29. $cursor->execute();
  30. return $cursor->fetchAll(PDO::FETCH_ASSOC);
  31. }
  32. public function get_article_categories()
  33. {
  34. $sql = 'SELECT * FROM article_categories';
  35. $cursor = $this->conn->prepare($sql);
  36. $cursor->execute();
  37. return $cursor->fetchAll(PDO::FETCH_ASSOC);
  38. }
  39. public function remove_article_categories_of_article($articleId)
  40. {
  41. $sql = 'DELETE FROM article_categories WHERE article_id=?';
  42. $cursor = $this->conn->prepare($sql);
  43. return $cursor->execute([$articleId]);
  44. }
  45. public function remove_article_categories_of_category($categoryId)
  46. {
  47. $sql = 'DELETE FROM article_categories WHERE category_id=?';
  48. $cursor = $this->conn->prepare($sql);
  49. return $cursor->execute([$categoryId]);
  50. }
  51. public function save_article_categories($articleId, $categories)
  52. {
  53. foreach ($categories as $category) {
  54. $sql = 'INSERT INTO article_categories (article_id, category_id) VALUES (?, ?)';
  55. $cursor = $this->conn->prepare($sql);
  56. $data = [$articleId, $category];
  57. $cursor->execute($data);
  58. }
  59. return 1;
  60. }
  61. public function get_categories()
  62. {
  63. $sql = 'SELECT * FROM categories';
  64. $cursor = $this->conn->prepare($sql);
  65. $cursor->execute();
  66. return $cursor->fetchAll(PDO::FETCH_ASSOC);
  67. }
  68. public function create_category($category)
  69. {
  70. $sql = 'INSERT INTO categories (name, icon) VALUES (?, ?)';
  71. $cursor = $this->conn->prepare($sql);
  72. $data = [$category["name"], $category["icon"]];
  73. $cursor->execute($data);
  74. return $this->conn->lastInsertId();
  75. }
  76. public function update_category($category)
  77. {
  78. $sql = 'UPDATE categories SET name=?, icon=? WHERE id=?';
  79. $cursor = $this->conn->prepare($sql);
  80. $data = [$category["name"], $category["icon"], $category["id"]];
  81. $cursor->execute($data);
  82. return $category["id"];
  83. }
  84. public function remove_category($id)
  85. {
  86. $sql = 'DELETE FROM categories WHERE id=?';
  87. $cursor = $this->conn->prepare($sql);
  88. $data = [$id];
  89. $result = $cursor->execute($data);
  90. if ($result) {
  91. $this->remove_article_categories_of_category($id);
  92. return $cursor->rowCount();
  93. } else
  94. return 0;
  95. }
  96. public function create_article($article)
  97. {
  98. $sql = 'INSERT INTO articles (name, description, price, code) VALUES (?, ?, ?, ?)';
  99. $cursor = $this->conn->prepare($sql);
  100. $data = [$article["name"], $article["description"], $article["price"], $article["code"]];
  101. $cursor->execute($data);
  102. return $this->conn->lastInsertId();
  103. }
  104. public function update_article($article)
  105. {
  106. $sql = 'UPDATE articles SET name=?, description=?, price=?, code=? WHERE id=?';
  107. $cursor = $this->conn->prepare($sql);
  108. $data = [$article["name"], $article["description"], $article["price"], $article["code"], $article["id"]];
  109. $cursor->execute($data);
  110. return $article["id"];
  111. }
  112. public function remove_article($id)
  113. {
  114. $sql = 'DELETE FROM articles WHERE id=?';
  115. $cursor = $this->conn->prepare($sql);
  116. $data = [$id];
  117. $result = $cursor->execute($data);
  118. if ($result) {
  119. $this->remove_article_categories_of_article($id);
  120. return $cursor->rowCount();
  121. } else
  122. return 0;
  123. }
  124. }