locations.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "last-added" => "Last Added",
  13. "internal-id" => "Internal ID",
  14. ];
  15. $page_title = "$customer->name Locations";
  16. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  17. $nvp = [
  18. "customer_id" => $customer->id,
  19. "archived" => 0,
  20. "closed" => 0,
  21. ];
  22. $id = http::value("id", "int");
  23. $ids = http::value("ids", "int");
  24. $internal_id = http::value("internal_id", "trim");
  25. $name = http::value("name", "trim");
  26. $legal_entity_name = http::value("legal_entity_name", "trim");
  27. $include_archived = (int) http::value("include_archived");
  28. $include_closed = (int) http::value("include_closed");
  29. $filter_count = 0;
  30. if($id) {
  31. $nvp["id"] = $id;
  32. $filter_count++;
  33. }
  34. if($ids) {
  35. $nvp["id"] = $ids;
  36. }
  37. if($internal_id) {
  38. $nvp["internal_id"] = Constraint::contains($internal_id);
  39. $filter_count++;
  40. }
  41. if($name) {
  42. $nvp["name"] = Constraint::contains($name);
  43. $filter_count++;
  44. }
  45. if($legal_entity_name) {
  46. $nvp["legal_entity_name"] = Constraint::contains($legal_entity_name);
  47. $filter_count++;
  48. }
  49. if($include_archived) {
  50. unset($nvp["archived"]);
  51. $filter_count++;
  52. }
  53. if($include_closed) {
  54. unset($nvp["closed"]);
  55. $filter_count++;
  56. }
  57. $sort = http::value("sort");
  58. switch($sort) {
  59. case "last-added":
  60. $query_sort = "id";
  61. $order = "desc";
  62. break;
  63. default:
  64. $query_sort = "name";
  65. $order = "asc";
  66. break;
  67. }
  68. $locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
  69. if($sort == "internal-id") {
  70. usort($locations, function($a, $b) {
  71. return strnatcasecmp($a->internal_id, $b->internal_id);
  72. });
  73. }
  74. $action = http::value("action");
  75. if(!empty($action)) {
  76. switch($action) {
  77. case "export":
  78. if(!Core::$user->canAdmin()) throw new RuntimeException("You do not have permission to access this resource.");
  79. $export = new LocationCsvExport($locations);
  80. $export->export("$customer_slug-export.csv");
  81. exit();
  82. case "export-kml":
  83. $export = new LocationKmlExport($locations);
  84. $export->setCustomer($customer);
  85. $export->export("$customer_slug-export.kml");
  86. exit;
  87. }
  88. }
  89. View::includeOnce("header", compact("page_title"));
  90. View::include("locations", compact("customer", "include_archived", "include_closed", "filter_count", "sorts", "sort", "locations"));
  91. View::include("modal_add_locations_to_project", compact("customer"));
  92. View::includeOnce("footer");