Advertisement
Zgragselus

Untitled

Jul 6th, 2023
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. This file is subject to the terms and conditions defined in
  6. file 'LICENSE', which is part of this source code package.
  7.  
  8. © 2022 OtteIT s.r.o.
  9. All Rights Reserved.
  10.  
  11. Author: Vilem Otte <dev@otte.cz>
  12.  
  13. */
  14.  
  15. /**
  16.  * Read project (possibly filtered by ID or type)
  17.  *
  18.  * @param int Reference, Project ID to read - id
  19.  * @param int Reference, project type (see project_type table records) - type
  20.  *
  21.  * @return string JSON, result (HTTP response code), error (in case of any), project (either blank array or holding resulting record as JSON)
  22.  */
  23.  
  24. header("Access-Control-Allow-Origin: *");
  25. header("Content-Type: application/json; charset=UTF-8");
  26. header("Access-Control-Allow-Methods: POST");
  27. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  28.  
  29. require_once(__DIR__."/../db.php");
  30. require_once(__DIR__."/../util.php");
  31. require_once(__DIR__."/project.php");
  32.  
  33. $data = json_decode(file_get_contents("php://input"));
  34.  
  35. // Payload requirements
  36. $payloadError = Util::PayloadCheck($data);
  37. if ($payloadError != false)
  38. {
  39.     http_response_code(500);
  40.     echo json_encode(array("result" => 500, "error" => $payloadError));
  41.  
  42.     exit();
  43. }
  44.  
  45. // Connect to database
  46. $db = new Database();
  47.  
  48. // Read records
  49. $project = new Project($db);
  50.  
  51. if (isset($data->id))
  52. {
  53.     $project->id = $data->id;
  54. }
  55.  
  56. if (isset($data->type))
  57. {
  58.     $project->type = $data->type;
  59. }
  60.  
  61. $result = $project->Read();
  62.  
  63. if ($result != null)
  64. {
  65.     // Successfully read record
  66.     http_response_code(200);
  67.     echo json_encode(array("result" => 200, "project" => $result));
  68. }
  69. else
  70. {
  71.     if ($db->GetLastError())
  72.     {
  73.         // DB Error during record reading
  74.         http_response_code(200);
  75.         echo json_encode(array("result" => 500, "error" => $db->GetLastError()));
  76.     }
  77.     else
  78.     {
  79.         // Successfully read, but no record matching
  80.         http_response_code(200);
  81.         echo json_encode(array("result" => 200, "project" => []));
  82.     }
  83. }
  84.  
  85. // Close database connection
  86. $db->Disconnect();
  87.  
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement