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