1
0

locations.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  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 = ["customer_id" => $customer->id];
  16. $id = http::value("id", "int");
  17. $ids = http::value("ids", "int");
  18. $internal_id = http::value("internal_id", "trim");
  19. $name = http::value("name", "trim");
  20. if($id) $nvp["id"] = $id;
  21. if($ids) $nvp["id"] = $ids;
  22. if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
  23. if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($name));
  24. $sort = http::value("sort");
  25. switch($sort) {
  26. case "last-added":
  27. $query_sort = "id";
  28. $order = "desc";
  29. break;
  30. default:
  31. $query_sort = "name";
  32. $order = "asc";
  33. break;
  34. }
  35. $locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
  36. if($sort == "internal-id") {
  37. usort($locations, function($a, $b) {
  38. return strnatcasecmp($a->internal_id, $b->internal_id);
  39. });
  40. }
  41. $action = http::value("action");
  42. if(!empty($action)) {
  43. switch($action) {
  44. case "export":
  45. if(!Core::$user->canAdmin()) throw new Exception("You do not have permission to access this resource.");
  46. header("Content-Type: text/csv; charset=utf-8");
  47. header("Content-Disposition: attachment; filename=$customer_slug-export.csv");
  48. $handle = fopen("php://output", "w");
  49. fputcsv($handle, [
  50. "ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
  51. ]);
  52. foreach($locations as $location) {
  53. fputcsv($handle, [
  54. $location->id,
  55. $location->internal_id,
  56. $location->city,
  57. $location->legal_entity_name,
  58. $location->region,
  59. $location->area_manager,
  60. $location->area_manager_email,
  61. "",
  62. "",
  63. $location->name,
  64. $location->address_1,
  65. $location->postal_code,
  66. $location->email,
  67. $location->latitude,
  68. $location->longitude,
  69. ]);
  70. }
  71. fclose($handle);
  72. exit;
  73. case "export-kml":
  74. header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  75. header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
  76. $xml = simplexml_load_file(ROOT_DIR . "/files/empty.kml");
  77. $folder_node = $xml->addChild("Folder");
  78. $folder_node->addChild("name", $customer->name);
  79. $folder_node->addChild("open", 1);
  80. $folder_node->addChild("description", $customer->name . " Locations");
  81. foreach($locations as $location) {
  82. $placemark_node = $folder_node->addChild("Placemark");
  83. $placemark_node->addChild("name", htmlspecialchars($location->name));
  84. $placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
  85. $point_node = $placemark_node->addChild("Point");
  86. $point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
  87. }
  88. echo $xml->asXML();
  89. exit;
  90. }
  91. }
  92. $filter_count = count($nvp) - 1;
  93. View::includeOnce("header", compact("page_title"));
  94. View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
  95. View::includeOnce("footer");
  96. ?>