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\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Database; 22use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Tree; 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 const DEFAULT_NUMBER = '10'; 34 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 bool $template 64 * @param string[] $cfg 65 * 66 * @return string 67 */ 68 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 69 { 70 global $ctype; 71 72 $num = (int) $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 73 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 74 75 extract($cfg, EXTR_OVERWRITE); 76 77 // Use the count of base surnames. 78 $top_surnames = Database::prepare( 79 "SELECT n_surn FROM `##name`" . 80 " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" . 81 " GROUP BY n_surn" . 82 " ORDER BY COUNT(n_surn) DESC" . 83 " LIMIT :limit" 84 )->execute([ 85 'tree_id' => $tree->id(), 86 'limit' => $num, 87 ])->fetchOneColumn(); 88 89 $all_surnames = []; 90 foreach ($top_surnames as $top_surname) { 91 $variants = Database::prepare( 92 "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1" 93 )->execute([ 94 'collate' => I18N::collation(), 95 'surname' => $top_surname, 96 'tree_id' => $tree->id(), 97 ])->fetchAssoc(); 98 99 $variants = array_map(function (string $count): int { 100 return (int) $count; 101 }, $variants); 102 103 $all_surnames[$top_surname] = $variants; 104 } 105 106 switch ($infoStyle) { 107 case 'tagcloud': 108 uksort($all_surnames, [I18N::class, 'strcasecmp']); 109 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 110 break; 111 case 'list': 112 uasort($all_surnames, [$this, 'surnameCountSort']); 113 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 114 break; 115 case 'array': 116 uasort($all_surnames, [$this, 'surnameCountSort']); 117 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 118 break; 119 case 'table': 120 default: 121 $content = view('lists/surnames-table', [ 122 'surnames' => $all_surnames, 123 'route' => 'individual-list', 124 'tree' => $tree, 125 ]); 126 break; 127 } 128 129 if ($template) { 130 $num = count($top_surnames); 131 if ($num === 1) { 132 // I18N: i.e. most popular surname. 133 $title = I18N::translate('Top surname'); 134 } else { 135 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 136 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 137 } 138 139 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 140 $config_url = route('tree-page-block-edit', [ 141 'block_id' => $block_id, 142 'ged' => $tree->getName(), 143 ]); 144 } elseif ($ctype === 'user' && Auth::check()) { 145 $config_url = route('user-page-block-edit', [ 146 'block_id' => $block_id, 147 'ged' => $tree->getName(), 148 ]); 149 } else { 150 $config_url = ''; 151 } 152 153 return view('modules/block-template', [ 154 'block' => str_replace('_', '-', $this->getName()), 155 'id' => $block_id, 156 'config_url' => $config_url, 157 'title' => $title, 158 'content' => $content, 159 ]); 160 } 161 162 return $content; 163 } 164 165 /** {@inheritdoc} */ 166 public function loadAjax(): bool 167 { 168 return false; 169 } 170 171 /** {@inheritdoc} */ 172 public function isUserBlock(): bool 173 { 174 return true; 175 } 176 177 /** {@inheritdoc} */ 178 public function isGedcomBlock(): bool 179 { 180 return true; 181 } 182 183 /** 184 * Update the configuration for a block. 185 * 186 * @param Request $request 187 * @param int $block_id 188 * 189 * @return void 190 */ 191 public function saveBlockConfiguration(Request $request, int $block_id) 192 { 193 $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 194 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 195 } 196 197 /** 198 * An HTML form to edit block settings 199 * 200 * @param Tree $tree 201 * @param int $block_id 202 * 203 * @return void 204 */ 205 public function editBlockConfiguration(Tree $tree, int $block_id) 206 { 207 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 208 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 209 210 $info_styles = [ 211 /* I18N: An option in a list-box */ 212 'list' => I18N::translate('bullet list'), 213 /* I18N: An option in a list-box */ 214 'array' => I18N::translate('compact list'), 215 /* I18N: An option in a list-box */ 216 'table' => I18N::translate('table'), 217 /* I18N: An option in a list-box */ 218 'tagcloud' => I18N::translate('tag cloud'), 219 ]; 220 221 echo view('modules/top10_surnames/config', [ 222 'num' => $num, 223 'infoStyle' => $infoStyle, 224 'info_styles' => $info_styles, 225 ]); 226 } 227 228 /** 229 * Sort (lists of counts of similar) surname by total count. 230 * 231 * @param string[] $a 232 * @param string[] $b 233 * 234 * @return int 235 */ 236 private function surnameCountSort(array $a, array $b): int 237 { 238 return array_sum($b) - array_sum($a); 239 } 240} 241