locations.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "Internal ID", "City", "Legal Entity", "Region", "Area", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
  51. ]);
  52. foreach($locations as $location) {
  53. fputcsv($handle, [
  54. $location->internal_id,
  55. $location->city,
  56. $location->legal_entity_name,
  57. $location->region,
  58. $location->area_manager,
  59. "",
  60. "",
  61. $location->name,
  62. $location->address_1,
  63. $location->postal_code,
  64. $location->email,
  65. $location->latitude,
  66. $location->longitude,
  67. ]);
  68. }
  69. fclose($handle);
  70. exit;
  71. case "export-kml":
  72. header("Content-Type: text/xml; charset=utf-8");
  73. header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
  74. $xml = simplexml_load_file(ROOT_DIR . "/files/empty.kml");
  75. $folder_node = $xml->addChild("Folder");
  76. $folder_node->addChild("name", $customer->name);
  77. $folder_node->addChild("open", 1);
  78. $folder_node->addChild("description", $customer->name . " Locations");
  79. foreach($locations as $location) {
  80. $placemark_node = $folder_node->addChild("Placemark");
  81. $placemark_node->addChild("name", htmlspecialchars($location->name));
  82. $placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
  83. $point_node = $placemark_node->addChild("Point");
  84. $point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
  85. }
  86. echo $xml->asXML();
  87. exit;
  88. }
  89. }
  90. $filter_count = count($nvp) - 1;
  91. View::includeOnce("header", compact("page_title"));
  92. View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
  93. View::includeOnce("footer");
  94. ?>