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->getTreeId(), 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->getTreeId(), 97 ])->fetchAssoc(); 98 99 $all_surnames[$top_surname] = $variants; 100 } 101 102 switch ($infoStyle) { 103 case 'tagcloud': 104 uksort($all_surnames, [I18N::class, 'strcasecmp']); 105 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 106 break; 107 case 'list': 108 uasort($all_surnames, [$this, 'surnameCountSort']); 109 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 110 break; 111 case 'array': 112 uasort($all_surnames, [$this, 'surnameCountSort']); 113 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 114 break; 115 case 'table': 116 default: 117 $content = view('lists/surnames-table', [ 118 'surnames' => $all_surnames, 119 'route' => 'individual-list', 120 'tree' => $tree, 121 ]); 122 break; 123 } 124 125 if ($template) { 126 $num = count($top_surnames); 127 if ($num === 1) { 128 // I18N: i.e. most popular surname. 129 $title = I18N::translate('Top surname'); 130 } else { 131 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 132 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 133 } 134 135 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 136 $config_url = route('tree-page-block-edit', [ 137 'block_id' => $block_id, 138 'ged' => $tree->getName(), 139 ]); 140 } elseif ($ctype === 'user' && Auth::check()) { 141 $config_url = route('user-page-block-edit', [ 142 'block_id' => $block_id, 143 'ged' => $tree->getName(), 144 ]); 145 } else { 146 $config_url = ''; 147 } 148 149 return view('modules/block-template', [ 150 'block' => str_replace('_', '-', $this->getName()), 151 'id' => $block_id, 152 'config_url' => $config_url, 153 'title' => $title, 154 'content' => $content, 155 ]); 156 } 157 158 return $content; 159 } 160 161 /** {@inheritdoc} */ 162 public function loadAjax(): bool 163 { 164 return false; 165 } 166 167 /** {@inheritdoc} */ 168 public function isUserBlock(): bool 169 { 170 return true; 171 } 172 173 /** {@inheritdoc} */ 174 public function isGedcomBlock(): bool 175 { 176 return true; 177 } 178 179 /** 180 * Update the configuration for a block. 181 * 182 * @param Request $request 183 * @param int $block_id 184 * 185 * @return void 186 */ 187 public function saveBlockConfiguration(Request $request, int $block_id) 188 { 189 $this->setBlockSetting($block_id, 'num', $request->get('num', self::DEFAULT_NUMBER)); 190 $this->setBlockSetting($block_id, 'infoStyle', $request->get('infoStyle', self::DEFAULT_STYLE)); 191 } 192 193 /** 194 * An HTML form to edit block settings 195 * 196 * @param Tree $tree 197 * @param int $block_id 198 * 199 * @return void 200 */ 201 public function editBlockConfiguration(Tree $tree, int $block_id) 202 { 203 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 204 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 205 206 $info_styles = [ 207 /* I18N: An option in a list-box */ 208 'list' => I18N::translate('bullet list'), 209 /* I18N: An option in a list-box */ 210 'array' => I18N::translate('compact list'), 211 /* I18N: An option in a list-box */ 212 'table' => I18N::translate('table'), 213 /* I18N: An option in a list-box */ 214 'tagcloud' => I18N::translate('tag cloud'), 215 ]; 216 217 echo view('modules/top10_surnames/config', [ 218 'num' => $num, 219 'infoStyle' => $infoStyle, 220 'info_styles' => $info_styles, 221 ]); 222 } 223 224 /** 225 * Sort (lists of counts of similar) surname by total count. 226 * 227 * @param string[] $a 228 * @param string[] $b 229 * 230 * @return int 231 */ 232 private function surnameCountSort(array $a, array $b): int 233 { 234 return array_sum($b) - array_sum($a); 235 } 236} 237