xref: /webtrees/app/Module/FamilyNavigatorModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
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->addSubmenu(new Menu($this->getParents($spouse)));
128			?>
129			<tr>
130				<td class="facts_label">
131					<ul class="nav">
132						<?= $menu->bootstrap4() ?>
133					</ul>
134				</td>
135				<td class="center <?= $controller->getPersonStyle($spouse) ?> nam">
136					<?php if ($spouse->canShow()): ?>
137					<a class="famnav_link" href="<?= $spouse->getHtmlUrl() ?>">
138						<?= $spouse->getFullName() ?>
139					</a>
140					<div class="font9">
141						<?= $spouse->getLifeSpan() ?>
142					</div>
143					<?php else: ?>
144						<?= $spouse->getFullName() ?>
145					<?php endif ?>
146				</td>
147			</tr>
148		<?php
149		}
150
151		foreach ($family->getChildren() as $child) {
152			$menu = new Menu(Functions::getCloseRelationshipName($controller->record, $child));
153			$menu->addSubmenu(new Menu($this->getFamily($child)));
154			?>
155			<tr>
156				<td class="facts_label">
157					<ul class="nav">
158						<?= $menu->bootstrap4() ?>
159					</ul>
160				</td>
161				<td class="center <?= $controller->getPersonStyle($child) ?> nam">
162					<?php if ($child->canShow()): ?>
163					<a class="famnav_link" href="<?= $child->getHtmlUrl() ?>">
164						<?= $child->getFullName() ?>
165					</a>
166					<div class="font9">
167						<?= $child->getLifeSpan() ?>
168					</div>
169					<?php else: ?>
170						<?= $child->getFullName() ?>
171					<?php endif ?>
172				</td>
173			</tr>
174		<?php
175		}
176	}
177
178	/**
179	 * Format an individual.
180	 *
181	 * @param      $person
182	 * @param bool $showUnknown
183	 *
184	 * @return string
185	 */
186	private function getHTML($person, $showUnknown = false) {
187		if ($person instanceof Individual) {
188			return sprintf(self::LNK, $person->getHtmlUrl(), $person->getFullName());
189		} elseif ($showUnknown) {
190			return sprintf(self::MSG, I18N::translate('unknown'));
191		} else {
192			return '';
193		}
194	}
195
196	/**
197	 * Forat the parents of an individual.
198	 *
199	 * @param Individual $person
200	 *
201	 * @return string
202	 */
203	private function getParents(Individual $person) {
204		$father = null;
205		$mother = null;
206		$html   = sprintf(self::TTL, I18N::translate('Parents'));
207		$family = $person->getPrimaryChildFamily();
208		if ($person->canShowName() && $family !== null) {
209			$father = $family->getHusband();
210			$mother = $family->getWife();
211			$html .= $this->getHTML($father) .
212					 $this->getHTML($mother);
213
214			// Can only have a step parent if one & only one parent found at this point
215			if ($father instanceof Individual xor $mother instanceof Individual) {
216				$stepParents = '';
217				foreach ($person->getChildStepFamilies() as $family) {
218					if (!$father instanceof Individual) {
219						$stepParents .= $this->getHTML($family->getHusband());
220					} else {
221						$stepParents .= $this->getHTML($family->getWife());
222					}
223				}
224				if ($stepParents) {
225					$relationship = $father instanceof Individual ?
226						I18N::translateContext('father’s wife', 'step-mother') : I18N::translateContext('mother’s husband', 'step-father');
227					$html .= sprintf(self::TTL, $relationship) . $stepParents;
228				}
229			}
230		}
231		if (!($father instanceof Individual || $mother instanceof Individual)) {
232			$html .= sprintf(self::MSG, I18N::translateContext('unknown family', 'unknown'));
233		}
234
235		return $html;
236	}
237
238	/**
239	 * Format a family.
240	 *
241	 * @param Individual $person
242	 *
243	 * @return string
244	 */
245	private function getFamily(Individual $person) {
246		$html = '';
247		if ($person->canShowName()) {
248			foreach ($person->getSpouseFamilies() as $family) {
249				$spouse = $family->getSpouse($person);
250				$html .= $this->getHTML($spouse, true);
251				$children = $family->getChildren();
252				if (count($children) > 0) {
253					$html .= "<ul class='clist'>";
254					foreach ($children as $child) {
255						$html .= '<li>' . $this->getHTML($child) . '</li>';
256					}
257					$html .= '</ul>';
258				}
259			}
260		}
261		if (!$html) {
262			$html = sprintf(self::MSG, I18N::translate('none'));
263		}
264
265		return sprintf(self::TTL, I18N::translate('Family')) . $html;
266	}
267
268}
269