1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Statistics\Google; 19 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Module\ModuleThemeInterface; 22use Fisharebest\Webtrees\Statistics\AbstractGoogle; 23use Fisharebest\Webtrees\Theme; 24use Fisharebest\Webtrees\Tree; 25 26/** 27 * 28 */ 29class ChartCommonSurname extends AbstractGoogle 30{ 31 /** 32 * @var Tree 33 */ 34 private $tree; 35 36 /** 37 * Constructor. 38 * 39 * @param Tree $tree 40 */ 41 public function __construct(Tree $tree) 42 { 43 $this->tree = $tree; 44 } 45 46 /** 47 * Create a chart of common surnames. 48 * 49 * @param int $tot_indi The total number of individuals 50 * @param array $all_surnames The list of common surnames 51 * @param string|null $size 52 * @param string|null $color_from 53 * @param string|null $color_to 54 * 55 * @return string 56 */ 57 public function chartCommonSurnames( 58 int $tot_indi, 59 array $all_surnames, 60 string $size = null, 61 string $color_from = null, 62 string $color_to = null 63 ): string { 64 $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values'); 65 $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values'); 66 $chart_x = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x'); 67 $chart_y = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y'); 68 69 $size = $size ?? ($chart_x . 'x' . $chart_y); 70 $color_from = $color_from ?? $chart_color1; 71 $color_to = $color_to ?? $chart_color2; 72 $sizes = explode('x', $size); 73 74 if (empty($all_surnames)) { 75 return ''; 76 } 77 78 $surname_tradition = $this->tree->getPreference('SURNAME_TRADITION'); 79 80 $tot = 0; 81 82 foreach ($all_surnames as $surn => $surnames) { 83 $tot += array_sum($surnames); 84 } 85 86 $chd = ''; 87 $chl = []; 88 89 /** @var array $surns */ 90 foreach ($all_surnames as $surns) { 91 $count_per = 0; 92 $max_name = 0; 93 $top_name = ''; 94 95 foreach ($surns as $spfxsurn => $count) { 96 $per = $count; 97 $count_per += $per; 98 99 // select most common surname from all variants 100 if ($per > $max_name) { 101 $max_name = $per; 102 $top_name = $spfxsurn; 103 } 104 } 105 106 if ($surname_tradition === 'polish') { 107 // Most common surname should be in male variant (Kowalski, not Kowalska) 108 $top_name = preg_replace([ 109 '/ska$/', 110 '/cka$/', 111 '/dzka$/', 112 '/żka$/', 113 ], [ 114 'ski', 115 'cki', 116 'dzki', 117 'żki', 118 ], $top_name); 119 } 120 121 $per = intdiv(100 * $count_per, $tot_indi); 122 $chd .= $this->arrayToExtendedEncoding([$per]); 123 $chl[] = $top_name . ' - ' . I18N::number($count_per); 124 } 125 126 $per = intdiv(100 * ($tot_indi - $tot), $tot_indi); 127 $chd .= $this->arrayToExtendedEncoding([$per]); 128 $chl[] = I18N::translate('Other') . ' - ' . I18N::number($tot_indi - $tot); 129 130 $chart_title = implode(I18N::$list_separator, $chl); 131 $chl = rawurlencode(implode('|', $chl)); 132 $colors = [$color_from, $color_to]; 133 134 return view( 135 'statistics/other/chart-google', 136 [ 137 'chart_title' => $chart_title, 138 'chart_url' => $this->getPieChartUrl($chd, $size, $colors, $chl), 139 'sizes' => $sizes, 140 ] 141 ); 142 } 143} 144