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