xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 225e381f36d59b49c8cdac0060465fa5af2fc308)
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;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
218cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
243763c3f2SGreg Roach
253763c3f2SGreg Roach/**
263763c3f2SGreg Roach * Class TopGivenNamesModule
273763c3f2SGreg Roach */
28e2a378d3SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
293763c3f2SGreg Roach	/** {@inheritdoc} */
303763c3f2SGreg Roach	public function getTitle() {
313763c3f2SGreg Roach		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names');
323763c3f2SGreg Roach	}
333763c3f2SGreg Roach
343763c3f2SGreg Roach	/** {@inheritdoc} */
353763c3f2SGreg Roach	public function getDescription() {
363763c3f2SGreg Roach		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
373763c3f2SGreg Roach	}
383763c3f2SGreg Roach
3976692c8bSGreg Roach	/**
4076692c8bSGreg Roach	 * Generate the HTML content of this block.
4176692c8bSGreg Roach	 *
4276692c8bSGreg Roach	 * @param int      $block_id
4376692c8bSGreg Roach	 * @param bool     $template
44727f238cSGreg Roach	 * @param string[] $cfg
4576692c8bSGreg Roach	 *
4676692c8bSGreg Roach	 * @return string
4776692c8bSGreg Roach	 */
48a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
493763c3f2SGreg Roach		global $ctype, $WT_TREE;
503763c3f2SGreg Roach
51e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
52e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
533763c3f2SGreg Roach
5413abd6f3SGreg Roach		foreach (['num', 'infoStyle'] as $name) {
553763c3f2SGreg Roach			if (array_key_exists($name, $cfg)) {
563763c3f2SGreg Roach				$$name = $cfg[$name];
573763c3f2SGreg Roach			}
583763c3f2SGreg Roach		}
593763c3f2SGreg Roach
603763c3f2SGreg Roach		$stats = new Stats($WT_TREE);
613763c3f2SGreg Roach
623763c3f2SGreg Roach		switch ($infoStyle) {
6345831e7fSGreg Roach			case 'list':
6445831e7fSGreg Roach				$males   = $stats->commonGivenMaleTotals([1, $num, 'rcount']);
6545831e7fSGreg Roach				$females = $stats->commonGivenFemaleTotals([1, $num, 'rcount']);
66*225e381fSGreg Roach				$content = view('blocks/top-given-names-list', [
6745831e7fSGreg Roach					'males'   => $males,
6845831e7fSGreg Roach					'females' => $females,
6945831e7fSGreg Roach				]);
703763c3f2SGreg Roach				break;
7145831e7fSGreg Roach			default:
7245831e7fSGreg Roach			case 'table':
7345831e7fSGreg Roach				$males   = $stats->commonGivenMaleTable([1, $num, 'rcount']);
7445831e7fSGreg Roach				$females = $stats->commonGivenFemaleTable([1, $num, 'rcount']);
75*225e381fSGreg Roach				$content = view('blocks/top-given-names-table', [
7645831e7fSGreg Roach					'males'   => $males,
7745831e7fSGreg Roach					'females' => $females,
7845831e7fSGreg Roach				]);
793763c3f2SGreg Roach				break;
803763c3f2SGreg Roach		}
813763c3f2SGreg Roach
823763c3f2SGreg Roach		if ($template) {
838cbbfdceSGreg Roach			if ($num == 1) {
848cbbfdceSGreg Roach				// I18N: i.e. most popular given name.
858cbbfdceSGreg Roach				$title = I18N::translate('Top given name');
868cbbfdceSGreg Roach			} else {
878cbbfdceSGreg 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
888cbbfdceSGreg Roach				$title = I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
898cbbfdceSGreg Roach			}
908cbbfdceSGreg Roach
918cbbfdceSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
928cbbfdceSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
938cbbfdceSGreg Roach			} else {
948cbbfdceSGreg Roach				$config_url = '';
958cbbfdceSGreg Roach			}
968cbbfdceSGreg Roach
97*225e381fSGreg Roach			return view('blocks/template', [
989c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
999c6524dcSGreg Roach				'id'         => $block_id,
1008cbbfdceSGreg Roach				'config_url' => $config_url,
1018cbbfdceSGreg Roach				'title'      => $title,
1029c6524dcSGreg Roach				'content'    => $content,
1039c6524dcSGreg Roach			]);
1043763c3f2SGreg Roach		} else {
1053763c3f2SGreg Roach			return $content;
1063763c3f2SGreg Roach		}
1073763c3f2SGreg Roach	}
1083763c3f2SGreg Roach
1093763c3f2SGreg Roach	/** {@inheritdoc} */
110a9430be8SGreg Roach	public function loadAjax(): bool {
1114ecf4f82SGreg Roach		return false;
1123763c3f2SGreg Roach	}
1133763c3f2SGreg Roach
1143763c3f2SGreg Roach	/** {@inheritdoc} */
115a9430be8SGreg Roach	public function isUserBlock(): bool {
1163763c3f2SGreg Roach		return true;
1173763c3f2SGreg Roach	}
1183763c3f2SGreg Roach
1193763c3f2SGreg Roach	/** {@inheritdoc} */
120a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1213763c3f2SGreg Roach		return true;
1223763c3f2SGreg Roach	}
1233763c3f2SGreg Roach
12476692c8bSGreg Roach	/**
12576692c8bSGreg Roach	 * An HTML form to edit block settings
12676692c8bSGreg Roach	 *
12776692c8bSGreg Roach	 * @param int $block_id
128a9430be8SGreg Roach	 *
129a9430be8SGreg Roach	 * @return void
13076692c8bSGreg Roach	 */
131be9a728cSGreg Roach	public function configureBlock($block_id) {
1323763c3f2SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
133e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
134e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
1353763c3f2SGreg Roach		}
1363763c3f2SGreg Roach
137e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
138e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1393763c3f2SGreg Roach
14015d603e7SGreg Roach		?>
14115d603e7SGreg Roach		<div class="form-group row">
14215d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="num">
14315d603e7SGreg Roach				<?= /* I18N: ... to show in a list */ I18N::translate('Number of given names') ?>
14415d603e7SGreg Roach			</label>
14515d603e7SGreg Roach			<div class="col-sm-9">
14615d603e7SGreg Roach				<input type="text" id="num" name="num" size="2" value="<?= $num ?>">
14715d603e7SGreg Roach			</div>
14815d603e7SGreg Roach		</div>
1493763c3f2SGreg Roach
15015d603e7SGreg Roach		<div class="form-group row">
15115d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
15215d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
15315d603e7SGreg Roach			</label>
15415d603e7SGreg Roach			<div class="col-sm-9">
15515d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
15615d603e7SGreg Roach			</div>
15715d603e7SGreg Roach		</div>
15815d603e7SGreg Roach		<?php
1593763c3f2SGreg Roach	}
1603763c3f2SGreg Roach}
161