xref: /webtrees/app/Statistics/Google/ChartFamilyLargest.php (revision 9219296a1acfac69c7d7f13505951ddeee5f8899)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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\AbstractGoogle;
24use Fisharebest\Webtrees\Theme;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27
28/**
29 *
30 */
31class ChartFamilyLargest extends AbstractGoogle
32{
33    /**
34     * @var Tree
35     */
36    private $tree;
37
38    /**
39     * Constructor.
40     *
41     * @param Tree $tree
42     */
43    public function __construct(Tree $tree)
44    {
45        $this->tree = $tree;
46    }
47
48    /**
49     * Returns the related database records.
50     *
51     * @param int $total
52     *
53     * @return \stdClass[]
54     */
55    private function queryRecords(int $total): array
56    {
57        $query = DB::table('families')
58            ->select(['f_numchil AS tot', 'f_id AS id'])
59            ->where('f_file', '=', $this->tree->id())
60            ->orderBy('tot', 'desc')
61            ->limit($total);
62
63        return $query->get()->all();
64    }
65
66    /**
67     * Create a chart of the largest families.
68     *
69     * @param string|null $size
70     * @param string|null $color_from
71     * @param string|null $color_to
72     * @param int         $total
73     *
74     * @return string
75     */
76    public function chartLargestFamilies(
77        string $size       = null,
78        string $color_from = null,
79        string $color_to   = null,
80        int    $total      = 10
81    ): string {
82        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
83        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
84        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-large-chart-x');
85        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
86
87        $size       = $size ?? $chart_x . 'x' . $chart_y;
88        $color_from = $color_from ?? $chart_color1;
89        $color_to   = $color_to ?? $chart_color2;
90        $sizes      = explode('x', $size);
91        $rows       = $this->queryRecords($total);
92
93        if (!isset($rows[0])) {
94            return '';
95        }
96
97        $tot = 0;
98        foreach ($rows as $row) {
99            $tot += $row->tot;
100        }
101
102        $chd = '';
103        $chl = [];
104
105        foreach ($rows as $row) {
106            $family = Family::getInstance($row->id, $this->tree);
107
108            if ($family && $family->canShow()) {
109                if ($tot === 0) {
110                    $per = 0;
111                } else {
112                    $per = intdiv(100 * $row->tot, $tot);
113                }
114
115                $chd .= $this->arrayToExtendedEncoding([$per]);
116                $chl[] = htmlspecialchars_decode(strip_tags($family->getFullName())) . ' - ' . I18N::number($row->tot);
117            }
118        }
119
120        $chl    = rawurlencode(implode('|', $chl));
121        $colors = [$color_from, $color_to];
122
123        return view(
124            'statistics/other/chart-google',
125            [
126                'chart_title' => I18N::translate('Largest families'),
127                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
128                'sizes'       => $sizes,
129            ]
130        );
131    }
132}
133