xref: /webtrees/app/Module/FamilyNavigatorModule.php (revision 764a01d9433bb50d2ecbce7851cba4793c7674e2)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roach/**
208c2e8227SGreg Roach * Class FamilyNavigatorModule
218c2e8227SGreg Roach */
228c2e8227SGreg Roachclass FamilyNavigatorModule extends Module implements ModuleSidebarInterface {
238c2e8227SGreg Roach
248c2e8227SGreg Roach	const TTL = "<div class='flyout2'>%s</div>";
258c2e8227SGreg Roach	const LNK = "<div class='flyout3' data-href='%s'>%s</div>";
268c2e8227SGreg Roach	const MSG = "<div class='flyout4'>(%s)</div>"; // class flyout4 not used in standard themes
278c2e8227SGreg Roach
288c2e8227SGreg Roach	/** {@inheritdoc} */
298c2e8227SGreg Roach	public function getTitle() {
308c2e8227SGreg Roach		return /* I18N: Name of a module/sidebar */ I18N::translate('Family navigator');
318c2e8227SGreg Roach	}
328c2e8227SGreg Roach
338c2e8227SGreg Roach	/** {@inheritdoc} */
348c2e8227SGreg Roach	public function getDescription() {
358c2e8227SGreg Roach		return /* I18N: Description of the “Family navigator” module */ I18N::translate('A sidebar showing an individual’s close families and relatives.');
368c2e8227SGreg Roach	}
378c2e8227SGreg Roach
388c2e8227SGreg Roach	/** {@inheritdoc} */
398c2e8227SGreg Roach	public function defaultSidebarOrder() {
408c2e8227SGreg Roach		return 20;
418c2e8227SGreg Roach	}
428c2e8227SGreg Roach
438c2e8227SGreg Roach	/** {@inheritdoc} */
448c2e8227SGreg Roach	public function hasSidebarContent() {
458c2e8227SGreg Roach		return !Auth::isSearchEngine();
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
488c2e8227SGreg Roach	/** {@inheritdoc} */
498c2e8227SGreg Roach	public function getSidebarAjaxContent() {
508c2e8227SGreg Roach		return '';
518c2e8227SGreg Roach	}
528c2e8227SGreg Roach
538c2e8227SGreg Roach	/** {@inheritdoc} */
548c2e8227SGreg Roach	public function getSidebarContent() {
558c2e8227SGreg Roach		global $controller;
568c2e8227SGreg Roach
578c2e8227SGreg Roach		$controller->addInlineJavascript('
588c2e8227SGreg Roach			jQuery("#sb_family_nav_content")
598c2e8227SGreg Roach				.on("click", ".flyout a", function() {
608c2e8227SGreg Roach					return false;
618c2e8227SGreg Roach				})
628c2e8227SGreg Roach				.on("click", ".flyout3", function() {
638c2e8227SGreg Roach					window.location.href = jQuery(this).data("href");
648c2e8227SGreg Roach					return false;
658c2e8227SGreg Roach				});
668c2e8227SGreg Roach		');
678c2e8227SGreg Roach
688c2e8227SGreg Roach		ob_start();
698c2e8227SGreg Roach
708c2e8227SGreg Roach		?>
718c2e8227SGreg Roach		<div id="sb_family_nav_content">
728c2e8227SGreg Roach			<table class="nav_content">
738c2e8227SGreg Roach
748c2e8227SGreg Roach		<?php
758c2e8227SGreg Roach		//-- parent families -------------------------------------------------------------
768c2e8227SGreg Roach		foreach ($controller->record->getChildFamilies() as $family) {
778c2e8227SGreg Roach			$this->drawFamily($family, $controller->record->getChildFamilyLabel($family));
788c2e8227SGreg Roach		}
798c2e8227SGreg Roach		//-- step parents ----------------------------------------------------------------
808c2e8227SGreg Roach		foreach ($controller->record->getChildStepFamilies() as $family) {
818c2e8227SGreg Roach			$this->drawFamily($family, $controller->record->getStepFamilyLabel($family));
828c2e8227SGreg Roach		}
838c2e8227SGreg Roach		//-- spouse and children --------------------------------------------------
848c2e8227SGreg Roach		foreach ($controller->record->getSpouseFamilies() as $family) {
858c2e8227SGreg Roach			$this->drawFamily($family, $controller->record->getSpouseFamilyLabel($family));
868c2e8227SGreg Roach		}
878c2e8227SGreg Roach		//-- step children ----------------------------------------------------------------
888c2e8227SGreg Roach		foreach ($controller->record->getSpouseStepFamilies() as $family) {
898c2e8227SGreg Roach			$this->drawFamily($family, $family->getFullName());
908c2e8227SGreg Roach		}
918c2e8227SGreg Roach		?>
928c2e8227SGreg Roach			</table>
938c2e8227SGreg Roach		</div>
948c2e8227SGreg Roach		<?php
958c2e8227SGreg Roach
968c2e8227SGreg Roach		return ob_get_clean();
978c2e8227SGreg Roach	}
988c2e8227SGreg Roach
998c2e8227SGreg Roach	/**
1008c2e8227SGreg Roach	 * @param Family $family
1018c2e8227SGreg Roach	 * @param string $title
1028c2e8227SGreg Roach	 */
1038c2e8227SGreg Roach	private function drawFamily(Family $family, $title) {
1048c2e8227SGreg Roach		global $controller, $SHOW_PRIVATE_RELATIONSHIPS;
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach		?>
1078c2e8227SGreg Roach		<tr>
1088c2e8227SGreg Roach			<td class="center" colspan="2">
1098c2e8227SGreg Roach				<a class="famnav_title" href="<?php echo $family->getHtmlUrl(); ?>">
1108c2e8227SGreg Roach					<?php echo $title; ?>
1118c2e8227SGreg Roach				</a>
1128c2e8227SGreg Roach			</td>
1138c2e8227SGreg Roach		</tr>
1148c2e8227SGreg Roach		<?php
1158c2e8227SGreg Roach		$access_level = $SHOW_PRIVATE_RELATIONSHIPS ? WT_PRIV_HIDE : WT_USER_ACCESS_LEVEL;
1168c2e8227SGreg Roach		$facts = array_merge($family->getFacts('HUSB', false, $access_level), $family->getFacts('WIFE', false, $access_level));
1178c2e8227SGreg Roach		foreach ($facts as $fact) {
1188c2e8227SGreg Roach			$spouse = $fact->getTarget();
1198c2e8227SGreg Roach			if ($spouse instanceof Individual) {
1208c2e8227SGreg Roach				$menu = new Menu(get_close_relationship_name($controller->record, $spouse));
1218c2e8227SGreg Roach				$menu->addClass('', 'submenu flyout');
1228c2e8227SGreg Roach				$menu->addSubmenu(new Menu($this->getParents($spouse)));
1238c2e8227SGreg Roach				?>
1248c2e8227SGreg Roach				<tr>
1258c2e8227SGreg Roach					<td class="facts_label">
1268c2e8227SGreg Roach						<?php echo $menu->getMenu(); ?>
1278c2e8227SGreg Roach					</td>
1288c2e8227SGreg Roach					<td class="center <?php echo $controller->getPersonStyle($spouse); ?> nam">
1298c2e8227SGreg Roach						<a class="famnav_link" href="<?php echo $spouse->getHtmlUrl(); ?>">
1308c2e8227SGreg Roach							<?php echo $spouse->getFullName(); ?>
1318c2e8227SGreg Roach						</a>
1328c2e8227SGreg Roach						<div class="font9">
1338c2e8227SGreg Roach							<?php echo $spouse->getLifeSpan(); ?>
1348c2e8227SGreg Roach						</div>
1358c2e8227SGreg Roach					</td>
1368c2e8227SGreg Roach				</tr>
1378c2e8227SGreg Roach				<?php
1388c2e8227SGreg Roach			}
1398c2e8227SGreg Roach		}
1408c2e8227SGreg Roach
1418c2e8227SGreg Roach		foreach ($family->getFacts('CHIL', false, $access_level) as $fact) {
1428c2e8227SGreg Roach			$child = $fact->getTarget();
1438c2e8227SGreg Roach			if ($child instanceof Individual) {
1448c2e8227SGreg Roach				$menu = new Menu(get_close_relationship_name($controller->record, $child));
1458c2e8227SGreg Roach				$menu->addClass('', 'submenu flyout');
1468c2e8227SGreg Roach				$menu->addSubmenu(new Menu($this->getFamily($child)));
1478c2e8227SGreg Roach				?>
1488c2e8227SGreg Roach				<tr>
1498c2e8227SGreg Roach					<td class="facts_label">
1508c2e8227SGreg Roach						<?php echo $menu->getMenu(); ?>
1518c2e8227SGreg Roach					</td>
1528c2e8227SGreg Roach					<td class="center <?php echo $controller->getPersonStyle($child); ?> nam">
1538c2e8227SGreg Roach						<a class="famnav_link" href="<?php echo $child->getHtmlUrl(); ?>">
1548c2e8227SGreg Roach							<?php echo $child->getFullName(); ?>
1558c2e8227SGreg Roach						</a>
1568c2e8227SGreg Roach						<div class="font9">
1578c2e8227SGreg Roach							<?php echo $child->getLifeSpan(); ?>
1588c2e8227SGreg Roach						</div>
1598c2e8227SGreg Roach					</td>
1608c2e8227SGreg Roach				</tr>
1618c2e8227SGreg Roach				<?php
1628c2e8227SGreg Roach			}
1638c2e8227SGreg Roach		}
1648c2e8227SGreg Roach	}
1658c2e8227SGreg Roach
1668c2e8227SGreg Roach	/**
1678c2e8227SGreg Roach	 * @param         $person
1688c2e8227SGreg Roach	 * @param boolean $showUnknown
1698c2e8227SGreg Roach	 *
1708c2e8227SGreg Roach	 * @return string
1718c2e8227SGreg Roach	 */
1728c2e8227SGreg Roach	private function getHTML($person, $showUnknown = false) {
1738c2e8227SGreg Roach		if ($person instanceof Individual) {
1748c2e8227SGreg Roach			return sprintf(self::LNK, $person->getHtmlUrl(), $person->getFullName());
1758c2e8227SGreg Roach		} elseif ($showUnknown) {
1768c2e8227SGreg Roach			return sprintf(self::MSG, I18N::translate('unknown'));
1778c2e8227SGreg Roach		} else {
1788c2e8227SGreg Roach			return '';
1798c2e8227SGreg Roach		}
1808c2e8227SGreg Roach	}
1818c2e8227SGreg Roach
1828c2e8227SGreg Roach	/**
1838c2e8227SGreg Roach	 * @param Individual $person
1848c2e8227SGreg Roach	 *
1858c2e8227SGreg Roach	 * @return string
1868c2e8227SGreg Roach	 */
1878c2e8227SGreg Roach	private function getParents(Individual $person) {
1888c2e8227SGreg Roach		$father = null;
1898c2e8227SGreg Roach		$mother = null;
1908c2e8227SGreg Roach		$html = sprintf(self::TTL, I18N::translate('Parents'));
1918c2e8227SGreg Roach		$family = $person->getPrimaryChildFamily();
1928c2e8227SGreg Roach		if (!Auth::isSearchEngine() && $person->canShowName() && $family !== null) {
1938c2e8227SGreg Roach			$father = $family->getHusband();
1948c2e8227SGreg Roach			$mother = $family->getWife();
1958c2e8227SGreg Roach			$html .= $this->getHTML($father) .
1968c2e8227SGreg Roach					 $this->getHTML($mother);
1978c2e8227SGreg Roach
1988c2e8227SGreg Roach			// Can only have a step parent if one & only one parent found at this point
1998c2e8227SGreg Roach			if ($father instanceof Individual xor $mother instanceof Individual) {
2008c2e8227SGreg Roach				$stepParents = '';
2018c2e8227SGreg Roach				foreach ($person->getChildStepFamilies() as $family) {
2028c2e8227SGreg Roach					if (!$father instanceof Individual) {
2038c2e8227SGreg Roach						$stepParents .= $this->getHTML($family->getHusband());
2048c2e8227SGreg Roach					} else {
2058c2e8227SGreg Roach						$stepParents .= $this->getHTML($family->getWife());
2068c2e8227SGreg Roach					}
2078c2e8227SGreg Roach				}
2088c2e8227SGreg Roach				if ($stepParents) {
2098c2e8227SGreg Roach					$relationship = $father instanceof Individual ?
210*764a01d9SGreg Roach						I18N::translateContext("father’s wife", "step-mother") : I18N::translateContext("mother’s husband", "step-father");
2118c2e8227SGreg Roach					$html .= sprintf(self::TTL, $relationship) . $stepParents;
2128c2e8227SGreg Roach				}
2138c2e8227SGreg Roach			}
2148c2e8227SGreg Roach		}
2158c2e8227SGreg Roach		if (!($father instanceof Individual || $mother instanceof Individual)) {
216*764a01d9SGreg Roach			$html .= sprintf(self::MSG, I18N::translateContext('unknown family', 'unknown'));
2178c2e8227SGreg Roach		}
2188c2e8227SGreg Roach		return $html;
2198c2e8227SGreg Roach	}
2208c2e8227SGreg Roach
2218c2e8227SGreg Roach	/**
2228c2e8227SGreg Roach	 * @param Individual $person
2238c2e8227SGreg Roach	 *
2248c2e8227SGreg Roach	 * @return string
2258c2e8227SGreg Roach	 */
2268c2e8227SGreg Roach	private function getFamily(Individual $person) {
2278c2e8227SGreg Roach		$html = '';
2288c2e8227SGreg Roach		if ($person->canShowName() && !Auth::isSearchEngine()) {
2298c2e8227SGreg Roach			foreach ($person->getSpouseFamilies() as $family) {
2308c2e8227SGreg Roach				$spouse = $family->getSpouse($person);
2318c2e8227SGreg Roach				$html .= $this->getHTML($spouse, true);
2328c2e8227SGreg Roach				$children = $family->getChildren();
2338c2e8227SGreg Roach				if (count($children) > 0) {
2348c2e8227SGreg Roach					$html .= "<ul class='clist'>";
2358c2e8227SGreg Roach					foreach ($children as $child) {
2368c2e8227SGreg Roach						$html .= '<li>' . $this->getHTML($child) . '</li>';
2378c2e8227SGreg Roach					}
2388c2e8227SGreg Roach					$html .= '</ul>';
2398c2e8227SGreg Roach				}
2408c2e8227SGreg Roach			}
2418c2e8227SGreg Roach		}
2428c2e8227SGreg Roach		if (!$html) {
2438c2e8227SGreg Roach			$html = sprintf(self::MSG, I18N::translate('none'));
2448c2e8227SGreg Roach		}
2458c2e8227SGreg Roach		return sprintf(self::TTL, I18N::translate('Family')) . $html;
2468c2e8227SGreg Roach	}
2478c2e8227SGreg Roach
2488c2e8227SGreg Roach}
249