xref: /webtrees/app/Statistics/Google/ChartDistribution.php (revision b3679361640bf9d3bb5424fb15815c51cf0c1513)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Statistics\Repository\Interfaces\IndividualRepositoryInterface;
24use Fisharebest\Webtrees\Statistics\Repository\Interfaces\PlaceRepositoryInterface;
25use Fisharebest\Webtrees\Statistics\Service\CountryService;
26use Fisharebest\Webtrees\Tree;
27use Illuminate\Database\Capsule\Manager as DB;
28use Illuminate\Database\Query\Expression;
29use Illuminate\Database\Query\JoinClause;
30
31use function array_key_exists;
32use function preg_match_all;
33use function view;
34
35/**
36 * A chart showing the distribution of different events on a map.
37 */
38class ChartDistribution
39{
40    private Tree $tree;
41
42    private CountryService $country_service;
43
44    private IndividualRepositoryInterface $individual_repository;
45
46    private PlaceRepositoryInterface $place_repository;
47
48    /**
49     * @var array<string>
50     */
51    private array $country_to_iso3166;
52
53    /**
54     * @param Tree           $tree
55     * @param CountryService $country_service
56     */
57    public function __construct(
58        Tree $tree,
59        CountryService $country_service,
60        IndividualRepositoryInterface $individual_repository,
61        PlaceRepositoryInterface $place_repository
62    ) {
63        $this->tree                  = $tree;
64        $this->country_service       = $country_service;
65        $this->individual_repository = $individual_repository;
66        $this->place_repository      = $place_repository;
67
68        // Get the country names for each language
69        $this->country_to_iso3166 = $this->getIso3166Countries();
70    }
71
72    /**
73     * Returns the country names for each language.
74     *
75     * @return array<string>
76     */
77    private function getIso3166Countries(): array
78    {
79        // Get the country names for each language
80        $country_to_iso3166 = [];
81
82        $current_language = I18N::languageTag();
83
84        foreach (I18N::activeLocales() as $locale) {
85            I18N::init($locale->languageTag());
86
87            $countries = $this->country_service->getAllCountries();
88
89            foreach ($this->country_service->iso3166() as $three => $two) {
90                $country_to_iso3166[$three]             = $two;
91                $country_to_iso3166[$countries[$three]] = $two;
92            }
93        }
94
95        I18N::init($current_language);
96
97        return $country_to_iso3166;
98    }
99
100    /**
101     * Returns the data structure required by google geochart.
102     *
103     * @param array<int|string,int> $places
104     *
105     * @return array
106     */
107    private function createChartData(array $places): array
108    {
109        $data = [
110            [
111                I18N::translate('Country'),
112                I18N::translate('Total'),
113            ],
114        ];
115
116        // webtrees uses 3-letter country codes and localised country names, but google uses 2 letter codes.
117        foreach ($places as $country => $count) {
118            $data[] = [
119                [
120                    'v' => $country,
121                    'f' => $this->country_service->mapTwoLetterToName($country),
122                ],
123                $count
124            ];
125        }
126
127        return $data;
128    }
129
130    /**
131     * Returns the google geochart data for birth fact.
132     *
133     * @return array
134     */
135    private function getBirthChartData(): array
136    {
137        // Count how many people were born in each country
138        $surn_countries = [];
139        $b_countries    = $this->place_repository->statsPlaces('INDI', 'BIRT', 0, true);
140
141        foreach ($b_countries as $country => $count) {
142            // Consolidate places (Germany, DEU => DE)
143            if (array_key_exists($country, $this->country_to_iso3166)) {
144                $country_code = $this->country_to_iso3166[$country];
145
146                if (array_key_exists($country_code, $surn_countries)) {
147                    $surn_countries[$country_code] += $count;
148                } else {
149                    $surn_countries[$country_code] = $count;
150                }
151            }
152        }
153
154        return $this->createChartData($surn_countries);
155    }
156
157    /**
158     * Returns the google geochart data for death fact.
159     *
160     * @return array
161     */
162    private function getDeathChartData(): array
163    {
164        // Count how many people were death in each country
165        $surn_countries = [];
166        $d_countries    = $this->place_repository->statsPlaces('INDI', 'DEAT', 0, true);
167
168        foreach ($d_countries as $country => $count) {
169            // Consolidate places (Germany, DEU => DE)
170            if (array_key_exists($country, $this->country_to_iso3166)) {
171                $country_code = $this->country_to_iso3166[$country];
172
173                if (array_key_exists($country_code, $surn_countries)) {
174                    $surn_countries[$country_code] += $count;
175                } else {
176                    $surn_countries[$country_code] = $count;
177                }
178            }
179        }
180
181        return $this->createChartData($surn_countries);
182    }
183
184    /**
185     * Returns the google geochart data for marriages.
186     *
187     * @return array
188     */
189    private function getMarriageChartData(): array
190    {
191        // Count how many families got marriage in each country
192        $surn_countries = [];
193        $m_countries    = $this->place_repository->statsPlaces('FAM');
194
195        // webtrees uses 3 letter country codes and localised country names, but google uses 2 letter codes.
196        foreach ($m_countries as $place) {
197            // Consolidate places (Germany, DEU => DE)
198            if (array_key_exists($place->country, $this->country_to_iso3166)) {
199                $country_code = $this->country_to_iso3166[$place->country];
200
201                if (array_key_exists($country_code, $surn_countries)) {
202                    $surn_countries[$country_code] += $place->tot;
203                } else {
204                    $surn_countries[$country_code] = $place->tot;
205                }
206            }
207        }
208
209        return $this->createChartData($surn_countries);
210    }
211
212    /**
213     * Returns the related database records.
214     *
215     * @param string $surname
216     *
217     * @return array<object>
218     */
219    private function queryRecords(string $surname): array
220    {
221        $query = DB::table('individuals')
222            ->select(['i_gedcom'])
223            ->join('name', static function (JoinClause $join): void {
224                $join->on('n_id', '=', 'i_id')
225                    ->on('n_file', '=', 'i_file');
226            })
227            ->where('n_file', '=', $this->tree->id())
228            ->where(new Expression('n_surn /*! COLLATE ' . I18N::collation() . ' */'), '=', $surname);
229
230        return $query->get()->all();
231    }
232
233    /**
234     * Returns the google geochart data for surnames.
235     *
236     * @param string $surname The surname used to create the chart
237     *
238     * @return array
239     */
240    private function getSurnameChartData(string $surname): array
241    {
242        if ($surname === '') {
243            $surname = $this->individual_repository->getCommonSurname();
244        }
245
246        // Count how many people are events in each country
247        $surn_countries = [];
248        $records        = $this->queryRecords($surname);
249
250        foreach ($records as $row) {
251            if (preg_match_all('/^2 PLAC (?:.*, *)*(.*)/m', $row->i_gedcom, $matches)) {
252                // webtrees uses 3 letter country codes and localised country names,
253                // but google uses 2 letter codes.
254                foreach ($matches[1] as $country) {
255                    // Consolidate places (Germany, DEU => DE)
256                    if (array_key_exists($country, $this->country_to_iso3166)) {
257                        $country_code = $this->country_to_iso3166[$country];
258
259                        if (array_key_exists($country_code, $surn_countries)) {
260                            $surn_countries[$country_code]++;
261                        } else {
262                            $surn_countries[$country_code] = 1;
263                        }
264                    }
265                }
266            }
267        }
268
269        return $this->createChartData($surn_countries);
270    }
271
272    /**
273     * Returns the google geochart data for individuals.
274     *
275     * @return array
276     */
277    private function getIndivdualChartData(): array
278    {
279        // Count how many people have events in each country
280        $surn_countries = [];
281        $a_countries    = $this->place_repository->statsPlaces('INDI');
282
283        // webtrees uses 3 letter country codes and localised country names, but google uses 2 letter codes.
284        foreach ($a_countries as $place) {
285            // Consolidate places (Germany, DEU => DE)
286            if (array_key_exists($place->country, $this->country_to_iso3166)) {
287                $country_code = $this->country_to_iso3166[$place->country];
288
289                if (array_key_exists($country_code, $surn_countries)) {
290                    $surn_countries[$country_code] += $place->tot;
291                } else {
292                    $surn_countries[$country_code] = $place->tot;
293                }
294            }
295        }
296
297        return $this->createChartData($surn_countries);
298    }
299
300    /**
301     * Create a chart showing where events occurred.
302     *
303     * @param string $chart_shows The type of chart map to show
304     * @param string $chart_type  The type of chart to show
305     * @param string $surname     The surname for surname based distribution chart
306     *
307     * @return string
308     */
309    public function chartDistribution(
310        string $chart_shows = 'world',
311        string $chart_type = '',
312        string $surname = ''
313    ): string {
314        switch ($chart_type) {
315            case 'surname_distribution_chart':
316                $chart_title = I18N::translate('Surname distribution chart') . ': ' . $surname;
317                $data        = $this->getSurnameChartData($surname);
318                break;
319
320            case 'birth_distribution_chart':
321                $chart_title = I18N::translate('Birth by country');
322                $data        = $this->getBirthChartData();
323                break;
324
325            case 'death_distribution_chart':
326                $chart_title = I18N::translate('Death by country');
327                $data        = $this->getDeathChartData();
328                break;
329
330            case 'marriage_distribution_chart':
331                $chart_title = I18N::translate('Marriage by country');
332                $data        = $this->getMarriageChartData();
333                break;
334
335            case 'indi_distribution_chart':
336            default:
337                $chart_title = I18N::translate('Individual distribution chart');
338                $data        = $this->getIndivdualChartData();
339                break;
340        }
341
342        return view('statistics/other/charts/geo', [
343            'chart_title'  => $chart_title,
344            'chart_color2' => '84beff',
345            'chart_color3' => 'c3dfff',
346            'region'       => $chart_shows,
347            'data'         => $data,
348            'language'     => I18N::languageTag(),
349        ]);
350    }
351}
352