xref: /webtrees/app/Module/TopGivenNamesModule.php (revision 9c6524dcf4555157077b94309ec75cc0781acd78)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 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;
2115d603e7SGreg Roachuse Fisharebest\Webtrees\FontAwesome;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
25*9c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
263763c3f2SGreg Roach
273763c3f2SGreg Roach/**
283763c3f2SGreg Roach * Class TopGivenNamesModule
293763c3f2SGreg Roach */
30e2a378d3SGreg Roachclass TopGivenNamesModule extends AbstractModule implements ModuleBlockInterface {
313763c3f2SGreg Roach	/** {@inheritdoc} */
323763c3f2SGreg Roach	public function getTitle() {
333763c3f2SGreg Roach		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top given names');
343763c3f2SGreg Roach	}
353763c3f2SGreg Roach
363763c3f2SGreg Roach	/** {@inheritdoc} */
373763c3f2SGreg Roach	public function getDescription() {
383763c3f2SGreg Roach		return /* I18N: Description of the “Top given names” module */ I18N::translate('A list of the most popular given names.');
393763c3f2SGreg Roach	}
403763c3f2SGreg Roach
4176692c8bSGreg Roach	/**
4276692c8bSGreg Roach	 * Generate the HTML content of this block.
4376692c8bSGreg Roach	 *
4476692c8bSGreg Roach	 * @param int      $block_id
4576692c8bSGreg Roach	 * @param bool     $template
46727f238cSGreg Roach	 * @param string[] $cfg
4776692c8bSGreg Roach	 *
4876692c8bSGreg Roach	 * @return string
4976692c8bSGreg Roach	 */
5013abd6f3SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []) {
513763c3f2SGreg Roach		global $ctype, $WT_TREE;
523763c3f2SGreg Roach
53e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
54e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
553763c3f2SGreg Roach
5613abd6f3SGreg Roach		foreach (['num', 'infoStyle'] as $name) {
573763c3f2SGreg Roach			if (array_key_exists($name, $cfg)) {
583763c3f2SGreg Roach				$$name = $cfg[$name];
593763c3f2SGreg Roach			}
603763c3f2SGreg Roach		}
613763c3f2SGreg Roach
623763c3f2SGreg Roach		$stats = new Stats($WT_TREE);
633763c3f2SGreg Roach
643763c3f2SGreg Roach		$id    = $this->getName() . $block_id;
653763c3f2SGreg Roach		$class = $this->getName() . '_block';
663763c3f2SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
6715d603e7SGreg Roach			$title = FontAwesome::linkIcon('preferences', I18N::translate('Preferences'), ['class' => 'btn btn-link', 'href' => 'block_edit.php?block_id=' . $block_id . '&ged=' . $WT_TREE->getNameHtml() . '&ctype=' . $ctype]) . ' ';
683763c3f2SGreg Roach		} else {
693763c3f2SGreg Roach			$title = '';
703763c3f2SGreg Roach		}
713763c3f2SGreg Roach		if ($num == 1) {
723763c3f2SGreg Roach			// I18N: i.e. most popular given name.
733763c3f2SGreg Roach			$title .= I18N::translate('Top given name');
743763c3f2SGreg Roach		} else {
753763c3f2SGreg 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
763763c3f2SGreg Roach			$title .= I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
773763c3f2SGreg Roach		}
783763c3f2SGreg Roach
793763c3f2SGreg Roach		$content = '<div class="normal_inner_block">';
803763c3f2SGreg Roach		//Select List or Table
813763c3f2SGreg Roach		switch ($infoStyle) {
827a6ee1acSGreg Roach		case 'list': // Output style 1:  Simple list style. Better suited to left side of page.
833763c3f2SGreg Roach			if (I18N::direction() === 'ltr') {
843763c3f2SGreg Roach				$padding = 'padding-left: 15px';
853763c3f2SGreg Roach			} else {
863763c3f2SGreg Roach				$padding = 'padding-right: 15px';
873763c3f2SGreg Roach			}
8813abd6f3SGreg Roach			$params = [1, $num, 'rcount'];
893763c3f2SGreg Roach			// List Female names
903763c3f2SGreg Roach			$totals = $stats->commonGivenFemaleTotals($params);
913763c3f2SGreg Roach			if ($totals) {
923763c3f2SGreg Roach				$content .= '<b>' . I18N::translate('Females') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
933763c3f2SGreg Roach			}
943763c3f2SGreg Roach			// List Male names
953763c3f2SGreg Roach			$totals = $stats->commonGivenMaleTotals($params);
963763c3f2SGreg Roach			if ($totals) {
973763c3f2SGreg Roach				$content .= '<b>' . I18N::translate('Males') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
983763c3f2SGreg Roach			}
993763c3f2SGreg Roach			break;
1007a6ee1acSGreg Roach		case 'table': // Style 2: Tabular format. Narrow, 2 or 3 column table, good on right side of page
10113abd6f3SGreg Roach			$params = [1, $num, 'rcount'];
1023763c3f2SGreg Roach			$content .= '<table style="margin:auto;">
103a86dd8b1SGreg Roach						<tr>
1043763c3f2SGreg Roach						<td>' . $stats->commonGivenFemaleTable($params) . '</td>
1053763c3f2SGreg Roach						<td>' . $stats->commonGivenMaleTable($params) . '</td>';
1063763c3f2SGreg Roach			$content .= '</tr></table>';
1073763c3f2SGreg Roach			break;
1083763c3f2SGreg Roach		}
1097a6ee1acSGreg Roach		$content .= '</div>';
1103763c3f2SGreg Roach
1113763c3f2SGreg Roach		if ($template) {
112*9c6524dcSGreg Roach			return View::make('blocks/template', [
113*9c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
114*9c6524dcSGreg Roach				'id'         => $block_id,
115*9c6524dcSGreg Roach				'config_url' => '',
116*9c6524dcSGreg Roach				'title'      => $this->getTitle(),
117*9c6524dcSGreg Roach				'content'    => $content,
118*9c6524dcSGreg Roach			]);
1193763c3f2SGreg Roach		} else {
1203763c3f2SGreg Roach			return $content;
1213763c3f2SGreg Roach		}
1223763c3f2SGreg Roach	}
1233763c3f2SGreg Roach
1243763c3f2SGreg Roach	/** {@inheritdoc} */
1253763c3f2SGreg Roach	public function loadAjax() {
1264ecf4f82SGreg Roach		return false;
1273763c3f2SGreg Roach	}
1283763c3f2SGreg Roach
1293763c3f2SGreg Roach	/** {@inheritdoc} */
1303763c3f2SGreg Roach	public function isUserBlock() {
1313763c3f2SGreg Roach		return true;
1323763c3f2SGreg Roach	}
1333763c3f2SGreg Roach
1343763c3f2SGreg Roach	/** {@inheritdoc} */
1353763c3f2SGreg Roach	public function isGedcomBlock() {
1363763c3f2SGreg Roach		return true;
1373763c3f2SGreg Roach	}
1383763c3f2SGreg Roach
13976692c8bSGreg Roach	/**
14076692c8bSGreg Roach	 * An HTML form to edit block settings
14176692c8bSGreg Roach	 *
14276692c8bSGreg Roach	 * @param int $block_id
14376692c8bSGreg Roach	 */
1443763c3f2SGreg Roach	public function configureBlock($block_id) {
1453763c3f2SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
146e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
147e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
1483763c3f2SGreg Roach		}
1493763c3f2SGreg Roach
150e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
151e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1523763c3f2SGreg Roach
15315d603e7SGreg Roach		?>
15415d603e7SGreg Roach		<div class="form-group row">
15515d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="num">
15615d603e7SGreg Roach				<?= /* I18N: ... to show in a list */ I18N::translate('Number of given names') ?>
15715d603e7SGreg Roach			</label>
15815d603e7SGreg Roach			<div class="col-sm-9">
15915d603e7SGreg Roach				<input type="text" id="num" name="num" size="2" value="<?= $num ?>">
16015d603e7SGreg Roach			</div>
16115d603e7SGreg Roach		</div>
1623763c3f2SGreg Roach
16315d603e7SGreg Roach		<div class="form-group row">
16415d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
16515d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
16615d603e7SGreg Roach			</label>
16715d603e7SGreg Roach			<div class="col-sm-9">
16815d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('list'), 'table' => I18N::translate('table')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
16915d603e7SGreg Roach			</div>
17015d603e7SGreg Roach		</div>
17115d603e7SGreg Roach		<?php
1723763c3f2SGreg Roach	}
1733763c3f2SGreg Roach}
174