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