'']; $home = reportGeoPlace([])['country']; $countries = reportGeoGroup($all, 'country'); $country = (string) ($_GET['country'] ?? ($home !== '-' && geographyIn($all, 'country', $home) ? $home : 'all')); $shown = $country === 'all' ? [] : geographyIn($all, 'country', $country); if (!$shown) { $country = 'all'; $shown = $all; } $regions = $country === 'all' ? [] : reportGeoGroup($shown, 'region'); $region = $country === 'all' || (isset($_GET['at']) && $at[0] !== $country) ? '' : (string) ($_GET['region'] ?? ''); if ($region !== '') { $in_region = geographyIn($shown, 'region', $region); $region = $in_region ? $region : ''; $shown = $in_region ?: $shown; } $cities = reportGeoGroup($shown, 'city'); $city = $region === '' || (isset($_GET['at']) && $at[1] !== $region) ? '' : (string) ($_GET['city'] ?? ''); if ($city !== '') { $in_city = geographyIn($shown, 'city', $city); $city = $in_city ? $city : ''; $shown = $in_city ?: $shown; } $level = $city !== '' ? 3 : ($region !== '' ? 2 : ($country !== 'all' ? 1 : 0)); $country_name = $country === 'all' ? 'All countries' : $shown[0]['country_name']; $term = reportGeoRegionTerm($country); $terms = reportGeoRegionTerm($country, true); $path = [$country_name]; if ($level >= 2) { $path[] = $shown[0]['region_name']; } if ($level === 3) { $path[] = $shown[0]['city_name']; } $drill = function ($country, $region = '', $city = '') { return reportUrl(['country' => $country, 'region' => $region !== '' ? $region : null, 'city' => $city !== '' ? $city : null, 'at' => null, 'export' => null, 'table' => null]); }; $place_link = function ($url, $row) { return '' . escapeHtml($row['name']) . ''; }; $share_meter = function ($row) { return '
' . '' . reportPercent($row['share']) . '
'; }; // The places a step down from here: countries, or this country's states $areas = $level === 0 ? $countries : ($level === 1 ? $regions : []); foreach ($areas as &$row) { $row['share'] = reportShare($row['locations'], count($shown), 1); } unset($row); $area_columns = [ 'name' => ['label' => $level === 0 ? 'Country' : $term, 'html' => function ($row) use ($drill, $place_link, $level) { return $place_link($level === 0 ? $drill($row['key']) : $drill($row['country'], $row['key']), $row); }], 'clients' => ['label' => 'Clients', 'type' => 'count', 'class' => 'fw-semibold'], 'locations' => ['label' => 'Locations', 'type' => 'count'], 'cities' => ['label' => 'Cities', 'type' => 'count'], 'share' => ['label' => 'Share', 'type' => 'percent', 'title' => 'Share of the locations', 'html' => $share_meter], ]; $city_columns = [ 'name' => ['label' => 'City', 'html' => function ($row) use ($drill, $place_link) { return $place_link($drill($row['country'], $row['region'], $row['key']), $row); }], 'region_name' => ['label' => $level === 0 ? 'State or region' : $term, 'screen' => $level < 2], 'country_name' => ['label' => 'Country', 'screen' => $level === 0], 'clients' => ['label' => 'Clients', 'type' => 'count', 'class' => 'fw-semibold'], 'locations' => ['label' => 'Locations', 'type' => 'count'], ]; foreach ($shown as &$row) { $row['address'] = locationAddressText($row); } unset($row); $location_columns = [ 'client_name' => ['label' => 'Client', 'html' => function ($row) { return reportClientCell($row['client_id'], $row['client_name'], $row['client_abbreviation']); }], 'location_name' => ['label' => 'Location', 'html' => function ($row) { return '' . escapeHtml($row['location_name']) . '' . (intval($row['location_primary']) ? ' Primary' : ''); }], 'address' => ['label' => 'Address', 'class' => 'itflow-report-text', 'csv' => false], 'location_address' => ['label' => 'Street', 'screen' => false], 'location_city' => ['label' => 'City', 'screen' => false], 'location_state' => ['label' => 'State', 'screen' => false], 'location_zip' => ['label' => 'Postal code', 'screen' => false], 'location_country' => ['label' => 'Country', 'screen' => false], 'location_primary' => ['label' => 'Primary', 'screen' => false, 'csv' => function ($row) { return intval($row['location_primary']) ? 'Yes' : 'No'; }], ]; $csv_name = 'Geography ' . implode(' ', $path); if (reportExporting('areas') && $level <= 1) { reportCsv($csv_name . ($level === 0 ? ' by country' : ' by ' . strtolower($term)), $area_columns, $areas); } if (reportExporting('cities') && $level <= 2) { reportCsv($csv_name . ' by city', $city_columns, $cities); } if (reportExporting($level === 3 ? null : 'locations') && $level >= 2) { reportCsv($csv_name . ' locations', $location_columns, $shown); } require_once "includes/inc_report_layout.php"; // The figures $placed_clients = count(array_unique(array_column($all, 'client_id'))); $shown_clients = count(array_unique(array_column($shown, 'client_id'))); $next = [0 => $countries, 1 => $regions, 2 => $cities][$level] ?? []; $next_named = array_values(array_filter($next, function ($row) { return $row['key'] !== '-'; })); $next_label = [0 => 'Countries', 1 => $terms, 2 => 'Cities'][$level] ?? ''; // No city, a country ITFlow does not know, or a US state or Canadian province it cannot match $country_names = reportGeoCountries()['names']; $gaps = count(array_filter($shown, function ($location) use ($country_names) { $known = reportGeoRegions($location['country'])['names']; return $location['city'] === '-' || !isset($country_names[$location['country']]) || ($known && !isset($known[$location['region']])); })); $stats = [ ['label' => 'Clients', 'value' => reportCount($shown_clients), 'sub' => $level === 0 ? ($client_count > $placed_clients ? reportCount($client_count - $placed_clients) . ' with no location' : 'All have a location') : reportPercent(reportShare($shown_clients, $placed_clients, 0)) . ' of clients'], ['label' => 'Locations', 'value' => reportCount(count($shown)), 'sub' => $shown_clients ? number_format(count($shown) / $shown_clients, 1) . ' per client' : ''], ]; if ($next_label !== '') { $stats[] = ['label' => $next_label, 'value' => reportCount(count($next_named)), 'sub' => $next_named ? 'Most: ' . escapeHtml($next_named[0]['name']) . ' (' . geographyNoun($next_named[0]['clients'], 'client') . ')' : '']; } $stats[] = ['label' => 'Incomplete addresses', 'value' => reportCount($gaps), 'tone' => $gaps ? 'warning' : 'success', 'sub' => '' . ($gaps ? 'No city, or a country or state not recognised' : 'Every location placed') . '']; // The map: the world by country, or this country's states - shaded by clients or locations $map_name = $level === 0 ? 'world' : (REPORT_GEO_MAPS[$country] ?? ''); $map_rows = $level === 0 ? $countries : $regions; $values = $titles = $links = []; $off_map = 0; if ($map_name !== '') { $on_map = reportGeoMap($map_name)['regions'] ?? []; foreach ($map_rows as $row) { if (!isset($on_map[$row['key']])) { $off_map += $row['locations']; continue; } $values[$row['key']] = $row[$by]; $titles[$row['key']] = $row['name'] . ': ' . geographyNoun($row['clients'], 'client') . ', ' . geographyNoun($row['locations'], 'location'); $links[$row['key']] = $level === 0 ? $drill($row['key']) : $drill($country, $row['key']); } } $shade = ''; foreach (['clients' => 'Clients', 'locations' => 'Locations'] as $key => $text) { $shade .= '' . $text . ''; } $shade .= ''; // Without a map, the places a step down as a chart $chart_rows = $map_name === '' && $level <= 2 ? array_slice($level === 1 ? $regions : $cities, 0, 12) : []; $export = []; if ($level <= 1) { $export['areas'] = $level === 0 ? 'By country' : 'By ' . strtolower($term); } if ($level <= 2) { $export['cities'] = 'By city'; } if ($level === 2) { $export['locations'] = 'Locations'; } $controls = reportSelectControl('country', $country, ['all' => 'All countries'] + array_column($countries, 'name', 'key'), 'fa-globe-americas', '', 'col-sm-6 col-lg-auto'); if ($level >= 1) { $controls .= reportSelectControl('region', $region, ['' => 'All ' . strtolower($terms)] + array_column($regions, 'name', 'key'), 'fa-map', '', 'col-sm-6 col-lg-auto'); } if ($level >= 2) { $controls .= reportSelectControl('city', $city, ['' => 'All cities'] + array_column($cities, 'name', 'key'), 'fa-city', '', 'col-sm-6 col-lg-auto'); } $controls .= reportSwitchControl('leads', $leads, 'Include leads'); ?>
implode(' · ', $path), 'controls' => $controls, 'export' => $level === 3 ? true : $export, 'keep' => ['at' => "$country|$region"] + ($by === 'locations' ? ['by' => 'locations'] : []), 'print_meta' => implode(' · ', $path), ]); ?>
= 2 ? $region : '', ($level === 0 ? 'Countries' : $terms) . ' shaded by ' . $by) ?>
'bar', 'horizontal' => true, 'labels' => array_column($chart_rows, 'name'), 'series' => [['label' => $by === 'clients' ? 'Clients' : 'Locations', 'data' => array_column($chart_rows, $by), 'tone' => 'accent']], 'format' => 'count', 'legend' => false, ], '', 'How many ' . $by . ' are in each place') ?>
reportEmpty('No locations yet - add them on each client\'s Locations page.', 'fa-map-marker-alt', 'secondary')]) ?> 25, 'export_table' => 'cities']) ?> = 2) { ?> 100, 'export_table' => 'locations']) ?>