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