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