map_overview_ajax.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. JSON::header();
  3. $submit = http::value("submit");
  4. if($submit) {
  5. try {
  6. Core::validateCSRFToken($submit);
  7. switch($action) {
  8. // Gets all locations for a customer
  9. case "get-customer-locations": {
  10. $customer_id = http::value("customer_id", "int");
  11. $customer = Customer::get($customer_id);
  12. if(!$customer) throw new Exception("Customer not found.");
  13. $locations = $customer->getLocations();
  14. $filename = str_replace("-", "_", Util::slug($customer->name));
  15. $icon = null;
  16. if(file_exists("public/img/markers/$filename.png")) {
  17. $icon = "/img/markers/$filename.png";
  18. }
  19. $output = [];
  20. foreach($locations as $location) {
  21. $output[] = [
  22. "id" => (int) $location->id,
  23. "internalId" => $location->internal_id,
  24. "name" => $location->name,
  25. "latitude" => (float) $location->latitude,
  26. "longitude" => (float) $location->longitude,
  27. "icon" => $icon
  28. ];
  29. }
  30. JSON::success([
  31. "locations" => $output
  32. ]);
  33. break;
  34. }
  35. // Gets all locations for a project
  36. case "get-project-locations": {
  37. $project_id = http::value("project_id", "int");
  38. $project = Project::get($project_id);
  39. if(!$project) throw new Exception("Project not found.");
  40. $customer = $project->getCustomer();
  41. $tests = $project->getTests();
  42. $locations = Location::load(array_column($tests, "location_id"));
  43. $location_tests = [];
  44. foreach($tests as $test) {
  45. if($test->status == Test::STATUS_CANCELLED) continue;
  46. $location_tests[$test->location_id] = $test;
  47. }
  48. $output = [];
  49. foreach($locations as $location) {
  50. $test = $location_tests[$location->id] ?? false;
  51. $test_id = null;
  52. $status = 0;
  53. $status_text = "";
  54. $pass = 0;
  55. $pass_class = "";
  56. $pass_text = "";
  57. $pass_icon = "";
  58. $class = "secondary";
  59. if($test) {
  60. $test_id = $test->id;
  61. $class = $test->getStatusClass();
  62. $status = $test->status;
  63. $status_text = $test->getStatusText();
  64. $pass = $test->pass;
  65. $pass_class = $test->getPassClass();
  66. $pass_text = $test->getPassText();
  67. $pass_icon = $test->getPassIcon();
  68. }
  69. $filename = str_replace("-", "_", Util::slug($customer->name));
  70. $icon = null;
  71. if(file_exists("public/img/markers/$filename.png")) {
  72. $icon = "/img/markers/$filename.png";
  73. }
  74. $output[] = (object) [
  75. "id" => (int) $location->id,
  76. "internalId" => $location->internal_id,
  77. "testId" => (int) $test_id,
  78. "name" => $location->name,
  79. "address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
  80. "address1" => $location->address_1,
  81. "status" => $status,
  82. "statusText" => $status_text,
  83. "pass" => (int) $pass,
  84. "passClass" => $pass_class,
  85. "passText" => $pass_text,
  86. "passIcon" => $pass_icon,
  87. "class" => $class,
  88. "latitude" => (float) $location->latitude,
  89. "longitude" => (float) $location->longitude,
  90. "icon" => $icon
  91. ];
  92. }
  93. usort($output, function($a, $b) {
  94. return $a->status <=> $b->status;
  95. });
  96. JSON::success([
  97. "locations" => $output
  98. ]);
  99. break;
  100. }
  101. default:
  102. throw new Exception("Invalid request.");
  103. }
  104. }
  105. catch(Exception $e) {
  106. JSON::fail($e->getMessage());
  107. }
  108. }
  109. JSON::fail("Invalid request.");
  110. ?>