18add1155SRico Sonntag<?php 23976b470SGreg Roach 38add1155SRico Sonntag/** 48add1155SRico Sonntag * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 webtrees development team 68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify 78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by 88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or 98add1155SRico Sonntag * (at your option) any later version. 108add1155SRico Sonntag * This program is distributed in the hope that it will be useful, 118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of 128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138add1155SRico Sonntag * GNU General Public License for more details. 148add1155SRico Sonntag * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 168add1155SRico Sonntag */ 17fcfa147eSGreg Roach 188add1155SRico Sonntagdeclare(strict_types=1); 198add1155SRico Sonntag 208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository; 218add1155SRico Sonntag 22c8db8a43SGreg Roachuse Fisharebest\Webtrees\Family; 238add1155SRico Sonntaguse Fisharebest\Webtrees\Gedcom; 248add1155SRico Sonntaguse Fisharebest\Webtrees\I18N; 25c8db8a43SGreg Roachuse Fisharebest\Webtrees\Individual; 26c8db8a43SGreg Roachuse Fisharebest\Webtrees\Location; 278add1155SRico Sonntaguse Fisharebest\Webtrees\Place; 288add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartDistribution; 29f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Repository\Interfaces\IndividualRepositoryInterface; 308add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Repository\Interfaces\PlaceRepositoryInterface; 3193ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CountryService; 328add1155SRico Sonntaguse Fisharebest\Webtrees\Tree; 338add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB; 34a020b8bdSGreg Roachuse Illuminate\Database\Query\Expression; 358add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause; 368add1155SRico Sonntag 3771378461SGreg Roachuse function array_key_exists; 384c78e066SGreg Roachuse function arsort; 394c78e066SGreg Roachuse function end; 404c78e066SGreg Roachuse function explode; 414c78e066SGreg Roachuse function preg_match; 424c78e066SGreg Roachuse function trim; 434c78e066SGreg Roachuse function view; 4471378461SGreg Roach 458add1155SRico Sonntag/** 468add1155SRico Sonntag * A repository providing methods for place related statistics. 478add1155SRico Sonntag */ 488add1155SRico Sonntagclass PlaceRepository implements PlaceRepositoryInterface 498add1155SRico Sonntag{ 504c78e066SGreg Roach private Tree $tree; 514c78e066SGreg Roach 524c78e066SGreg Roach private CountryService $country_service; 538add1155SRico Sonntag 54f78da678SGreg Roach private IndividualRepositoryInterface $individual_repository; 55f78da678SGreg Roach 568add1155SRico Sonntag /** 578add1155SRico Sonntag * @param Tree $tree 584c78e066SGreg Roach * @param CountryService $country_service 59f78da678SGreg Roach * @param IndividualRepositoryInterface $individual_repository 608add1155SRico Sonntag */ 61f78da678SGreg Roach public function __construct( 62f78da678SGreg Roach Tree $tree, 63f78da678SGreg Roach CountryService $country_service, 64f78da678SGreg Roach IndividualRepositoryInterface $individual_repository 65f78da678SGreg Roach ) { 668add1155SRico Sonntag $this->tree = $tree; 674c78e066SGreg Roach $this->country_service = $country_service; 68f78da678SGreg Roach $this->individual_repository = $individual_repository; 698add1155SRico Sonntag } 708add1155SRico Sonntag 718add1155SRico Sonntag /** 728add1155SRico Sonntag * Places 738add1155SRico Sonntag * 748add1155SRico Sonntag * @param string $fact 758add1155SRico Sonntag * @param string $what 768add1155SRico Sonntag * 77*7edfe083SGreg Roach * @return array<int> 788add1155SRico Sonntag */ 79*7edfe083SGreg Roach private function queryFactPlaces(string $fact, string $what): array 808add1155SRico Sonntag { 818add1155SRico Sonntag $rows = []; 828add1155SRico Sonntag 838add1155SRico Sonntag if ($what === 'INDI') { 847aab89b4SGreg Roach $rows = DB::table('individuals') 857aab89b4SGreg Roach ->select(['i_gedcom as tree']) 867aab89b4SGreg Roach ->where('i_file', '=', $this->tree->id()) 877aab89b4SGreg Roach ->where('i_gedcom', 'LIKE', "%\n2 PLAC %") 887aab89b4SGreg Roach ->get() 897aab89b4SGreg Roach ->all(); 908add1155SRico Sonntag } elseif ($what === 'FAM') { 917aab89b4SGreg Roach $rows = DB::table('families')->select(['f_gedcom as tree']) 927aab89b4SGreg Roach ->where('f_file', '=', $this->tree->id()) 937aab89b4SGreg Roach ->where('f_gedcom', 'LIKE', "%\n2 PLAC %") 947aab89b4SGreg Roach ->get() 957aab89b4SGreg Roach ->all(); 968add1155SRico Sonntag } 978add1155SRico Sonntag 988add1155SRico Sonntag $placelist = []; 998add1155SRico Sonntag 1008add1155SRico Sonntag foreach ($rows as $row) { 101d72b284aSGreg Roach if (preg_match('/\n1 ' . $fact . '(?:\n[2-9].*)*\n2 PLAC (.+)/', $row->tree, $match)) { 1028add1155SRico Sonntag $place = $match[1]; 1038add1155SRico Sonntag 1047aab89b4SGreg Roach $placelist[$place] = ($placelist[$place] ?? 0) + 1; 1058add1155SRico Sonntag } 1068add1155SRico Sonntag } 1078add1155SRico Sonntag 1088add1155SRico Sonntag return $placelist; 1098add1155SRico Sonntag } 1108add1155SRico Sonntag 1118add1155SRico Sonntag /** 1128add1155SRico Sonntag * Get the top 10 places list. 1138add1155SRico Sonntag * 114f78da678SGreg Roach * @param array<int> $places 1158add1155SRico Sonntag * 116f78da678SGreg Roach * @return array<array<string,int|Place>> 1178add1155SRico Sonntag */ 1188add1155SRico Sonntag private function getTop10Places(array $places): array 1198add1155SRico Sonntag { 1208add1155SRico Sonntag $top10 = []; 1218add1155SRico Sonntag $i = 0; 1228add1155SRico Sonntag 1238add1155SRico Sonntag arsort($places); 1248add1155SRico Sonntag 1258add1155SRico Sonntag foreach ($places as $place => $count) { 126ac701fbdSGreg Roach $tmp = new Place((string) $place, $this->tree); 1278add1155SRico Sonntag $top10[] = [ 1288add1155SRico Sonntag 'place' => $tmp, 1298add1155SRico Sonntag 'count' => $count, 1308add1155SRico Sonntag ]; 1318add1155SRico Sonntag 1328add1155SRico Sonntag ++$i; 1338add1155SRico Sonntag 1348add1155SRico Sonntag if ($i === 10) { 1358add1155SRico Sonntag break; 1368add1155SRico Sonntag } 1378add1155SRico Sonntag } 1388add1155SRico Sonntag 1398add1155SRico Sonntag return $top10; 1408add1155SRico Sonntag } 1418add1155SRico Sonntag 1428add1155SRico Sonntag /** 1438add1155SRico Sonntag * Renders the top 10 places list. 1448add1155SRico Sonntag * 145ac701fbdSGreg Roach * @param array<int|string,int> $places 1468add1155SRico Sonntag * 1478add1155SRico Sonntag * @return string 1488add1155SRico Sonntag */ 1498add1155SRico Sonntag private function renderTop10(array $places): string 1508add1155SRico Sonntag { 1518add1155SRico Sonntag $top10Records = $this->getTop10Places($places); 1528add1155SRico Sonntag 1538add1155SRico Sonntag return view( 1548add1155SRico Sonntag 'statistics/other/top10-list', 1558add1155SRico Sonntag [ 1568add1155SRico Sonntag 'records' => $top10Records, 1578add1155SRico Sonntag ] 1588add1155SRico Sonntag ); 1598add1155SRico Sonntag } 1608add1155SRico Sonntag 1618add1155SRico Sonntag /** 1628add1155SRico Sonntag * A list of common birth places. 1638add1155SRico Sonntag * 1648add1155SRico Sonntag * @return string 1658add1155SRico Sonntag */ 1668add1155SRico Sonntag public function commonBirthPlacesList(): string 1678add1155SRico Sonntag { 1688add1155SRico Sonntag $places = $this->queryFactPlaces('BIRT', 'INDI'); 1698add1155SRico Sonntag return $this->renderTop10($places); 1708add1155SRico Sonntag } 1718add1155SRico Sonntag 1728add1155SRico Sonntag /** 1738add1155SRico Sonntag * A list of common death places. 1748add1155SRico Sonntag * 1758add1155SRico Sonntag * @return string 1768add1155SRico Sonntag */ 1778add1155SRico Sonntag public function commonDeathPlacesList(): string 1788add1155SRico Sonntag { 1798add1155SRico Sonntag $places = $this->queryFactPlaces('DEAT', 'INDI'); 1808add1155SRico Sonntag return $this->renderTop10($places); 1818add1155SRico Sonntag } 1828add1155SRico Sonntag 1838add1155SRico Sonntag /** 1848add1155SRico Sonntag * A list of common marriage places. 1858add1155SRico Sonntag * 1868add1155SRico Sonntag * @return string 1878add1155SRico Sonntag */ 1888add1155SRico Sonntag public function commonMarriagePlacesList(): string 1898add1155SRico Sonntag { 1908add1155SRico Sonntag $places = $this->queryFactPlaces('MARR', 'FAM'); 1918add1155SRico Sonntag return $this->renderTop10($places); 1928add1155SRico Sonntag } 1938add1155SRico Sonntag 1948add1155SRico Sonntag /** 1958add1155SRico Sonntag * A list of common countries. 1968add1155SRico Sonntag * 1978add1155SRico Sonntag * @return string 1988add1155SRico Sonntag */ 1998add1155SRico Sonntag public function commonCountriesList(): string 2008add1155SRico Sonntag { 201a020b8bdSGreg Roach $countries = DB::table('places') 202a020b8bdSGreg Roach ->join('placelinks', static function (JoinClause $join): void { 203a020b8bdSGreg Roach $join 204a020b8bdSGreg Roach ->on('pl_file', '=', 'p_file') 205a020b8bdSGreg Roach ->on('pl_p_id', '=', 'p_id'); 206a020b8bdSGreg Roach }) 207a020b8bdSGreg Roach ->where('p_file', '=', $this->tree->id()) 208a020b8bdSGreg Roach ->where('p_parent_id', '=', 0) 209a020b8bdSGreg Roach ->groupBy(['p_place']) 210a020b8bdSGreg Roach ->orderByDesc(new Expression('COUNT(*)')) 211a020b8bdSGreg Roach ->orderBy('p_place') 212a020b8bdSGreg Roach ->pluck(new Expression('COUNT(*)'), 'p_place') 213a020b8bdSGreg Roach ->map(static fn(string $col): int => (int) $col) 214a020b8bdSGreg Roach ->all(); 2158add1155SRico Sonntag 216320f6a24SGreg Roach if ($countries === []) { 217c908635bSGreg Roach return I18N::translate('This information is not available.'); 2188add1155SRico Sonntag } 2198add1155SRico Sonntag 2208add1155SRico Sonntag $top10 = []; 2218add1155SRico Sonntag $i = 1; 2228add1155SRico Sonntag 2238add1155SRico Sonntag // Get the country names for each language 2248add1155SRico Sonntag $country_names = []; 22590a2f718SGreg Roach $old_language = I18N::languageTag(); 22690a2f718SGreg Roach 2278add1155SRico Sonntag foreach (I18N::activeLocales() as $locale) { 2288add1155SRico Sonntag I18N::init($locale->languageTag()); 22993ccd686SRico Sonntag $all_countries = $this->country_service->getAllCountries(); 2308add1155SRico Sonntag foreach ($all_countries as $country_code => $country_name) { 2318add1155SRico Sonntag $country_names[$country_name] = $country_code; 2328add1155SRico Sonntag } 2338add1155SRico Sonntag } 2348add1155SRico Sonntag 23590a2f718SGreg Roach I18N::init($old_language); 2368add1155SRico Sonntag 2378add1155SRico Sonntag $all_db_countries = []; 238a020b8bdSGreg Roach 239a020b8bdSGreg Roach foreach ($countries as $country => $count) { 2406ccdf4f0SGreg Roach if (array_key_exists($country, $country_names)) { 2418add1155SRico Sonntag if (isset($all_db_countries[$country_names[$country]][$country])) { 242a020b8bdSGreg Roach $all_db_countries[$country_names[$country]][$country] += (int) $count; 2438add1155SRico Sonntag } else { 244a020b8bdSGreg Roach $all_db_countries[$country_names[$country]][$country] = (int) $count; 2458add1155SRico Sonntag } 2468add1155SRico Sonntag } 2478add1155SRico Sonntag } 2488add1155SRico Sonntag 2498add1155SRico Sonntag // get all the user’s countries names 25093ccd686SRico Sonntag $all_countries = $this->country_service->getAllCountries(); 2518add1155SRico Sonntag 2528add1155SRico Sonntag foreach ($all_db_countries as $country_code => $country) { 2538add1155SRico Sonntag foreach ($country as $country_name => $tot) { 2548add1155SRico Sonntag $tmp = new Place($country_name, $this->tree); 2558add1155SRico Sonntag 2568add1155SRico Sonntag $top10[] = [ 2578add1155SRico Sonntag 'place' => $tmp, 2588add1155SRico Sonntag 'count' => $tot, 2598add1155SRico Sonntag 'name' => $all_countries[$country_code], 2608add1155SRico Sonntag ]; 2618add1155SRico Sonntag } 2628add1155SRico Sonntag 2638add1155SRico Sonntag if ($i++ === 10) { 2648add1155SRico Sonntag break; 2658add1155SRico Sonntag } 2668add1155SRico Sonntag } 2678add1155SRico Sonntag 2688add1155SRico Sonntag return view( 2698add1155SRico Sonntag 'statistics/other/top10-list', 2708add1155SRico Sonntag [ 2718add1155SRico Sonntag 'records' => $top10, 2728add1155SRico Sonntag ] 2738add1155SRico Sonntag ); 2748add1155SRico Sonntag } 2758add1155SRico Sonntag 2768add1155SRico Sonntag /** 2778add1155SRico Sonntag * Count total places. 2788add1155SRico Sonntag * 2798add1155SRico Sonntag * @return int 2808add1155SRico Sonntag */ 2818add1155SRico Sonntag private function totalPlacesQuery(): int 2828add1155SRico Sonntag { 2838add1155SRico Sonntag return DB::table('places') 2848add1155SRico Sonntag ->where('p_file', '=', $this->tree->id()) 2858add1155SRico Sonntag ->count(); 2868add1155SRico Sonntag } 2878add1155SRico Sonntag 2888add1155SRico Sonntag /** 2898add1155SRico Sonntag * Count total places. 2908add1155SRico Sonntag * 2918add1155SRico Sonntag * @return string 2928add1155SRico Sonntag */ 2938add1155SRico Sonntag public function totalPlaces(): string 2948add1155SRico Sonntag { 2958add1155SRico Sonntag return I18N::number($this->totalPlacesQuery()); 2968add1155SRico Sonntag } 2978add1155SRico Sonntag 2988add1155SRico Sonntag /** 2998add1155SRico Sonntag * Create a chart showing where events occurred. 3008add1155SRico Sonntag * 3018add1155SRico Sonntag * @param string $chart_shows 3028add1155SRico Sonntag * @param string $chart_type 3038add1155SRico Sonntag * @param string $surname 3048add1155SRico Sonntag * 3058add1155SRico Sonntag * @return string 3068add1155SRico Sonntag */ 3078add1155SRico Sonntag public function chartDistribution( 3088add1155SRico Sonntag string $chart_shows = 'world', 3098add1155SRico Sonntag string $chart_type = '', 3108add1155SRico Sonntag string $surname = '' 3118add1155SRico Sonntag ): string { 312a020b8bdSGreg Roach return (new ChartDistribution($this->tree, $this->country_service, $this->individual_repository)) 31388de55fdSRico Sonntag ->chartDistribution($chart_shows, $chart_type, $surname); 3148add1155SRico Sonntag } 3158add1155SRico Sonntag} 316