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; 34*a020b8bdSGreg 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 * @param bool $country 778add1155SRico Sonntag * 78ac701fbdSGreg Roach * @return array<int|string,int> 798add1155SRico Sonntag */ 808add1155SRico Sonntag private function queryFactPlaces(string $fact, string $what = 'ALL', bool $country = false): array 818add1155SRico Sonntag { 828add1155SRico Sonntag $rows = []; 838add1155SRico Sonntag 848add1155SRico Sonntag if ($what === 'INDI') { 857aab89b4SGreg Roach $rows = DB::table('individuals') 867aab89b4SGreg Roach ->select(['i_gedcom as tree']) 877aab89b4SGreg Roach ->where('i_file', '=', $this->tree->id()) 887aab89b4SGreg Roach ->where('i_gedcom', 'LIKE', "%\n2 PLAC %") 897aab89b4SGreg Roach ->get() 907aab89b4SGreg Roach ->all(); 918add1155SRico Sonntag } elseif ($what === 'FAM') { 927aab89b4SGreg Roach $rows = DB::table('families')->select(['f_gedcom as tree']) 937aab89b4SGreg Roach ->where('f_file', '=', $this->tree->id()) 947aab89b4SGreg Roach ->where('f_gedcom', 'LIKE', "%\n2 PLAC %") 957aab89b4SGreg Roach ->get() 967aab89b4SGreg Roach ->all(); 978add1155SRico Sonntag } 988add1155SRico Sonntag 998add1155SRico Sonntag $placelist = []; 1008add1155SRico Sonntag 1018add1155SRico Sonntag foreach ($rows as $row) { 102d72b284aSGreg Roach if (preg_match('/\n1 ' . $fact . '(?:\n[2-9].*)*\n2 PLAC (.+)/', $row->tree, $match)) { 1038add1155SRico Sonntag if ($country) { 1048add1155SRico Sonntag $tmp = explode(Gedcom::PLACE_SEPARATOR, $match[1]); 1058add1155SRico Sonntag $place = end($tmp); 1068add1155SRico Sonntag } else { 1078add1155SRico Sonntag $place = $match[1]; 1088add1155SRico Sonntag } 1098add1155SRico Sonntag 1107aab89b4SGreg Roach $placelist[$place] = ($placelist[$place] ?? 0) + 1; 1118add1155SRico Sonntag } 1128add1155SRico Sonntag } 1138add1155SRico Sonntag 1148add1155SRico Sonntag return $placelist; 1158add1155SRico Sonntag } 1168add1155SRico Sonntag 1178add1155SRico Sonntag /** 1188add1155SRico Sonntag * Get the top 10 places list. 1198add1155SRico Sonntag * 120f78da678SGreg Roach * @param array<int> $places 1218add1155SRico Sonntag * 122f78da678SGreg Roach * @return array<array<string,int|Place>> 1238add1155SRico Sonntag */ 1248add1155SRico Sonntag private function getTop10Places(array $places): array 1258add1155SRico Sonntag { 1268add1155SRico Sonntag $top10 = []; 1278add1155SRico Sonntag $i = 0; 1288add1155SRico Sonntag 1298add1155SRico Sonntag arsort($places); 1308add1155SRico Sonntag 1318add1155SRico Sonntag foreach ($places as $place => $count) { 132ac701fbdSGreg Roach $tmp = new Place((string) $place, $this->tree); 1338add1155SRico Sonntag $top10[] = [ 1348add1155SRico Sonntag 'place' => $tmp, 1358add1155SRico Sonntag 'count' => $count, 1368add1155SRico Sonntag ]; 1378add1155SRico Sonntag 1388add1155SRico Sonntag ++$i; 1398add1155SRico Sonntag 1408add1155SRico Sonntag if ($i === 10) { 1418add1155SRico Sonntag break; 1428add1155SRico Sonntag } 1438add1155SRico Sonntag } 1448add1155SRico Sonntag 1458add1155SRico Sonntag return $top10; 1468add1155SRico Sonntag } 1478add1155SRico Sonntag 1488add1155SRico Sonntag /** 1498add1155SRico Sonntag * Renders the top 10 places list. 1508add1155SRico Sonntag * 151ac701fbdSGreg Roach * @param array<int|string,int> $places 1528add1155SRico Sonntag * 1538add1155SRico Sonntag * @return string 1548add1155SRico Sonntag */ 1558add1155SRico Sonntag private function renderTop10(array $places): string 1568add1155SRico Sonntag { 1578add1155SRico Sonntag $top10Records = $this->getTop10Places($places); 1588add1155SRico Sonntag 1598add1155SRico Sonntag return view( 1608add1155SRico Sonntag 'statistics/other/top10-list', 1618add1155SRico Sonntag [ 1628add1155SRico Sonntag 'records' => $top10Records, 1638add1155SRico Sonntag ] 1648add1155SRico Sonntag ); 1658add1155SRico Sonntag } 1668add1155SRico Sonntag 1678add1155SRico Sonntag /** 1688add1155SRico Sonntag * A list of common birth places. 1698add1155SRico Sonntag * 1708add1155SRico Sonntag * @return string 1718add1155SRico Sonntag */ 1728add1155SRico Sonntag public function commonBirthPlacesList(): string 1738add1155SRico Sonntag { 1748add1155SRico Sonntag $places = $this->queryFactPlaces('BIRT', 'INDI'); 1758add1155SRico Sonntag return $this->renderTop10($places); 1768add1155SRico Sonntag } 1778add1155SRico Sonntag 1788add1155SRico Sonntag /** 1798add1155SRico Sonntag * A list of common death places. 1808add1155SRico Sonntag * 1818add1155SRico Sonntag * @return string 1828add1155SRico Sonntag */ 1838add1155SRico Sonntag public function commonDeathPlacesList(): string 1848add1155SRico Sonntag { 1858add1155SRico Sonntag $places = $this->queryFactPlaces('DEAT', 'INDI'); 1868add1155SRico Sonntag return $this->renderTop10($places); 1878add1155SRico Sonntag } 1888add1155SRico Sonntag 1898add1155SRico Sonntag /** 1908add1155SRico Sonntag * A list of common marriage places. 1918add1155SRico Sonntag * 1928add1155SRico Sonntag * @return string 1938add1155SRico Sonntag */ 1948add1155SRico Sonntag public function commonMarriagePlacesList(): string 1958add1155SRico Sonntag { 1968add1155SRico Sonntag $places = $this->queryFactPlaces('MARR', 'FAM'); 1978add1155SRico Sonntag return $this->renderTop10($places); 1988add1155SRico Sonntag } 1998add1155SRico Sonntag 2008add1155SRico Sonntag /** 2018add1155SRico Sonntag * A list of common countries. 2028add1155SRico Sonntag * 2038add1155SRico Sonntag * @return string 2048add1155SRico Sonntag */ 2058add1155SRico Sonntag public function commonCountriesList(): string 2068add1155SRico Sonntag { 207*a020b8bdSGreg Roach $countries = DB::table('places') 208*a020b8bdSGreg Roach ->join('placelinks', static function (JoinClause $join): void { 209*a020b8bdSGreg Roach $join 210*a020b8bdSGreg Roach ->on('pl_file', '=', 'p_file') 211*a020b8bdSGreg Roach ->on('pl_p_id', '=', 'p_id'); 212*a020b8bdSGreg Roach }) 213*a020b8bdSGreg Roach ->where('p_file', '=', $this->tree->id()) 214*a020b8bdSGreg Roach ->where('p_parent_id', '=', 0) 215*a020b8bdSGreg Roach ->groupBy(['p_place']) 216*a020b8bdSGreg Roach ->orderByDesc(new Expression('COUNT(*)')) 217*a020b8bdSGreg Roach ->orderBy('p_place') 218*a020b8bdSGreg Roach ->pluck(new Expression('COUNT(*)'), 'p_place') 219*a020b8bdSGreg Roach ->map(static fn(string $col): int => (int) $col) 220*a020b8bdSGreg Roach ->all(); 2218add1155SRico Sonntag 222320f6a24SGreg Roach if ($countries === []) { 223c908635bSGreg Roach return I18N::translate('This information is not available.'); 2248add1155SRico Sonntag } 2258add1155SRico Sonntag 2268add1155SRico Sonntag $top10 = []; 2278add1155SRico Sonntag $i = 1; 2288add1155SRico Sonntag 2298add1155SRico Sonntag // Get the country names for each language 2308add1155SRico Sonntag $country_names = []; 23190a2f718SGreg Roach $old_language = I18N::languageTag(); 23290a2f718SGreg Roach 2338add1155SRico Sonntag foreach (I18N::activeLocales() as $locale) { 2348add1155SRico Sonntag I18N::init($locale->languageTag()); 23593ccd686SRico Sonntag $all_countries = $this->country_service->getAllCountries(); 2368add1155SRico Sonntag foreach ($all_countries as $country_code => $country_name) { 2378add1155SRico Sonntag $country_names[$country_name] = $country_code; 2388add1155SRico Sonntag } 2398add1155SRico Sonntag } 2408add1155SRico Sonntag 24190a2f718SGreg Roach I18N::init($old_language); 2428add1155SRico Sonntag 2438add1155SRico Sonntag $all_db_countries = []; 244*a020b8bdSGreg Roach 245*a020b8bdSGreg Roach foreach ($countries as $country => $count) { 2466ccdf4f0SGreg Roach if (array_key_exists($country, $country_names)) { 2478add1155SRico Sonntag if (isset($all_db_countries[$country_names[$country]][$country])) { 248*a020b8bdSGreg Roach $all_db_countries[$country_names[$country]][$country] += (int) $count; 2498add1155SRico Sonntag } else { 250*a020b8bdSGreg Roach $all_db_countries[$country_names[$country]][$country] = (int) $count; 2518add1155SRico Sonntag } 2528add1155SRico Sonntag } 2538add1155SRico Sonntag } 2548add1155SRico Sonntag 2558add1155SRico Sonntag // get all the user’s countries names 25693ccd686SRico Sonntag $all_countries = $this->country_service->getAllCountries(); 2578add1155SRico Sonntag 2588add1155SRico Sonntag foreach ($all_db_countries as $country_code => $country) { 2598add1155SRico Sonntag foreach ($country as $country_name => $tot) { 2608add1155SRico Sonntag $tmp = new Place($country_name, $this->tree); 2618add1155SRico Sonntag 2628add1155SRico Sonntag $top10[] = [ 2638add1155SRico Sonntag 'place' => $tmp, 2648add1155SRico Sonntag 'count' => $tot, 2658add1155SRico Sonntag 'name' => $all_countries[$country_code], 2668add1155SRico Sonntag ]; 2678add1155SRico Sonntag } 2688add1155SRico Sonntag 2698add1155SRico Sonntag if ($i++ === 10) { 2708add1155SRico Sonntag break; 2718add1155SRico Sonntag } 2728add1155SRico Sonntag } 2738add1155SRico Sonntag 2748add1155SRico Sonntag return view( 2758add1155SRico Sonntag 'statistics/other/top10-list', 2768add1155SRico Sonntag [ 2778add1155SRico Sonntag 'records' => $top10, 2788add1155SRico Sonntag ] 2798add1155SRico Sonntag ); 2808add1155SRico Sonntag } 2818add1155SRico Sonntag 2828add1155SRico Sonntag /** 2838add1155SRico Sonntag * Count total places. 2848add1155SRico Sonntag * 2858add1155SRico Sonntag * @return int 2868add1155SRico Sonntag */ 2878add1155SRico Sonntag private function totalPlacesQuery(): int 2888add1155SRico Sonntag { 2898add1155SRico Sonntag return DB::table('places') 2908add1155SRico Sonntag ->where('p_file', '=', $this->tree->id()) 2918add1155SRico Sonntag ->count(); 2928add1155SRico Sonntag } 2938add1155SRico Sonntag 2948add1155SRico Sonntag /** 2958add1155SRico Sonntag * Count total places. 2968add1155SRico Sonntag * 2978add1155SRico Sonntag * @return string 2988add1155SRico Sonntag */ 2998add1155SRico Sonntag public function totalPlaces(): string 3008add1155SRico Sonntag { 3018add1155SRico Sonntag return I18N::number($this->totalPlacesQuery()); 3028add1155SRico Sonntag } 3038add1155SRico Sonntag 3048add1155SRico Sonntag /** 3058add1155SRico Sonntag * Create a chart showing where events occurred. 3068add1155SRico Sonntag * 3078add1155SRico Sonntag * @param string $chart_shows 3088add1155SRico Sonntag * @param string $chart_type 3098add1155SRico Sonntag * @param string $surname 3108add1155SRico Sonntag * 3118add1155SRico Sonntag * @return string 3128add1155SRico Sonntag */ 3138add1155SRico Sonntag public function chartDistribution( 3148add1155SRico Sonntag string $chart_shows = 'world', 3158add1155SRico Sonntag string $chart_type = '', 3168add1155SRico Sonntag string $surname = '' 3178add1155SRico Sonntag ): string { 318*a020b8bdSGreg Roach return (new ChartDistribution($this->tree, $this->country_service, $this->individual_repository)) 31988de55fdSRico Sonntag ->chartDistribution($chart_shows, $chart_type, $surname); 3208add1155SRico Sonntag } 3218add1155SRico Sonntag} 322