locations.php 3.3 KB

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