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