xref: /webtrees/app/Module/TopGivenNamesModule.php (revision c385536d61a9b9bed37314ecc08afa8429a269ef)
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;
223763c3f2SGreg Roach
233763c3f2SGreg Roach/**
243763c3f2SGreg Roach * Class TopGivenNamesModule
253763c3f2SGreg Roach */
26e2a378d3SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
273763c3f2SGreg Roach	/** {@inheritdoc} */
283763c3f2SGreg Roach	public function getTitle() {
293763c3f2SGreg Roach		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names');
303763c3f2SGreg Roach	}
313763c3f2SGreg Roach
323763c3f2SGreg Roach	/** {@inheritdoc} */
333763c3f2SGreg Roach	public function getDescription() {
343763c3f2SGreg Roach		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
353763c3f2SGreg Roach	}
363763c3f2SGreg Roach
3776692c8bSGreg Roach	/**
3876692c8bSGreg Roach	 * Generate the HTML content of this block.
3976692c8bSGreg Roach	 *
4076692c8bSGreg Roach	 * @param int      $block_id
4176692c8bSGreg Roach	 * @param bool     $template
42727f238cSGreg Roach	 * @param string[] $cfg
4376692c8bSGreg Roach	 *
4476692c8bSGreg Roach	 * @return string
4576692c8bSGreg Roach	 */
46a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
473763c3f2SGreg Roach		global $ctype, $WT_TREE;
483763c3f2SGreg Roach
49e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
50e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
513763c3f2SGreg Roach
52*c385536dSGreg Roach		extract($cfg, EXTR_OVERWRITE);
533763c3f2SGreg Roach
543763c3f2SGreg Roach		$stats = new Stats($WT_TREE);
553763c3f2SGreg Roach
563763c3f2SGreg Roach		switch ($infoStyle) {
5745831e7fSGreg Roach			case 'list':
5845831e7fSGreg Roach				$males   = $stats->commonGivenMaleTotals([1, $num, 'rcount']);
5945831e7fSGreg Roach				$females = $stats->commonGivenFemaleTotals([1, $num, 'rcount']);
60225e381fSGreg Roach				$content = view('blocks/top-given-names-list', [
6145831e7fSGreg Roach					'males'   => $males,
6245831e7fSGreg Roach					'females' => $females,
6345831e7fSGreg Roach				]);
643763c3f2SGreg Roach				break;
6545831e7fSGreg Roach			default:
6645831e7fSGreg Roach			case 'table':
6745831e7fSGreg Roach				$males   = $stats->commonGivenMaleTable([1, $num, 'rcount']);
6845831e7fSGreg Roach				$females = $stats->commonGivenFemaleTable([1, $num, 'rcount']);
69225e381fSGreg Roach				$content = view('blocks/top-given-names-table', [
7045831e7fSGreg Roach					'males'   => $males,
7145831e7fSGreg Roach					'females' => $females,
7245831e7fSGreg Roach				]);
733763c3f2SGreg Roach				break;
743763c3f2SGreg Roach		}
753763c3f2SGreg Roach
763763c3f2SGreg Roach		if ($template) {
778cbbfdceSGreg Roach			if ($num == 1) {
788cbbfdceSGreg Roach				// I18N: i.e. most popular given name.
798cbbfdceSGreg Roach				$title = I18N::translate('Top given name');
808cbbfdceSGreg Roach			} else {
818cbbfdceSGreg 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
828cbbfdceSGreg Roach				$title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
838cbbfdceSGreg Roach			}
848cbbfdceSGreg Roach
85397e599aSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE)) {
86397e599aSGreg Roach				$config_url = route('tree-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
87397e599aSGreg Roach			} elseif ($ctype === 'user' && Auth::check()) {
88397e599aSGreg Roach				$config_url = route('user-page-block-edit', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
898cbbfdceSGreg Roach			} else {
908cbbfdceSGreg Roach				$config_url = '';
918cbbfdceSGreg Roach			}
928cbbfdceSGreg Roach
93225e381fSGreg Roach			return view('blocks/template', [
949c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
959c6524dcSGreg Roach				'id'         => $block_id,
968cbbfdceSGreg Roach				'config_url' => $config_url,
978cbbfdceSGreg Roach				'title'      => $title,
989c6524dcSGreg Roach				'content'    => $content,
999c6524dcSGreg Roach			]);
1003763c3f2SGreg Roach		} else {
1013763c3f2SGreg Roach			return $content;
1023763c3f2SGreg Roach		}
1033763c3f2SGreg Roach	}
1043763c3f2SGreg Roach
1053763c3f2SGreg Roach	/** {@inheritdoc} */
106a9430be8SGreg Roach	public function loadAjax(): bool {
1074ecf4f82SGreg Roach		return false;
1083763c3f2SGreg Roach	}
1093763c3f2SGreg Roach
1103763c3f2SGreg Roach	/** {@inheritdoc} */
111a9430be8SGreg Roach	public function isUserBlock(): bool {
1123763c3f2SGreg Roach		return true;
1133763c3f2SGreg Roach	}
1143763c3f2SGreg Roach
1153763c3f2SGreg Roach	/** {@inheritdoc} */
116a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1173763c3f2SGreg Roach		return true;
1183763c3f2SGreg Roach	}
1193763c3f2SGreg Roach
12076692c8bSGreg Roach	/**
12176692c8bSGreg Roach	 * An HTML form to edit block settings
12276692c8bSGreg Roach	 *
12376692c8bSGreg Roach	 * @param int $block_id
124a9430be8SGreg Roach	 *
125a9430be8SGreg Roach	 * @return void
12676692c8bSGreg Roach	 */
127be9a728cSGreg Roach	public function configureBlock($block_id) {
128*c385536dSGreg Roach		if ($_SERVER['REQUEST_METHOD'] === 'POST') {
129e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
130e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
131*c385536dSGreg Roach
132*c385536dSGreg Roach			return;
1333763c3f2SGreg Roach		}
1343763c3f2SGreg Roach
135e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
136e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1373763c3f2SGreg Roach
138*c385536dSGreg Roach		$info_styles = [
139*c385536dSGreg Roach			'list'  => /* I18N: An option in a list-box */ I18N::translate('list'),
140*c385536dSGreg Roach			'table' => /* I18N: An option in a list-box */ I18N::translate('table'),
141*c385536dSGreg Roach		];
1423763c3f2SGreg Roach
143*c385536dSGreg Roach		echo view('blocks/top-given-names-config', [
144*c385536dSGreg Roach			'infoStyle'   => $infoStyle,
145*c385536dSGreg Roach			'info_styles' => $info_styles,
146*c385536dSGreg Roach			'num'         => $num,
147*c385536dSGreg Roach		]);
1483763c3f2SGreg Roach	}
1493763c3f2SGreg Roach}
150