1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Statistics\Google; 21 22use Fisharebest\Localization\Locale\LocaleInterface; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Module\ModuleThemeInterface; 25use Fisharebest\Webtrees\Statistics\Service\ColorService; 26use Fisharebest\Webtrees\Tree; 27use Psr\Http\Message\ServerRequestInterface; 28 29use function app; 30use function assert; 31use function count; 32 33/** 34 * A chart showing the top surnames. 35 */ 36class ChartCommonSurname 37{ 38 /** 39 * @var ModuleThemeInterface 40 */ 41 private $theme; 42 43 /** 44 * @var string 45 */ 46 private $surname_tradition; 47 48 /** 49 * @var ColorService 50 */ 51 private $color_service; 52 53 /** 54 * Constructor. 55 * 56 * @param Tree $tree 57 */ 58 public function __construct(Tree $tree) 59 { 60 $this->theme = app(ModuleThemeInterface::class); 61 $this->surname_tradition = $tree->getPreference('SURNAME_TRADITION'); 62 $this->color_service = new ColorService(); 63 } 64 65 /** 66 * Count up the different versions of a name and returns the one with the most matches. Takes 67 * different surname traditions into account. 68 * 69 * @param array $surns 70 * 71 * @return array [ name, count ] 72 */ 73 private function getTopNameAndCount(array $surns): array 74 { 75 $max_name = 0; 76 $count_per = 0; 77 $top_name = ''; 78 79 foreach ($surns as $spfxsurn => $count) { 80 $per = $count; 81 $count_per += $per; 82 83 // select most common surname from all variants 84 if ($per > $max_name) { 85 $max_name = $per; 86 $top_name = $spfxsurn; 87 } 88 } 89 90 if ($this->surname_tradition === 'polish') { 91 // Most common surname should be in male variant (Kowalski, not Kowalska) 92 $top_name = preg_replace( 93 [ 94 '/ska$/', 95 '/cka$/', 96 '/dzka$/', 97 '/żka$/', 98 ], 99 [ 100 'ski', 101 'cki', 102 'dzki', 103 'żki', 104 ], 105 $top_name 106 ); 107 } 108 109 return [ 110 $top_name, 111 $count_per 112 ]; 113 } 114 115 /** 116 * Create a chart of common surnames. 117 * 118 * @param int $tot_indi The total number of individuals 119 * @param array $all_surnames The list of common surnames 120 * @param string|null $color_from 121 * @param string|null $color_to 122 * 123 * @return string 124 */ 125 public function chartCommonSurnames( 126 int $tot_indi, 127 array $all_surnames, 128 string $color_from = null, 129 string $color_to = null 130 ): string { 131 $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values'); 132 $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values'); 133 $color_from = $color_from ?? $chart_color1; 134 $color_to = $color_to ?? $chart_color2; 135 136 $tot = 0; 137 foreach ($all_surnames as $surn => $surnames) { 138 $tot += array_sum($surnames); 139 } 140 141 $data = [ 142 [ 143 I18N::translate('Name'), 144 I18N::translate('Total') 145 ], 146 ]; 147 148 foreach ($all_surnames as $surns) { 149 $data[] = $this->getTopNameAndCount($surns); 150 } 151 152 $data[] = [ 153 I18N::translate('Other'), 154 $tot_indi - $tot 155 ]; 156 157 $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); 158 159 $locale = app(ServerRequestInterface::class)->getAttribute('locale'); 160 assert($locale instanceof LocaleInterface); 161 162 return view('statistics/other/charts/pie', [ 163 'title' => null, 164 'data' => $data, 165 'colors' => $colors, 166 'language' => $locale->languageTag(), 167 ]); 168 } 169} 170