| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- 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.");
- $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");
- $internal_id = http::value("internal_id", "trim");
- $name = http::value("name", "trim");
- if($id) $nvp["id"] = $id;
- 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=export.csv");
- $handle = fopen("php://output", "w");
- fputcsv($handle, [
- "Internal ID", "City", "Legal Entity", "Region", "Area", "Open", "Close", "Name", "Address", "Postcode", "Email", "Latitude", "Longitude"
- ]);
- foreach($locations as $location) {
- fputcsv($handle, [
- $location->internal_id,
- $location->city,
- $location->legal_entity_name,
- $location->region,
- $location->area_manager,
- "",
- "",
- $location->name,
- $location->address_1,
- $location->postal_code,
- $location->email,
- $location->latitude,
- $location->longitude,
- ]);
- }
- fclose($handle);
- 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");
- ?>
|