xref: /webtrees/app/Statistics/Google/ChartCommonSurname.php (revision 6fd4f6e3d5a2c64c159e88e10883a1ccab4d4a42)
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\Tree;
24
25/**
26 *
27 */
28class ChartCommonSurname extends AbstractGoogle
29{
30    /**
31     * @var Tree
32     */
33    private $tree;
34
35    /**
36     * Constructor.
37     *
38     * @param Tree $tree
39     */
40    public function __construct(Tree $tree)
41    {
42        $this->tree = $tree;
43    }
44
45    /**
46     * Create a chart of common surnames.
47     *
48     * @param int         $tot_indi     The total number of individuals
49     * @param array       $all_surnames The list of common surnames
50     * @param string|null $size
51     * @param string|null $color_from
52     * @param string|null $color_to
53     *
54     * @return string
55     */
56    public function chartCommonSurnames(
57        int $tot_indi,
58        array $all_surnames,
59        string $size = null,
60        string $color_from = null,
61        string $color_to = null
62    ): string {
63        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
64        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
65        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
66        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
67
68        $size       = $size ?? ($chart_x . 'x' . $chart_y);
69        $color_from = $color_from ?? $chart_color1;
70        $color_to   = $color_to ?? $chart_color2;
71        $sizes      = explode('x', $size);
72
73        if (empty($all_surnames)) {
74            return '';
75        }
76
77        $surname_tradition = $this->tree->getPreference('SURNAME_TRADITION');
78
79        $tot = 0;
80
81        foreach ($all_surnames as $surn => $surnames) {
82            $tot += array_sum($surnames);
83        }
84
85        $chd = '';
86        $chl = [];
87
88        /** @var array $surns */
89        foreach ($all_surnames as $surns) {
90            $count_per = 0;
91            $max_name  = 0;
92            $top_name  = '';
93
94            foreach ($surns as $spfxsurn => $count) {
95                $per = $count;
96                $count_per += $per;
97
98                // select most common surname from all variants
99                if ($per > $max_name) {
100                    $max_name = $per;
101                    $top_name = $spfxsurn;
102                }
103            }
104
105            if ($surname_tradition === 'polish') {
106                // Most common surname should be in male variant (Kowalski, not Kowalska)
107                $top_name = preg_replace([
108                    '/ska$/',
109                    '/cka$/',
110                    '/dzka$/',
111                    '/żka$/',
112                ], [
113                    'ski',
114                    'cki',
115                    'dzki',
116                    'żki',
117                ], $top_name);
118            }
119
120            $per   = intdiv(100 * $count_per, $tot_indi);
121            $chd .= $this->arrayToExtendedEncoding([$per]);
122            $chl[] = $top_name . ' - ' . I18N::number($count_per);
123        }
124
125        $per   = intdiv(100 * ($tot_indi - $tot), $tot_indi);
126        $chd .= $this->arrayToExtendedEncoding([$per]);
127        $chl[] = I18N::translate('Other') . ' - ' . I18N::number($tot_indi - $tot);
128
129        $chart_title = implode(I18N::$list_separator, $chl);
130        $chl         = rawurlencode(implode('|', $chl));
131        $colors      = [$color_from, $color_to];
132
133        return view(
134            'statistics/other/chart-google',
135            [
136                'chart_title' => $chart_title,
137                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
138                'sizes'       => $sizes,
139            ]
140        );
141    }
142}
143