18add1155SRico Sonntag<?php 23976b470SGreg Roach 38add1155SRico Sonntag/** 48add1155SRico Sonntag * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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\Google; 218add1155SRico Sonntag 228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N; 2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService; 24f78da678SGreg Roachuse Fisharebest\Webtrees\SurnameTradition\PolishSurnameTradition; 25f78da678SGreg Roachuse Fisharebest\Webtrees\SurnameTradition\SurnameTraditionInterface; 268add1155SRico Sonntag 2734b20f29SGreg Roachuse function array_sum; 2871378461SGreg Roachuse function count; 2934b20f29SGreg Roachuse function preg_replace; 3034b20f29SGreg Roachuse function view; 3171378461SGreg Roach 328add1155SRico Sonntag/** 3393ccd686SRico Sonntag * A chart showing the top surnames. 348add1155SRico Sonntag */ 3593ccd686SRico Sonntagclass ChartCommonSurname 368add1155SRico Sonntag{ 3734b20f29SGreg Roach private ColorService $color_service; 3893ccd686SRico Sonntag 39f78da678SGreg Roach private SurnameTraditionInterface $surname_tradition; 40f78da678SGreg Roach 4193ccd686SRico Sonntag /** 42f78da678SGreg Roach * @param ColorService $color_service 43f78da678SGreg Roach * @param SurnameTraditionInterface $surname_tradition 448add1155SRico Sonntag */ 45f78da678SGreg Roach public function __construct(ColorService $color_service, SurnameTraditionInterface $surname_tradition) 468add1155SRico Sonntag { 47f78da678SGreg Roach $this->surname_tradition = $surname_tradition; 48f78da678SGreg Roach $this->color_service = $color_service; 498add1155SRico Sonntag } 508add1155SRico Sonntag 518add1155SRico Sonntag /** 5288de55fdSRico Sonntag * Count up the different versions of a name and returns the one with the most matches. Takes 5388de55fdSRico Sonntag * different surname traditions into account. 548add1155SRico Sonntag * 5552664097SGreg Roach * @param array<string,int> $surns 568add1155SRico Sonntag * 5752664097SGreg Roach * @return array{0:string,1:int} 588add1155SRico Sonntag */ 5988de55fdSRico Sonntag private function getTopNameAndCount(array $surns): array 6088de55fdSRico Sonntag { 618add1155SRico Sonntag $max_name = 0; 6288de55fdSRico Sonntag $count_per = 0; 638add1155SRico Sonntag $top_name = ''; 648add1155SRico Sonntag 658add1155SRico Sonntag foreach ($surns as $spfxsurn => $count) { 668add1155SRico Sonntag $per = $count; 678add1155SRico Sonntag $count_per += $per; 688add1155SRico Sonntag 698add1155SRico Sonntag // select most common surname from all variants 708add1155SRico Sonntag if ($per > $max_name) { 718add1155SRico Sonntag $max_name = $per; 728add1155SRico Sonntag $top_name = $spfxsurn; 738add1155SRico Sonntag } 748add1155SRico Sonntag } 758add1155SRico Sonntag 76f78da678SGreg Roach if ($this->surname_tradition instanceof PolishSurnameTradition) { 778add1155SRico Sonntag // Most common surname should be in male variant (Kowalski, not Kowalska) 7888de55fdSRico Sonntag $top_name = preg_replace( 7988de55fdSRico Sonntag [ 808add1155SRico Sonntag '/ska$/', 818add1155SRico Sonntag '/cka$/', 828add1155SRico Sonntag '/dzka$/', 838add1155SRico Sonntag '/żka$/', 8488de55fdSRico Sonntag ], 8588de55fdSRico Sonntag [ 868add1155SRico Sonntag 'ski', 878add1155SRico Sonntag 'cki', 888add1155SRico Sonntag 'dzki', 898add1155SRico Sonntag 'żki', 9088de55fdSRico Sonntag ], 9188de55fdSRico Sonntag $top_name 9288de55fdSRico Sonntag ); 938add1155SRico Sonntag } 948add1155SRico Sonntag 9588de55fdSRico Sonntag return [ 96fc26b4f6SGreg Roach (string) $top_name, 970d047a8cSGreg Roach $count_per 9888de55fdSRico Sonntag ]; 998add1155SRico Sonntag } 1008add1155SRico Sonntag 10188de55fdSRico Sonntag /** 10288de55fdSRico Sonntag * Create a chart of common surnames. 10388de55fdSRico Sonntag * 10488de55fdSRico Sonntag * @param int $tot_indi The total number of individuals 10576d39c55SGreg Roach * @param array<array<int>> $all_surnames The list of common surnames 10688de55fdSRico Sonntag * @param string|null $color_from 10788de55fdSRico Sonntag * @param string|null $color_to 10888de55fdSRico Sonntag * 10988de55fdSRico Sonntag * @return string 11088de55fdSRico Sonntag */ 11188de55fdSRico Sonntag public function chartCommonSurnames( 11288de55fdSRico Sonntag int $tot_indi, 11388de55fdSRico Sonntag array $all_surnames, 1142c6f1bd5SGreg Roach string|null $color_from = null, 115*b50dba3aSGreg Roach string|null $color_to = null 11688de55fdSRico Sonntag ): string { 1173529c469SGreg Roach $color_from ??= 'ffffff'; 1183529c469SGreg Roach $color_to ??= '84beff'; 1198add1155SRico Sonntag 12088de55fdSRico Sonntag $tot = 0; 121d85de00fSGreg Roach foreach ($all_surnames as $surnames) { 12288de55fdSRico Sonntag $tot += array_sum($surnames); 12388de55fdSRico Sonntag } 12488de55fdSRico Sonntag 12588de55fdSRico Sonntag $data = [ 12688de55fdSRico Sonntag [ 12788de55fdSRico Sonntag I18N::translate('Name'), 12888de55fdSRico Sonntag I18N::translate('Total') 12988de55fdSRico Sonntag ], 13088de55fdSRico Sonntag ]; 13188de55fdSRico Sonntag 13288de55fdSRico Sonntag foreach ($all_surnames as $surns) { 13388de55fdSRico Sonntag $data[] = $this->getTopNameAndCount($surns); 13488de55fdSRico Sonntag } 13588de55fdSRico Sonntag 13688de55fdSRico Sonntag $data[] = [ 13788de55fdSRico Sonntag I18N::translate('Other'), 13888de55fdSRico Sonntag $tot_indi - $tot 13988de55fdSRico Sonntag ]; 14088de55fdSRico Sonntag 1416ccdf4f0SGreg Roach $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); 1428add1155SRico Sonntag 14390a2f718SGreg Roach return view('statistics/other/charts/pie', [ 14488de55fdSRico Sonntag 'title' => null, 14588de55fdSRico Sonntag 'data' => $data, 14688de55fdSRico Sonntag 'colors' => $colors, 14765cf5706SGreg Roach 'language' => I18N::languageTag(), 14890a2f718SGreg Roach ]); 1498add1155SRico Sonntag } 1508add1155SRico Sonntag} 151