xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision dc6db0ea276a95e5879acc3ff82c2e17924cb0b6)
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     * @param ColorService $color_service
44     * @param Tree         $tree
45     */
46    public function __construct(ColorService $color_service, Tree $tree)
47    {
48        $this->tree          = $tree;
49        $this->color_service = $color_service;
50    }
51
52    /**
53     * Returns the related database records.
54     *
55     * @param int $total
56     *
57     * @return array<object>
58     */
59    private function queryRecords(int $total): array
60    {
61        $query = DB::table('families')
62            ->select(['f_numchil AS total', 'f_id AS id'])
63            ->where('f_file', '=', $this->tree->id())
64            ->orderBy('total', 'desc')
65            ->limit($total);
66
67        return $query->get()->all();
68    }
69
70    /**
71     * Create a chart of the largest families.
72     *
73     * @param string|null $color_from
74     * @param string|null $color_to
75     * @param int         $total
76     *
77     * @return string
78     */
79    public function chartLargestFamilies(
80        string $color_from = null,
81        string $color_to = null,
82        int $total = 10
83    ): string {
84        $color_from = $color_from ?? 'ffffff';
85        $color_to   = $color_to ?? '84beff';
86
87        $data = [
88            [
89                I18N::translate('Type'),
90                I18N::translate('Total')
91            ],
92        ];
93
94        foreach ($this->queryRecords($total) as $record) {
95            $family = Registry::familyFactory()->make($record->id, $this->tree);
96
97            if ($family && $family->canShow()) {
98                $data[] = [
99                    htmlspecialchars_decode(strip_tags($family->fullName())),
100                    $record->total
101                ];
102            }
103        }
104
105        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
106
107        return view('statistics/other/charts/pie', [
108            'title'    => I18N::translate('Largest families'),
109            'data'     => $data,
110            'colors'   => $colors,
111            'language' => I18N::languageTag(),
112        ]);
113    }
114}
115