1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Statistics\Google; 19 20use Fisharebest\Webtrees\I18N; 21use Fisharebest\Webtrees\Statistics\Service\CenturyService; 22use Fisharebest\Webtrees\Tree; 23use Illuminate\Database\Capsule\Manager as DB; 24use Illuminate\Database\Query\JoinClause; 25 26/** 27 * A chart showing the number of families with no children by century. 28 */ 29class ChartNoChildrenFamilies 30{ 31 /** 32 * @var Tree 33 */ 34 private $tree; 35 36 /** 37 * @var CenturyService 38 */ 39 private $century_service; 40 41 /** 42 * Constructor. 43 * 44 * @param Tree $tree 45 */ 46 public function __construct(Tree $tree) 47 { 48 $this->tree = $tree; 49 $this->century_service = new CenturyService(); 50 } 51 52 /** 53 * Returns the related database records. 54 * 55 * @param int $year1 56 * @param int $year2 57 * 58 * @return \stdClass[] 59 */ 60 private function queryRecords(int $year1, int $year2): array 61 { 62 $query = DB::table('families') 63 ->selectRaw('ROUND((d_year + 49) / 100) AS century') 64 ->selectRaw('COUNT(*) AS total') 65 ->join('dates', function (JoinClause $join) { 66 $join->on('d_file', '=', 'f_file') 67 ->on('d_gid', '=', 'f_id'); 68 }) 69 ->where('f_file', '=', $this->tree->id()) 70 ->where('f_numchil', '=', 0) 71 ->where('d_fact', '=', 'MARR') 72 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 73 ->groupBy(['century']) 74 ->orderBy('century'); 75 76 if ($year1 >= 0 && $year2 >= 0) { 77 $query->whereBetween('d_year', [$year1, $year2]); 78 } 79 80 return $query->get()->all(); 81 } 82 83 /** 84 * Create a chart of children with no families. 85 * 86 * @param int $no_child_fam The number of families with no children 87 * @param int $year1 88 * @param int $year2 89 * 90 * @return string 91 */ 92 public function chartNoChildrenFamilies( 93 int $no_child_fam, 94 int $year1 = -1, 95 int $year2 = -1 96 ): string { 97 $data = [ 98 [ 99 I18N::translate('Century'), 100 I18N::translate('Total') 101 ] 102 ]; 103 104 $total = 0; 105 106 foreach ($this->queryRecords($year1, $year2) as $record) { 107 $total += $record->total; 108 109 $data[] = [ 110 $this->century_service->centuryName((int) $record->century), 111 $record->total 112 ]; 113 } 114 115 if ($total) { 116 $data[] = [ 117 I18N::translateContext('unknown century', 'Unknown'), 118 $no_child_fam - $total 119 ]; 120 } 121 122 $chart_title = I18N::translate('Number of families without children'); 123 $chart_options = [ 124 'title' => $chart_title, 125 'subtitle' => '', 126 'legend' => [ 127 'position' => 'none', 128 ], 129 'vAxis' => [ 130 'title' => I18N::translate('Total families'), 131 ], 132 'hAxis' => [ 133 'title' => I18N::translate('Century'), 134 ], 135 'colors' => [ 136 '#84beff' 137 ], 138 ]; 139 140 return view( 141 'statistics/other/charts/column', 142 [ 143 'data' => $data, 144 'chart_options' => $chart_options, 145 'chart_title' => $chart_title, 146 ] 147 ); 148 } 149} 150