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