locations.php 4.0 KB

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