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; 25use stdClass; 26 27/** 28 * A chart showing the marriage ages by century. 29 */ 30class ChartMarriageAge 31{ 32 /** 33 * @var Tree 34 */ 35 private $tree; 36 37 /** 38 * @var CenturyService 39 */ 40 private $century_service; 41 42 /** 43 * Constructor. 44 * 45 * @param Tree $tree 46 */ 47 public function __construct(Tree $tree) 48 { 49 $this->tree = $tree; 50 $this->century_service = new CenturyService(); 51 } 52 53 /** 54 * Returns the related database records. 55 * 56 * @return stdClass[] 57 */ 58 private function queryRecords(): array 59 { 60 $prefix = DB::connection()->getTablePrefix(); 61 62 $male = DB::table('dates as married') 63 ->select([ 64 DB::raw('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'), 65 DB::raw('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'), 66 DB::raw("'M' as sex") 67 ]) 68 ->join('families as fam', static function (JoinClause $join): void { 69 $join->on('fam.f_id', '=', 'married.d_gid') 70 ->on('fam.f_file', '=', 'married.d_file'); 71 }) 72 ->join('dates as birth', static function (JoinClause $join): void { 73 $join->on('birth.d_gid', '=', 'fam.f_husb') 74 ->on('birth.d_file', '=', 'fam.f_file'); 75 }) 76 ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 77 ->where('married.d_file', '=', $this->tree->id()) 78 ->where('married.d_fact', '=', 'MARR') 79 ->where('married.d_julianday1', '>', 'birth.d_julianday1') 80 ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 81 ->where('birth.d_fact', '=', 'BIRT') 82 ->where('birth.d_julianday1', '<>', 0) 83 ->groupBy(['century', 'sex']); 84 85 $female = DB::table('dates as married') 86 ->select([ 87 DB::raw('ROUND(AVG(' . $prefix . 'married.d_julianday2 - ' . $prefix . 'birth.d_julianday1 - 182.5) / 365.25, 1) AS age'), 88 DB::raw('ROUND((' . $prefix . 'married.d_year + 49) / 100) AS century'), 89 DB::raw("'F' as sex") 90 ]) 91 ->join('families as fam', static function (JoinClause $join): void { 92 $join->on('fam.f_id', '=', 'married.d_gid') 93 ->on('fam.f_file', '=', 'married.d_file'); 94 }) 95 ->join('dates as birth', static function (JoinClause $join): void { 96 $join->on('birth.d_gid', '=', 'fam.f_wife') 97 ->on('birth.d_file', '=', 'fam.f_file'); 98 }) 99 ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 100 ->where('married.d_file', '=', $this->tree->id()) 101 ->where('married.d_fact', '=', 'MARR') 102 ->where('married.d_julianday1', '>', 'birth.d_julianday1') 103 ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 104 ->where('birth.d_fact', '=', 'BIRT') 105 ->where('birth.d_julianday1', '<>', 0) 106 ->groupBy(['century', 'sex']); 107 108 return $male->unionAll($female) 109 ->orderBy('century') 110 ->get() 111 ->all(); 112 } 113 114 /** 115 * General query on ages at marriage. 116 * 117 * @return string 118 */ 119 public function chartMarriageAge(): string 120 { 121 $out = []; 122 123 foreach ($this->queryRecords() as $record) { 124 $out[(int) $record->century][$record->sex] = (float) $record->age; 125 } 126 127 $data = [ 128 [ 129 I18N::translate('Century'), 130 I18N::translate('Males'), 131 I18N::translate('Females'), 132 I18N::translate('Average age'), 133 ] 134 ]; 135 136 foreach ($out as $century => $values) { 137 $female_age = $values['F'] ?? 0; 138 $male_age = $values['M'] ?? 0; 139 $average_age = ($female_age + $male_age) / 2.0; 140 141 $data[] = [ 142 $this->century_service->centuryName($century), 143 $male_age, 144 $female_age, 145 $average_age, 146 ]; 147 } 148 149 $chart_title = I18N::translate('Average age in century of marriage'); 150 $chart_options = [ 151 'title' => $chart_title, 152 'subtitle' => I18N::translate('Average age at marriage'), 153 'vAxis' => [ 154 'title' => I18N::translate('Age'), 155 ], 156 'hAxis' => [ 157 'title' => I18N::translate('Century'), 158 ], 159 'colors' => [ 160 '#84beff', 161 '#ffd1dc', 162 '#ff0000', 163 ], 164 ]; 165 166 return view( 167 'statistics/other/charts/combo', 168 [ 169 'data' => $data, 170 'chart_options' => $chart_options, 171 'chart_title' => $chart_title, 172 ] 173 ); 174 } 175} 176