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