| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- use Exports\LocationCsvExport;
- use Exports\LocationKmlExport;
- 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,
- "archived" => 0,
- "closed" => 0,
- ];
- $id = http::value("id", "int");
- $ids = http::value("ids", "int");
- $internal_id = http::value("internal_id", "trim");
- $name = http::value("name", "trim");
- $legal_entity_name = http::value("legal_entity_name", "trim");
- $include_archived = (int) http::value("include_archived");
- $include_closed = (int) http::value("include_closed");
- $filter_count = 0;
- if($id) {
- $nvp["id"] = $id;
- $filter_count++;
- }
- if($ids) {
- $nvp["id"] = $ids;
- }
- if($internal_id) {
- $nvp["internal_id"] = Constraint::contains($internal_id);
- $filter_count++;
- }
- if($name) {
- $nvp["name"] = Constraint::contains($name);
- $filter_count++;
- }
- if($legal_entity_name) {
- $nvp["legal_entity_name"] = Constraint::contains($legal_entity_name);
- $filter_count++;
- }
- if($include_archived) {
- unset($nvp["archived"]);
- $filter_count++;
- }
- if($include_closed) {
- unset($nvp["closed"]);
- $filter_count++;
- }
- $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 RuntimeException("You do not have permission to access this resource.");
- $export = new LocationCsvExport($locations);
- $export->export("$customer_slug-export.csv");
- exit();
- case "export-kml":
- $export = new LocationKmlExport($locations);
- $export->setCustomer($customer);
- $export->export("$customer_slug-export.kml");
- exit;
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("locations", compact("customer", "include_archived", "include_closed", "filter_count", "sorts", "sort", "locations"));
- View::include("modal_add_locations_to_project", compact("customer"));
- View::includeOnce("footer");
|