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