xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 34b20f294deb14f65541b617e67c4eb4ff84d013)
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\Registry;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Statistics\Service\ColorService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27
28use function count;
29use function htmlspecialchars_decode;
30use function strip_tags;
31use function view;
32
33/**
34 * A chart showing the largest families (Families with most children).
35 */
36class ChartFamilyLargest
37{
38    private Tree $tree;
39
40    private ColorService $color_service;
41
42    /**
43     * Constructor.
44     *
45     * @param Tree $tree
46     */
47    public function __construct(Tree $tree)
48    {
49        $this->tree          = $tree;
50        $this->color_service = new ColorService();
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 $color_from = null,
82        string $color_to = null,
83        int $total = 10
84    ): string {
85        $color_from = $color_from ?? 'ffffff';
86        $color_to   = $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 && $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