xref: /webtrees/app/Statistics/Google/ChartDivorce.php (revision df27497a949ee94233b78915e33f253447a14e62)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Statistics\Google;
21
22use Fisharebest\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\ModuleThemeInterface;
25use Fisharebest\Webtrees\Statistics\Service\CenturyService;
26use Fisharebest\Webtrees\Statistics\Service\ColorService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Database\Capsule\Manager as DB;
29use Psr\Http\Message\ServerRequestInterface;
30use stdClass;
31
32use function app;
33use function assert;
34use function count;
35
36/**
37 * A chart showing the divorces by century.
38 */
39class ChartDivorce
40{
41    /**
42     * @var Tree
43     */
44    private $tree;
45
46    /**
47     * @var ModuleThemeInterface
48     */
49    private $theme;
50
51    /**
52     * @var CenturyService
53     */
54    private $century_service;
55
56    /**
57     * @var ColorService
58     */
59    private $color_service;
60
61    /**
62     * Constructor.
63     *
64     * @param Tree $tree
65     */
66    public function __construct(Tree $tree)
67    {
68        $this->tree            = $tree;
69        $this->theme           = app(ModuleThemeInterface::class);
70        $this->century_service = new CenturyService();
71        $this->color_service   = new ColorService();
72    }
73
74    /**
75     * Returns the related database records.
76     *
77     * @return stdClass[]
78     */
79    private function queryRecords(): array
80    {
81        $query = DB::table('dates')
82            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
83            ->selectRaw('COUNT(*) AS total')
84            ->where('d_file', '=', $this->tree->id())
85            ->where('d_year', '<>', 0)
86            ->where('d_fact', '=', 'DIV')
87            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
88            ->groupBy(['century'])
89            ->orderBy('century');
90
91        return $query->get()->all();
92    }
93
94    /**
95     * General query on divorces.
96     *
97     * @param string|null $color_from
98     * @param string|null $color_to
99     *
100     * @return string
101     */
102    public function chartDivorce(string $color_from = null, string $color_to = null): string
103    {
104        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
105        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
106        $color_from   = $color_from ?? $chart_color1;
107        $color_to     = $color_to   ?? $chart_color2;
108
109        $data = [
110            [
111                I18N::translate('Century'),
112                I18N::translate('Total')
113            ],
114        ];
115
116        foreach ($this->queryRecords() as $record) {
117            $data[] = [
118                $this->century_service->centuryName((int) $record->century),
119                $record->total
120            ];
121        }
122
123        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
124
125        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
126        assert($locale instanceof LocaleInterface);
127
128        return view('statistics/other/charts/pie', [
129            'title'    => I18N::translate('Divorces by century'),
130            'data'     => $data,
131            'colors'   => $colors,
132            'language' => $locale->languageTag(),
133        ]);
134    }
135}
136