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