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