xref: /webtrees/app/Module/SourcesTabModule.php (revision 6fd6cde88b0af0681ba223a45cebb0e30f27a280)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/**
20 * Class SourcesTabModule
21 */
22class SourcesTabModule extends Module implements ModuleTabInterface {
23	private $facts;
24
25	/** {@inheritdoc} */
26	public function getTitle() {
27		return /* I18N: Name of a module */ I18N::translate('Sources');
28	}
29
30	/** {@inheritdoc} */
31	public function getDescription() {
32		return /* I18N: Description of the “Sources” module */ I18N::translate('A tab showing the sources linked to an individual.');
33	}
34
35	/** {@inheritdoc} */
36	public function defaultTabOrder() {
37		return 30;
38	}
39
40	/** {@inheritdoc} */
41	public function hasTabContent() {
42		return WT_USER_CAN_EDIT || $this->getFactsWithSources();
43	}
44
45	/** {@inheritdoc} */
46	public function isGrayedOut() {
47		return !$this->getFactsWithSources();
48	}
49
50	/** {@inheritdoc} */
51	public function getTabContent() {
52		global $controller, $WT_TREE;
53
54		ob_start();
55		?>
56		<table class="facts_table">
57			<tr>
58				<td colspan="2" class="descriptionbox rela">
59				<input id="checkbox_sour2" type="checkbox" <?php echo $WT_TREE->getPreference('SHOW_LEVEL2_NOTES') ? 'checked' : ''; ?> onclick="jQuery('tr.row_sour2').toggle();">
60				<label for="checkbox_sour2"><?php echo I18N::translate('Show all sources'); ?></label>
61				</td>
62			</tr>
63			<?php
64			foreach ($this->getFactsWithSources() as $fact) {
65				if ($fact->getTag() == 'SOUR') {
66					print_main_sources($fact, 1);
67				} else {
68					print_main_sources($fact, 2);
69				}
70			}
71			if (!$this->getFactsWithSources()) {
72				echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no source citations for this individual.'), '</td></tr>';
73			}
74
75			// New Source Link
76			if ($controller->record->canEdit()) {
77				?>
78				<tr>
79					<td class="facts_label">
80						<?php echo GedcomTag::getLabel('SOUR'); ?>
81					</td>
82					<td class="facts_value">
83						<a href="#" onclick="add_new_record('<?php echo $controller->record->getXref(); ?>','SOUR'); return false;">
84							<?php echo I18N::translate('Add a new source citation'); ?>
85						</a>
86					</td>
87				</tr>
88			<?php
89			}
90			?>
91		</table>
92		<?php
93		if (!$WT_TREE->getPreference('SHOW_LEVEL2_NOTES')) {
94			echo '<script>jQuery("tr.row_sour2").toggle();</script>';
95		}
96
97		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
98	}
99
100	/**
101	 * Get all the facts for an individual which contain sources.
102	 *
103	 * @return Fact[]
104	 */
105	private function getFactsWithSources() {
106		global $controller;
107
108		if ($this->facts === null) {
109			$facts = $controller->record->getFacts();
110			foreach ($controller->record->getSpouseFamilies() as $family) {
111				if ($family->canShow()) {
112					foreach ($family->getFacts() as $fact) {
113						$facts[] = $fact;
114					}
115				}
116			}
117			$this->facts = array();
118			foreach ($facts as $fact) {
119				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
120					$this->facts[] = $fact;
121				}
122			}
123			sort_facts($this->facts);
124		}
125
126		return $this->facts;
127	}
128
129	/** {@inheritdoc} */
130	public function canLoadAjax() {
131		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
132	}
133
134	/** {@inheritdoc} */
135	public function getPreLoadContent() {
136		return '';
137	}
138}
139