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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. require_once 'dao.php';
  3. class PostHandler
  4. {
  5. private $valid_types = ["article", "category", "article_categories"];
  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 (count($this->data) > 0) {
  32. if ($this->action == "create")
  33. $result = $this->create();
  34. else if ($this->action == "update")
  35. $result = $this->update();
  36. else if ($this->action == "remove")
  37. $result = $this->remove();
  38. else
  39. $this->setUnknownActionResponse();
  40. } else if ($this->action == "get")
  41. $result = $this->get();
  42. else
  43. $this->setUnknownDataResponse();
  44. if ($this->responseArray["status"] == 0 && $result < 0) {
  45. $this->setProcessingErrorResponse();
  46. $result = -1;
  47. }
  48. $this->responseArray["data"] = $result;
  49. return $this->responseArray;
  50. }
  51. function create()
  52. {
  53. $result = -1;
  54. if ($this->type == "article") {
  55. $result = $this->dao->create_article($this->data);
  56. } else if ($this->type == "category") {
  57. $result = $this->dao->create_category($this->data);
  58. } else if ($this->type == "article_categories") {
  59. $result = $this->dao->remove_article_categories_of_article($this->data['id']);
  60. if ($result)
  61. $result = $this->dao->save_article_categories($this->data['id'], $this->data['categories']);
  62. } else
  63. $this->setUnknownTypeResponse();
  64. return $result;
  65. }
  66. function update()
  67. {
  68. $result = -1;
  69. if ($this->type == "article") {
  70. $result = $this->dao->update_article($this->data);
  71. } else if ($this->type == "category") {
  72. $result = $this->dao->update_category($this->data);
  73. } else
  74. $this->setUnknownTypeResponse();
  75. return $result;
  76. }
  77. function remove()
  78. {
  79. $result = -1;
  80. if ($this->type == "article") {
  81. $result = $this->dao->remove_article($this->data);
  82. } else if ($this->type == "category") {
  83. $result = $this->dao->remove_category($this->data);
  84. } else
  85. $this->setUnknownTypeResponse();
  86. if ($result == 0)
  87. return -1;
  88. else
  89. return 1;
  90. }
  91. function get()
  92. {
  93. $result = -1;
  94. if ($this->type == "article") {
  95. $result = $this->dao->get_articles();
  96. } else if ($this->type == "category") {
  97. $result = $this->dao->get_categories();
  98. } else if ($this->type == "article_categories") {
  99. $result = $this->dao->get_article_categories();
  100. } else
  101. $this->setUnknownTypeResponse();
  102. return $result;
  103. }
  104. function setUnknownTypeResponse()
  105. {
  106. $this->responseArray["status"] = 1;
  107. $this->responseArray["message"] = "Error: Unknown type";
  108. }
  109. function setUnknownActionResponse()
  110. {
  111. $this->responseArray["status"] = 2;
  112. $this->responseArray["message"] = "Error: Unknown action";
  113. }
  114. function setUnknownDataResponse()
  115. {
  116. $this->responseArray["status"] = 3;
  117. $this->responseArray["message"] = "Error: Unknown data";
  118. }
  119. function setProcessingErrorResponse()
  120. {
  121. $this->responseArray["status"] = 4;
  122. $this->responseArray["message"] = "Error: Data processing error";
  123. }
  124. private function get_action($inputData)
  125. {
  126. if (!in_array($inputData["action"], $this->valid_actions))
  127. return "";
  128. else
  129. return $inputData["action"];
  130. }
  131. private function get_type($inputData)
  132. {
  133. if (!in_array($inputData["type"], $this->valid_types))
  134. return "";
  135. else
  136. return $inputData["type"];
  137. }
  138. private function get_data($inputData)
  139. {
  140. if ($inputData["data"] == null)
  141. return [];
  142. else
  143. return $inputData["data"];
  144. }
  145. }