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\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Tree; 24use Illuminate\Database\Capsule\Manager as DB; 25use Symfony\Component\HttpFoundation\Request; 26 27/** 28 * Class TopSurnamesModule 29 */ 30class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 31{ 32 // Default values for new blocks. 33 private const DEFAULT_NUMBER = '10'; 34 private const DEFAULT_STYLE = 'table'; 35 36 /** 37 * How should this module be labelled on tabs, menus, etc.? 38 * 39 * @return string 40 */ 41 public function getTitle(): string 42 { 43 /* I18N: Name of a module. Top=Most common */ 44 return I18N::translate('Top surnames'); 45 } 46 47 /** 48 * A sentence describing what this module does. 49 * 50 * @return string 51 */ 52 public function getDescription(): string 53 { 54 /* I18N: Description of the “Top surnames” module */ 55 return I18N::translate('A list of the most popular surnames.'); 56 } 57 58 /** 59 * Generate the HTML content of this block. 60 * 61 * @param Tree $tree 62 * @param int $block_id 63 * @param string $ctype 64 * @param string[] $cfg 65 * 66 * @return string 67 */ 68 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 69 { 70 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 71 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 72 73 extract($cfg, EXTR_OVERWRITE); 74 75 // Use the count of base surnames. 76 $top_surnames = DB::table('name') 77 ->where('n_file', '=', $tree->id()) 78 ->where('n_type', '<>', '_MARNM') 79 ->whereNotIn('n_surn', ['@N.N.', '']) 80 ->groupBy('n_surn') 81 ->orderByDesc(DB::raw('COUNT(n_surn)')) 82 ->take($num) 83 ->pluck('n_surn'); 84 85 $all_surnames = []; 86 87 foreach ($top_surnames as $top_surname) { 88 $variants = DB::table('name') 89 ->where('n_file', '=', $tree->id()) 90 ->where(DB::raw('n_surn /*! COLLATE utf8_bin */'), '=', $top_surname) 91 ->groupBy('surname') 92 ->select([DB::raw('n_surname /*! COLLATE utf8_bin */ AS surname'), DB::raw('count(*) AS total')]) 93 ->pluck('total', 'surname') 94 ->all(); 95 96 $all_surnames[$top_surname] = $variants; 97 } 98 99 switch ($infoStyle) { 100 case 'tagcloud': 101 uksort($all_surnames, [I18N::class, 'strcasecmp']); 102 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 103 break; 104 case 'list': 105 uasort($all_surnames, [$this, 'surnameCountSort']); 106 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 107 break; 108 case 'array': 109 uasort($all_surnames, [$this, 'surnameCountSort']); 110 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 111 break; 112 case 'table': 113 default: 114 $content = view('lists/surnames-table', [ 115 'surnames' => $all_surnames, 116 'route' => 'individual-list', 117 'tree' => $tree, 118 ]); 119 break; 120 } 121 122 if ($ctype !== '') { 123 $num = count($top_surnames); 124 if ($num === 1) { 125 // I18N: i.e. most popular surname. 126 $title = I18N::translate('Top surname'); 127 } else { 128 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 129 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 130 } 131 132 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 133 $config_url = route('tree-page-block-edit', [ 134 'block_id' => $block_id, 135 'ged' => $tree->name(), 136 ]); 137 } elseif ($ctype === 'user' && Auth::check()) { 138 $config_url = route('user-page-block-edit', [ 139 'block_id' => $block_id, 140 'ged' => $tree->name(), 141 ]); 142 } else { 143 $config_url = ''; 144 } 145 146 return view('modules/block-template', [ 147 'block' => str_replace('_', '-', $this->getName()), 148 'id' => $block_id, 149 'config_url' => $config_url, 150 'title' => $title, 151 'content' => $content, 152 ]); 153 } 154 155 return $content; 156 } 157 158 /** {@inheritdoc} */ 159 public function loadAjax(): bool 160 { 161 return false; 162 } 163 164 /** {@inheritdoc} */ 165 public function isUserBlock(): bool 166 { 167 return true; 168 } 169 170 /** {@inheritdoc} */ 171 public function isGedcomBlock(): bool 172 { 173 return true; 174 } 175 176 /** 177 * Update the configuration for a block. 178 * 179 * @param Request $request 180 * @param int $block_id 181 * 182 * @return void 183 */ 184 public function saveBlockConfiguration(Request $request, int $block_id) 185 { 186 $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 187 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 188 } 189 190 /** 191 * An HTML form to edit block settings 192 * 193 * @param Tree $tree 194 * @param int $block_id 195 * 196 * @return void 197 */ 198 public function editBlockConfiguration(Tree $tree, int $block_id) 199 { 200 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 201 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 202 203 $info_styles = [ 204 /* I18N: An option in a list-box */ 205 'list' => I18N::translate('bullet list'), 206 /* I18N: An option in a list-box */ 207 'array' => I18N::translate('compact list'), 208 /* I18N: An option in a list-box */ 209 'table' => I18N::translate('table'), 210 /* I18N: An option in a list-box */ 211 'tagcloud' => I18N::translate('tag cloud'), 212 ]; 213 214 echo view('modules/top10_surnames/config', [ 215 'num' => $num, 216 'infoStyle' => $infoStyle, 217 'info_styles' => $info_styles, 218 ]); 219 } 220 221 /** 222 * Sort (lists of counts of similar) surname by total count. 223 * 224 * @param string[] $a 225 * @param string[] $b 226 * 227 * @return int 228 */ 229 private function surnameCountSort(array $a, array $b): int 230 { 231 return array_sum($b) - array_sum($a); 232 } 233} 234