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\Module\ModuleThemeInterface; 24use Fisharebest\Webtrees\Statistics\Service\CenturyService; 25use Fisharebest\Webtrees\Statistics\Service\ColorService; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Capsule\Manager as DB; 28use stdClass; 29 30use function count; 31 32/** 33 * A chart showing the divorces by century. 34 */ 35class ChartDivorce 36{ 37 /** 38 * @var Tree 39 */ 40 private $tree; 41 42 /** 43 * @var ModuleThemeInterface 44 */ 45 private $theme; 46 47 /** 48 * @var CenturyService 49 */ 50 private $century_service; 51 52 /** 53 * @var ColorService 54 */ 55 private $color_service; 56 57 /** 58 * Constructor. 59 * 60 * @param Tree $tree 61 */ 62 public function __construct(Tree $tree) 63 { 64 $this->tree = $tree; 65 $this->theme = app(ModuleThemeInterface::class); 66 $this->century_service = new CenturyService(); 67 $this->color_service = new ColorService(); 68 } 69 70 /** 71 * Returns the related database records. 72 * 73 * @return stdClass[] 74 */ 75 private function queryRecords(): array 76 { 77 $query = DB::table('dates') 78 ->selectRaw('ROUND((d_year + 49) / 100) AS century') 79 ->selectRaw('COUNT(*) AS total') 80 ->where('d_file', '=', $this->tree->id()) 81 ->where('d_year', '<>', 0) 82 ->where('d_fact', '=', 'DIV') 83 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 84 ->groupBy(['century']) 85 ->orderBy('century'); 86 87 return $query->get()->all(); 88 } 89 90 /** 91 * General query on divorces. 92 * 93 * @param string|null $color_from 94 * @param string|null $color_to 95 * 96 * @return string 97 */ 98 public function chartDivorce(string $color_from = null, string $color_to = null): string 99 { 100 $chart_color1 = (string) $this->theme->parameter('distribution-chart-no-values'); 101 $chart_color2 = (string) $this->theme->parameter('distribution-chart-high-values'); 102 $color_from = $color_from ?? $chart_color1; 103 $color_to = $color_to ?? $chart_color2; 104 105 $data = [ 106 [ 107 I18N::translate('Century'), 108 I18N::translate('Total') 109 ], 110 ]; 111 112 foreach ($this->queryRecords() as $record) { 113 $data[] = [ 114 $this->century_service->centuryName((int) $record->century), 115 $record->total 116 ]; 117 } 118 119 $colors = $this->color_service->interpolateRgb($color_from, $color_to, count($data) - 1); 120 121 return view( 122 'statistics/other/charts/pie', 123 [ 124 'title' => I18N::translate('Divorces by century'), 125 'data' => $data, 126 'colors' => $colors, 127 ] 128 ); 129 } 130} 131