1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Statistics\Google; 21 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Module\ModuleThemeInterface; 24use Fisharebest\Webtrees\Statistics\Repository\IndividualRepository; 25use Fisharebest\Webtrees\Statistics\Repository\PlaceRepository; 26use Fisharebest\Webtrees\Statistics\Service\CountryService; 27use Fisharebest\Webtrees\Tree; 28use Illuminate\Database\Capsule\Manager as DB; 29use Illuminate\Database\Query\Expression; 30use Illuminate\Database\Query\JoinClause; 31 32use function app; 33use function array_key_exists; 34 35/** 36 * A chart showing the distribution of different events on a map. 37 */ 38class ChartDistribution 39{ 40 /** 41 * @var Tree 42 */ 43 private $tree; 44 45 /** 46 * @var ModuleThemeInterface 47 */ 48 private $theme; 49 50 /** 51 * @var CountryService 52 */ 53 private $country_service; 54 55 /** 56 * @var IndividualRepository 57 */ 58 private $individualRepository; 59 60 /** 61 * @var PlaceRepository 62 */ 63 private $placeRepository; 64 65 /** 66 * @var array<string> 67 */ 68 private array $country_to_iso3166; 69 70 /** 71 * Constructor. 72 * 73 * @param Tree $tree 74 * @param CountryService $country_service 75 */ 76 public function __construct(Tree $tree, CountryService $country_service) 77 { 78 $this->tree = $tree; 79 $this->theme = app(ModuleThemeInterface::class); 80 $this->country_service = new CountryService(); 81 $this->individualRepository = new IndividualRepository($tree); 82 $this->placeRepository = new PlaceRepository($tree, $country_service); 83 84 // Get the country names for each language 85 $this->country_to_iso3166 = $this->getIso3166Countries(); 86 } 87 88 /** 89 * Returns the country names for each language. 90 * 91 * @return array<string> 92 */ 93 private function getIso3166Countries(): array 94 { 95 // Get the country names for each language 96 $country_to_iso3166 = []; 97 98 $current_language = I18N::languageTag(); 99 100 foreach (I18N::activeLocales() as $locale) { 101 I18N::init($locale->languageTag()); 102 103 $countries = $this->country_service->getAllCountries(); 104 105 foreach ($this->country_service->iso3166() as $three => $two) { 106 $country_to_iso3166[$three] = $two; 107 $country_to_iso3166[$countries[$three]] = $two; 108 } 109 } 110 111 I18N::init($current_language); 112 113 return $country_to_iso3166; 114 } 115 116 /** 117 * Returns the data structure required by google geochart. 118 * 119 * @param array<int|string,int> $places 120 * 121 * @return array 122 */ 123 private function createChartData(array $places): array 124 { 125 $data = [ 126 [ 127 I18N::translate('Country'), 128 I18N::translate('Total'), 129 ], 130 ]; 131 132 // webtrees uses 3-letter country codes and localised country names, but google uses 2 letter codes. 133 foreach ($places as $country => $count) { 134 $data[] = [ 135 [ 136 'v' => $country, 137 'f' => $this->country_service->mapTwoLetterToName($country), 138 ], 139 $count 140 ]; 141 } 142 143 return $data; 144 } 145 146 /** 147 * Returns the google geochart data for birth fact. 148 * 149 * @return array 150 */ 151 private function getBirthChartData(): array 152 { 153 // Count how many people were born in each country 154 $surn_countries = []; 155 $b_countries = $this->placeRepository->statsPlaces('INDI', 'BIRT', 0, true); 156 157 foreach ($b_countries as $country => $count) { 158 // Consolidate places (Germany, DEU => DE) 159 if (array_key_exists($country, $this->country_to_iso3166)) { 160 $country_code = $this->country_to_iso3166[$country]; 161 162 if (array_key_exists($country_code, $surn_countries)) { 163 $surn_countries[$country_code] += $count; 164 } else { 165 $surn_countries[$country_code] = $count; 166 } 167 } 168 } 169 170 return $this->createChartData($surn_countries); 171 } 172 173 /** 174 * Returns the google geochart data for death fact. 175 * 176 * @return array 177 */ 178 private function getDeathChartData(): array 179 { 180 // Count how many people were death in each country 181 $surn_countries = []; 182 $d_countries = $this->placeRepository->statsPlaces('INDI', 'DEAT', 0, true); 183 184 foreach ($d_countries as $country => $count) { 185 // Consolidate places (Germany, DEU => DE) 186 if (array_key_exists($country, $this->country_to_iso3166)) { 187 $country_code = $this->country_to_iso3166[$country]; 188 189 if (array_key_exists($country_code, $surn_countries)) { 190 $surn_countries[$country_code] += $count; 191 } else { 192 $surn_countries[$country_code] = $count; 193 } 194 } 195 } 196 197 return $this->createChartData($surn_countries); 198 } 199 200 /** 201 * Returns the google geochart data for marriages. 202 * 203 * @return array 204 */ 205 private function getMarriageChartData(): array 206 { 207 // Count how many families got marriage in each country 208 $surn_countries = []; 209 $m_countries = $this->placeRepository->statsPlaces('FAM'); 210 211 // webtrees uses 3 letter country codes and localised country names, but google uses 2 letter codes. 212 foreach ($m_countries as $place) { 213 // Consolidate places (Germany, DEU => DE) 214 if (array_key_exists($place->country, $this->country_to_iso3166)) { 215 $country_code = $this->country_to_iso3166[$place->country]; 216 217 if (array_key_exists($country_code, $surn_countries)) { 218 $surn_countries[$country_code] += $place->tot; 219 } else { 220 $surn_countries[$country_code] = $place->tot; 221 } 222 } 223 } 224 225 return $this->createChartData($surn_countries); 226 } 227 228 /** 229 * Returns the related database records. 230 * 231 * @param string $surname 232 * 233 * @return array<object> 234 */ 235 private function queryRecords(string $surname): array 236 { 237 $query = DB::table('individuals') 238 ->select(['i_gedcom']) 239 ->join('name', static function (JoinClause $join): void { 240 $join->on('n_id', '=', 'i_id') 241 ->on('n_file', '=', 'i_file'); 242 }) 243 ->where('n_file', '=', $this->tree->id()) 244 ->where(new Expression('n_surn /*! COLLATE ' . I18N::collation() . ' */'), '=', $surname); 245 246 return $query->get()->all(); 247 } 248 249 /** 250 * Returns the google geochart data for surnames. 251 * 252 * @param string $surname The surname used to create the chart 253 * 254 * @return array 255 */ 256 private function getSurnameChartData(string $surname): array 257 { 258 if ($surname === '') { 259 $surname = $this->individualRepository->getCommonSurname(); 260 } 261 262 // Count how many people are events in each country 263 $surn_countries = []; 264 $records = $this->queryRecords($surname); 265 266 foreach ($records as $row) { 267 if (preg_match_all('/^2 PLAC (?:.*, *)*(.*)/m', $row->i_gedcom, $matches)) { 268 // webtrees uses 3 letter country codes and localised country names, 269 // but google uses 2 letter codes. 270 foreach ($matches[1] as $country) { 271 // Consolidate places (Germany, DEU => DE) 272 if (array_key_exists($country, $this->country_to_iso3166)) { 273 $country_code = $this->country_to_iso3166[$country]; 274 275 if (array_key_exists($country_code, $surn_countries)) { 276 $surn_countries[$country_code]++; 277 } else { 278 $surn_countries[$country_code] = 1; 279 } 280 } 281 } 282 } 283 } 284 285 return $this->createChartData($surn_countries); 286 } 287 288 /** 289 * Returns the google geochart data for individuals. 290 * 291 * @return array 292 */ 293 private function getIndivdualChartData(): array 294 { 295 // Count how many people have events in each country 296 $surn_countries = []; 297 $a_countries = $this->placeRepository->statsPlaces('INDI'); 298 299 // webtrees uses 3 letter country codes and localised country names, but google uses 2 letter codes. 300 foreach ($a_countries as $place) { 301 // Consolidate places (Germany, DEU => DE) 302 if (array_key_exists($place->country, $this->country_to_iso3166)) { 303 $country_code = $this->country_to_iso3166[$place->country]; 304 305 if (array_key_exists($country_code, $surn_countries)) { 306 $surn_countries[$country_code] += $place->tot; 307 } else { 308 $surn_countries[$country_code] = $place->tot; 309 } 310 } 311 } 312 313 return $this->createChartData($surn_countries); 314 } 315 316 /** 317 * Create a chart showing where events occurred. 318 * 319 * @param string $chart_shows The type of chart map to show 320 * @param string $chart_type The type of chart to show 321 * @param string $surname The surname for surname based distribution chart 322 * 323 * @return string 324 */ 325 public function chartDistribution( 326 string $chart_shows = 'world', 327 string $chart_type = '', 328 string $surname = '' 329 ): string { 330 switch ($chart_type) { 331 case 'surname_distribution_chart': 332 $chart_title = I18N::translate('Surname distribution chart') . ': ' . $surname; 333 $data = $this->getSurnameChartData($surname); 334 break; 335 336 case 'birth_distribution_chart': 337 $chart_title = I18N::translate('Birth by country'); 338 $data = $this->getBirthChartData(); 339 break; 340 341 case 'death_distribution_chart': 342 $chart_title = I18N::translate('Death by country'); 343 $data = $this->getDeathChartData(); 344 break; 345 346 case 'marriage_distribution_chart': 347 $chart_title = I18N::translate('Marriage by country'); 348 $data = $this->getMarriageChartData(); 349 break; 350 351 case 'indi_distribution_chart': 352 default: 353 $chart_title = I18N::translate('Individual distribution chart'); 354 $data = $this->getIndivdualChartData(); 355 break; 356 } 357 358 $chart_color2 = $this->theme->parameter('distribution-chart-high-values'); 359 $chart_color3 = $this->theme->parameter('distribution-chart-low-values'); 360 361 return view('statistics/other/charts/geo', [ 362 'chart_title' => $chart_title, 363 'chart_color2' => $chart_color2, 364 'chart_color3' => $chart_color3, 365 'region' => $chart_shows, 366 'data' => $data, 367 'language' => I18N::languageTag(), 368 ]); 369 } 370} 371