attachments.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. header("Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once '../config/database.php';
  10. require_once '../models/Attachment.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $attachment = new Attachment($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $attachment->id = $_GET['id'];
  19. $attachment->readOne();
  20. if($attachment->item_id != null) {
  21. $attachment_arr = array(
  22. "id" => $attachment->id,
  23. "item_id" => $attachment->item_id,
  24. "filename" => $attachment->filename,
  25. "original_name" => $attachment->original_name,
  26. "file_type" => $attachment->file_type,
  27. "file_path" => $attachment->file_path,
  28. "file_size" => $attachment->file_size,
  29. "mime_type" => $attachment->mime_type,
  30. "created_at" => $attachment->created_at
  31. );
  32. http_response_code(200);
  33. echo json_encode($attachment_arr);
  34. } else {
  35. http_response_code(404);
  36. echo json_encode(array("message" => "Attachment not found."));
  37. }
  38. } elseif(isset($_GET['item_id'])) {
  39. $attachment->item_id = $_GET['item_id'];
  40. $stmt = $attachment->read();
  41. $num = $stmt->rowCount();
  42. if($num > 0) {
  43. $attachments_arr = array();
  44. $attachments_arr["records"] = array();
  45. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  46. extract($row);
  47. $attachment_item = array(
  48. "id" => $id,
  49. "item_id" => $item_id,
  50. "filename" => $filename,
  51. "original_name" => $original_name,
  52. "file_type" => $file_type,
  53. "file_path" => $file_path,
  54. "file_size" => $file_size,
  55. "mime_type" => $mime_type,
  56. "created_at" => $created_at
  57. );
  58. array_push($attachments_arr["records"], $attachment_item);
  59. }
  60. http_response_code(200);
  61. echo json_encode($attachments_arr);
  62. } else {
  63. http_response_code(200);
  64. echo json_encode(array("records" => array()));
  65. }
  66. } else {
  67. http_response_code(400);
  68. echo json_encode(array("message" => "Missing item_id parameter."));
  69. }
  70. break;
  71. case 'POST':
  72. if(isset($_FILES['attachment']) && isset($_POST['item_id']) && isset($_POST['file_type'])) {
  73. $item_id = $_POST['item_id'];
  74. $file_type = $_POST['file_type'];
  75. $result = $attachment->uploadFile($_FILES['attachment'], $item_id, $file_type);
  76. if($result['success']) {
  77. http_response_code(201);
  78. echo json_encode(array(
  79. "message" => "Attachment uploaded successfully.",
  80. "id" => $result['id'],
  81. "url" => $result['url']
  82. ));
  83. } else {
  84. http_response_code(400);
  85. echo json_encode(array("message" => $result['message']));
  86. }
  87. } else {
  88. http_response_code(400);
  89. echo json_encode(array("message" => "Missing required parameters."));
  90. }
  91. break;
  92. case 'DELETE':
  93. if(isset($_GET['id'])) {
  94. $attachment->id = $_GET['id'];
  95. if($attachment->delete()) {
  96. http_response_code(200);
  97. echo json_encode(array("message" => "Attachment was deleted."));
  98. } else {
  99. http_response_code(503);
  100. echo json_encode(array("message" => "Unable to delete attachment."));
  101. }
  102. } else {
  103. http_response_code(400);
  104. echo json_encode(array("message" => "Unable to delete attachment. ID is missing."));
  105. }
  106. break;
  107. default:
  108. http_response_code(405);
  109. echo json_encode(array("message" => "Method not allowed."));
  110. break;
  111. }
  112. ?>