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