xref: /webtrees/app/Module/FamilyNavigatorModule.php (revision 1062a1429914c995339f502856821457aa975a5a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*1062a142SGreg Roach * Copyright (C) 2018 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\Family;
193d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\Functions;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu;
238c2e8227SGreg Roach
248c2e8227SGreg Roach/**
258c2e8227SGreg Roach * Class FamilyNavigatorModule
268c2e8227SGreg Roach */
27e2a378d3SGreg Roachclass FamilyNavigatorModule extends AbstractModule implements ModuleSidebarInterface {
288c2e8227SGreg Roach	const TTL = "<div class='flyout2'>%s</div>";
298c2e8227SGreg Roach	const LNK = "<div class='flyout3' data-href='%s'>%s</div>";
308c2e8227SGreg Roach	const MSG = "<div class='flyout4'>(%s)</div>"; // class flyout4 not used in standard themes
318c2e8227SGreg Roach
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getTitle() {
348c2e8227SGreg Roach		return /* I18N: Name of a module/sidebar */ I18N::translate('Family navigator');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function getDescription() {
398c2e8227SGreg Roach		return /* I18N: Description of the “Family navigator” module */ I18N::translate('A sidebar showing an individual’s close families and relatives.');
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/** {@inheritdoc} */
438c2e8227SGreg Roach	public function defaultSidebarOrder() {
448c2e8227SGreg Roach		return 20;
458c2e8227SGreg Roach	}
468c2e8227SGreg Roach
478c2e8227SGreg Roach	/** {@inheritdoc} */
488c2e8227SGreg Roach	public function hasSidebarContent() {
4938a9583bSGreg Roach		return true;
508c2e8227SGreg Roach	}
518c2e8227SGreg Roach
528c2e8227SGreg Roach	/** {@inheritdoc} */
538c2e8227SGreg Roach	public function getSidebarAjaxContent() {
548c2e8227SGreg Roach		return '';
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
5776692c8bSGreg Roach	/**
5876692c8bSGreg Roach	 * Load this sidebar synchronously.
5976692c8bSGreg Roach	 *
6076692c8bSGreg Roach	 * @return string
6176692c8bSGreg Roach	 */
628c2e8227SGreg Roach	public function getSidebarContent() {
638c2e8227SGreg Roach		global $controller;
648c2e8227SGreg Roach
658c2e8227SGreg Roach		$controller->addInlineJavascript('
66564ae2d7SGreg Roach			$("#sb_family_nav_content")
678c2e8227SGreg Roach				.on("click", ".flyout a", function() {
688c2e8227SGreg Roach					return false;
698c2e8227SGreg Roach				})
708c2e8227SGreg Roach				.on("click", ".flyout3", function() {
71564ae2d7SGreg Roach					window.location.href = $(this).data("href");
728c2e8227SGreg Roach					return false;
738c2e8227SGreg Roach				});
748c2e8227SGreg Roach		');
758c2e8227SGreg Roach
768c2e8227SGreg Roach		ob_start();
778c2e8227SGreg Roach
788c2e8227SGreg Roach		?>
798c2e8227SGreg Roach		<div id="sb_family_nav_content">
80e0486a06SGreg Roach			<div class="nav_content">
818c2e8227SGreg Roach
828c2e8227SGreg Roach		<?php
838c2e8227SGreg Roach		//-- parent families -------------------------------------------------------------
848c2e8227SGreg Roach		foreach ($controller->record->getChildFamilies() as $family) {
858c2e8227SGreg Roach			$this->drawFamily($family, $controller->record->getChildFamilyLabel($family));
868c2e8227SGreg Roach		}
878c2e8227SGreg Roach		//-- step parents ----------------------------------------------------------------
888c2e8227SGreg Roach		foreach ($controller->record->getChildStepFamilies() as $family) {
898c2e8227SGreg Roach			$this->drawFamily($family, $controller->record->getStepFamilyLabel($family));
908c2e8227SGreg Roach		}
918c2e8227SGreg Roach		//-- spouse and children --------------------------------------------------
928c2e8227SGreg Roach		foreach ($controller->record->getSpouseFamilies() as $family) {
93268bcb45SGreg Roach			$this->drawFamily($family, $controller->getSpouseFamilyLabel($family, $controller->record));
948c2e8227SGreg Roach		}
958c2e8227SGreg Roach		//-- step children ----------------------------------------------------------------
968c2e8227SGreg Roach		foreach ($controller->record->getSpouseStepFamilies() as $family) {
978c2e8227SGreg Roach			$this->drawFamily($family, $family->getFullName());
988c2e8227SGreg Roach		}
998c2e8227SGreg Roach		?>
100e0486a06SGreg Roach			</div>
1018c2e8227SGreg Roach		</div>
1028c2e8227SGreg Roach		<?php
1038c2e8227SGreg Roach
1048c2e8227SGreg Roach		return ob_get_clean();
1058c2e8227SGreg Roach	}
1068c2e8227SGreg Roach
1078c2e8227SGreg Roach	/**
10876692c8bSGreg Roach	 * Format a family.
10976692c8bSGreg Roach	 *
1108c2e8227SGreg Roach	 * @param Family $family
1118c2e8227SGreg Roach	 * @param string $title
1128c2e8227SGreg Roach	 */
1138c2e8227SGreg Roach	private function drawFamily(Family $family, $title) {
1143bb191c1SGreg Roach		global $controller;
1158c2e8227SGreg Roach
1168c2e8227SGreg Roach		?>
117e0486a06SGreg Roach		<table class="table table-sm wt-facts-table">
118e0486a06SGreg Roach		<caption class="text-center">
119b1f1e4efSGreg Roach			<a class="famnav_title" href="<?= e($family->url()) ?>">
120564ae2d7SGreg Roach				<?= $title ?>
1218c2e8227SGreg Roach			</a>
122e0486a06SGreg Roach		</caption>
123e0486a06SGreg Roach		<tbody>
1248c2e8227SGreg Roach			<?php
1253bb191c1SGreg Roach				foreach ($family->getSpouses() as $spouse) {
12689713119SGreg Roach					$icon = $controller->record === $spouse ? '<i class="icon-selected"></i>' : '';
12789713119SGreg Roach					$menu = new Menu($icon . Functions::getCloseRelationshipName($controller->record, $spouse));
1288c2e8227SGreg Roach					$menu->addSubmenu(new Menu($this->getParents($spouse)));
1298c2e8227SGreg Roach					?>
130e0486a06SGreg Roach					<tr class="text-center wt-parent wt-gender-<?= $spouse->getSex() ?>">
131e0486a06SGreg Roach						<th scope="row">
13215d603e7SGreg Roach							<ul class="nav">
13315d603e7SGreg Roach								<?= $menu->bootstrap4() ?>
13415d603e7SGreg Roach							</ul>
135e0486a06SGreg Roach						</th>
136e0486a06SGreg Roach						<td>
137a5bc968eSGreg Roach							<?php if ($spouse->canShow()): ?>
138b1f1e4efSGreg Roach							<a class="famnav_link" href="<?= e($spouse->url()) ?>">
139564ae2d7SGreg Roach								<?= $spouse->getFullName() ?>
1408c2e8227SGreg Roach							</a>
141e0486a06SGreg Roach							<div class="small">
142564ae2d7SGreg Roach								<?= $spouse->getLifeSpan() ?>
1438c2e8227SGreg Roach							</div>
144a5bc968eSGreg Roach							<?php else: ?>
145564ae2d7SGreg Roach								<?= $spouse->getFullName() ?>
146564ae2d7SGreg Roach							<?php endif ?>
1478c2e8227SGreg Roach						</td>
1488c2e8227SGreg Roach					</tr>
1498c2e8227SGreg Roach				<?php
1508c2e8227SGreg Roach				}
1518c2e8227SGreg Roach
1523bb191c1SGreg Roach				foreach ($family->getChildren() as $child) {
15389713119SGreg Roach					$icon = $controller->record === $child ? '<i class="icon-selected"></i>' : '';
15489713119SGreg Roach					$menu = new Menu($icon . Functions::getCloseRelationshipName($controller->record, $child));
1558c2e8227SGreg Roach					$menu->addSubmenu(new Menu($this->getFamily($child)));
1568c2e8227SGreg Roach					?>
157e0486a06SGreg Roach					<tr class="text-center wt-child wt-gender-<?= $child->getSex() ?>">
158e0486a06SGreg Roach						<th scope="row">
15915d603e7SGreg Roach							<ul class="nav">
16015d603e7SGreg Roach								<?= $menu->bootstrap4() ?>
16115d603e7SGreg Roach							</ul>
162e0486a06SGreg Roach						</th>
163e0486a06SGreg Roach						<td>
164a5bc968eSGreg Roach							<?php if ($child->canShow()): ?>
165b1f1e4efSGreg Roach							<a class="famnav_link" href="<?= e($child->url()) ?>">
166564ae2d7SGreg Roach								<?= $child->getFullName() ?>
1678c2e8227SGreg Roach							</a>
168e0486a06SGreg Roach							<div class="small">
169564ae2d7SGreg Roach								<?= $child->getLifeSpan() ?>
1708c2e8227SGreg Roach							</div>
171a5bc968eSGreg Roach							<?php else: ?>
172564ae2d7SGreg Roach								<?= $child->getFullName() ?>
173564ae2d7SGreg Roach							<?php endif ?>
1748c2e8227SGreg Roach						</td>
1758c2e8227SGreg Roach					</tr>
1768c2e8227SGreg Roach				<?php
1778c2e8227SGreg Roach				}
178e0486a06SGreg Roach				?>
179e0486a06SGreg Roach			</tbody>
180e0486a06SGreg Roach		</table>
181e0486a06SGreg Roach		<?php
1828c2e8227SGreg Roach	}
1838c2e8227SGreg Roach
1848c2e8227SGreg Roach	/**
18576692c8bSGreg Roach	 * Format an individual.
18676692c8bSGreg Roach	 *
1878c2e8227SGreg Roach	 * @param      $person
188cbc1590aSGreg Roach	 * @param bool $showUnknown
1898c2e8227SGreg Roach	 *
1908c2e8227SGreg Roach	 * @return string
1918c2e8227SGreg Roach	 */
1928c2e8227SGreg Roach	private function getHTML($person, $showUnknown = false) {
1938c2e8227SGreg Roach		if ($person instanceof Individual) {
194b1f1e4efSGreg Roach			return sprintf(self::LNK, e($person->url()), $person->getFullName());
1958c2e8227SGreg Roach		} elseif ($showUnknown) {
1968c2e8227SGreg Roach			return sprintf(self::MSG, I18N::translate('unknown'));
1978c2e8227SGreg Roach		} else {
1988c2e8227SGreg Roach			return '';
1998c2e8227SGreg Roach		}
2008c2e8227SGreg Roach	}
2018c2e8227SGreg Roach
2028c2e8227SGreg Roach	/**
20376692c8bSGreg Roach	 * Forat the parents of an individual.
20476692c8bSGreg Roach	 *
2058c2e8227SGreg Roach	 * @param Individual $person
2068c2e8227SGreg Roach	 *
2078c2e8227SGreg Roach	 * @return string
2088c2e8227SGreg Roach	 */
2098c2e8227SGreg Roach	private function getParents(Individual $person) {
2108c2e8227SGreg Roach		$father = null;
2118c2e8227SGreg Roach		$mother = null;
2128c2e8227SGreg Roach		$html   = sprintf(self::TTL, I18N::translate('Parents'));
2138c2e8227SGreg Roach		$family = $person->getPrimaryChildFamily();
21438a9583bSGreg Roach		if ($person->canShowName() && $family !== null) {
2158c2e8227SGreg Roach			$father = $family->getHusband();
2168c2e8227SGreg Roach			$mother = $family->getWife();
2178c2e8227SGreg Roach			$html .= $this->getHTML($father) .
2188c2e8227SGreg Roach					 $this->getHTML($mother);
2198c2e8227SGreg Roach
2208c2e8227SGreg Roach			// Can only have a step parent if one & only one parent found at this point
2218c2e8227SGreg Roach			if ($father instanceof Individual xor $mother instanceof Individual) {
2228c2e8227SGreg Roach				$stepParents = '';
2238c2e8227SGreg Roach				foreach ($person->getChildStepFamilies() as $family) {
2248c2e8227SGreg Roach					if (!$father instanceof Individual) {
2258c2e8227SGreg Roach						$stepParents .= $this->getHTML($family->getHusband());
2268c2e8227SGreg Roach					} else {
2278c2e8227SGreg Roach						$stepParents .= $this->getHTML($family->getWife());
2288c2e8227SGreg Roach					}
2298c2e8227SGreg Roach				}
2308c2e8227SGreg Roach				if ($stepParents) {
2318c2e8227SGreg Roach					$relationship = $father instanceof Individual ?
2327a6ee1acSGreg Roach						I18N::translateContext('father’s wife', 'step-mother') : I18N::translateContext('mother’s husband', 'step-father');
2338c2e8227SGreg Roach					$html .= sprintf(self::TTL, $relationship) . $stepParents;
2348c2e8227SGreg Roach				}
2358c2e8227SGreg Roach			}
2368c2e8227SGreg Roach		}
2378c2e8227SGreg Roach		if (!($father instanceof Individual || $mother instanceof Individual)) {
238764a01d9SGreg Roach			$html .= sprintf(self::MSG, I18N::translateContext('unknown family', 'unknown'));
2398c2e8227SGreg Roach		}
240cbc1590aSGreg Roach
2418c2e8227SGreg Roach		return $html;
2428c2e8227SGreg Roach	}
2438c2e8227SGreg Roach
2448c2e8227SGreg Roach	/**
24576692c8bSGreg Roach	 * Format a family.
24676692c8bSGreg Roach	 *
2478c2e8227SGreg Roach	 * @param Individual $person
2488c2e8227SGreg Roach	 *
2498c2e8227SGreg Roach	 * @return string
2508c2e8227SGreg Roach	 */
2518c2e8227SGreg Roach	private function getFamily(Individual $person) {
2528c2e8227SGreg Roach		$html = '';
25338a9583bSGreg Roach		if ($person->canShowName()) {
2548c2e8227SGreg Roach			foreach ($person->getSpouseFamilies() as $family) {
2558c2e8227SGreg Roach				$spouse = $family->getSpouse($person);
2568c2e8227SGreg Roach				$html .= $this->getHTML($spouse, true);
2578c2e8227SGreg Roach				$children = $family->getChildren();
2588c2e8227SGreg Roach				if (count($children) > 0) {
2598c2e8227SGreg Roach					$html .= "<ul class='clist'>";
2608c2e8227SGreg Roach					foreach ($children as $child) {
2618c2e8227SGreg Roach						$html .= '<li>' . $this->getHTML($child) . '</li>';
2628c2e8227SGreg Roach					}
2638c2e8227SGreg Roach					$html .= '</ul>';
2648c2e8227SGreg Roach				}
2658c2e8227SGreg Roach			}
2668c2e8227SGreg Roach		}
2678c2e8227SGreg Roach		if (!$html) {
2688c2e8227SGreg Roach			$html = sprintf(self::MSG, I18N::translate('none'));
2698c2e8227SGreg Roach		}
270cbc1590aSGreg Roach
2718c2e8227SGreg Roach		return sprintf(self::TTL, I18N::translate('Family')) . $html;
2728c2e8227SGreg Roach	}
2738c2e8227SGreg Roach}
274