xref: /webtrees/app/Statistics/Google/ChartBirth.php (revision 6ccdf4f0fd1b65a5d54259c969912382ce49629d)
18add1155SRico Sonntag<?php
28add1155SRico Sonntag/**
38add1155SRico Sonntag * webtrees: online genealogy
4242a7862SGreg Roach * Copyright (C) 2019 webtrees development team
58add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
68add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
78add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
88add1155SRico Sonntag * (at your option) any later version.
98add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
108add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
118add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128add1155SRico Sonntag * GNU General Public License for more details.
138add1155SRico Sonntag * You should have received a copy of the GNU General Public License
148add1155SRico Sonntag * along with this program. If not, see <http://www.gnu.org/licenses/>.
158add1155SRico Sonntag */
168add1155SRico Sonntagdeclare(strict_types=1);
178add1155SRico Sonntag
188add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Google;
198add1155SRico Sonntag
20*6ccdf4f0SGreg Roachuse function count;
218add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
2293ccd686SRico Sonntaguse Fisharebest\Webtrees\Module\ModuleThemeInterface;
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;
27*6ccdf4f0SGreg Roachuse stdClass;
288add1155SRico Sonntag
298add1155SRico Sonntag/**
3093ccd686SRico Sonntag * A chart showing birth by century.
318add1155SRico Sonntag */
3293ccd686SRico Sonntagclass ChartBirth
338add1155SRico Sonntag{
348add1155SRico Sonntag    /**
3593ccd686SRico Sonntag     * @var Tree
368add1155SRico Sonntag     */
3793ccd686SRico Sonntag    private $tree;
3893ccd686SRico Sonntag
3993ccd686SRico Sonntag    /**
4093ccd686SRico Sonntag     * @var ModuleThemeInterface
4193ccd686SRico Sonntag     */
4293ccd686SRico Sonntag    private $theme;
4393ccd686SRico Sonntag
4493ccd686SRico Sonntag    /**
4593ccd686SRico Sonntag     * @var CenturyService
4693ccd686SRico Sonntag     */
4793ccd686SRico Sonntag    private $century_service;
4893ccd686SRico Sonntag
4993ccd686SRico Sonntag    /**
5093ccd686SRico Sonntag     * @var ColorService
5193ccd686SRico Sonntag     */
5293ccd686SRico Sonntag    private $color_service;
538add1155SRico Sonntag
548add1155SRico Sonntag    /**
558add1155SRico Sonntag     * Constructor.
568add1155SRico Sonntag     *
578add1155SRico Sonntag     * @param Tree $tree
588add1155SRico Sonntag     */
598add1155SRico Sonntag    public function __construct(Tree $tree)
608add1155SRico Sonntag    {
6193ccd686SRico Sonntag        $this->tree            = $tree;
62cab242e7SGreg Roach        $this->theme           = app(ModuleThemeInterface::class);
6393ccd686SRico Sonntag        $this->century_service = new CenturyService();
6493ccd686SRico Sonntag        $this->color_service   = new ColorService();
658add1155SRico Sonntag    }
668add1155SRico Sonntag
678add1155SRico Sonntag    /**
688add1155SRico Sonntag     * Returns the related database records.
698add1155SRico Sonntag     *
70*6ccdf4f0SGreg Roach     * @return stdClass[]
718add1155SRico Sonntag     */
728add1155SRico Sonntag    private function queryRecords(): array
738add1155SRico Sonntag    {
748add1155SRico Sonntag        $query = DB::table('dates')
753413ec75SRico Sonntag            ->selectRaw('ROUND((d_year + 49) / 100) AS century')
768add1155SRico Sonntag            ->selectRaw('COUNT(*) AS total')
778add1155SRico Sonntag            ->where('d_file', '=', $this->tree->id())
788add1155SRico Sonntag            ->where('d_year', '<>', 0)
798add1155SRico Sonntag            ->where('d_fact', '=', 'BIRT')
808add1155SRico Sonntag            ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
818add1155SRico Sonntag            ->groupBy(['century'])
828add1155SRico Sonntag            ->orderBy('century');
838add1155SRico Sonntag
848add1155SRico Sonntag        return $query->get()->all();
858add1155SRico Sonntag    }
868add1155SRico Sonntag
878add1155SRico Sonntag    /**
888add1155SRico Sonntag     * Create a chart of birth places.
898add1155SRico Sonntag     *
908add1155SRico Sonntag     * @param string|null $color_from
918add1155SRico Sonntag     * @param string|null $color_to
928add1155SRico Sonntag     *
938add1155SRico Sonntag     * @return string
948add1155SRico Sonntag     */
9588de55fdSRico Sonntag    public function chartBirth(string $color_from = null, string $color_to = null): string
968add1155SRico Sonntag    {
9788de55fdSRico Sonntag        $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values');
9888de55fdSRico Sonntag        $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values');
998add1155SRico Sonntag        $color_from   = $color_from ?? $chart_color1;
1008add1155SRico Sonntag        $color_to     = $color_to   ?? $chart_color2;
1018add1155SRico Sonntag
10288de55fdSRico Sonntag        $data = [
10388de55fdSRico Sonntag            [
10488de55fdSRico Sonntag                I18N::translate('Century'),
10588de55fdSRico Sonntag                I18N::translate('Total')
10688de55fdSRico Sonntag            ],
10788de55fdSRico Sonntag        ];
1088add1155SRico Sonntag
10988de55fdSRico Sonntag        foreach ($this->queryRecords() as $record) {
11088de55fdSRico Sonntag            $data[] = [
11193ccd686SRico Sonntag                $this->century_service->centuryName((int) $record->century),
11288de55fdSRico Sonntag                $record->total
11388de55fdSRico Sonntag            ];
1148add1155SRico Sonntag        }
1158add1155SRico Sonntag
116*6ccdf4f0SGreg Roach        $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1);
1178add1155SRico Sonntag
1188add1155SRico Sonntag        return view(
11988de55fdSRico Sonntag            'statistics/other/charts/pie',
1208add1155SRico Sonntag            [
12188de55fdSRico Sonntag                'title'  => I18N::translate('Births by century'),
12288de55fdSRico Sonntag                'data'   => $data,
12388de55fdSRico Sonntag                'colors' => $colors,
1248add1155SRico Sonntag            ]
1258add1155SRico Sonntag        );
1268add1155SRico Sonntag    }
1278add1155SRico Sonntag}
128