| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- Core::addScript("public/js/app-locations.js");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/locations", "Locations");
- $customer = Customer::getForAdmin();
- if(!$customer) throw new Exception("You do not have permission to access this page.");
- $customer_slug = Util::slug($customer->name);
- $sorts = [
- "name" => "Name",
- "last-added" => "Last Added",
- "internal-id" => "Internal ID",
- ];
- $page_title = "$customer->name Locations";
- Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
- $nvp = ["customer_id" => $customer->id];
- $id = http::value("id", "int");
- $ids = http::value("ids", "int");
- $internal_id = http::value("internal_id", "trim");
- $name = http::value("name", "trim");
- if($id) $nvp["id"] = $id;
- if($ids) $nvp["id"] = $ids;
- if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
- if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($name));
- $sort = http::value("sort");
- switch($sort) {
- case "last-added":
- $query_sort = "id";
- $order = "desc";
- break;
- default:
- $query_sort = "name";
- $order = "asc";
- break;
- }
- $locations = Location::query($nvp, ["sort" => $query_sort, "order" => $order]);
- if($sort == "internal-id") {
- usort($locations, function($a, $b) {
- return strnatcasecmp($a->internal_id, $b->internal_id);
- });
- }
- $action = http::value("action");
- if(!empty($action)) {
- switch($action) {
- case "export":
- if(!Core::$user->canAdmin()) throw new Exception("You do not have permission to access this resource.");
- header("Content-Type: text/csv; charset=utf-8");
- header("Content-Disposition: attachment; filename=$customer_slug-export.csv");
- $handle = fopen("php://output", "w");
- fputcsv($handle, [
- "ID", "Internal ID", "City", "Legal Entity", "Region", "Area Manager", "Area Manager Email", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
- ]);
- foreach($locations as $location) {
- fputcsv($handle, [
- $location->id,
- $location->internal_id,
- $location->city,
- $location->legal_entity_name,
- $location->region,
- $location->area_manager,
- $location->area_manager_email,
- "",
- "",
- $location->name,
- $location->address_1,
- $location->postal_code,
- $location->email,
- $location->latitude,
- $location->longitude,
- ]);
- }
- fclose($handle);
- exit;
- case "export-kml":
- header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
- header("Content-Disposition: attachment; filename=$customer_slug-export.kml");
- $xml = simplexml_load_file(ROOT_DIR . "/files/empty.kml");
- $folder_node = $xml->addChild("Folder");
- $folder_node->addChild("name", $customer->name);
- $folder_node->addChild("open", 1);
- $folder_node->addChild("description", $customer->name . " Locations");
- foreach($locations as $location) {
- $placemark_node = $folder_node->addChild("Placemark");
- $placemark_node->addChild("name", htmlspecialchars($location->name));
- $placemark_node->addChild("description", htmlspecialchars($location->getDisplayAddress()));
- $point_node = $placemark_node->addChild("Point");
- $point_node->addChild("coordinates", $location->longitude . "," . $location->latitude);
- }
- echo $xml->asXML();
- exit;
- }
- }
- $filter_count = count($nvp) - 1;
- View::includeOnce("header", compact("page_title"));
- View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
- View::includeOnce("footer");
- ?>
|