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