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