xref: /webtrees/app/Module/SourcesTabModule.php (revision e2a378d30d9bd3fff591da7a11c7cb5ead502323)
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 AbstractModule 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		global $WT_TREE;
43
44		return Auth::isEditor($WT_TREE) || $this->getFactsWithSources();
45	}
46
47	/** {@inheritdoc} */
48	public function isGrayedOut() {
49		return !$this->getFactsWithSources();
50	}
51
52	/** {@inheritdoc} */
53	public function getTabContent() {
54		global $controller, $WT_TREE;
55
56		ob_start();
57		?>
58		<table class="facts_table">
59			<tr>
60				<td colspan="2" class="descriptionbox rela">
61				<input id="checkbox_sour2" type="checkbox" <?php echo $WT_TREE->getPreference('SHOW_LEVEL2_NOTES') ? 'checked' : ''; ?> onclick="jQuery('tr.row_sour2').toggle();">
62				<label for="checkbox_sour2"><?php echo I18N::translate('Show all sources'); ?></label>
63				</td>
64			</tr>
65			<?php
66			foreach ($this->getFactsWithSources() as $fact) {
67				if ($fact->getTag() == 'SOUR') {
68					print_main_sources($fact, 1);
69				} else {
70					print_main_sources($fact, 2);
71				}
72			}
73			if (!$this->getFactsWithSources()) {
74				echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no source citations for this individual.'), '</td></tr>';
75			}
76
77			// New Source Link
78			if ($controller->record->canEdit()) {
79				?>
80				<tr>
81					<td class="facts_label">
82						<?php echo GedcomTag::getLabel('SOUR'); ?>
83					</td>
84					<td class="facts_value">
85						<a href="#" onclick="add_new_record('<?php echo $controller->record->getXref(); ?>','SOUR'); return false;">
86							<?php echo I18N::translate('Add a new source citation'); ?>
87						</a>
88					</td>
89				</tr>
90			<?php
91			}
92			?>
93		</table>
94		<?php
95		if (!$WT_TREE->getPreference('SHOW_LEVEL2_NOTES')) {
96			echo '<script>jQuery("tr.row_sour2").toggle();</script>';
97		}
98
99		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
100	}
101
102	/**
103	 * Get all the facts for an individual which contain sources.
104	 *
105	 * @return Fact[]
106	 */
107	private function getFactsWithSources() {
108		global $controller;
109
110		if ($this->facts === null) {
111			$facts = $controller->record->getFacts();
112			foreach ($controller->record->getSpouseFamilies() as $family) {
113				if ($family->canShow()) {
114					foreach ($family->getFacts() as $fact) {
115						$facts[] = $fact;
116					}
117				}
118			}
119			$this->facts = array();
120			foreach ($facts as $fact) {
121				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
122					$this->facts[] = $fact;
123				}
124			}
125			sort_facts($this->facts);
126		}
127
128		return $this->facts;
129	}
130
131	/** {@inheritdoc} */
132	public function canLoadAjax() {
133		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
134	}
135
136	/** {@inheritdoc} */
137	public function getPreLoadContent() {
138		return '';
139	}
140}
141