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