1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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\Module\ModuleThemeInterface; 22use Fisharebest\Webtrees\Statistics\Helper\Century; 23use Fisharebest\Webtrees\Statistics\AbstractGoogle; 24use Fisharebest\Webtrees\Theme; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27 28/** 29 * 30 */ 31class ChartDeath extends AbstractGoogle 32{ 33 /** 34 * @var Tree 35 */ 36 private $tree; 37 38 /** 39 * @var Century 40 */ 41 private $centuryHelper; 42 43 /** 44 * Constructor. 45 * 46 * @param Tree $tree 47 */ 48 public function __construct(Tree $tree) 49 { 50 $this->tree = $tree; 51 $this->centuryHelper = new Century(); 52 } 53 54 /** 55 * Returns the related database records. 56 * 57 * @return \stdClass[] 58 */ 59 private function queryRecords(): array 60 { 61 $query = DB::table('dates') 62 ->selectRaw('FLOOR(d_year / 100 + 1) AS century') 63 ->selectRaw('COUNT(*) AS total') 64 ->where('d_file', '=', $this->tree->id()) 65 ->where('d_year', '<>', 0) 66 ->where('d_fact', '=', 'DEAT') 67 ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) 68 ->groupBy(['century']) 69 ->orderBy('century'); 70 71 return $query->get()->all(); 72 } 73 74 /** 75 * Create a chart of death places. 76 * 77 * @param string|null $size 78 * @param string|null $color_from 79 * @param string|null $color_to 80 * 81 * @return string 82 */ 83 public function chartDeath(string $size = null, string $color_from = null, string $color_to = null): string 84 { 85 $chart_color1 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-no-values'); 86 $chart_color2 = (string) app()->make(ModuleThemeInterface::class)->parameter('distribution-chart-high-values'); 87 $chart_x = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-x'); 88 $chart_y = app()->make(ModuleThemeInterface::class)->parameter('stats-small-chart-y'); 89 90 $size = $size ?? ($chart_x . 'x' . $chart_y); 91 $color_from = $color_from ?? $chart_color1; 92 $color_to = $color_to ?? $chart_color2; 93 94 $sizes = explode('x', $size); 95 $tot = 0; 96 $rows = $this->queryRecords(); 97 98 foreach ($rows as $values) { 99 $values->total = (int) $values->total; 100 $tot += $values->total; 101 } 102 103 // Beware divide by zero 104 if ($tot === 0) { 105 return ''; 106 } 107 108 $centuries = ''; 109 $counts = []; 110 foreach ($rows as $values) { 111 $counts[] = intdiv(100 * $values->total, $tot); 112 $centuries .= $this->centuryHelper->centuryName((int) $values->century) . ' - ' . I18N::number($values->total) . '|'; 113 } 114 115 $chd = $this->arrayToExtendedEncoding($counts); 116 $chl = rawurlencode(substr($centuries, 0, -1)); 117 $colors = [$color_from, $color_to]; 118 119 return view( 120 'statistics/other/chart-google', 121 [ 122 'chart_title' => I18N::translate('Deaths by century'), 123 'chart_url' => $this->getPieChartUrl($chd, $size, $colors, $chl), 124 'sizes' => $sizes, 125 ] 126 ); 127 } 128} 129