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