locations.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. Core::addScript("public/js/app-locations.js");
  3. Core::addBreadcrumb("/account", "Account");
  4. Core::addBreadcrumb("/account/locations", "Locations");
  5. $customer = Customer::getForAdmin();
  6. if(!$customer) throw new Exception("You do not have permission to access this page.");
  7. $customer_slug = Util::slug($customer->name);
  8. $sorts = [
  9. "name" => "Name",
  10. "last-added" => "Last Added",
  11. "internal-id" => "Internal ID",
  12. ];
  13. $page_title = "$customer->name Locations";
  14. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
  15. $nvp = [
  16. "customer_id" => $customer->id,
  17. "archived" => 0,
  18. "closed" => 0,
  19. ];
  20. $id = http::value("id", "int");
  21. $ids = http::value("ids", "int");
  22. $internal_id = http::value("internal_id", "trim");
  23. $name = http::value("name", "trim");
  24. $legal_entity_name = http::value("legal_entity_name", "trim");
  25. $include_archived = (int) http::value("include_archived");
  26. $include_closed = (int) http::value("include_closed");
  27. $filter_count = 0;
  28. if($id) {
  29. $nvp["id"] = $id;
  30. $filter_count++;
  31. }
  32. if($ids) {
  33. $nvp["id"] = $ids;
  34. }
  35. if($internal_id) {
  36. $nvp["internal_id"] = Constraint::contains($internal_id);
  37. $filter_count++;
  38. }
  39. if($name) {
  40. $nvp["name"] = Constraint::contains($name);
  41. $filter_count++;
  42. }
  43. if($legal_entity_name) {
  44. $nvp["legal_entity_name"] = Constraint::contains($legal_entity_name);
  45. $filter_count++;
  46. }
  47. if($include_archived) {
  48. unset($nvp["archived"]);
  49. $filter_count++;
  50. }
  51. if($include_closed) {
  52. unset($nvp["closed"]);
  53. $filter_count++;
  54. }
  55. $sort = http::value("sort");
  56. switch($sort) {
  57. case "last-added":
  58. $query_sort = "id";
  59. $order = "desc";
  60. break;
  61. default:
  62. $query_sort = "name";
  63. $order = "asc";
  64. break;
  65. }
  66. $locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
  67. if($sort == "internal-id") {
  68. usort($locations, function($a, $b) {
  69. return strnatcasecmp($a->internal_id, $b->internal_id);
  70. });
  71. }
  72. $action = http::value("action");
  73. if(!empty($action)) {
  74. switch($action) {
  75. case "export":
  76. if(!Core::$user->canAdmin()) throw new Exception("You do not have permission to access this resource.");
  77. header("Content-Type: text/csv; charset=utf-8");
  78. header("Content-Disposition: attachment; filename=$customer_slug-export.csv");
  79. $handle = fopen("php://output", "w");
  80. fputcsv($handle, [
  81. "ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
  82. ]);
  83. foreach($locations as $location) {
  84. fputcsv($handle, [
  85. $location->id,
  86. $location->internal_id,
  87. $location->city,
  88. $location->legal_entity_name,
  89. $location->region,
  90. $location->area_manager,
  91. $location->area_manager_email,
  92. "",
  93. "",
  94. $location->name,
  95. $location->address_1,
  96. $location->postal_code,
  97. $location->email,
  98. $location->latitude,
  99. $location->longitude,
  100. ]);
  101. }
  102. fclose($handle);
  103. exit;
  104. case "export-kml":
  105. header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  106. header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
  107. $xml = simplexml_load_file(ROOT_DIR . "/files/empty.kml");
  108. $folder_node = $xml->addChild("Folder");
  109. $folder_node->addChild("name", $customer->name);
  110. $folder_node->addChild("open", 1);
  111. $folder_node->addChild("description", $customer->name . " Locations");
  112. foreach($locations as $location) {
  113. $placemark_node = $folder_node->addChild("Placemark");
  114. $placemark_node->addChild("name", htmlspecialchars($location->name));
  115. $placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
  116. $point_node = $placemark_node->addChild("Point");
  117. $point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
  118. }
  119. echo $xml->asXML();
  120. exit;
  121. }
  122. }
  123. View::includeOnce("header", compact("page_title"));
  124. View::include("locations", compact("customer", "include_archived", "include_closed", "filter_count", "sorts", "sort", "locations"));
  125. View::include("modal_add_locations_to_project", compact("customer"));
  126. View::includeOnce("footer");