xref: /webtrees/app/Statistics/Google/ChartDivorce.php (revision 11eb858145c6c7c490e5c0cd7b0bd51e519264f0)
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\Statistics\AbstractGoogle;
22use Fisharebest\Webtrees\Statistics\Helper\Century;
23use Fisharebest\Webtrees\Theme;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26
27/**
28 *
29 */
30class ChartDivorce 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', '=', 'DIV')
66            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
67            ->groupBy(['century'])
68            ->orderBy('century');
69
70        return $query->get()->all();
71    }
72
73    /**
74     * General query on divorces.
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 chartDivorce(string $size = null, string $color_from = null, string $color_to = null): string
83    {
84        $chart_color1 = (string) Theme::theme()->parameter('distribution-chart-no-values');
85        $chart_color2 = (string) Theme::theme()->parameter('distribution-chart-high-values');
86        $chart_x      = Theme::theme()->parameter('stats-small-chart-x');
87        $chart_y      = Theme::theme()->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        // Beware divide by zero
102        if ($tot === 0) {
103            return '';
104        }
105        $centuries = '';
106        $counts    = [];
107
108        foreach ($rows as $values) {
109            $counts[] = intdiv(100 * $values->total, $tot);
110            $centuries .= $this->centuryHelper->centuryName((int) $values->century) . ' - ' . I18N::number($values->total) . '|';
111        }
112
113        $chd    = $this->arrayToExtendedEncoding($counts);
114        $chl    = substr($centuries, 0, -1);
115        $colors = [$color_from, $color_to];
116
117        return view(
118            'statistics/other/chart-google',
119            [
120                'chart_title' => I18N::translate('Divorces by century'),
121                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
122                'sizes'       => $sizes,
123            ]
124        );
125    }
126}
127