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.

zapetteHandler.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. require_once 'dao.php';
  3. class ZapetteHandler
  4. {
  5. private $valid_actions = ["scan", "validate",];
  6. private $action;
  7. private $data;
  8. private $given_password;
  9. private $dao;
  10. private $password;
  11. private $responseArray = array(
  12. "status" => 0,
  13. "message" => "Success",
  14. "data" => "",
  15. );
  16. public function __construct($post)
  17. {
  18. $this->read_password();
  19. $this->given_password = $this->get_password($post);
  20. $this->action = $this->get_action($post);
  21. $this->data = $this->get_data($post);
  22. $this->dao = new Dao();
  23. }
  24. public function do_action()
  25. {
  26. $result = "";
  27. if ($this->is_password_valid()) {
  28. if ($this->action == "scan") {
  29. $result = $this->get_scanned_article();
  30. if (sizeof($result) == 0)
  31. $this->setUnknownCodeErrorResponse();
  32. } else if ($this->action == "validate") {
  33. $result = $this->update_stock();
  34. if (!$result)
  35. $this->setUpdateStockErrorResponse();
  36. }
  37. } else {
  38. $this->setWrongPasswordErrorResponse();
  39. }
  40. $this->responseArray["data"] = $result;
  41. return $this->responseArray;
  42. }
  43. private function read_password() {
  44. $real_path = __DIR__ . "/.htpasszapette";
  45. $fp = fopen($real_path, 'r');
  46. $this->password = trim(fread($fp, filesize($real_path)));
  47. fclose($fp);
  48. }
  49. private function is_password_valid() {
  50. return $this->given_password == $this->password;
  51. }
  52. private function get_scanned_article() {
  53. $article = [];
  54. if ($this->data != "") {
  55. $article = $this->dao->get_article_of_code($this->data);
  56. }
  57. return $article;
  58. }
  59. private function update_stock() {
  60. $result = false;
  61. if ($this->data != "") {
  62. foreach ($this->data as $row) {
  63. $result = $this->dao->update_article_stock($row["id"], $row["quantity"]);
  64. }
  65. }
  66. return $result;
  67. }
  68. function setWrongPasswordErrorResponse()
  69. {
  70. $this->responseArray["status"] = 1;
  71. $this->responseArray["message"] = "Error: Wrong password";
  72. }
  73. function setUnknownCodeErrorResponse()
  74. {
  75. $this->responseArray["status"] = 2;
  76. $this->responseArray["message"] = "Error: Unknown code scanned";
  77. }
  78. function setUpdateStockErrorResponse()
  79. {
  80. $this->responseArray["status"] = 3;
  81. $this->responseArray["message"] = "Error: Impossible to update stock";
  82. }
  83. private function get_action($inputData)
  84. {
  85. if (!in_array($inputData["action"], $this->valid_actions))
  86. return "";
  87. else
  88. return $inputData["action"];
  89. }
  90. private function get_data($inputData)
  91. {
  92. if ($inputData["data"] == null)
  93. return "";
  94. else
  95. return $inputData["data"];
  96. }
  97. private function get_password($inputData)
  98. {
  99. if ($inputData["password"] == null)
  100. return "";
  101. else
  102. return $inputData["password"];
  103. }
  104. }