locations.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $where_strings = [];
  33. if($id) {
  34. $where_strings[] = "`location`.`id`='$id'";
  35. $filter_count++;
  36. }
  37. if($ids) {
  38. $where_strings[] = sprintf("`location`.`id` IN (%s)", implode(",", $ids));
  39. }
  40. if($internal_id) {
  41. $where_strings[] = sprintf("`location`.`internal_id` LIKE '%%%s%%'", DB::escape($internal_id));
  42. $filter_count++;
  43. }
  44. if($name) {
  45. $where_strings[] = sprintf("`location`.`name` LIKE '%%%s%%'", DB::escape($name));
  46. $filter_count++;
  47. }
  48. if($legal_entity_name) {
  49. $where_strings[] = sprintf("`location`.`legal_entity_name` LIKE '%%%s%%'", DB::escape($legal_entity_name));
  50. $filter_count++;
  51. }
  52. if($include_archived) {
  53. $filter_count++;
  54. }
  55. else {
  56. $where_strings[] = "`location`.`archived`='0'";
  57. }
  58. if($include_closed) {
  59. $filter_count++;
  60. }
  61. else {
  62. $where_strings[] = "`location`.`closed`='0'";
  63. }
  64. $sort = http::value("sort");
  65. switch($sort) {
  66. case "last-added":
  67. $query_sort = "id";
  68. $order = "desc";
  69. break;
  70. case "test-count":
  71. $query_sort = "test_count";
  72. $order = "desc";
  73. break;
  74. case "retest-count":
  75. $query_sort = "retest_count";
  76. $order = "desc";
  77. break;
  78. default:
  79. $query_sort = "name";
  80. $order = "asc";
  81. break;
  82. }
  83. $where_string = "";
  84. if($where_strings) {
  85. $where_string = "AND " . implode(" AND ", $where_strings);
  86. }
  87. [$date_start, $date_end] = $customer->getYearCycleDates();
  88. $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count`
  89. FROM `location`
  90. LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end'
  91. WHERE `customer_id`='$customer->id' $where_string GROUP BY `location`.`id`
  92. ORDER BY $query_sort $order
  93. ";
  94. $locations = Location::findBySql($sql);
  95. if($sort === "internal-id") {
  96. usort($locations, static function(Location $a, Location $b) {
  97. return strnatcasecmp($a->internal_id, $b->internal_id);
  98. });
  99. }
  100. $action = http::value("action");
  101. if(!empty($action)) {
  102. switch($action) {
  103. case "export":
  104. if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
  105. $export = new LocationCsvExport($locations);
  106. $export->export("$customer_slug-export.csv");
  107. exit();
  108. case "export-kml":
  109. $export = new LocationKmlExport($locations);
  110. $export->setCustomer($customer);
  111. $export->export("$customer_slug-export.kml");
  112. exit;
  113. }
  114. }
  115. View::includeOnce("header", compact("page_title"));
  116. View::include("locations", compact("customer", "include_archived", "include_closed", "filter_count", "sorts", "sort", "locations"));
  117. View::include("modal_add_locations_to_project", compact("customer"));
  118. View::includeOnce("footer");