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

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