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; 248add1155SRico Sonntaguse Fisharebest\Webtrees\Tree; 258add1155SRico Sonntaguse Illuminate\Database\Capsule\Manager as DB; 268add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause; 278add1155SRico Sonntag 288add1155SRico Sonntag/** 2993ccd686SRico Sonntag * A chart showing the number of families with no children by century. 308add1155SRico Sonntag */ 3193ccd686SRico Sonntagclass ChartNoChildrenFamilies 328add1155SRico Sonntag{ 338add1155SRico Sonntag /** 3493ccd686SRico Sonntag * @var Tree 358add1155SRico Sonntag */ 3693ccd686SRico Sonntag private $tree; 3793ccd686SRico Sonntag 3893ccd686SRico Sonntag /** 3993ccd686SRico Sonntag * @var CenturyService 4093ccd686SRico Sonntag */ 4193ccd686SRico Sonntag private $century_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(); 528add1155SRico Sonntag } 538add1155SRico Sonntag 548add1155SRico Sonntag /** 558add1155SRico Sonntag * Returns the related database records. 568add1155SRico Sonntag * 578add1155SRico Sonntag * @param int $year1 588add1155SRico Sonntag * @param int $year2 598add1155SRico Sonntag * 60*f70bcff5SGreg Roach * @return array<object> 618add1155SRico Sonntag */ 628add1155SRico Sonntag private function queryRecords(int $year1, int $year2): array 638add1155SRico Sonntag { 648add1155SRico Sonntag $query = DB::table('families') 653413ec75SRico Sonntag ->selectRaw('ROUND((d_year + 49) / 100) AS century') 6688de55fdSRico Sonntag ->selectRaw('COUNT(*) AS total') 670b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 688add1155SRico Sonntag $join->on('d_file', '=', 'f_file') 698add1155SRico Sonntag ->on('d_gid', '=', 'f_id'); 708add1155SRico Sonntag }) 718add1155SRico Sonntag ->where('f_file', '=', $this->tree->id()) 728add1155SRico Sonntag ->where('f_numchil', '=', 0) 738add1155SRico Sonntag ->where('d_fact', '=', 'MARR') 748add1155SRico Sonntag ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 758add1155SRico Sonntag ->groupBy(['century']) 768add1155SRico Sonntag ->orderBy('century'); 778add1155SRico Sonntag 788add1155SRico Sonntag if ($year1 >= 0 && $year2 >= 0) { 798add1155SRico Sonntag $query->whereBetween('d_year', [$year1, $year2]); 808add1155SRico Sonntag } 818add1155SRico Sonntag 828add1155SRico Sonntag return $query->get()->all(); 838add1155SRico Sonntag } 848add1155SRico Sonntag 858add1155SRico Sonntag /** 868add1155SRico Sonntag * Create a chart of children with no families. 878add1155SRico Sonntag * 888add1155SRico Sonntag * @param int $no_child_fam The number of families with no children 898add1155SRico Sonntag * @param int $year1 908add1155SRico Sonntag * @param int $year2 918add1155SRico Sonntag * 928add1155SRico Sonntag * @return string 938add1155SRico Sonntag */ 948add1155SRico Sonntag public function chartNoChildrenFamilies( 958add1155SRico Sonntag int $no_child_fam, 968add1155SRico Sonntag int $year1 = -1, 978add1155SRico Sonntag int $year2 = -1 988add1155SRico Sonntag ): string { 9988de55fdSRico Sonntag $data = [ 10088de55fdSRico Sonntag [ 10188de55fdSRico Sonntag I18N::translate('Century'), 10288de55fdSRico Sonntag I18N::translate('Total') 10388de55fdSRico Sonntag ] 10488de55fdSRico Sonntag ]; 1058add1155SRico Sonntag 10688de55fdSRico Sonntag $total = 0; 10788de55fdSRico Sonntag 10888de55fdSRico Sonntag foreach ($this->queryRecords($year1, $year2) as $record) { 109151a7600SGreg Roach $total += (int) $record->total; 11088de55fdSRico Sonntag 11188de55fdSRico Sonntag $data[] = [ 11293ccd686SRico Sonntag $this->century_service->centuryName((int) $record->century), 113151a7600SGreg Roach (int) $record->total 11488de55fdSRico Sonntag ]; 1158add1155SRico Sonntag } 1168add1155SRico Sonntag 117dd7dd2a1SRico Sonntag if ($total) { 11888de55fdSRico Sonntag $data[] = [ 11988de55fdSRico Sonntag I18N::translateContext('unknown century', 'Unknown'), 120b5c9e2c4SGreg Roach $no_child_fam - $total, 12188de55fdSRico Sonntag ]; 122dd7dd2a1SRico Sonntag } 1238add1155SRico Sonntag 124a81e5019SRico Sonntag $chart_title = I18N::translate('Number of families without children'); 125a81e5019SRico Sonntag $chart_options = [ 126a81e5019SRico Sonntag 'title' => $chart_title, 127a81e5019SRico Sonntag 'subtitle' => '', 128a81e5019SRico Sonntag 'legend' => [ 129a81e5019SRico Sonntag 'position' => 'none', 130a81e5019SRico Sonntag ], 131a81e5019SRico Sonntag 'vAxis' => [ 132a81e5019SRico Sonntag 'title' => I18N::translate('Total families'), 133a81e5019SRico Sonntag ], 134a81e5019SRico Sonntag 'hAxis' => [ 135a81e5019SRico Sonntag 'title' => I18N::translate('Century'), 136a81e5019SRico Sonntag ], 137a81e5019SRico Sonntag 'colors' => [ 138a81e5019SRico Sonntag '#84beff' 139a81e5019SRico Sonntag ], 140a81e5019SRico Sonntag ]; 141a81e5019SRico Sonntag 14290a2f718SGreg Roach return view('statistics/other/charts/column', [ 14388de55fdSRico Sonntag 'data' => $data, 144a81e5019SRico Sonntag 'chart_options' => $chart_options, 145a81e5019SRico Sonntag 'chart_title' => $chart_title, 14665cf5706SGreg Roach 'language' => I18N::languageTag(), 14790a2f718SGreg Roach ]); 1488add1155SRico Sonntag } 1498add1155SRico Sonntag} 150