locations.php 4.2 KB

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