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