xref: /webtrees/app/Module/SourcesTabModule.php (revision 35ae0518ce02c85ca1323f6dd1adedd710adc899)
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'), help_link('show_fact_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						<?php echo help_link('add_source'); ?>
87					</td>
88				</tr>
89			<?php
90			}
91			?>
92		</table>
93		<?php
94		if (!$WT_TREE->getPreference('SHOW_LEVEL2_NOTES')) {
95			echo '<script>jQuery("tr.row_sour2").toggle();</script>';
96		}
97
98		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
99	}
100
101	/**
102	 * Get all the facts for an individual which contain sources.
103	 *
104	 * @return Fact[]
105	 */
106	private function getFactsWithSources() {
107		global $controller;
108
109		if ($this->facts === null) {
110			$facts = $controller->record->getFacts();
111			foreach ($controller->record->getSpouseFamilies() as $family) {
112				if ($family->canShow()) {
113					foreach ($family->getFacts() as $fact) {
114						$facts[] = $fact;
115					}
116				}
117			}
118			$this->facts = array();
119			foreach ($facts as $fact) {
120				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
121					$this->facts[] = $fact;
122				}
123			}
124			sort_facts($this->facts);
125		}
126
127		return $this->facts;
128	}
129
130	/** {@inheritdoc} */
131	public function canLoadAjax() {
132		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
133	}
134
135	/** {@inheritdoc} */
136	public function getPreLoadContent() {
137		return '';
138	}
139}
140