1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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\DB; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Statistics\Service\CenturyService; 25use Fisharebest\Webtrees\Statistics\Service\ColorService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Support\Collection; 28 29use function count; 30use function view; 31 32/** 33 * A chart showing the divorces by century. 34 */ 35class ChartDivorce 36{ 37 private Tree $tree; 38 39 private CenturyService $century_service; 40 41 private ColorService $color_service; 42 43 /** 44 * @param CenturyService $century_service 45 * @param ColorService $color_service 46 * @param Tree $tree 47 */ 48 public function __construct(CenturyService $century_service, ColorService $color_service, Tree $tree) 49 { 50 $this->century_service = $century_service; 51 $this->color_service = $color_service; 52 $this->tree = $tree; 53 } 54 55 /** 56 * Returns the related database records. 57 * 58 * @return Collection<array-key,object> 59 */ 60 private function queryRecords(): Collection 61 { 62 return DB::table('dates') 63 ->selectRaw('ROUND((d_year + 49) / 100, 0) AS century') 64 ->selectRaw('COUNT(*) AS total') 65 ->where('d_file', '=', $this->tree->id()) 66 ->where('d_year', '<>', 0) 67 ->where('d_fact', '=', 'DIV') 68 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 69 ->groupBy(['century']) 70 ->orderBy('century') 71 ->get() 72 ->map(static fn (object $row): object => (object) [ 73 'century' => (int) $row->century, 74 'total' => (float) $row->total, 75 ]); 76 } 77 78 /** 79 * General query on divorces. 80 * 81 * @param string|null $color_from 82 * @param string|null $color_to 83 * 84 * @return string 85 */ 86 public function chartDivorce(string|null $color_from = null, string|null $color_to = null): string 87 { 88 $color_from ??= 'ffffff'; 89 $color_to ??= '84beff'; 90 91 $data = [ 92 [ 93 I18N::translate('Century'), 94 I18N::translate('Total') 95 ], 96 ]; 97 98 foreach ($this->queryRecords() as $record) { 99 $data[] = [ 100 $this->century_service->centuryName($record->century), 101 $record->total 102 ]; 103 } 104 105 $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); 106 107 return view('statistics/other/charts/pie', [ 108 'title' => I18N::translate('Divorces by century'), 109 'data' => $data, 110 'colors' => $colors, 111 'language' => I18N::languageTag(), 112 ]); 113 } 114} 115