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