xref: /webtrees/app/Module/TopSurnamesModule.php (revision be9a728c03bbf624813113eeca03830a7825a5b1)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
46bdf7674SGreg Roach * Copyright (C) 2017 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1776692c8bSGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
1915d603e7SGreg Roachuse Fisharebest\Webtrees\Bootstrap4;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
213d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsDb;
223d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintLists;
238cbbfdceSGreg Roachuse Fisharebest\Webtrees\Html;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Query\QueryName;
269c6524dcSGreg Roachuse Fisharebest\Webtrees\View;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class TopSurnamesModule
308c2e8227SGreg Roach */
31e2a378d3SGreg Roachclass TopSurnamesModule extends AbstractModule implements ModuleBlockInterface {
3276692c8bSGreg Roach	/**
3376692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
3476692c8bSGreg Roach	 *
3576692c8bSGreg Roach	 * @return string
3676692c8bSGreg Roach	 */
378c2e8227SGreg Roach	public function getTitle() {
388c2e8227SGreg Roach		return /* I18N: Name of a module. Top=Most common */ I18N::translate('Top surnames');
398c2e8227SGreg Roach	}
408c2e8227SGreg Roach
4176692c8bSGreg Roach	/**
4276692c8bSGreg Roach	 * A sentence describing what this module does.
4376692c8bSGreg Roach	 *
4476692c8bSGreg Roach	 * @return string
4576692c8bSGreg Roach	 */
468c2e8227SGreg Roach	public function getDescription() {
478c2e8227SGreg Roach		return /* I18N: Description of the “Top surnames” module */ I18N::translate('A list of the most popular surnames.');
488c2e8227SGreg Roach	}
498c2e8227SGreg Roach
5076692c8bSGreg Roach	/**
5176692c8bSGreg Roach	 * Generate the HTML content of this block.
5276692c8bSGreg Roach	 *
5376692c8bSGreg Roach	 * @param int      $block_id
5476692c8bSGreg Roach	 * @param bool     $template
55727f238cSGreg Roach	 * @param string[] $cfg
5676692c8bSGreg Roach	 *
5776692c8bSGreg Roach	 * @return string
5876692c8bSGreg Roach	 */
59a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
608c2e8227SGreg Roach		global $WT_TREE, $ctype;
618c2e8227SGreg Roach
62e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
63e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
648c2e8227SGreg Roach
6513abd6f3SGreg Roach		foreach (['num', 'infoStyle'] as $name) {
668c2e8227SGreg Roach			if (array_key_exists($name, $cfg)) {
678c2e8227SGreg Roach				$$name = $cfg[$name];
688c2e8227SGreg Roach			}
698c2e8227SGreg Roach		}
708c2e8227SGreg Roach
718c2e8227SGreg Roach		// This next function is a bit out of date, and doesn't cope well with surname variants
72784e1b4bSGreg Roach		$top_surnames = FunctionsDb::getTopSurnames($WT_TREE->getTreeId(), 0, $num);
738c2e8227SGreg Roach
7413abd6f3SGreg Roach		$all_surnames = [];
758c2e8227SGreg Roach		$i            = 0;
768c2e8227SGreg Roach		foreach (array_keys($top_surnames) as $top_surname) {
77e06aff55SGreg Roach			$all_surnames = array_merge($all_surnames, QueryName::surnames($WT_TREE, $top_surname, '', false, false));
788c2e8227SGreg Roach			if (++$i == $num) {
798c2e8227SGreg Roach				break;
808c2e8227SGreg Roach			}
818c2e8227SGreg Roach		}
828c2e8227SGreg Roach		if ($i < $num) {
838c2e8227SGreg Roach			$num = $i;
848c2e8227SGreg Roach		}
858c2e8227SGreg Roach
868c2e8227SGreg Roach		switch ($infoStyle) {
878c2e8227SGreg Roach			case 'tagcloud':
880e62c4b8SGreg Roach				uksort($all_surnames, '\Fisharebest\Webtrees\I18N::strcasecmp');
893d7a8a4cSGreg Roach				$content = FunctionsPrintLists::surnameTagCloud($all_surnames, 'indilist.php', true, $WT_TREE);
908c2e8227SGreg Roach				break;
918c2e8227SGreg Roach			case 'list':
920e62c4b8SGreg Roach				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
93f36626dbSGreg Roach				$content = FunctionsPrintLists::surnameList($all_surnames, 1, true, 'indilist.php', $WT_TREE);
948c2e8227SGreg Roach				break;
958c2e8227SGreg Roach			case 'array':
960e62c4b8SGreg Roach				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
97f36626dbSGreg Roach				$content = FunctionsPrintLists::surnameList($all_surnames, 2, true, 'indilist.php', $WT_TREE);
988c2e8227SGreg Roach				break;
998c2e8227SGreg Roach			case 'table':
1008c2e8227SGreg Roach			default:
1010e62c4b8SGreg Roach				uasort($all_surnames, '\Fisharebest\Webtrees\Module\TopSurnamesModule::surnameCountSort');
1023d7a8a4cSGreg Roach				$content = FunctionsPrintLists::surnameTable($all_surnames, 'indilist.php', $WT_TREE);
1038c2e8227SGreg Roach				break;
1048c2e8227SGreg Roach		}
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach		if ($template) {
1078cbbfdceSGreg Roach			if ($num == 1) {
1088cbbfdceSGreg Roach				// I18N: i.e. most popular surname.
1098cbbfdceSGreg Roach				$title = I18N::translate('Top surname');
1108cbbfdceSGreg Roach			} else {
1118cbbfdceSGreg Roach				// I18N: Title for a list of the most common surnames, %s is a number. Note that a separate translation exists when %s is 1
1128cbbfdceSGreg Roach				$title = I18N::plural('Top %s surname', 'Top %s surnames', $num, I18N::number($num));
1138cbbfdceSGreg Roach			}
1148cbbfdceSGreg Roach
1158cbbfdceSGreg Roach			if ($ctype === 'gedcom' && Auth::isManager($WT_TREE) || $ctype === 'user' && Auth::check()) {
1168cbbfdceSGreg Roach				$config_url = Html::url('block_edit.php', ['block_id' => $block_id, 'ged' => $WT_TREE->getName()]);
1178cbbfdceSGreg Roach			} else {
1188cbbfdceSGreg Roach				$config_url = '';
1198cbbfdceSGreg Roach			}
1208cbbfdceSGreg Roach
1219c6524dcSGreg Roach			return View::make('blocks/template', [
1229c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
1239c6524dcSGreg Roach				'id'         => $block_id,
1248cbbfdceSGreg Roach				'config_url' => $config_url,
1258cbbfdceSGreg Roach				'title'      => $title,
1269c6524dcSGreg Roach				'content'    => $content,
1279c6524dcSGreg Roach			]);
1288c2e8227SGreg Roach		} else {
1298c2e8227SGreg Roach			return $content;
1308c2e8227SGreg Roach		}
1318c2e8227SGreg Roach	}
1328c2e8227SGreg Roach
1338c2e8227SGreg Roach	/** {@inheritdoc} */
134a9430be8SGreg Roach	public function loadAjax(): bool {
1354ecf4f82SGreg Roach		return false;
1368c2e8227SGreg Roach	}
1378c2e8227SGreg Roach
1388c2e8227SGreg Roach	/** {@inheritdoc} */
139a9430be8SGreg Roach	public function isUserBlock(): bool {
1408c2e8227SGreg Roach		return true;
1418c2e8227SGreg Roach	}
1428c2e8227SGreg Roach
1438c2e8227SGreg Roach	/** {@inheritdoc} */
144a9430be8SGreg Roach	public function isGedcomBlock(): bool {
1458c2e8227SGreg Roach		return true;
1468c2e8227SGreg Roach	}
1478c2e8227SGreg Roach
14876692c8bSGreg Roach	/**
14976692c8bSGreg Roach	 * An HTML form to edit block settings
15076692c8bSGreg Roach	 *
15176692c8bSGreg Roach	 * @param int $block_id
152a9430be8SGreg Roach	 *
153a9430be8SGreg Roach	 * @return void
15476692c8bSGreg Roach	 */
155*be9a728cSGreg Roach	public function configureBlock($block_id) {
1568c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
157e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'num', Filter::postInteger('num', 1, 10000, 10));
158e2a378d3SGreg Roach			$this->setBlockSetting($block_id, 'infoStyle', Filter::post('infoStyle', 'list|array|table|tagcloud', 'table'));
1598c2e8227SGreg Roach		}
1608c2e8227SGreg Roach
161e2a378d3SGreg Roach		$num       = $this->getBlockSetting($block_id, 'num', '10');
162e2a378d3SGreg Roach		$infoStyle = $this->getBlockSetting($block_id, 'infoStyle', 'table');
1638c2e8227SGreg Roach
16415d603e7SGreg Roach		?>
16515d603e7SGreg Roach		<div class="form-group row">
16615d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="num">
16715d603e7SGreg Roach				<?= /* I18N: ... to show in a list */ I18N::translate('Number of surnames') ?>
16815d603e7SGreg Roach			</label>
16915d603e7SGreg Roach			<div class="col-sm-9">
17015d603e7SGreg Roach				<input type="text" name="num" size="2" value="<?= $num ?>">
17115d603e7SGreg Roach			</div>
17215d603e7SGreg Roach		</div>
1738c2e8227SGreg Roach
17415d603e7SGreg Roach		<div class="form-group row">
17515d603e7SGreg Roach			<label class="col-sm-3 col-form-label" for="infoStyle">
17615d603e7SGreg Roach				<?= I18N::translate('Presentation style') ?>
17715d603e7SGreg Roach			</label>
17815d603e7SGreg Roach			<div class="col-sm-9">
17915d603e7SGreg Roach				<?= Bootstrap4::select(['list' => I18N::translate('bullet list'), 'array' => I18N::translate('compact list'), 'table' => I18N::translate('table'), 'tagcloud' => I18N::translate('tag cloud')], $infoStyle, ['id' => 'infoStyle', 'name' => 'infoStyle']) ?>
18015d603e7SGreg Roach			</div>
18115d603e7SGreg Roach		</div>
18215d603e7SGreg Roach		<?php
1838c2e8227SGreg Roach	}
1848c2e8227SGreg Roach
1858c2e8227SGreg Roach	/**
1868c2e8227SGreg Roach	 * Sort (lists of counts of similar) surname by total count.
1878c2e8227SGreg Roach	 *
188cc5ab399SGreg Roach	 * @param string[][] $a
189cc5ab399SGreg Roach	 * @param string[][] $b
1908c2e8227SGreg Roach	 *
191cbc1590aSGreg Roach	 * @return int
1928c2e8227SGreg Roach	 */
1938c2e8227SGreg Roach	private static function surnameCountSort($a, $b) {
1948c2e8227SGreg Roach		$counta = 0;
1958c2e8227SGreg Roach		foreach ($a as $x) {
1968c2e8227SGreg Roach			$counta += count($x);
1978c2e8227SGreg Roach		}
1988c2e8227SGreg Roach		$countb = 0;
1998c2e8227SGreg Roach		foreach ($b as $x) {
2008c2e8227SGreg Roach			$countb += count($x);
2018c2e8227SGreg Roach		}
2028c2e8227SGreg Roach
2038c2e8227SGreg Roach		return $countb - $counta;
2048c2e8227SGreg Roach	}
2058c2e8227SGreg Roach}
206