xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 774eb6bada09fdd46a086fd31e84fd157a6351c4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\DB;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Statistics\Service\ColorService;
27use Fisharebest\Webtrees\Tree;
28
29use function count;
30use function htmlspecialchars_decode;
31use function strip_tags;
32use function view;
33
34/**
35 * A chart showing the largest families (Families with most children).
36 */
37class ChartFamilyLargest
38{
39    private Tree $tree;
40
41    private ColorService $color_service;
42
43    /**
44     * @param ColorService $color_service
45     * @param Tree         $tree
46     */
47    public function __construct(ColorService $color_service, Tree $tree)
48    {
49        $this->tree          = $tree;
50        $this->color_service = $color_service;
51    }
52
53    /**
54     * Returns the related database records.
55     *
56     * @param int $total
57     *
58     * @return array<object>
59     */
60    private function queryRecords(int $total): array
61    {
62        $query = DB::table('families')
63            ->select(['f_numchil AS total', 'f_id AS id'])
64            ->where('f_file', '=', $this->tree->id())
65            ->orderBy('total', 'desc')
66            ->limit($total);
67
68        return $query->get()->all();
69    }
70
71    /**
72     * Create a chart of the largest families.
73     *
74     * @param string|null $color_from
75     * @param string|null $color_to
76     * @param int         $total
77     *
78     * @return string
79     */
80    public function chartLargestFamilies(
81        string|null $color_from = null,
82        string|null $color_to = null,
83        int $total = 10
84    ): string {
85        $color_from ??= 'ffffff';
86        $color_to ??= '84beff';
87
88        $data = [
89            [
90                I18N::translate('Type'),
91                I18N::translate('Total')
92            ],
93        ];
94
95        foreach ($this->queryRecords($total) as $record) {
96            $family = Registry::familyFactory()->make($record->id, $this->tree);
97
98            if ($family instanceof Family && $family->canShow()) {
99                $data[] = [
100                    htmlspecialchars_decode(strip_tags($family->fullName())),
101                    $record->total
102                ];
103            }
104        }
105
106        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
107
108        return view('statistics/other/charts/pie', [
109            'title'    => I18N::translate('Largest families'),
110            'data'     => $data,
111            'colors'   => $colors,
112            'language' => I18N::languageTag(),
113        ]);
114    }
115}
116