contact_persons.php 6.7 KB

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