xref: /webtrees/app/Statistics/Google/ChartDivorce.php (revision 8add1155cb77caede54752196ff44696d1346431)
1*8add1155SRico Sonntag<?php
2*8add1155SRico Sonntag/**
3*8add1155SRico Sonntag * webtrees: online genealogy
4*8add1155SRico Sonntag * Copyright (C) 2018 webtrees development team
5*8add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
6*8add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
7*8add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
8*8add1155SRico Sonntag * (at your option) any later version.
9*8add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
10*8add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*8add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*8add1155SRico Sonntag * GNU General Public License for more details.
13*8add1155SRico Sonntag * You should have received a copy of the GNU General Public License
14*8add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
15*8add1155SRico Sonntag */
16*8add1155SRico Sonntagdeclare(strict_types=1);
17*8add1155SRico Sonntag
18*8add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
19*8add1155SRico Sonntag
20*8add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
21*8add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\AbstractGoogle;
22*8add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Helper\Century;
23*8add1155SRico Sonntaguse Fisharebest\Webtrees\Theme;
24*8add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
25*8add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
26*8add1155SRico Sonntag
27*8add1155SRico Sonntag/**
28*8add1155SRico Sonntag *
29*8add1155SRico Sonntag */
30*8add1155SRico Sonntagclass ChartDivorce extends AbstractGoogle
31*8add1155SRico Sonntag{
32*8add1155SRico Sonntag    /**
33*8add1155SRico Sonntag     * @var Tree
34*8add1155SRico Sonntag     */
35*8add1155SRico Sonntag    private $tree;
36*8add1155SRico Sonntag
37*8add1155SRico Sonntag    /**
38*8add1155SRico Sonntag     * @var Century
39*8add1155SRico Sonntag     */
40*8add1155SRico Sonntag    private $centuryHelper;
41*8add1155SRico Sonntag
42*8add1155SRico Sonntag    /**
43*8add1155SRico Sonntag     * Constructor.
44*8add1155SRico Sonntag     *
45*8add1155SRico Sonntag     * @param Tree $tree
46*8add1155SRico Sonntag     */
47*8add1155SRico Sonntag    public function __construct(Tree $tree)
48*8add1155SRico Sonntag    {
49*8add1155SRico Sonntag        $this->tree          = $tree;
50*8add1155SRico Sonntag        $this->centuryHelper = new Century();
51*8add1155SRico Sonntag    }
52*8add1155SRico Sonntag
53*8add1155SRico Sonntag    /**
54*8add1155SRico Sonntag     * Returns the related database records.
55*8add1155SRico Sonntag     *
56*8add1155SRico Sonntag     * @return \stdClass[]
57*8add1155SRico Sonntag     */
58*8add1155SRico Sonntag    private function queryRecords(): array
59*8add1155SRico Sonntag    {
60*8add1155SRico Sonntag        $query = DB::table('dates')
61*8add1155SRico Sonntag            ->selectRaw('FLOOR(d_year / 100 + 1) AS century')
62*8add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
63*8add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
64*8add1155SRico Sonntag            ->where('d_year', '<>', 0)
65*8add1155SRico Sonntag            ->where('d_fact', '=', 'DIV')
66*8add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
67*8add1155SRico Sonntag            ->groupBy(['century'])
68*8add1155SRico Sonntag            ->orderBy('century');
69*8add1155SRico Sonntag
70*8add1155SRico Sonntag        return $query->get()->all();
71*8add1155SRico Sonntag    }
72*8add1155SRico Sonntag
73*8add1155SRico Sonntag    /**
74*8add1155SRico Sonntag     * General query on divorces.
75*8add1155SRico Sonntag     *
76*8add1155SRico Sonntag     * @param string|null $size
77*8add1155SRico Sonntag     * @param string|null $color_from
78*8add1155SRico Sonntag     * @param string|null $color_to
79*8add1155SRico Sonntag     *
80*8add1155SRico Sonntag     * @return string
81*8add1155SRico Sonntag     */
82*8add1155SRico Sonntag    public function chartDivorce(string $size = null, string $color_from = null, string $color_to = null): string
83*8add1155SRico Sonntag    {
84*8add1155SRico Sonntag        $chart_color1 = (string) Theme::theme()->parameter('distribution-chart-no-values');
85*8add1155SRico Sonntag        $chart_color2 = (string) Theme::theme()->parameter('distribution-chart-high-values');
86*8add1155SRico Sonntag        $chart_x      = Theme::theme()->parameter('stats-small-chart-x');
87*8add1155SRico Sonntag        $chart_y      = Theme::theme()->parameter('stats-small-chart-y');
88*8add1155SRico Sonntag
89*8add1155SRico Sonntag        $size       = $size ?? ($chart_x . 'x' . $chart_y);
90*8add1155SRico Sonntag        $color_from = $color_from ?? $chart_color1;
91*8add1155SRico Sonntag        $color_to   = $color_to ?? $chart_color2;
92*8add1155SRico Sonntag
93*8add1155SRico Sonntag        $sizes = explode('x', $size);
94*8add1155SRico Sonntag        $tot   = 0;
95*8add1155SRico Sonntag        $rows  = $this->queryRecords();
96*8add1155SRico Sonntag
97*8add1155SRico Sonntag        foreach ($rows as $values) {
98*8add1155SRico Sonntag            $values->total = (int) $values->total;
99*8add1155SRico Sonntag            $tot += $values->total;
100*8add1155SRico Sonntag        }
101*8add1155SRico Sonntag        // Beware divide by zero
102*8add1155SRico Sonntag        if ($tot === 0) {
103*8add1155SRico Sonntag            return '';
104*8add1155SRico Sonntag        }
105*8add1155SRico Sonntag        $centuries = '';
106*8add1155SRico Sonntag        $counts    = [];
107*8add1155SRico Sonntag
108*8add1155SRico Sonntag        foreach ($rows as $values) {
109*8add1155SRico Sonntag            $counts[] = intdiv(100 * $values->total, $tot);
110*8add1155SRico Sonntag            $centuries .= $this->centuryHelper->centuryName((int) $values->century) . ' - ' . I18N::number($values->total) . '|';
111*8add1155SRico Sonntag        }
112*8add1155SRico Sonntag
113*8add1155SRico Sonntag        $chd    = $this->arrayToExtendedEncoding($counts);
114*8add1155SRico Sonntag        $chl    = substr($centuries, 0, -1);
115*8add1155SRico Sonntag        $colors = [$color_from, $color_to];
116*8add1155SRico Sonntag
117*8add1155SRico Sonntag        return view(
118*8add1155SRico Sonntag            'statistics/other/chart-google',
119*8add1155SRico Sonntag            [
120*8add1155SRico Sonntag                'chart_title' => I18N::translate('Divorces by century'),
121*8add1155SRico Sonntag                'chart_url'   => $this->getPieChartUrl($chd, $size, $colors, $chl),
122*8add1155SRico Sonntag                'sizes'       => $sizes,
123*8add1155SRico Sonntag            ]
124*8add1155SRico Sonntag        );
125*8add1155SRico Sonntag    }
126*8add1155SRico Sonntag}
127