xref: /webtrees/app/Statistics/Google/ChartMarriage.php (revision 34b20f294deb14f65541b617e67c4eb4ff84d013)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
218add1155SRico Sonntag
228add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2393ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService;
2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\ColorService;
258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
268add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB;
2734e3587bSGreg Roachuse Illuminate\Support\Collection;
288add1155SRico Sonntag
2971378461SGreg Roachuse function count;
30*34b20f29SGreg Roachuse function view;
3171378461SGreg Roach
328add1155SRico Sonntag/**
3393ccd686SRico Sonntag * A chart showing the marriages by century.
348add1155SRico Sonntag */
3593ccd686SRico Sonntagclass ChartMarriage
368add1155SRico Sonntag{
37*34b20f29SGreg Roach    private Tree $tree;
3893ccd686SRico Sonntag
39*34b20f29SGreg Roach    private CenturyService $century_service;
4093ccd686SRico Sonntag
41*34b20f29SGreg Roach    private ColorService $color_service;
428add1155SRico Sonntag
438add1155SRico Sonntag    /**
448add1155SRico Sonntag     * Constructor.
458add1155SRico Sonntag     *
468add1155SRico Sonntag     * @param Tree $tree
478add1155SRico Sonntag     */
488add1155SRico Sonntag    public function __construct(Tree $tree)
498add1155SRico Sonntag    {
5093ccd686SRico Sonntag        $this->tree            = $tree;
5193ccd686SRico Sonntag        $this->century_service = new CenturyService();
5293ccd686SRico Sonntag        $this->color_service   = new ColorService();
538add1155SRico Sonntag    }
548add1155SRico Sonntag
558add1155SRico Sonntag    /**
568add1155SRico Sonntag     * Returns the related database records.
578add1155SRico Sonntag     *
58f70bcff5SGreg Roach     * @return Collection<object>
598add1155SRico Sonntag     */
6034e3587bSGreg Roach    private function queryRecords(): Collection
618add1155SRico Sonntag    {
6234e3587bSGreg Roach        return DB::table('dates')
633413ec75SRico Sonntag            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
648add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
658add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
668add1155SRico Sonntag            ->where('d_year', '<>', 0)
678add1155SRico Sonntag            ->where('d_fact', '=', 'MARR')
688add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
698add1155SRico Sonntag            ->groupBy(['century'])
7034e3587bSGreg Roach            ->orderBy('century')
7134e3587bSGreg Roach            ->get()
72f70bcff5SGreg Roach            ->map(static function (object $row): object {
7334e3587bSGreg Roach                return (object) [
7434e3587bSGreg Roach                    'century' => (int) $row->century,
7534e3587bSGreg Roach                    'total'   => (float) $row->total,
7634e3587bSGreg Roach                ];
7734e3587bSGreg Roach            });
788add1155SRico Sonntag    }
798add1155SRico Sonntag
808add1155SRico Sonntag    /**
818add1155SRico Sonntag     * General query on marriages.
828add1155SRico Sonntag     *
838add1155SRico Sonntag     * @param string|null $color_from
848add1155SRico Sonntag     * @param string|null $color_to
858add1155SRico Sonntag     *
868add1155SRico Sonntag     * @return string
878add1155SRico Sonntag     */
8888de55fdSRico Sonntag    public function chartMarriage(string $color_from = null, string $color_to = null): string
898add1155SRico Sonntag    {
90*34b20f29SGreg Roach        $color_from = $color_from ?? 'ffffff';
91*34b20f29SGreg Roach        $color_to   = $color_to ?? '84beff';
928add1155SRico Sonntag
9388de55fdSRico Sonntag        $data = [
9488de55fdSRico Sonntag            [
9588de55fdSRico Sonntag                I18N::translate('Century'),
9688de55fdSRico Sonntag                I18N::translate('Total')
9788de55fdSRico Sonntag            ],
9888de55fdSRico Sonntag        ];
998add1155SRico Sonntag
10088de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
10188de55fdSRico Sonntag            $data[] = [
10234e3587bSGreg Roach                $this->century_service->centuryName($record->century),
10388de55fdSRico Sonntag                $record->total
10488de55fdSRico Sonntag            ];
1058add1155SRico Sonntag        }
1068add1155SRico Sonntag
1076ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1088add1155SRico Sonntag
10990a2f718SGreg Roach        return view('statistics/other/charts/pie', [
11093ccd686SRico Sonntag            'title'    => I18N::translate('Marriages by century'),
11188de55fdSRico Sonntag            'data'     => $data,
11288de55fdSRico Sonntag            'colors'   => $colors,
11365cf5706SGreg Roach            'language' => I18N::languageTag(),
11490a2f718SGreg Roach        ]);
1158add1155SRico Sonntag    }
1168add1155SRico Sonntag}
117