1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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\Tree; 25use Illuminate\Database\Capsule\Manager as DB; 26use Illuminate\Database\Query\JoinClause; 27use stdClass; 28 29/** 30 * A chart showing the number of families with no children by century. 31 */ 32class ChartNoChildrenFamilies 33{ 34 /** 35 * @var Tree 36 */ 37 private $tree; 38 39 /** 40 * @var CenturyService 41 */ 42 private $century_service; 43 44 /** 45 * Constructor. 46 * 47 * @param Tree $tree 48 */ 49 public function __construct(Tree $tree) 50 { 51 $this->tree = $tree; 52 $this->century_service = new CenturyService(); 53 } 54 55 /** 56 * Returns the related database records. 57 * 58 * @param int $year1 59 * @param int $year2 60 * 61 * @return stdClass[] 62 */ 63 private function queryRecords(int $year1, int $year2): array 64 { 65 $query = DB::table('families') 66 ->selectRaw('ROUND((d_year + 49) / 100) AS century') 67 ->selectRaw('COUNT(*) AS total') 68 ->join('dates', static function (JoinClause $join): void { 69 $join->on('d_file', '=', 'f_file') 70 ->on('d_gid', '=', 'f_id'); 71 }) 72 ->where('f_file', '=', $this->tree->id()) 73 ->where('f_numchil', '=', 0) 74 ->where('d_fact', '=', 'MARR') 75 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 76 ->groupBy(['century']) 77 ->orderBy('century'); 78 79 if ($year1 >= 0 && $year2 >= 0) { 80 $query->whereBetween('d_year', [$year1, $year2]); 81 } 82 83 return $query->get()->all(); 84 } 85 86 /** 87 * Create a chart of children with no families. 88 * 89 * @param int $no_child_fam The number of families with no children 90 * @param int $year1 91 * @param int $year2 92 * 93 * @return string 94 */ 95 public function chartNoChildrenFamilies( 96 int $no_child_fam, 97 int $year1 = -1, 98 int $year2 = -1 99 ): string { 100 $data = [ 101 [ 102 I18N::translate('Century'), 103 I18N::translate('Total') 104 ] 105 ]; 106 107 $total = 0; 108 109 foreach ($this->queryRecords($year1, $year2) as $record) { 110 $total += (int) $record->total; 111 112 $data[] = [ 113 $this->century_service->centuryName((int) $record->century), 114 (int) $record->total 115 ]; 116 } 117 118 if ($total) { 119 $data[] = [ 120 I18N::translateContext('unknown century', 'Unknown'), 121 $total - $no_child_fam 122 ]; 123 } 124 125 $chart_title = I18N::translate('Number of families without children'); 126 $chart_options = [ 127 'title' => $chart_title, 128 'subtitle' => '', 129 'legend' => [ 130 'position' => 'none', 131 ], 132 'vAxis' => [ 133 'title' => I18N::translate('Total families'), 134 ], 135 'hAxis' => [ 136 'title' => I18N::translate('Century'), 137 ], 138 'colors' => [ 139 '#84beff' 140 ], 141 ]; 142 143 return view('statistics/other/charts/column', [ 144 'data' => $data, 145 'chart_options' => $chart_options, 146 'chart_title' => $chart_title, 147 'language' => I18N::languageTag(), 148 ]); 149 } 150} 151