xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Statistics\Google;
19
20use Fisharebest\Webtrees\Family;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Module\ModuleThemeInterface;
23use Fisharebest\Webtrees\Statistics\Service\ColorService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26
27/**
28 * A chart showing the largest families (Families with most children).
29 */
30class ChartFamilyLargest
31{
32    /**
33     * @var Tree
34     */
35    private $tree;
36
37    /**
38     * @var ModuleThemeInterface
39     */
40    private $theme;
41
42    /**
43     * @var ColorService
44     */
45    private $color_service;
46
47    /**
48     * Constructor.
49     *
50     * @param Tree $tree
51     */
52    public function __construct(Tree $tree)
53    {
54        $this->tree          = $tree;
55        $this->theme         = app()->make(ModuleThemeInterface::class);
56        $this->color_service = new ColorService();
57    }
58
59    /**
60     * Returns the related database records.
61     *
62     * @param int $total
63     *
64     * @return \stdClass[]
65     */
66    private function queryRecords(int $total): array
67    {
68        $query = DB::table('families')
69            ->select(['f_numchil AS total', 'f_id AS id'])
70            ->where('f_file', '=', $this->tree->id())
71            ->orderBy('total', 'desc')
72            ->limit($total);
73
74        return $query->get()->all();
75    }
76
77    /**
78     * Create a chart of the largest families.
79     *
80     * @param string|null $color_from
81     * @param string|null $color_to
82     * @param int         $total
83     *
84     * @return string
85     */
86    public function chartLargestFamilies(
87        string $color_from = null,
88        string $color_to   = null,
89        int    $total      = 10
90    ): string {
91        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
92        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
93        $color_from   = $color_from ?? $chart_color1;
94        $color_to     = $color_to   ?? $chart_color2;
95
96        $data = [
97            [
98                I18N::translate('Type'),
99                I18N::translate('Total')
100            ],
101        ];
102
103        foreach ($this->queryRecords($total) as $record) {
104            $family = Family::getInstance($record->id, $this->tree);
105
106            if ($family && $family->canShow()) {
107                $data[] = [
108                    htmlspecialchars_decode(strip_tags($family->fullName())),
109                    $record->total
110                ];
111            }
112        }
113
114        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
115
116        return view(
117            'statistics/other/charts/pie',
118            [
119                'title'       => I18N::translate('Largest families'),
120                'data'        => $data,
121                'colors'      => $colors,
122            ]
123        );
124    }
125}
126