xref: /webtrees/app/Module/FamilyNavigatorModule.php (revision 6bdf767435631ad1dc27ec1ffd855d43dbdce907)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\Module;
17
18use Fisharebest\Webtrees\Family;
19use Fisharebest\Webtrees\Functions\Functions;
20use Fisharebest\Webtrees\I18N;
21use Fisharebest\Webtrees\Individual;
22use Fisharebest\Webtrees\Menu;
23
24/**
25 * Class FamilyNavigatorModule
26 */
27class FamilyNavigatorModule extends AbstractModule implements ModuleSidebarInterface {
28	const TTL = "<div class='flyout2'>%s</div>";
29	const LNK = "<div class='flyout3' data-href='%s'>%s</div>";
30	const MSG = "<div class='flyout4'>(%s)</div>"; // class flyout4 not used in standard themes
31
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module/sidebar */ I18N::translate('Family navigator');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “Family navigator” module */ I18N::translate('A sidebar showing an individual’s close families and relatives.');
40	}
41
42	/** {@inheritdoc} */
43	public function defaultSidebarOrder() {
44		return 20;
45	}
46
47	/** {@inheritdoc} */
48	public function hasSidebarContent() {
49		return true;
50	}
51
52	/** {@inheritdoc} */
53	public function getSidebarAjaxContent() {
54		return '';
55	}
56
57	/**
58	 * Load this sidebar synchronously.
59	 *
60	 * @return string
61	 */
62	public function getSidebarContent() {
63		global $controller;
64
65		$controller->addInlineJavascript('
66			$("#sb_family_nav_content")
67				.on("click", ".flyout a", function() {
68					return false;
69				})
70				.on("click", ".flyout3", function() {
71					window.location.href = $(this).data("href");
72					return false;
73				});
74		');
75
76		ob_start();
77
78		?>
79		<div id="sb_family_nav_content">
80			<table class="nav_content">
81
82		<?php
83		//-- parent families -------------------------------------------------------------
84		foreach ($controller->record->getChildFamilies() as $family) {
85			$this->drawFamily($family, $controller->record->getChildFamilyLabel($family));
86		}
87		//-- step parents ----------------------------------------------------------------
88		foreach ($controller->record->getChildStepFamilies() as $family) {
89			$this->drawFamily($family, $controller->record->getStepFamilyLabel($family));
90		}
91		//-- spouse and children --------------------------------------------------
92		foreach ($controller->record->getSpouseFamilies() as $family) {
93			$this->drawFamily($family, $controller->getSpouseFamilyLabel($family, $controller->record));
94		}
95		//-- step children ----------------------------------------------------------------
96		foreach ($controller->record->getSpouseStepFamilies() as $family) {
97			$this->drawFamily($family, $family->getFullName());
98		}
99		?>
100			</table>
101		</div>
102		<?php
103
104		return ob_get_clean();
105	}
106
107	/**
108	 * Format a family.
109	 *
110	 * @param Family $family
111	 * @param string $title
112	 */
113	private function drawFamily(Family $family, $title) {
114		global $controller;
115
116		?>
117		<tr>
118			<td class="center" colspan="2">
119				<a class="famnav_title" href="<?= $family->getHtmlUrl() ?>">
120					<?= $title ?>
121				</a>
122			</td>
123		</tr>
124		<?php
125		foreach ($family->getSpouses() as $spouse) {
126			$menu = new Menu(Functions::getCloseRelationshipName($controller->record, $spouse));
127			$menu->addClass('', 'submenu flyout');
128			$menu->addSubmenu(new Menu($this->getParents($spouse)));
129			?>
130			<tr>
131				<td class="facts_label">
132					<?= $menu->getMenu() ?>
133				</td>
134				<td class="center <?= $controller->getPersonStyle($spouse) ?> nam">
135					<?php if ($spouse->canShow()): ?>
136					<a class="famnav_link" href="<?= $spouse->getHtmlUrl() ?>">
137						<?= $spouse->getFullName() ?>
138					</a>
139					<div class="font9">
140						<?= $spouse->getLifeSpan() ?>
141					</div>
142					<?php else: ?>
143						<?= $spouse->getFullName() ?>
144					<?php endif ?>
145				</td>
146			</tr>
147		<?php
148		}
149
150		foreach ($family->getChildren() as $child) {
151			$menu = new Menu(Functions::getCloseRelationshipName($controller->record, $child));
152			$menu->addClass('', 'submenu flyout');
153			$menu->addSubmenu(new Menu($this->getFamily($child)));
154			?>
155			<tr>
156				<td class="facts_label">
157					<?= $menu->getMenu() ?>
158				</td>
159				<td class="center <?= $controller->getPersonStyle($child) ?> nam">
160					<?php if ($child->canShow()): ?>
161					<a class="famnav_link" href="<?= $child->getHtmlUrl() ?>">
162						<?= $child->getFullName() ?>
163					</a>
164					<div class="font9">
165						<?= $child->getLifeSpan() ?>
166					</div>
167					<?php else: ?>
168						<?= $child->getFullName() ?>
169					<?php endif ?>
170				</td>
171			</tr>
172		<?php
173		}
174	}
175
176	/**
177	 * Format an individual.
178	 *
179	 * @param      $person
180	 * @param bool $showUnknown
181	 *
182	 * @return string
183	 */
184	private function getHTML($person, $showUnknown = false) {
185		if ($person instanceof Individual) {
186			return sprintf(self::LNK, $person->getHtmlUrl(), $person->getFullName());
187		} elseif ($showUnknown) {
188			return sprintf(self::MSG, I18N::translate('unknown'));
189		} else {
190			return '';
191		}
192	}
193
194	/**
195	 * Forat the parents of an individual.
196	 *
197	 * @param Individual $person
198	 *
199	 * @return string
200	 */
201	private function getParents(Individual $person) {
202		$father = null;
203		$mother = null;
204		$html   = sprintf(self::TTL, I18N::translate('Parents'));
205		$family = $person->getPrimaryChildFamily();
206		if ($person->canShowName() && $family !== null) {
207			$father = $family->getHusband();
208			$mother = $family->getWife();
209			$html .= $this->getHTML($father) .
210					 $this->getHTML($mother);
211
212			// Can only have a step parent if one & only one parent found at this point
213			if ($father instanceof Individual xor $mother instanceof Individual) {
214				$stepParents = '';
215				foreach ($person->getChildStepFamilies() as $family) {
216					if (!$father instanceof Individual) {
217						$stepParents .= $this->getHTML($family->getHusband());
218					} else {
219						$stepParents .= $this->getHTML($family->getWife());
220					}
221				}
222				if ($stepParents) {
223					$relationship = $father instanceof Individual ?
224						I18N::translateContext('father’s wife', 'step-mother') : I18N::translateContext('mother’s husband', 'step-father');
225					$html .= sprintf(self::TTL, $relationship) . $stepParents;
226				}
227			}
228		}
229		if (!($father instanceof Individual || $mother instanceof Individual)) {
230			$html .= sprintf(self::MSG, I18N::translateContext('unknown family', 'unknown'));
231		}
232
233		return $html;
234	}
235
236	/**
237	 * Format a family.
238	 *
239	 * @param Individual $person
240	 *
241	 * @return string
242	 */
243	private function getFamily(Individual $person) {
244		$html = '';
245		if ($person->canShowName()) {
246			foreach ($person->getSpouseFamilies() as $family) {
247				$spouse = $family->getSpouse($person);
248				$html .= $this->getHTML($spouse, true);
249				$children = $family->getChildren();
250				if (count($children) > 0) {
251					$html .= "<ul class='clist'>";
252					foreach ($children as $child) {
253						$html .= '<li>' . $this->getHTML($child) . '</li>';
254					}
255					$html .= '</ul>';
256				}
257			}
258		}
259		if (!$html) {
260			$html = sprintf(self::MSG, I18N::translate('none'));
261		}
262
263		return sprintf(self::TTL, I18N::translate('Family')) . $html;
264	}
265
266}
267