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 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Filter; 21use Fisharebest\Webtrees\Functions\FunctionsPrintLists; 22use Fisharebest\Webtrees\I18N; 23use Fisharebest\Webtrees\Tree; 24 25/** 26 * Class TopSurnamesModule 27 */ 28class TopSurnamesModule extends AbstractModule implements ModuleBlockInterface 29{ 30 // Default values for new blocks. 31 const DEFAULT_NUMBER = 10; 32 const DEFAULT_STYLE = 'table'; 33 34 /** 35 * How should this module be labelled on tabs, menus, etc.? 36 * 37 * @return string 38 */ 39 public function getTitle() 40 { 41 return /* I18N: Name of a module. Top=Most common */ 42 I18N::translate('Top surnames'); 43 } 44 45 /** 46 * A sentence describing what this module does. 47 * 48 * @return string 49 */ 50 public function getDescription() 51 { 52 return /* I18N: Description of the “Top surnames” module */ 53 I18N::translate('A list of the most popular surnames.'); 54 } 55 56 /** 57 * Generate the HTML content of this block. 58 * 59 * @param Tree $tree 60 * @param int $block_id 61 * @param bool $template 62 * @param string[] $cfg 63 * 64 * @return string 65 */ 66 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 67 { 68 global $ctype; 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 = Database::prepare( 77 "SELECT n_surn FROM `##name`" . 78 " WHERE n_file = :tree_id AND n_type != '_MARNM' AND n_surn NOT IN ('@N.N.', '')" . 79 " GROUP BY n_surn" . 80 " ORDER BY COUNT(n_surn) DESC" . 81 " LIMIT :limit" 82 )->execute([ 83 'tree_id' => $tree->getTreeId(), 84 'limit' => $num, 85 ])->fetchOneColumn(); 86 87 $all_surnames = []; 88 foreach ($top_surnames as $top_surname) { 89 $variants = Database::prepare( 90 "SELECT n_surname COLLATE utf8_bin, COUNT(*) FROM `##name` WHERE n_file = :tree_id AND n_surn COLLATE :collate = :surname GROUP BY 1" 91 )->execute([ 92 'collate' => I18N::collation(), 93 'surname' => $top_surname, 94 'tree_id' => $tree->getTreeId(), 95 ])->fetchAssoc(); 96 97 $all_surnames[$top_surname] = $variants; 98 } 99 100 switch ($infoStyle) { 101 case 'tagcloud': 102 uksort($all_surnames, [I18N::class, 'strcasecmp']); 103 $content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'individual-list', true, $tree); 104 break; 105 case 'list': 106 $content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'individual-list', $tree); 107 break; 108 case 'array': 109 $content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'individual-list', $tree); 110 break; 111 case 'table': 112 default: 113 $content = view('lists/surnames-table', [ 114 'surnames' => $all_surnames, 115 'route' => 'individual-list', 116 'tree' => $tree, 117 ]); 118 break; 119 } 120 121 if ($template) { 122 $num = count($top_surnames); 123 if ($num === 1) { 124 // I18N: i.e. most popular surname. 125 $title = I18N::translate('Top surname'); 126 } else { 127 // I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1 128 $title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num)); 129 } 130 131 if ($ctype === 'gedcom' && Auth::isManager($tree)) { 132 $config_url = route('tree-page-block-edit', [ 133 'block_id' => $block_id, 134 'ged' => $tree->getName(), 135 ]); 136 } elseif ($ctype === 'user' && Auth::check()) { 137 $config_url = route('user-page-block-edit', [ 138 'block_id' => $block_id, 139 'ged' => $tree->getName(), 140 ]); 141 } else { 142 $config_url = ''; 143 } 144 145 return view('modules/block-template', [ 146 'block' => str_replace('_', '-', $this->getName()), 147 'id' => $block_id, 148 'config_url' => $config_url, 149 'title' => $title, 150 'content' => $content, 151 ]); 152 } else { 153 return $content; 154 } 155 } 156 157 /** {@inheritdoc} */ 158 public function loadAjax(): bool 159 { 160 return false; 161 } 162 163 /** {@inheritdoc} */ 164 public function isUserBlock(): bool 165 { 166 return true; 167 } 168 169 /** {@inheritdoc} */ 170 public function isGedcomBlock(): bool 171 { 172 return true; 173 } 174 175 /** 176 * An HTML form to edit block settings 177 * 178 * @param Tree $tree 179 * @param int $block_id 180 * 181 * @return void 182 */ 183 public function configureBlock(Tree $tree, int $block_id) 184 { 185 if ($_SERVER['REQUEST_METHOD'] === 'POST') { 186 $this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10)); 187 $this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', self::DEFAULT_STYLE)); 188 189 return; 190 } 191 192 $num = $this->getBlockSetting($block_id, 'num', self::DEFAULT_NUMBER); 193 $infoStyle = $this->getBlockSetting($block_id, 'infoStyle', self::DEFAULT_STYLE); 194 195 $info_styles = [ 196 'list' => /* I18N: An option in a list-box */ 197 I18N::translate('bullet list'), 198 'array' => /* I18N: An option in a list-box */ 199 I18N::translate('compact list'), 200 'table' => /* I18N: An option in a list-box */ 201 I18N::translate('table'), 202 'tagcloud' => /* I18N: An option in a list-box */ 203 I18N::translate('tag cloud'), 204 ]; 205 206 echo view('modules/top10_surnames/config', [ 207 'num' => $num, 208 'infoStyle' => $infoStyle, 209 'info_styles' => $info_styles, 210 ]); 211 } 212} 213