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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. require_once 'dao.php';
  3. class PostHandler
  4. {
  5. private $valid_types = ["article", "category"];
  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. } else if ($this->type == "category") {
  56. $result = $this->dao->create_category($this->data);
  57. } else
  58. $this->setUnknownTypeResponse();
  59. return $result;
  60. }
  61. function update()
  62. {
  63. $result = -1;
  64. if ($this->type == "article") {
  65. } else if ($this->type == "category") {
  66. $result = $this->dao->update_category($this->data);
  67. } else
  68. $this->setUnknownTypeResponse();
  69. return $result;
  70. }
  71. function remove()
  72. {
  73. $result = -1;
  74. if ($this->type == "article") {
  75. } else if ($this->type == "category") {
  76. $result = $this->dao->remove_category($this->data);
  77. } else
  78. $this->setUnknownTypeResponse();
  79. if ($result == 0)
  80. return -1;
  81. else
  82. return 1;
  83. }
  84. function get()
  85. {
  86. $result = -1;
  87. if ($this->type == "article") {
  88. } else if ($this->type == "category") {
  89. $result = $this->dao->get_categories();
  90. } else
  91. $this->setUnknownTypeResponse();
  92. return $result;
  93. }
  94. function setUnknownTypeResponse()
  95. {
  96. $this->responseArray["status"] = 1;
  97. $this->responseArray["message"] = "Error: Unknown type";
  98. }
  99. function setUnknownActionResponse()
  100. {
  101. $this->responseArray["status"] = 2;
  102. $this->responseArray["message"] = "Error: Unknown action";
  103. }
  104. function setUnknownDataResponse()
  105. {
  106. $this->responseArray["status"] = 3;
  107. $this->responseArray["message"] = "Error: Unknown data";
  108. }
  109. function setProcessingErrorResponse()
  110. {
  111. $this->responseArray["status"] = 4;
  112. $this->responseArray["message"] = "Error: Data processing error";
  113. }
  114. private function get_action($inputData)
  115. {
  116. if (!in_array($inputData["action"], $this->valid_actions))
  117. return "";
  118. else
  119. return $inputData["action"];
  120. }
  121. private function get_type($inputData)
  122. {
  123. if (!in_array($inputData["type"], $this->valid_types))
  124. return "";
  125. else
  126. return $inputData["type"];
  127. }
  128. private function get_data($inputData)
  129. {
  130. if ($inputData["data"] == null)
  131. return [];
  132. else
  133. return $inputData["data"];
  134. }
  135. }