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 8.3KB

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