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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. $fp = fopen($this->stockFile, "w");
  61. $array = array(
  62. "types" => $this->dao->get_categories(),
  63. "articles" => $this->get_articles_json_list(),
  64. );
  65. fwrite($fp, json_encode($array));
  66. fclose($fp);
  67. $this->responseArray["data"] = $result;
  68. return $this->responseArray;
  69. }
  70. public function get_articles_json_list()
  71. {
  72. $articles = $this->dao->get_articles();
  73. $formatted_articles = [];
  74. foreach ($articles as $article) {
  75. $article["type"] = $this->dao->get_categories_of_article($article["id"]);
  76. $article["image"] = $this->imageBaseUrl . $article["id"] . ".jpg";
  77. //EXPERIMENTAL
  78. $product = $this->get_openfoodfacts_product($article["code"]);
  79. if($product != null){
  80. $article["nutri-score"] = $product["nutrition_grade_fr"];
  81. if(!empty($product["ingredients_text_fr"])) $article["ingredients"] = $product["ingredients_text_fr"];
  82. else $article["ingredients"] = $product["ingredients_text"];
  83. if(!empty($product["generic_name_fr"])) $article["generic"] = $product["generic_name_fr"];
  84. else $article["generic"] = $product["generic_name"];
  85. }
  86. else {
  87. $article["nutri-score"] = null;
  88. $article["generic"] = null;
  89. $article["ingredients"] = null;
  90. }
  91. array_push($formatted_articles, $article);
  92. }
  93. return $formatted_articles;
  94. }
  95. private function get_openfoodfacts_product($barcode)
  96. {
  97. $country = 'fr';
  98. $productSlug = 'produit';
  99. $url = 'https://{country}.openfoodfacts.org/api/v0/{product}/{scan}.json';
  100. $url = str_replace(['{country}','{product}','{scan}'],[$country,$productSlug,$barcode],$url);
  101. $result = json_decode(file_get_contents($url), true);
  102. if ($result["status"] == 1) return $result["product"];
  103. else return null;
  104. }
  105. private function save_image()
  106. {
  107. $success = true;
  108. if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
  109. $uploadPath = $this->uploadBaseDir . $this->data . ".jpg";
  110. if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
  111. $this->responseArray["message"] = "Image upload success";
  112. } else {
  113. $this->responseArray["message"] = "Image upload failure: " . $uploadPath;
  114. $this->responseArray["status"] = 1;
  115. $success = false;
  116. }
  117. } else {
  118. $this->responseArray["message"] = "No valid file to send";
  119. $this->responseArray["status"] = 1;
  120. $success = false;
  121. }
  122. if ($success)
  123. return 0;
  124. else
  125. return json_encode($this->filesData) . "id: " . $this->data;
  126. }
  127. private function remove_image()
  128. {
  129. $uploadPath = $this->uploadBaseDir . $this->data["id"] . ".jpg";
  130. if (file_exists($uploadPath) && unlink($uploadPath)) {
  131. $this->responseArray["message"] = "Success: Deleted image";
  132. } else if (!file_exists($uploadPath)) {
  133. $this->responseArray["message"] = "Success: No image to delete";
  134. } else {
  135. $this->responseArray["message"] = "Success: Could not delete image";
  136. }
  137. }
  138. function create()
  139. {
  140. $result = -1;
  141. if ($this->type == "article") {
  142. $result = $this->dao->create_article($this->data);
  143. } else if ($this->type == "category") {
  144. $result = $this->dao->create_category($this->data);
  145. } else if ($this->type == "article_categories") {
  146. $result = $this->dao->remove_article_categories_of_article($this->data['id']);
  147. if ($result)
  148. $result = $this->dao->save_article_categories($this->data['id'], $this->data['categories']);
  149. } else
  150. $this->setUnknownTypeResponse();
  151. return $result;
  152. }
  153. function update()
  154. {
  155. $result = -1;
  156. if ($this->type == "article") {
  157. $result = $this->dao->update_article($this->data);
  158. } else if ($this->type == "category") {
  159. $result = $this->dao->update_category($this->data);
  160. } else
  161. $this->setUnknownTypeResponse();
  162. return $result;
  163. }
  164. function remove()
  165. {
  166. $result = -1;
  167. if ($this->type == "article") {
  168. $result = $this->dao->remove_article($this->data);
  169. if ($result != 0)
  170. $this->remove_image();
  171. } else if ($this->type == "category") {
  172. $result = $this->dao->remove_category($this->data);
  173. } else
  174. $this->setUnknownTypeResponse();
  175. if ($result == 0)
  176. return -1;
  177. else
  178. return 1;
  179. }
  180. function get()
  181. {
  182. $result = -1;
  183. if ($this->type == "article") {
  184. $result = $this->dao->get_articles();
  185. } else if ($this->type == "category") {
  186. $result = $this->dao->get_categories();
  187. } else if ($this->type == "article_categories") {
  188. $result = $this->dao->get_article_categories();
  189. } else
  190. $this->setUnknownTypeResponse();
  191. return $result;
  192. }
  193. function updateStock()
  194. {
  195. $result = 0;
  196. foreach ($this->data as $row) {
  197. $value = $row["value"];
  198. if ($this->action == "sell")
  199. $value = -$value;
  200. $result = $this->dao->update_article_stock($row["id"], $value);
  201. if (!$result)
  202. break;
  203. }
  204. return $result;
  205. }
  206. function setUnknownTypeResponse()
  207. {
  208. $this->responseArray["status"] = 1;
  209. $this->responseArray["message"] = "Error: Unknown type";
  210. }
  211. function setUnknownActionResponse()
  212. {
  213. $this->responseArray["status"] = 2;
  214. $this->responseArray["message"] = "Error: Unknown action";
  215. }
  216. function setUnknownDataResponse()
  217. {
  218. $this->responseArray["status"] = 3;
  219. $this->responseArray["message"] = "Error: Unknown data";
  220. }
  221. function setProcessingErrorResponse()
  222. {
  223. $this->responseArray["status"] = 4;
  224. $this->responseArray["message"] = "Error: Data processing error";
  225. }
  226. private function get_action($inputData)
  227. {
  228. if (!in_array($inputData["action"], $this->valid_actions))
  229. return "";
  230. else
  231. return $inputData["action"];
  232. }
  233. private function get_type($inputData)
  234. {
  235. if (!in_array($inputData["type"], $this->valid_types))
  236. return "";
  237. else
  238. return $inputData["type"];
  239. }
  240. private function get_data($inputData)
  241. {
  242. if ($inputData["data"] == null)
  243. return [];
  244. else
  245. return $inputData["data"];
  246. }
  247. }