locations.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. use Exports\LocationCsvExport;
  3. use Exports\LocationKmlExport;
  4. use Searches\LocationSearch;
  5. Core::addScript("public/js/app-locations.js");
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/locations", "Locations");
  8. $customer = Customer::getForAdmin();
  9. if(!$customer) {
  10. throw new Exception("You do not have permission to access this page.");
  11. }
  12. $customer_slug = Util::slug($customer->name);
  13. $sorts = [
  14. "name" => "Name",
  15. "internal-id" => "Internal ID",
  16. "last-added" => "Most recently added first",
  17. "test-count" => "Highest test count first",
  18. "retest-count" => "Highest retest count first",
  19. ];
  20. $filter_options = [
  21. "" => "No Filter",
  22. "untested-current" => "Untested Only (Current Cycle)",
  23. "failed-current" => "Failed Only (Current Cycle)",
  24. "untested-all" => "Untested Only (All Time)",
  25. "failed-all" => "Failed Only (All Time)",
  26. ];
  27. $page_title = "$customer->name Locations";
  28. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  29. $nvp = [
  30. "customer_id" => $customer->id,
  31. "archived" => 0,
  32. "closed" => 0,
  33. ];
  34. $search = new LocationSearch($customer);
  35. $id = http::value("id", "int");
  36. $search->setId($id);
  37. $ids = http::value("ids", "int");
  38. $search->setIds($ids);
  39. $internal_id = http::value("internal_id", "trim");
  40. $search->setInternalId($internal_id);
  41. $name = http::value("name", "trim");
  42. $search->setName($name);
  43. $legal_entity_name = http::value("legal_entity_name", "trim");
  44. $search->setLegalEntityName($legal_entity_name);
  45. $min_date = http::value("min_date");
  46. $search->setMinTestDate($min_date);
  47. $max_date = http::value("max_date");
  48. $search->setMaxTestDate($max_date);
  49. $include_archived = (int) http::value("include_archived");
  50. $search->setIncludeArchived($include_archived);
  51. $include_closed = (int) http::value("include_closed");
  52. $search->setIncludeClosed($include_closed);
  53. $filter = http::value("filter");
  54. $search->setFilter($filter);
  55. $filter_count = 0;
  56. $where_strings = [];
  57. if($id) {
  58. $filter_count++;
  59. }
  60. if($internal_id) {
  61. $filter_count++;
  62. }
  63. if($name) {
  64. $filter_count++;
  65. }
  66. if($legal_entity_name) {
  67. $filter_count++;
  68. }
  69. if($include_archived) {
  70. $filter_count++;
  71. }
  72. if($include_closed) {
  73. $filter_count++;
  74. }
  75. if($min_date) {
  76. $filter_count++;
  77. }
  78. if($max_date) {
  79. $filter_count++;
  80. }
  81. if($filter) {
  82. $filter_count++;
  83. }
  84. $sort = http::value("sort");
  85. switch($sort) {
  86. case "last-added":
  87. $query_sort = "id";
  88. $order = "desc";
  89. break;
  90. case "test-count":
  91. $query_sort = "test_count";
  92. $order = "desc";
  93. break;
  94. case "retest-count":
  95. $query_sort = "retest_count";
  96. $order = "desc";
  97. break;
  98. default:
  99. $query_sort = "name";
  100. $order = "asc";
  101. break;
  102. }
  103. $locations = $search->run();
  104. $location_ids = array_column($locations, "id") ?: [0];
  105. $location_ids_csv = implode(", ", $location_ids);
  106. [$date_start, $date_end] = $customer->getYearCycleDates();
  107. $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count`
  108. FROM `location`
  109. LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end'
  110. WHERE `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
  111. ORDER BY $query_sort $order
  112. ";
  113. $locations = Location::findBySql($sql);
  114. if($sort === "internal-id") {
  115. usort($locations, static function(Location $a, Location $b) {
  116. return strnatcasecmp($a->internal_id, $b->internal_id);
  117. });
  118. }
  119. $action = http::value("action");
  120. if(!empty($action)) {
  121. switch($action) {
  122. case "export":
  123. if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
  124. $export = new LocationCsvExport($locations);
  125. $export->export("$customer_slug-export.csv");
  126. exit();
  127. case "export-kml":
  128. $export = new LocationKmlExport($locations);
  129. $export->setCustomer($customer);
  130. $export->export("$customer_slug-export.kml");
  131. exit;
  132. }
  133. }
  134. View::includeOnce("header", compact("page_title"));
  135. View::include("locations", compact("customer", "search", "filter_count", "sorts", "sort", "filter_options", "locations"));
  136. View::include("modal_add_locations_to_project", compact("customer"));
  137. View::includeOnce("footer");