18add1155SRico Sonntag<?php 23976b470SGreg Roach 38add1155SRico Sonntag/** 48add1155SRico Sonntag * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 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 22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 238add1155SRico Sonntaguse Fisharebest\Webtrees\I18N; 2493ccd686SRico Sonntaguse Fisharebest\Webtrees\Statistics\Service\CenturyService; 258add1155SRico Sonntaguse Fisharebest\Webtrees\Tree; 268add1155SRico Sonntaguse Illuminate\Database\Query\JoinClause; 278add1155SRico Sonntag 2834b20f29SGreg Roachuse function view; 2934b20f29SGreg Roach 308add1155SRico Sonntag/** 3193ccd686SRico Sonntag * A chart showing the number of families with no children by century. 328add1155SRico Sonntag */ 3393ccd686SRico Sonntagclass ChartNoChildrenFamilies 348add1155SRico Sonntag{ 3534b20f29SGreg Roach private Tree $tree; 3693ccd686SRico Sonntag 3734b20f29SGreg Roach private CenturyService $century_service; 388add1155SRico Sonntag 398add1155SRico Sonntag /** 40f78da678SGreg Roach * @param CenturyService $century_service 418add1155SRico Sonntag * @param Tree $tree 428add1155SRico Sonntag */ 43f78da678SGreg Roach public function __construct(CenturyService $century_service, Tree $tree) 448add1155SRico Sonntag { 45f78da678SGreg Roach $this->century_service = $century_service; 4693ccd686SRico Sonntag $this->tree = $tree; 478add1155SRico Sonntag } 488add1155SRico Sonntag 498add1155SRico Sonntag /** 508add1155SRico Sonntag * Returns the related database records. 518add1155SRico Sonntag * 528add1155SRico Sonntag * @param int $year1 538add1155SRico Sonntag * @param int $year2 548add1155SRico Sonntag * 55f70bcff5SGreg Roach * @return array<object> 568add1155SRico Sonntag */ 578add1155SRico Sonntag private function queryRecords(int $year1, int $year2): array 588add1155SRico Sonntag { 598add1155SRico Sonntag $query = DB::table('families') 60cde98219SGreg Roach ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century') 6188de55fdSRico Sonntag ->selectRaw('COUNT(*) AS total') 620b5fd0a6SGreg Roach ->join('dates', static function (JoinClause $join): void { 638add1155SRico Sonntag $join->on('d_file', '=', 'f_file') 648add1155SRico Sonntag ->on('d_gid', '=', 'f_id'); 658add1155SRico Sonntag }) 668add1155SRico Sonntag ->where('f_file', '=', $this->tree->id()) 678add1155SRico Sonntag ->where('f_numchil', '=', 0) 688add1155SRico Sonntag ->where('d_fact', '=', 'MARR') 698add1155SRico Sonntag ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 708add1155SRico Sonntag ->groupBy(['century']) 718add1155SRico Sonntag ->orderBy('century'); 728add1155SRico Sonntag 738add1155SRico Sonntag if ($year1 >= 0 && $year2 >= 0) { 748add1155SRico Sonntag $query->whereBetween('d_year', [$year1, $year2]); 758add1155SRico Sonntag } 768add1155SRico Sonntag 778add1155SRico Sonntag return $query->get()->all(); 788add1155SRico Sonntag } 798add1155SRico Sonntag 808add1155SRico Sonntag /** 818add1155SRico Sonntag * Create a chart of children with no families. 828add1155SRico Sonntag * 838add1155SRico Sonntag * @param int $no_child_fam The number of families with no children 848add1155SRico Sonntag * @param int $year1 858add1155SRico Sonntag * @param int $year2 868add1155SRico Sonntag * 878add1155SRico Sonntag * @return string 888add1155SRico Sonntag */ 898add1155SRico Sonntag public function chartNoChildrenFamilies( 908add1155SRico Sonntag int $no_child_fam, 918add1155SRico Sonntag int $year1 = -1, 928add1155SRico Sonntag int $year2 = -1 938add1155SRico Sonntag ): string { 9488de55fdSRico Sonntag $data = [ 9588de55fdSRico Sonntag [ 9688de55fdSRico Sonntag I18N::translate('Century'), 9788de55fdSRico Sonntag I18N::translate('Total') 9888de55fdSRico Sonntag ] 9988de55fdSRico Sonntag ]; 1008add1155SRico Sonntag 10188de55fdSRico Sonntag $total = 0; 10288de55fdSRico Sonntag 10388de55fdSRico Sonntag foreach ($this->queryRecords($year1, $year2) as $record) { 104151a7600SGreg Roach $total += (int) $record->total; 10588de55fdSRico Sonntag 10688de55fdSRico Sonntag $data[] = [ 10793ccd686SRico Sonntag $this->century_service->centuryName((int) $record->century), 108151a7600SGreg Roach (int) $record->total 10988de55fdSRico Sonntag ]; 1108add1155SRico Sonntag } 1118add1155SRico Sonntag 112ef475b14SGreg Roach if ($total > 0) { 11388de55fdSRico Sonntag $data[] = [ 11488de55fdSRico Sonntag I18N::translateContext('unknown century', 'Unknown'), 115b5c9e2c4SGreg Roach $no_child_fam - $total, 11688de55fdSRico Sonntag ]; 117dd7dd2a1SRico Sonntag } 1188add1155SRico Sonntag 119a81e5019SRico Sonntag $chart_title = I18N::translate('Number of families without children'); 120a81e5019SRico Sonntag $chart_options = [ 121a81e5019SRico Sonntag 'title' => $chart_title, 122a81e5019SRico Sonntag 'subtitle' => '', 123a81e5019SRico Sonntag 'legend' => [ 124a81e5019SRico Sonntag 'position' => 'none', 125a81e5019SRico Sonntag ], 126a81e5019SRico Sonntag 'vAxis' => [ 127a81e5019SRico Sonntag 'title' => I18N::translate('Total families'), 128a81e5019SRico Sonntag ], 129a81e5019SRico Sonntag 'hAxis' => [ 13076e7c249SGreg Roach 'showTextEvery' => 1, 13176e7c249SGreg Roach 'slantedText' => false, 132a81e5019SRico Sonntag 'title' => I18N::translate('Century'), 133a81e5019SRico Sonntag ], 134a81e5019SRico Sonntag 'colors' => [ 135a81e5019SRico Sonntag '#84beff' 136a81e5019SRico Sonntag ], 137a81e5019SRico Sonntag ]; 138a81e5019SRico Sonntag 13990a2f718SGreg Roach return view('statistics/other/charts/column', [ 14088de55fdSRico Sonntag 'data' => $data, 141a81e5019SRico Sonntag 'chart_options' => $chart_options, 142a81e5019SRico Sonntag 'chart_title' => $chart_title, 14365cf5706SGreg Roach 'language' => I18N::languageTag(), 14490a2f718SGreg Roach ]); 1458add1155SRico Sonntag } 1468add1155SRico Sonntag} 147