xref: /webtrees/app/Module/RelativesTabModule.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\Auth;
19use Fisharebest\Webtrees\Date;
20use Fisharebest\Webtrees\Family;
21use Fisharebest\Webtrees\Functions\Functions;
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Theme;
26
27/**
28 * Class RelativesTabModule
29 */
30class RelativesTabModule extends AbstractModule implements ModuleTabInterface {
31	/**
32	 * How should this module be labelled on tabs, menus, etc.?
33	 *
34	 * @return string
35	 */
36	public function getTitle() {
37		return /* I18N: Name of a module */ I18N::translate('Families');
38	}
39
40	/**
41	 * A sentence describing what this module does.
42	 *
43	 * @return string
44	 */
45	public function getDescription() {
46		return /* I18N: Description of the “Families” module */ I18N::translate('A tab showing the close relatives of an individual.');
47	}
48
49	/**
50	 * The user can re-arrange the tab order, but until they do, this
51	 * is the order in which tabs are shown.
52	 *
53	 * @return int
54	 */
55	public function defaultTabOrder() {
56		return 20;
57	}
58
59	/**
60	 * Display the age difference between marriages and the births of children.
61	 *
62	 * @param Date $prev
63	 * @param Date $next
64	 * @param int  $child_number
65	 *
66	 * @return string
67	 */
68	private static function ageDifference(Date $prev, Date $next, $child_number = 0) {
69		if ($prev->isOK() && $next->isOK()) {
70			$days = $next->maximumJulianDay() - $prev->minimumJulianDay();
71			if ($days < 0) {
72				// Show warning triangle if dates in reverse order
73				$diff = '<i class="icon-warning"></i> ';
74			} elseif ($child_number > 1 && $days > 1 && $days < 240) {
75				// Show warning triangle if children born too close together
76				$diff = '<i class="icon-warning"></i> ';
77			} else {
78				$diff = '';
79			}
80
81			$months = round($days * 12 / 365.25); // Approximate - we do not know the calendar
82			if (abs($months) == 12 || abs($months) >= 24) {
83				$diff .= I18N::plural('%s year', '%s years', round($months / 12), I18N::number(round($months / 12)));
84			} elseif ($months != 0) {
85				$diff .= I18N::plural('%s month', '%s months', $months, I18N::number($months));
86			}
87
88			return '<div class="elderdate age">' . $diff . '</div>';
89		} else {
90			return '';
91		}
92	}
93
94	/**
95	 * Print a family group.
96	 *
97	 * @param Family $family
98	 * @param string $type
99	 * @param string $label
100	 */
101	private function printFamily(Family $family, $type, $label) {
102		global $controller;
103
104		if ($family->getTree()->getPreference('SHOW_PRIVATE_RELATIONSHIPS')) {
105			$access_level = Auth::PRIV_HIDE;
106		} else {
107			$access_level = Auth::accessLevel($family->getTree());
108		}
109
110		?>
111		<table>
112			<tr>
113				<td>
114					<i class="icon-cfamily"></i>
115				</td>
116				<td>
117					<span class="subheaders"> <?= $label ?></span>
118					<a href="<?= $family->getHtmlUrl() ?>"> - <?= I18N::translate('View this family') ?></a>
119				</td>
120			</tr>
121		</table>
122
123		<table class="table table-sm wt-facts-table">
124		<caption></caption>
125		<tbody>
126		<?php
127
128		///// HUSB /////
129		$found = false;
130		foreach ($family->getFacts('HUSB', false, $access_level) as $fact) {
131			$found |= !$fact->isPendingDeletion();
132			$person = $fact->getTarget();
133			if ($person instanceof Individual) {
134				$row_class = 'wt-gender-' . $person->getSex();
135				if ($fact->isPendingAddition()) {
136					$row_class .= ' new';
137				} elseif ($fact->isPendingDeletion()) {
138					$row_class .= ' old';
139				}
140				?>
141					<tr class="<?= $row_class ?>">
142						<th scope="row">
143							<?= Functions::getCloseRelationshipName($controller->record, $person) ?>
144						</th>
145						<td class="border-0 p-0">
146							<?= Theme::theme()->individualBoxLarge($person) ?>
147						</td>
148					</tr>
149				<?php
150			}
151		}
152		if (!$found && $family->canEdit()) {
153			?>
154			<tr>
155				<th></th>
156				<td scope="row">
157					<a href="edit_interface.php?action=add_spouse_to_family&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;famtag=HUSB">
158						<?= I18N::translate('Add a husband to this family') ?>
159					</a>
160					</td>
161			</tr>
162			<?php
163		}
164
165		///// WIFE /////
166		$found = false;
167		foreach ($family->getFacts('WIFE', false, $access_level) as $fact) {
168			$person = $fact->getTarget();
169			if ($person instanceof Individual) {
170				$found |= !$fact->isPendingDeletion();
171				$row_class = 'wt-gender-' . $person->getSex();
172				if ($fact->isPendingAddition()) {
173					$row_class .= ' new';
174				} elseif ($fact->isPendingDeletion()) {
175					$row_class .= ' old';
176				}
177				?>
178				<tr class="<?= $row_class ?>">
179					<th scope="row">
180						<?= Functions::getCloseRelationshipName($controller->record, $person) ?>
181					</th>
182					<td class="border-0 p-0">
183						<?= Theme::theme()->individualBoxLarge($person) ?>
184					</td>
185				</tr>
186				<?php
187			}
188		}
189		if (!$found && $family->canEdit()) {
190			?>
191			<tr>
192				<th scope="row"></th>
193				<td>
194					<a href="edit_interface.php?action=add_spouse_to_family&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;famtag=WIFE">
195						<?= I18N::translate('Add a wife to this family') ?>
196					</a>
197				</td>
198			</tr>
199			<?php
200		}
201
202		///// MARR /////
203		$found = false;
204		$prev  = new Date('');
205		foreach ($family->getFacts(WT_EVENTS_MARR . '|' . WT_EVENTS_DIV, true) as $fact) {
206			$found |= !$fact->isPendingDeletion();
207			if ($fact->isPendingAddition()) {
208				$row_class = 'new';
209			} elseif ($fact->isPendingDeletion()) {
210				$row_class = 'old';
211			} else {
212				$row_class = '';
213			}
214			?>
215			<tr class="<?= $row_class ?>">
216				<th scope="row">
217				</th>
218				<td>
219					<?= GedcomTag::getLabelValue($fact->getTag(), $fact->getDate()->display() . ' — ' . $fact->getPlace()->getFullName()) ?>
220				</td>
221			</tr>
222			<?php
223			if (!$prev->isOK() && $fact->getDate()->isOK()) {
224				$prev = $fact->getDate();
225			}
226		}
227		if (!$found && $family->canShow() && $family->canEdit()) {
228			// Add a new marriage
229			?>
230			<tr>
231				<th scope="row">
232				</th>
233				<td>
234					<a href="edit_interface.php?action=add&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;fact=MARR">
235						<?= I18N::translate('Add marriage details') ?>
236					</a>
237				</td>
238			</tr>
239			<?php
240		}
241
242		///// CHIL /////
243		$child_number = 0;
244		foreach ($family->getFacts('CHIL', false, $access_level) as $fact) {
245			$person = $fact->getTarget();
246			if ($person instanceof Individual) {
247				$row_class = 'wt-gender-' . $person->getSex();
248				if ($fact->isPendingAddition()) {
249					$child_number++;
250					$row_class .= ' new';
251				} elseif ($fact->isPendingDeletion()) {
252					$row_class .= ' old';
253				} else {
254					$child_number++;
255				}
256				$next = new Date('');
257				foreach ($person->getFacts(WT_EVENTS_BIRT, true) as $bfact) {
258					if ($bfact->getDate()->isOK()) {
259						$next = $bfact->getDate();
260						break;
261					}
262				}
263				?>
264				<tr class="<?= $row_class ?>">
265					<th scope="row">
266						<?= self::ageDifference($prev, $next, $child_number) ?>
267						<?= Functions::getCloseRelationshipName($controller->record, $person) ?>
268					</th>
269					<td class="border-0 p-0">
270						<?= Theme::theme()->individualBoxLarge($person) ?>
271					</td>
272				</tr>
273				<?php
274				$prev = $next;
275			}
276		}
277		// Re-order children / add a new child
278		if ($family->canEdit()) {
279			if ($type == 'FAMS') {
280				$add_child_text = I18N::translate('Add a son or daughter');
281			} else {
282				$add_child_text = I18N::translate('Add a brother or sister');
283			}
284			?>
285			<tr>
286				<th scope="row">
287					<?php if (count($family->getChildren()) > 1): ?>
288					<a href="edit_interface.php?action=reorder-children&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>">
289						<i class="icon-media-shuffle"></i> <?= I18N::translate('Re-order children') ?>
290					</a>
291					<?php endif; ?>
292				</th>
293				<td>
294					<a href="edit_interface.php?action=add_child_to_family&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;gender=U">
295						<?= $add_child_text ?>
296					</a>
297					<span style='white-space:nowrap;'>
298						<a href="edit_interface.php?action=add_child_to_family&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;gender=M" class="icon-sex_m_15x15"></a>
299						<a href="edit_interface.php?action=add_child_to_family&amp;ged=<?= $family->getTree()->getNameHtml() ?>&amp;xref=<?= $family->getXref() ?>&amp;gender=F" class="icon-sex_f_15x15"></a>
300					</span>
301				</td>
302			</tr>
303			<?php
304		}
305
306		echo '</tbody>';
307		echo '</table>';
308	}
309
310	/** {@inheritdoc} */
311	public function getTabContent() {
312		global $controller;
313
314		ob_start();
315		?>
316		<table class="table table-sm wt-facts-table" role="presentation">
317			<tbody>
318				<tr>
319					<td>
320						<label>
321							<input id="show-date-differences" type="checkbox" checked>
322							<?= I18N::translate('Date differences') ?>
323						</label>
324					</td>
325				</tr>
326			</tbody>
327		</table>
328		<?php
329		$families = $controller->record->getChildFamilies();
330		if (!$families && $controller->record->canEdit()) {
331			?>
332			<table class="table table-sm wt-facts-table">
333				<tbody>
334					<tr>
335						<td>
336							<a href="edit_interface.php?action=add_parent_to_individual&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;gender=M">
337								<?= I18N::translate('Add a father') ?>
338							</a>
339						</td>
340					</tr>
341					<tr>
342						<td>
343							<a href="edit_interface.php?action=add_parent_to_individual&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;gender=F">
344								<?= I18N::translate('Add a mother') ?>
345							</a>
346						</td>
347					</tr>
348				</tbody>
349			</table>
350			<?php
351		}
352
353		// parents
354		foreach ($families as $family) {
355			$this->printFamily($family, 'FAMC', $controller->record->getChildFamilyLabel($family));
356		}
357
358		// step-parents
359		foreach ($controller->record->getChildStepFamilies() as $family) {
360			$this->printFamily($family, 'FAMC', $controller->record->getStepFamilyLabel($family));
361		}
362
363		// spouses
364		$families = $controller->record->getSpouseFamilies();
365		foreach ($families as $family) {
366			$this->printFamily($family, 'FAMS', $controller->getSpouseFamilyLabel($family, $controller->record));
367		}
368
369		// step-children
370		foreach ($controller->record->getSpouseStepFamilies() as $family) {
371			$this->printFamily($family, 'FAMS', $family->getFullName());
372		}
373
374		if ($controller->record->canEdit()) {
375		?>
376		<br>
377		<table class="table table-sm wt-facts-table">
378			<tbody>
379				<?php if (count($families) > 1) { ?>
380				<tr>
381					<td>
382						<a href="edit_interface.php?action=reorder-spouses&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>">
383						<?= I18N::translate('Re-order families') ?>
384						</a>
385					</td>
386				</tr>
387			<?php } ?>
388				<tr>
389					<td>
390					<a href="edit_interface.php?action=addfamlink&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>"><?= I18N::translate('Link this individual to an existing family as a child') ?></a>
391					</td>
392				</tr>
393				<?php if ($controller->record->getSex() !== 'F') { ?>
394				<tr>
395					<td>
396					<a href="edit_interface.php?action=add_spouse_to_individual&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;sex=F"><?= I18N::translate('Add a wife') ?></a>
397					</td>
398				</tr>
399				<tr>
400					<td>
401					<a href="edit_interface.php?action=linkspouse&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;famtag=WIFE"><?= I18N::translate('Add a wife using an existing individual') ?></a>
402					</td>
403				</tr>
404				<?php } ?>
405				<?php if ($controller->record->getSex() !== 'M') { ?>
406				<tr>
407					<td>
408					<a href="edit_interface.php?action=add_spouse_to_individual&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;sex=M"><?= I18N::translate('Add a husband') ?></a>
409					</td>
410				</tr>
411				<tr>
412					<td>
413					<a href="edit_interface.php?action=linkspouse&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;famtag=HUSB"><?= I18N::translate('Add a husband using an existing individual') ?></a>
414					</td>
415				</tr>
416				<?php } ?>
417				<tr>
418					<td>
419						<a href="edit_interface.php?action=add_child_to_individual&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;gender=U">
420							<?= I18N::translate('Add a child to create a one-parent family') ?>
421						</a>
422					</td>
423				</tr>
424			</tbody>
425		</table>
426		<?php } ?>
427		<br>
428		<script>
429			//persistent_toggle("show-date-differences", ".elderdate");
430		</script>
431		<?php
432
433		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
434	}
435
436	/** {@inheritdoc} */
437	public function hasTabContent() {
438		return true;
439	}
440	/** {@inheritdoc} */
441	public function isGrayedOut() {
442		return false;
443	}
444	/** {@inheritdoc} */
445	public function canLoadAjax() {
446		return false;
447	}
448
449	/** {@inheritdoc} */
450	public function getPreLoadContent() {
451		return '';
452	}
453}
454