xref: /webtrees/app/Statistics/Google/ChartDivorce.php (revision db3aeb3eb846bb233f34557aedb1559aadf9275a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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\Service\CenturyService;
23use Fisharebest\Webtrees\Statistics\Service\ColorService;
24use Fisharebest\Webtrees\Tree;
25use Illuminate\Database\Capsule\Manager as DB;
26
27/**
28 * A chart showing the divorces by century.
29 */
30class ChartDivorce
31{
32    /**
33     * @var Tree
34     */
35    private $tree;
36
37    /**
38     * @var ModuleThemeInterface
39     */
40    private $theme;
41
42    /**
43     * @var CenturyService
44     */
45    private $century_service;
46
47    /**
48     * @var ColorService
49     */
50    private $color_service;
51
52    /**
53     * Constructor.
54     *
55     * @param Tree $tree
56     */
57    public function __construct(Tree $tree)
58    {
59        $this->tree            = $tree;
60        $this->theme           = app(ModuleThemeInterface::class);
61        $this->century_service = new CenturyService();
62        $this->color_service   = new ColorService();
63    }
64
65    /**
66     * Returns the related database records.
67     *
68     * @return \stdClass[]
69     */
70    private function queryRecords(): array
71    {
72        $query = DB::table('dates')
73            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
74            ->selectRaw('COUNT(*) AS total')
75            ->where('d_file', '=', $this->tree->id())
76            ->where('d_year', '<>', 0)
77            ->where('d_fact', '=', 'DIV')
78            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
79            ->groupBy(['century'])
80            ->orderBy('century');
81
82        return $query->get()->all();
83    }
84
85    /**
86     * General query on divorces.
87     *
88     * @param string|null $color_from
89     * @param string|null $color_to
90     *
91     * @return string
92     */
93    public function chartDivorce(string $color_from = null, string $color_to = null): string
94    {
95        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
96        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
97        $color_from   = $color_from ?? $chart_color1;
98        $color_to     = $color_to   ?? $chart_color2;
99
100        $data = [
101            [
102                I18N::translate('Century'),
103                I18N::translate('Total')
104            ],
105        ];
106
107        foreach ($this->queryRecords() as $record) {
108            $data[] = [
109                $this->century_service->centuryName((int) $record->century),
110                $record->total
111            ];
112        }
113
114        $colors = $this->color_service->interpolateRgb($color_from, $color_to, \count($data) - 1);
115
116        return view(
117            'statistics/other/charts/pie',
118            [
119                'title'  => I18N::translate('Divorces by century'),
120                'data'   => $data,
121                'colors' => $colors,
122            ]
123        );
124    }
125}
126