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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. require_once 'dao.php';
  3. class PostHandler
  4. {
  5. private $valid_types = ["article", "category", "article_categories", 'image'];
  6. private $valid_actions = ["create", "update", "remove", "get"];
  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 $responseArray = array(
  15. "status" => 0,
  16. "message" => "Success",
  17. "data" => "",
  18. );
  19. public function __construct($post, $files)
  20. {
  21. $this->filesData = $files;
  22. $this->action = $this->get_action($post);
  23. $this->type = $this->get_type($post);
  24. $this->data = $this->get_data($post);
  25. $this->postData = $post;
  26. $this->dao = new Dao();
  27. }
  28. public function do_action()
  29. {
  30. $result = -1;
  31. if ($this->type == "image") {
  32. $result = $this->save_image();
  33. } else if (count($this->data) > 0) {
  34. if ($this->action == "create")
  35. $result = $this->create();
  36. else if ($this->action == "update")
  37. $result = $this->update();
  38. else if ($this->action == "remove")
  39. $result = $this->remove();
  40. else
  41. $this->setUnknownActionResponse();
  42. } else if ($this->action == "get")
  43. $result = $this->get();
  44. else
  45. $this->setUnknownDataResponse();
  46. if ($this->responseArray["status"] == 0 && $result < 0) {
  47. $this->setProcessingErrorResponse();
  48. $result = -1;
  49. }
  50. $this->responseArray["data"] = $result;
  51. return $this->responseArray;
  52. }
  53. private function save_image()
  54. {
  55. $success = true;
  56. if ($this->filesData["image"]["size"] > 0 && $this->data != null) {
  57. $uploadPath = $this->uploadBaseDir.$this->data.".jpg";
  58. if (move_uploaded_file($this->filesData["image"]["tmp_name"], $uploadPath)) {
  59. $this->responseArray["message"] = "Image upload success";
  60. } else {
  61. $this->responseArray["message"] = "Image upload failure: ". $uploadPath;
  62. $this->responseArray["status"] = 1;
  63. $success = false;
  64. }
  65. } else {
  66. $this->responseArray["message"] = "No valid file to send";
  67. $this->responseArray["status"] = 1;
  68. $success = false;
  69. }
  70. if ($success)
  71. return 0;
  72. else
  73. return json_encode($this->filesData)."id: ".$this->data;
  74. }
  75. private function remove_image() {
  76. $uploadPath = $this->uploadBaseDir.$this->data["id"].".jpg";
  77. if (file_exists($uploadPath) && unlink($uploadPath)) {
  78. $this->responseArray["message"] = "Success: Deleted image";
  79. } else if (!file_exists($uploadPath)) {
  80. $this->responseArray["message"] = "Success: No image to delete";
  81. } else {
  82. $this->responseArray["message"] = "Success: Could not delete image";
  83. }
  84. }
  85. function create()
  86. {
  87. $result = -1;
  88. if ($this->type == "article") {
  89. $result = $this->dao->create_article($this->data);
  90. } else if ($this->type == "category") {
  91. $result = $this->dao->create_category($this->data);
  92. } else if ($this->type == "article_categories") {
  93. $result = $this->dao->remove_article_categories_of_article($this->data['id']);
  94. if ($result)
  95. $result = $this->dao->save_article_categories($this->data['id'], $this->data['categories']);
  96. } else
  97. $this->setUnknownTypeResponse();
  98. return $result;
  99. }
  100. function update()
  101. {
  102. $result = -1;
  103. if ($this->type == "article") {
  104. $result = $this->dao->update_article($this->data);
  105. } else if ($this->type == "category") {
  106. $result = $this->dao->update_category($this->data);
  107. } else
  108. $this->setUnknownTypeResponse();
  109. return $result;
  110. }
  111. function remove()
  112. {
  113. $result = -1;
  114. if ($this->type == "article") {
  115. $result = $this->dao->remove_article($this->data);
  116. if ($result != 0)
  117. $this->remove_image();
  118. } else if ($this->type == "category") {
  119. $result = $this->dao->remove_category($this->data);
  120. } else
  121. $this->setUnknownTypeResponse();
  122. if ($result == 0)
  123. return -1;
  124. else
  125. return 1;
  126. }
  127. function get()
  128. {
  129. $result = -1;
  130. if ($this->type == "article") {
  131. $result = $this->dao->get_articles();
  132. } else if ($this->type == "category") {
  133. $result = $this->dao->get_categories();
  134. } else if ($this->type == "article_categories") {
  135. $result = $this->dao->get_article_categories();
  136. } else
  137. $this->setUnknownTypeResponse();
  138. return $result;
  139. }
  140. function setUnknownTypeResponse()
  141. {
  142. $this->responseArray["status"] = 1;
  143. $this->responseArray["message"] = "Error: Unknown type";
  144. }
  145. function setUnknownActionResponse()
  146. {
  147. $this->responseArray["status"] = 2;
  148. $this->responseArray["message"] = "Error: Unknown action";
  149. }
  150. function setUnknownDataResponse()
  151. {
  152. $this->responseArray["status"] = 3;
  153. $this->responseArray["message"] = "Error: Unknown data";
  154. }
  155. function setProcessingErrorResponse()
  156. {
  157. $this->responseArray["status"] = 4;
  158. $this->responseArray["message"] = "Error: Data processing error";
  159. }
  160. private function get_action($inputData)
  161. {
  162. if (!in_array($inputData["action"], $this->valid_actions))
  163. return "";
  164. else
  165. return $inputData["action"];
  166. }
  167. private function get_type($inputData)
  168. {
  169. if (!in_array($inputData["type"], $this->valid_types))
  170. return "";
  171. else
  172. return $inputData["type"];
  173. }
  174. private function get_data($inputData)
  175. {
  176. if ($inputData["data"] == null)
  177. return [];
  178. else
  179. return $inputData["data"];
  180. }
  181. }