clients.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/Client.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $client = new Client($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $client->id = $_GET['id'];
  19. $client->readOne();
  20. if($client->email != null) {
  21. $client_arr = array(
  22. "id" => $client->id,
  23. "company_name" => $client->company_name,
  24. "y_tunnus" => $client->y_tunnus,
  25. "first_name" => $client->first_name,
  26. "last_name" => $client->last_name,
  27. "email" => $client->email,
  28. "phone" => $client->phone,
  29. "address" => $client->address,
  30. "city" => $client->city,
  31. "state" => $client->state,
  32. "postal_code" => $client->postal_code,
  33. "country" => $client->country,
  34. "notes" => $client->notes,
  35. "hour_price" => $client->hour_price,
  36. "created_at" => $client->created_at,
  37. "updated_at" => $client->updated_at
  38. );
  39. http_response_code(200);
  40. echo json_encode($client_arr);
  41. } else {
  42. http_response_code(404);
  43. echo json_encode(array("message" => "Client not found."));
  44. }
  45. } elseif(isset($_GET['search'])) {
  46. $search_term = $_GET['search'];
  47. $stmt = $client->search($search_term);
  48. $num = $stmt->rowCount();
  49. if($num > 0) {
  50. $clients_arr = array();
  51. $clients_arr["records"] = array();
  52. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  53. extract($row);
  54. $client_item = array(
  55. "id" => $id,
  56. "company_name" => $company_name,
  57. "first_name" => $first_name,
  58. "last_name" => $last_name,
  59. "email" => $email,
  60. "phone" => $phone,
  61. "address" => $address,
  62. "city" => $city,
  63. "state" => $state,
  64. "postal_code" => $postal_code,
  65. "country" => $country,
  66. "notes" => $notes,
  67. "created_at" => $created_at,
  68. "updated_at" => $updated_at
  69. );
  70. array_push($clients_arr["records"], $client_item);
  71. }
  72. http_response_code(200);
  73. echo json_encode($clients_arr);
  74. } else {
  75. http_response_code(200);
  76. echo json_encode(array("records" => array()));
  77. }
  78. } else {
  79. $stmt = $client->read();
  80. $num = $stmt->rowCount();
  81. if($num > 0) {
  82. $clients_arr = array();
  83. $clients_arr["records"] = array();
  84. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  85. extract($row);
  86. // Fetch contact persons for this client
  87. require_once __DIR__ . '/../models/ContactPerson.php';
  88. $contactPerson = new ContactPerson($db);
  89. $contactPerson->client_id = $id;
  90. $contact_stmt = $contactPerson->read();
  91. $contact_persons = array();
  92. while ($contact_row = $contact_stmt->fetch(PDO::FETCH_ASSOC)) {
  93. $contact_persons[] = array(
  94. "id" => $contact_row['id'],
  95. "first_name" => $contact_row['first_name'],
  96. "last_name" => $contact_row['last_name'],
  97. "email" => $contact_row['email'],
  98. "phone" => $contact_row['phone'],
  99. "position" => $contact_row['position'],
  100. "department" => $contact_row['department'],
  101. "is_primary" => $contact_row['is_primary'],
  102. "notes" => $contact_row['notes']
  103. );
  104. }
  105. $client_item = array(
  106. "id" => $id,
  107. "company_name" => $company_name,
  108. "y_tunnus" => $y_tunnus,
  109. "first_name" => $first_name,
  110. "last_name" => $last_name,
  111. "email" => $email,
  112. "phone" => $phone,
  113. "address" => $address,
  114. "city" => $city,
  115. "state" => $state,
  116. "postal_code" => $postal_code,
  117. "country" => $country,
  118. "notes" => $notes,
  119. "hour_price" => $hour_price,
  120. "contact_persons" => $contact_persons,
  121. "created_at" => $created_at,
  122. "updated_at" => $updated_at
  123. );
  124. array_push($clients_arr["records"], $client_item);
  125. }
  126. http_response_code(200);
  127. echo json_encode($clients_arr);
  128. } else {
  129. http_response_code(200);
  130. echo json_encode(array("records" => array()));
  131. }
  132. }
  133. break;
  134. case 'POST':
  135. $data = json_decode(file_get_contents("php://input"));
  136. if(!empty($data->first_name) && !empty($data->last_name) && !empty($data->email)) {
  137. $client->company_name = $data->company_name ?? '';
  138. $client->y_tunnus = $data->y_tunnus ?? '';
  139. $client->first_name = $data->first_name;
  140. $client->last_name = $data->last_name;
  141. $client->email = $data->email;
  142. $client->phone = $data->phone ?? '';
  143. $client->address = $data->address ?? '';
  144. $client->city = $data->city ?? '';
  145. $client->state = $data->state ?? '';
  146. $client->postal_code = $data->postal_code ?? '';
  147. $client->country = $data->country ?? '';
  148. $client->notes = $data->notes ?? '';
  149. $client->hour_price = $data->hour_price ?? 0;
  150. if($client->create()) {
  151. http_response_code(201);
  152. echo json_encode(array("message" => "Client was created."));
  153. } else {
  154. http_response_code(503);
  155. echo json_encode(array("message" => "Unable to create client."));
  156. }
  157. } else {
  158. http_response_code(400);
  159. echo json_encode(array("message" => "Unable to create client. Data is incomplete."));
  160. }
  161. break;
  162. case 'PUT':
  163. $data = json_decode(file_get_contents("php://input"));
  164. if(!empty($data->id) && !empty($data->first_name) && !empty($data->last_name) && !empty($data->email)) {
  165. $client->id = $data->id;
  166. $client->company_name = $data->company_name ?? '';
  167. $client->y_tunnus = $data->y_tunnus ?? '';
  168. $client->first_name = $data->first_name;
  169. $client->last_name = $data->last_name;
  170. $client->email = $data->email;
  171. $client->phone = $data->phone ?? '';
  172. $client->address = $data->address ?? '';
  173. $client->city = $data->city ?? '';
  174. $client->state = $data->state ?? '';
  175. $client->postal_code = $data->postal_code ?? '';
  176. $client->country = $data->country ?? '';
  177. $client->notes = $data->notes ?? '';
  178. $client->hour_price = $data->hour_price ?? 0;
  179. if($client->update()) {
  180. http_response_code(200);
  181. echo json_encode(array("message" => "Client was updated."));
  182. } else {
  183. http_response_code(503);
  184. echo json_encode(array("message" => "Unable to update client."));
  185. }
  186. } else {
  187. http_response_code(400);
  188. echo json_encode(array("message" => "Unable to update client. Data is incomplete."));
  189. }
  190. break;
  191. case 'DELETE':
  192. if(isset($_GET['id'])) {
  193. $client->id = $_GET['id'];
  194. if($client->delete()) {
  195. http_response_code(200);
  196. echo json_encode(array("message" => "Client was deleted."));
  197. } else {
  198. http_response_code(503);
  199. echo json_encode(array("message" => "Unable to delete client."));
  200. }
  201. } else {
  202. http_response_code(400);
  203. echo json_encode(array("message" => "Unable to delete client. ID is missing."));
  204. }
  205. break;
  206. default:
  207. http_response_code(405);
  208. echo json_encode(array("message" => "Method not allowed."));
  209. break;
  210. }
  211. ?>