locations.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. use Exports\LocationCsvExport;
  3. use Exports\LocationKmlExport;
  4. Core::addScript("public/js/app-locations.js");
  5. Core::addBreadcrumb("/account", "Account");
  6. Core::addBreadcrumb("/account/locations", "Locations");
  7. $customer = Customer::getForAdmin();
  8. if(!$customer) throw new Exception("You do not have permission to access this page.");
  9. $customer_slug = Util::slug($customer->name);
  10. $sorts = [
  11. "name" => "Name",
  12. "internal-id" => "Internal ID",
  13. "last-added" => "Most recently added first",
  14. "test-count" => "Highest test count first",
  15. "retest-count" => "Highest retest count first",
  16. ];
  17. $page_title = "$customer->name Locations";
  18. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  19. $nvp = [
  20. "customer_id" => $customer->id,
  21. "archived" => 0,
  22. "closed" => 0,
  23. ];
  24. $id = http::value("id", "int");
  25. $ids = http::value("ids", "int");
  26. $internal_id = http::value("internal_id", "trim");
  27. $name = http::value("name", "trim");
  28. $legal_entity_name = http::value("legal_entity_name", "trim");
  29. $include_archived = (int) http::value("include_archived");
  30. $include_closed = (int) http::value("include_closed");
  31. $filter_count = 0;
  32. if($id) {
  33. $nvp["id"] = $id;
  34. $filter_count++;
  35. }
  36. if($ids) {
  37. $nvp["id"] = $ids;
  38. }
  39. if($internal_id) {
  40. $nvp["internal_id"] = Constraint::contains($internal_id);
  41. $filter_count++;
  42. }
  43. if($name) {
  44. $nvp["name"] = Constraint::contains($name);
  45. $filter_count++;
  46. }
  47. if($legal_entity_name) {
  48. $nvp["legal_entity_name"] = Constraint::contains($legal_entity_name);
  49. $filter_count++;
  50. }
  51. if($include_archived) {
  52. unset($nvp["archived"]);
  53. $filter_count++;
  54. }
  55. if($include_closed) {
  56. unset($nvp["closed"]);
  57. $filter_count++;
  58. }
  59. $sort = http::value("sort");
  60. switch($sort) {
  61. case "last-added":
  62. $query_sort = "id";
  63. $order = "desc";
  64. break;
  65. case "test-count":
  66. $query_sort = "test_count";
  67. $order = "desc";
  68. break;
  69. case "retest-count":
  70. $query_sort = "retest_count";
  71. $order = "desc";
  72. break;
  73. default:
  74. $query_sort = "name";
  75. $order = "asc";
  76. break;
  77. }
  78. [$date_start, $date_end] = $customer->getYearCycleDates();
  79. $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count`
  80. FROM `location`
  81. LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end'
  82. WHERE `customer_id`='$customer->id' GROUP BY `location`.`id`
  83. ORDER BY $query_sort $order
  84. ";
  85. $locations = Location::findBySql($sql);
  86. if($sort === "internal-id") {
  87. usort($locations, static function(Location $a, Location $b) {
  88. return strnatcasecmp($a->internal_id, $b->internal_id);
  89. });
  90. }
  91. $action = http::value("action");
  92. if(!empty($action)) {
  93. switch($action) {
  94. case "export":
  95. if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
  96. $export = new LocationCsvExport($locations);
  97. $export->export("$customer_slug-export.csv");
  98. exit();
  99. case "export-kml":
  100. $export = new LocationKmlExport($locations);
  101. $export->setCustomer($customer);
  102. $export->export("$customer_slug-export.kml");
  103. exit;
  104. }
  105. }
  106. View::includeOnce("header", compact("page_title"));
  107. View::include("locations", compact("customer", "include_archived", "include_closed", "filter_count", "sorts", "sort", "locations"));
  108. View::include("modal_add_locations_to_project", compact("customer"));
  109. View::includeOnce("footer");