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