xref: /webtrees/app/Module/TopGivenNamesModule.php (revision a86dd8b13b4210d1576f0fdaf5e3c9e5653c4884)
13763c3f2SGreg Roach<?php
23763c3f2SGreg Roach/**
33763c3f2SGreg Roach * webtrees: online genealogy
4369c0ce6SGreg Roach * Copyright (C) 2016 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;
203d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsEdit;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Stats;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
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	 */
4876692c8bSGreg Roach	public function getBlock($block_id, $template = true, $cfg = array()) {
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');
53e2a378d3SGreg Roach		$block     = $this->getBlockSetting($block_id, 'block', '0');
543763c3f2SGreg Roach
553763c3f2SGreg Roach		foreach (array('num', 'infoStyle', 'block') as $name) {
563763c3f2SGreg Roach			if (array_key_exists($name, $cfg)) {
573763c3f2SGreg Roach				$$name = $cfg[$name];
583763c3f2SGreg Roach			}
593763c3f2SGreg Roach		}
603763c3f2SGreg Roach
613763c3f2SGreg Roach		$stats = new Stats($WT_TREE);
623763c3f2SGreg Roach
633763c3f2SGreg Roach		$id    = $this->getName() . $block_id;
643763c3f2SGreg Roach		$class = $this->getName() . '_block';
653763c3f2SGreg Roach		if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
669353052eSGreg Roach			$title = '<a class="icon-admin" title="' . I18N::translate('Configure') . '" href="block_edit.php?block_id=' . $block_id . '&amp;ged=' . $WT_TREE->getNameHtml() . '&amp;ctype=' . $ctype . '"></a>';
673763c3f2SGreg Roach		} else {
683763c3f2SGreg Roach			$title = '';
693763c3f2SGreg Roach		}
703763c3f2SGreg Roach		if ($num == 1) {
713763c3f2SGreg Roach			// I18N: i.e. most popular given name.
723763c3f2SGreg Roach			$title .= I18N::translate('Top given name');
733763c3f2SGreg Roach		} else {
743763c3f2SGreg 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
753763c3f2SGreg Roach			$title .= I18N::plural('Top %s given name', 'Top %s given names', $num, I18N::number($num));
763763c3f2SGreg Roach		}
773763c3f2SGreg Roach
783763c3f2SGreg Roach		$content = '<div class="normal_inner_block">';
793763c3f2SGreg Roach		//Select List or Table
803763c3f2SGreg Roach		switch ($infoStyle) {
813763c3f2SGreg Roach		case "list": // Output style 1:  Simple list style. Better suited to left side of page.
823763c3f2SGreg Roach			if (I18N::direction() === 'ltr') {
833763c3f2SGreg Roach				$padding = 'padding-left: 15px';
843763c3f2SGreg Roach			} else {
853763c3f2SGreg Roach				$padding = 'padding-right: 15px';
863763c3f2SGreg Roach			}
873763c3f2SGreg Roach			$params = array(1, $num, 'rcount');
883763c3f2SGreg Roach			// List Female names
893763c3f2SGreg Roach			$totals = $stats->commonGivenFemaleTotals($params);
903763c3f2SGreg Roach			if ($totals) {
913763c3f2SGreg Roach				$content .= '<b>' . I18N::translate('Females') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
923763c3f2SGreg Roach			}
933763c3f2SGreg Roach			// List Male names
943763c3f2SGreg Roach			$totals = $stats->commonGivenMaleTotals($params);
953763c3f2SGreg Roach			if ($totals) {
963763c3f2SGreg Roach				$content .= '<b>' . I18N::translate('Males') . '</b><div class="wrap" style="' . $padding . '">' . $totals . '</div><br>';
973763c3f2SGreg Roach			}
983763c3f2SGreg Roach			break;
993763c3f2SGreg Roach		case "table": // Style 2: Tabular format. Narrow, 2 or 3 column table, good on right side of page
1003763c3f2SGreg Roach			$params = array(1, $num, 'rcount');
1013763c3f2SGreg Roach			$content .= '<table style="margin:auto;">
102*a86dd8b1SGreg Roach						<tr>
1033763c3f2SGreg Roach						<td>' . $stats->commonGivenFemaleTable($params) . '</td>
1043763c3f2SGreg Roach						<td>' . $stats->commonGivenMaleTable($params) . '</td>';
1053763c3f2SGreg Roach			$content .= '</tr></table>';
1063763c3f2SGreg Roach			break;
1073763c3f2SGreg Roach		}
1083763c3f2SGreg Roach		$content .= "</div>";
1093763c3f2SGreg Roach
1103763c3f2SGreg Roach		if ($template) {
1113763c3f2SGreg Roach			if ($block) {
1123763c3f2SGreg Roach				$class .= ' small_inner_block';
1133763c3f2SGreg Roach			}
114cbc1590aSGreg Roach
1153763c3f2SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1163763c3f2SGreg Roach		} else {
1173763c3f2SGreg Roach			return $content;
1183763c3f2SGreg Roach		}
1193763c3f2SGreg Roach	}
1203763c3f2SGreg Roach
1213763c3f2SGreg Roach	/** {@inheritdoc} */
1223763c3f2SGreg Roach	public function loadAjax() {
1233763c3f2SGreg Roach		return true;
1243763c3f2SGreg Roach	}
1253763c3f2SGreg Roach
1263763c3f2SGreg Roach	/** {@inheritdoc} */
1273763c3f2SGreg Roach	public function isUserBlock() {
1283763c3f2SGreg Roach		return true;
1293763c3f2SGreg Roach	}
1303763c3f2SGreg Roach
1313763c3f2SGreg Roach	/** {@inheritdoc} */
1323763c3f2SGreg Roach	public function isGedcomBlock() {
1333763c3f2SGreg Roach		return true;
1343763c3f2SGreg Roach	}
1353763c3f2SGreg Roach
13676692c8bSGreg Roach	/**
13776692c8bSGreg Roach	 * An HTML form to edit block settings
13876692c8bSGreg Roach	 *
13976692c8bSGreg Roach	 * @param int $block_id
14076692c8bSGreg Roach	 */
1413763c3f2SGreg Roach	public function configureBlock($block_id) {
1423763c3f2SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
143e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
144e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|table', 'table'));
145e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
1463763c3f2SGreg Roach		}
1473763c3f2SGreg Roach
148e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
149e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
150e2a378d3SGreg Roach		$block     = $this->getBlockSetting($block_id, 'block', '0');
1513763c3f2SGreg Roach
1523763c3f2SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1533763c3f2SGreg Roach		echo I18N::translate('Number of items to show');
1543763c3f2SGreg Roach		echo '</td><td class="optionbox">';
1553763c3f2SGreg Roach		echo '<input type="text" name="num" size="2" value="', $num, '">';
1563763c3f2SGreg Roach		echo '</td></tr>';
1573763c3f2SGreg Roach
1583763c3f2SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1593763c3f2SGreg Roach		echo I18N::translate('Presentation style');
1603763c3f2SGreg Roach		echo '</td><td class="optionbox">';
1613d7a8a4cSGreg Roach		echo FunctionsEdit::selectEditControl('infoStyle', array('list' => I18N::translate('list'), 'table' => I18N::translate('table')), null, $infoStyle, '');
1623763c3f2SGreg Roach		echo '</td></tr>';
1633763c3f2SGreg Roach
1643763c3f2SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
1653763c3f2SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
1663763c3f2SGreg Roach		echo '</td><td class="optionbox">';
1673d7a8a4cSGreg Roach		echo FunctionsEdit::editFieldYesNo('block', $block);
1683763c3f2SGreg Roach		echo '</td></tr>';
1693763c3f2SGreg Roach	}
1703763c3f2SGreg Roach}
171