contact_persons.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, PUT, 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/ContactPerson.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $contactPerson = new ContactPerson($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $contactPerson->id = $_GET['id'];
  19. $contactPerson->readOne();
  20. if($contactPerson->first_name != null) {
  21. $contact_arr = array(
  22. "id" => $contactPerson->id,
  23. "client_id" => $contactPerson->client_id,
  24. "first_name" => $contactPerson->first_name,
  25. "last_name" => $contactPerson->last_name,
  26. "email" => $contactPerson->email,
  27. "phone" => $contactPerson->phone,
  28. "position" => $contactPerson->position,
  29. "is_primary" => $contactPerson->is_primary,
  30. "created_at" => $contactPerson->created_at,
  31. "updated_at" => $contactPerson->updated_at
  32. );
  33. http_response_code(200);
  34. echo json_encode($contact_arr);
  35. } else {
  36. http_response_code(404);
  37. echo json_encode(array("message" => "Contact person not found."));
  38. }
  39. } elseif(isset($_GET['client_id'])) {
  40. $contactPerson->client_id = $_GET['client_id'];
  41. $stmt = $contactPerson->read();
  42. $num = $stmt->rowCount();
  43. if($num > 0) {
  44. $contacts_arr = array();
  45. $contacts_arr["records"] = array();
  46. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  47. extract($row);
  48. $contact_item = array(
  49. "id" => $id,
  50. "client_id" => $client_id,
  51. "first_name" => $first_name,
  52. "last_name" => $last_name,
  53. "email" => $email,
  54. "phone" => $phone,
  55. "position" => $position,
  56. "is_primary" => $is_primary,
  57. "created_at" => $created_at,
  58. "updated_at" => $updated_at
  59. );
  60. array_push($contacts_arr["records"], $contact_item);
  61. }
  62. http_response_code(200);
  63. echo json_encode($contacts_arr);
  64. } else {
  65. http_response_code(200);
  66. echo json_encode(array("records" => array()));
  67. }
  68. } else {
  69. http_response_code(400);
  70. echo json_encode(array("message" => "Missing client_id parameter."));
  71. }
  72. break;
  73. case 'POST':
  74. $data = json_decode(file_get_contents("php://input"));
  75. if(!empty($data->client_id) && !empty($data->first_name) && !empty($data->last_name)) {
  76. $contactPerson->client_id = $data->client_id;
  77. $contactPerson->first_name = $data->first_name;
  78. $contactPerson->last_name = $data->last_name;
  79. $contactPerson->email = $data->email ?? '';
  80. $contactPerson->phone = $data->phone ?? '';
  81. $contactPerson->position = $data->position ?? '';
  82. $contactPerson->is_primary = $data->is_primary ?? false;
  83. if($contactPerson->create()) {
  84. http_response_code(201);
  85. echo json_encode(array("message" => "Contact person was created."));
  86. } else {
  87. http_response_code(503);
  88. echo json_encode(array("message" => "Unable to create contact person."));
  89. }
  90. } else {
  91. http_response_code(400);
  92. echo json_encode(array("message" => "Unable to create contact person. Data is incomplete."));
  93. }
  94. break;
  95. case 'PUT':
  96. $data = json_decode(file_get_contents("php://input"));
  97. if(!empty($data->id) && !empty($data->client_id) && !empty($data->first_name) && !empty($data->last_name)) {
  98. $contactPerson->id = $data->id;
  99. $contactPerson->client_id = $data->client_id;
  100. $contactPerson->first_name = $data->first_name;
  101. $contactPerson->last_name = $data->last_name;
  102. $contactPerson->email = $data->email ?? '';
  103. $contactPerson->phone = $data->phone ?? '';
  104. $contactPerson->position = $data->position ?? '';
  105. $contactPerson->is_primary = $data->is_primary ?? false;
  106. if($contactPerson->update()) {
  107. http_response_code(200);
  108. echo json_encode(array("message" => "Contact person was updated."));
  109. } else {
  110. http_response_code(503);
  111. echo json_encode(array("message" => "Unable to update contact person."));
  112. }
  113. } else {
  114. http_response_code(400);
  115. echo json_encode(array("message" => "Unable to update contact person. Data is incomplete."));
  116. }
  117. break;
  118. case 'DELETE':
  119. if(isset($_GET['id'])) {
  120. $contactPerson->id = $_GET['id'];
  121. if($contactPerson->delete()) {
  122. http_response_code(200);
  123. echo json_encode(array("message" => "Contact person was deleted."));
  124. } else {
  125. http_response_code(503);
  126. echo json_encode(array("message" => "Unable to delete contact person."));
  127. }
  128. } else {
  129. http_response_code(400);
  130. echo json_encode(array("message" => "Unable to delete contact person. ID is missing."));
  131. }
  132. break;
  133. default:
  134. http_response_code(405);
  135. echo json_encode(array("message" => "Method not allowed."));
  136. break;
  137. }
  138. ?>