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.

postHandler.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. require_once 'dao.php';
  3. class PostHandler
  4. {
  5. private $valid_types = ["article", "category", "article_categories", "image", "stock"];
  6. private $valid_actions = ["create", "update", "remove", "get", "buy", "sell"];
  7. private $action;
  8. private $type;
  9. private $postData;
  10. private $filesData;
  11. private $data;
  12. private $dao;
  13. private $uploadBaseDir = '../uploaded_images/';
  14. private $stockFile = "../data/stock-v2.json";
  15. private $imageBaseUrl = "https://etud.insa-toulouse.fr/~proximo/uploaded_images/";
  16. private $responseArray = array(
  17. "status" => 0,
  18. "message" => "Success",
  19. "data" => "",
  20. );
  21. public function __construct($post, $files)
  22. {
  23. $this->filesData = $files;
  24. $this->action = $this->get_action($post);
  25. $this->type = $this->get_type($post);
  26. $this->data = $this->get_data($post);
  27. $this->postData = $post;
  28. $this->dao = new Dao();
  29. }
  30. public function do_action()
  31. {
  32. $result = -1;
  33. if ($this->type == "image") {
  34. $result = $this->save_image();
  35. } else if ($this->type == "stock") {
  36. $result = $this->updateStock();
  37. } else if (count($this->data) > 0) {
  38. if ($this->action == "create")
  39. $result = $this->create();
  40. else if ($this->action == "update")
  41. $result = $this->update();
  42. else if ($this->action == "remove")
  43. $result = $this->remove();
  44. else
  45. $this->setUnknownActionResponse();
  46. } else if ($this->action == "get")
  47. $result = $this->get();
  48. else
  49. $this->setUnknownDataResponse();
  50. if ($this->responseArray["status"] == 0 && $result < 0) {
  51. $this->setProcessingErrorResponse();
  52. $result = -1;
  53. }
  54. $this->responseArray["data"] = $result;
  55. return $this->responseArray;
  56. }
  57. public function write_json()
  58. {
  59. $result = 0;
  60. $array = array(
  61. "types" => $this->dao->get_categories(),
  62. "articles" => $this->get_articles_json_list(),
  63. );
  64. $fp = fopen($this->stockFile, "w");
  65. if ($fp) {
  66. fwrite($fp, json_encode($array));
  67. fclose($fp);
  68. $this->responseArray["data"] = $result;
  69. } else {
  70. $this->setFileErrorResponse();
  71. }
  72. return $this->responseArray;
  73. }
  74. public function get_articles_json_list()
  75. {
  76. $articles = $this->dao->get_articles();
  77. $formatted_articles = [];
  78. foreach ($articles as $article) {
  79. $article["type"] = $this->dao->get_categories_of_article($article["id"]);
  80. $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
  81. array_push($formatted_articles, $article);
  82. }
  83. return $formatted_articles;
  84. }
  85. private function save_image()
  86. {
  87. $success = true;
  88. if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
  89. $uploadPath = $this->uploadBaseDir . $this->data . ".jpg";
  90. if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
  91. $this->responseArray["message"] = "Image upload success";
  92. } else {
  93. $this->responseArray["message"] = "Image upload failure: " . $uploadPath;
  94. $this->responseArray["status"] = 1;
  95. $success = false;
  96. }
  97. } else {
  98. $this->responseArray["message"] = "No valid file to send";
  99. $this->responseArray["status"] = 1;
  100. $success = false;
  101. }
  102. if ($success)
  103. return 0;
  104. else
  105. return json_encode($this->filesData) . "id: " . $this->data;
  106. }
  107. private function remove_image()
  108. {
  109. $uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
  110. if (file_exists($uploadPath) && unlink($uploadPath)) {
  111. $this->responseArray["message"] = "Success: Deleted image";
  112. } else if (!file_exists($uploadPath)) {
  113. $this->responseArray["message"] = "Success: No image to delete";
  114. } else {
  115. $this->responseArray["message"] = "Success: Could not delete image";
  116. }
  117. }
  118. function create()
  119. {
  120. $result = -1;
  121. if ($this->type == "article") {
  122. $result = $this->dao->create_article($this->data);
  123. } else if ($this->type == "category") {
  124. $result = $this->dao->create_category($this->data);
  125. } else if ($this->type == "article_categories") {
  126. $result = $this->dao->remove_article_categories_of_article($this->data['id']);
  127. if ($result)
  128. $result = $this->dao->save_article_categories($this->data['id'], $this->data['categories']);
  129. } else
  130. $this->setUnknownTypeResponse();
  131. return $result;
  132. }
  133. function update()
  134. {
  135. $result = -1;
  136. if ($this->type == "article") {
  137. $result = $this->dao->update_article($this->data);
  138. } else if ($this->type == "category") {
  139. $result = $this->dao->update_category($this->data);
  140. } else
  141. $this->setUnknownTypeResponse();
  142. return $result;
  143. }
  144. function remove()
  145. {
  146. $result = -1;
  147. if ($this->type == "article") {
  148. $result = $this->dao->remove_article($this->data);
  149. if ($result != 0)
  150. $this->remove_image();
  151. } else if ($this->type == "category") {
  152. $result = $this->dao->remove_category($this->data);
  153. } else
  154. $this->setUnknownTypeResponse();
  155. if ($result == 0)
  156. return -1;
  157. else
  158. return 1;
  159. }
  160. function get()
  161. {
  162. $result = -1;
  163. if ($this->type == "article") {
  164. $result = $this->dao->get_articles();
  165. } else if ($this->type == "category") {
  166. $result = $this->dao->get_categories();
  167. } else if ($this->type == "article_categories") {
  168. $result = $this->dao->get_article_categories();
  169. } else
  170. $this->setUnknownTypeResponse();
  171. return $result;
  172. }
  173. function updateStock()
  174. {
  175. $result = 0;
  176. foreach ($this->data as $row) {
  177. $value = $row["value"];
  178. if ($this->action == "sell")
  179. $value = -$value;
  180. $result = $this->dao->update_article_stock($row["id"], $value);
  181. if (!$result)
  182. break;
  183. }
  184. return $result;
  185. }
  186. function setUnknownTypeResponse()
  187. {
  188. $this->responseArray["status"] = 1;
  189. $this->responseArray["message"] = "Error: Unknown type";
  190. }
  191. function setUnknownActionResponse()
  192. {
  193. $this->responseArray["status"] = 2;
  194. $this->responseArray["message"] = "Error: Unknown action";
  195. }
  196. function setUnknownDataResponse()
  197. {
  198. $this->responseArray["status"] = 3;
  199. $this->responseArray["message"] = "Error: Unknown data";
  200. }
  201. function setProcessingErrorResponse()
  202. {
  203. $this->responseArray["status"] = 4;
  204. $this->responseArray["message"] = "Error: Data processing error";
  205. }
  206. function setFileErrorResponse()
  207. {
  208. $this->responseArray["status"] = 5;
  209. $this->responseArray["message"] = "Error: Could not open file";
  210. }
  211. private function get_action($inputData)
  212. {
  213. if (!in_array($inputData["action"], $this->valid_actions))
  214. return "";
  215. else
  216. return $inputData["action"];
  217. }
  218. private function get_type($inputData)
  219. {
  220. if (!in_array($inputData["type"], $this->valid_types))
  221. return "";
  222. else
  223. return $inputData["type"];
  224. }
  225. private function get_data($inputData)
  226. {
  227. if ($inputData["data"] == null)
  228. return [];
  229. else
  230. return $inputData["data"];
  231. }
  232. }