xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 147e99aae08229316226c46b32d24d4f8bce04e0)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
53763c3f2SGreg Roach * This program is free software: you can redistribute it and/or modify
63763c3f2SGreg Roach * it under the terms of the GNU General Public License as published by
73763c3f2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83763c3f2SGreg Roach * (at your option) any later version.
93763c3f2SGreg Roach * This program is distributed in the hope that it will be useful,
103763c3f2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113763c3f2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123763c3f2SGreg Roach * GNU General Public License for more details.
133763c3f2SGreg Roach * You should have received a copy of the GNU General Public License
143763c3f2SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153763c3f2SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
22e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
233763c3f2SGreg Roach
243763c3f2SGreg Roach/**
253763c3f2SGreg Roach * Class TopGivenNamesModule
263763c3f2SGreg Roach */
27e2a378d3SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
283763c3f2SGreg Roach	/** {@inheritdoc} */
293763c3f2SGreg Roach	public function getTitle() {
303763c3f2SGreg Roach		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names');
313763c3f2SGreg Roach	}
323763c3f2SGreg Roach
333763c3f2SGreg Roach	/** {@inheritdoc} */
343763c3f2SGreg Roach	public function getDescription() {
353763c3f2SGreg Roach		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
363763c3f2SGreg Roach	}
373763c3f2SGreg Roach
3876692c8bSGreg Roach	/**
3976692c8bSGreg Roach	 * Generate the HTML content of this block.
4076692c8bSGreg Roach	 *
41e490cd80SGreg Roach	 * @param Tree     $tree
4276692c8bSGreg Roach	 * @param int      $block_id
4376692c8bSGreg Roach	 * @param bool     $template
44727f238cSGreg Roach	 * @param string[] $cfg
4576692c8bSGreg Roach	 *
4676692c8bSGreg Roach	 * @return string
4776692c8bSGreg Roach	 */
48e490cd80SGreg Roach	public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string {
49e490cd80SGreg Roach		global $ctype;
503763c3f2SGreg Roach
51e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
52e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
533763c3f2SGreg Roach
54c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
553763c3f2SGreg Roach
56e490cd80SGreg Roach		$stats = new Stats($tree);
573763c3f2SGreg Roach
583763c3f2SGreg Roach		switch ($infoStyle) {
5945831e7fSGreg Roach			case 'list':
6045831e7fSGreg Roach				$males   = $stats->commonGivenMaleTotals([1, $num, 'rcount']);
6145831e7fSGreg Roach				$females = $stats->commonGivenFemaleTotals([1, $num, 'rcount']);
62*147e99aaSGreg Roach				$content = view('modules/top10_givnnames/list', [
6345831e7fSGreg Roach					'males'   => $males,
6445831e7fSGreg Roach					'females' => $females,
6545831e7fSGreg Roach				]);
663763c3f2SGreg Roach				break;
6745831e7fSGreg Roach			default:
6845831e7fSGreg Roach			case 'table':
6945831e7fSGreg Roach				$males   = $stats->commonGivenMaleTable([1, $num, 'rcount']);
7045831e7fSGreg Roach				$females = $stats->commonGivenFemaleTable([1, $num, 'rcount']);
71*147e99aaSGreg Roach				$content = view('modules/top10_givnnames/table', [
7245831e7fSGreg Roach					'males'   => $males,
7345831e7fSGreg Roach					'females' => $females,
7445831e7fSGreg Roach				]);
753763c3f2SGreg Roach				break;
763763c3f2SGreg Roach		}
773763c3f2SGreg Roach
783763c3f2SGreg Roach		if ($template) {
798cbbfdceSGreg Roach			if ($num == 1) {
808cbbfdceSGreg Roach				// I18N: i.e. most popular given name.
818cbbfdceSGreg Roach				$title = I18N::translate('Top given name');
828cbbfdceSGreg Roach			} else {
838cbbfdceSGreg Roach				// I18N: Title for a list of the most common given names, %s is a number. Note that a separate translation exists when %s is 1
848cbbfdceSGreg Roach				$title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
858cbbfdceSGreg Roach			}
868cbbfdceSGreg Roach
87e490cd80SGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($tree)) {
88e490cd80SGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
89397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
90e490cd80SGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $tree->getName()]);
918cbbfdceSGreg Roach			} else {
928cbbfdceSGreg Roach				$config_url = '';
938cbbfdceSGreg Roach			}
948cbbfdceSGreg Roach
95*147e99aaSGreg Roach			return view('modules/block-template', [
969c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
979c6524dcSGreg Roach				'id'         => $block_id,
988cbbfdceSGreg Roach				'config_url' => $config_url,
998cbbfdceSGreg Roach				'title'      => $title,
1009c6524dcSGreg Roach				'content'    => $content,
1019c6524dcSGreg Roach			]);
1023763c3f2SGreg Roach		} else {
1033763c3f2SGreg Roach			return $content;
1043763c3f2SGreg Roach		}
1053763c3f2SGreg Roach	}
1063763c3f2SGreg Roach
1073763c3f2SGreg Roach	/** {@inheritdoc} */
108a9430be8SGreg Roach	public function loadAjax(): bool {
1094ecf4f82SGreg Roach		return false;
1103763c3f2SGreg Roach	}
1113763c3f2SGreg Roach
1123763c3f2SGreg Roach	/** {@inheritdoc} */
113a9430be8SGreg Roach	public function isUserBlock(): bool {
1143763c3f2SGreg Roach		return true;
1153763c3f2SGreg Roach	}
1163763c3f2SGreg Roach
1173763c3f2SGreg Roach	/** {@inheritdoc} */
118a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1193763c3f2SGreg Roach		return true;
1203763c3f2SGreg Roach	}
1213763c3f2SGreg Roach
12276692c8bSGreg Roach	/**
12376692c8bSGreg Roach	 * An HTML form to edit block settings
12476692c8bSGreg Roach	 *
125e490cd80SGreg Roach	 * @param Tree $tree
12676692c8bSGreg Roach	 * @param int  $block_id
127a9430be8SGreg Roach	 *
128a9430be8SGreg Roach	 * @return void
12976692c8bSGreg Roach	 */
130e490cd80SGreg Roach	public function configureBlock(Tree $tree, int $block_id) {
131c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
132e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
133e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
134c385536dSGreg Roach
135c385536dSGreg Roach			return;
1363763c3f2SGreg Roach		}
1373763c3f2SGreg Roach
138e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
139e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1403763c3f2SGreg Roach
141c385536dSGreg Roach		$info_styles = [
142c385536dSGreg Roach			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
143c385536dSGreg Roach			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
144c385536dSGreg Roach		];
1453763c3f2SGreg Roach
146*147e99aaSGreg Roach		echo view('modules/top10_givnnames/config', [
147c385536dSGreg Roach			'infoStyle'   => $infoStyle,
148c385536dSGreg Roach			'info_styles' => $info_styles,
149c385536dSGreg Roach			'num'         => $num,
150c385536dSGreg Roach		]);
1513763c3f2SGreg Roach	}
1523763c3f2SGreg Roach}
153