xref: /webtrees/app/Statistics/Google/ChartDeath.php (revision 6fd4f6e3d5a2c64c159e88e10883a1ccab4d4a42)
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\I18N;
21use Fisharebest\Webtrees\Module\ModuleThemeInterface;
22use Fisharebest\Webtrees\Statistics\Helper\Century;
23use Fisharebest\Webtrees\Statistics\AbstractGoogle;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26
27/**
28 *
29 */
30class ChartDeath extends AbstractGoogle
31{
32    /**
33     * @var Tree
34     */
35    private $tree;
36
37    /**
38     * @var Century
39     */
40    private $centuryHelper;
41
42    /**
43     * Constructor.
44     *
45     * @param Tree $tree
46     */
47    public function __construct(Tree $tree)
48    {
49        $this->tree          = $tree;
50        $this->centuryHelper = new Century();
51    }
52
53    /**
54     * Returns the related database records.
55     *
56     * @return \stdClass[]
57     */
58    private function queryRecords(): array
59    {
60        $query = DB::table('dates')
61            ->selectRaw('FLOOR(d_year / 100 + 1) AS century')
62            ->selectRaw('COUNT(*) AS total')
63            ->where('d_file', '=', $this->tree->id())
64            ->where('d_year', '<>', 0)
65            ->where('d_fact', '=', 'DEAT')
66            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
67            ->groupBy(['century'])
68            ->orderBy('century');
69
70        return $query->get()->all();
71    }
72
73    /**
74     * Create a chart of death places.
75     *
76     * @param string|null $size
77     * @param string|null $color_from
78     * @param string|null $color_to
79     *
80     * @return string
81     */
82    public function chartDeath(string $size = null, string $color_from = null, string $color_to = null): string
83    {
84        $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values');
85        $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values');
86        $chart_x      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x');
87        $chart_y      = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y');
88
89        $size       = $size ?? ($chart_x . 'x' . $chart_y);
90        $color_from = $color_from ?? $chart_color1;
91        $color_to   = $color_to ?? $chart_color2;
92
93        $sizes = explode('x', $size);
94        $tot   = 0;
95        $rows  = $this->queryRecords();
96
97        foreach ($rows as $values) {
98            $values->total = (int) $values->total;
99            $tot += $values->total;
100        }
101
102        // Beware divide by zero
103        if ($tot === 0) {
104            return '';
105        }
106
107        $centuries = '';
108        $counts    = [];
109        foreach ($rows as $values) {
110            $counts[] = intdiv(100 * $values->total, $tot);
111            $centuries .= $this->centuryHelper->centuryName((int) $values->century) . ' - ' . I18N::number($values->total) . '|';
112        }
113
114        $chd    = $this->arrayToExtendedEncoding($counts);
115        $chl    = rawurlencode(substr($centuries, 0, -1));
116        $colors = [$color_from, $color_to];
117
118        return view(
119            'statistics/other/chart-google',
120            [
121                'chart_title' => I18N::translate('Deaths by century'),
122                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
123                'sizes'       => $sizes,
124            ]
125        );
126    }
127}
128