xref: /webtrees/app/Module/SourcesTabModule.php (revision 3d7a8a4ca809135634f38216b734b15acff479f7)
1<?php
2namespace Fisharebest\Webtrees\Module;
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 */
18use Fisharebest\Webtrees\Auth;
19use Fisharebest\Webtrees\Fact;
20use Fisharebest\Webtrees\Functions\Functions;
21use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
22use Fisharebest\Webtrees\GedcomTag;
23use Fisharebest\Webtrees\I18N;
24
25/**
26 * Class SourcesTabModule
27 */
28class SourcesTabModule extends AbstractModule implements ModuleTabInterface {
29	private $facts;
30
31	/** {@inheritdoc} */
32	public function getTitle() {
33		return /* I18N: Name of a module */ I18N::translate('Sources');
34	}
35
36	/** {@inheritdoc} */
37	public function getDescription() {
38		return /* I18N: Description of the “Sources” module */ I18N::translate('A tab showing the sources linked to an individual.');
39	}
40
41	/** {@inheritdoc} */
42	public function defaultTabOrder() {
43		return 30;
44	}
45
46	/** {@inheritdoc} */
47	public function hasTabContent() {
48		global $WT_TREE;
49
50		return Auth::isEditor($WT_TREE) || $this->getFactsWithSources();
51	}
52
53	/** {@inheritdoc} */
54	public function isGrayedOut() {
55		return !$this->getFactsWithSources();
56	}
57
58	/** {@inheritdoc} */
59	public function getTabContent() {
60		global $controller, $WT_TREE;
61
62		ob_start();
63		?>
64		<table class="facts_table">
65			<tr>
66				<td colspan="2" class="descriptionbox rela">
67				<input id="checkbox_sour2" type="checkbox" <?php echo $WT_TREE->getPreference('SHOW_LEVEL2_NOTES') ? 'checked' : ''; ?> onclick="jQuery('tr.row_sour2').toggle();">
68				<label for="checkbox_sour2"><?php echo I18N::translate('Show all sources'); ?></label>
69				</td>
70			</tr>
71			<?php
72			foreach ($this->getFactsWithSources() as $fact) {
73				if ($fact->getTag() == 'SOUR') {
74					FunctionsPrintFacts::printMainSources($fact, 1);
75				} else {
76					FunctionsPrintFacts::printMainSources($fact, 2);
77				}
78			}
79			if (!$this->getFactsWithSources()) {
80				echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no source citations for this individual.'), '</td></tr>';
81			}
82
83			// New Source Link
84			if ($controller->record->canEdit()) {
85				?>
86				<tr>
87					<td class="facts_label">
88						<?php echo GedcomTag::getLabel('SOUR'); ?>
89					</td>
90					<td class="facts_value">
91						<a href="#" onclick="add_new_record('<?php echo $controller->record->getXref(); ?>','SOUR'); return false;">
92							<?php echo I18N::translate('Add a new source citation'); ?>
93						</a>
94					</td>
95				</tr>
96			<?php
97			}
98			?>
99		</table>
100		<?php
101		if (!$WT_TREE->getPreference('SHOW_LEVEL2_NOTES')) {
102			echo '<script>jQuery("tr.row_sour2").toggle();</script>';
103		}
104
105		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
106	}
107
108	/**
109	 * Get all the facts for an individual which contain sources.
110	 *
111	 * @return Fact[]
112	 */
113	private function getFactsWithSources() {
114		global $controller;
115
116		if ($this->facts === null) {
117			$facts = $controller->record->getFacts();
118			foreach ($controller->record->getSpouseFamilies() as $family) {
119				if ($family->canShow()) {
120					foreach ($family->getFacts() as $fact) {
121						$facts[] = $fact;
122					}
123				}
124			}
125			$this->facts = array();
126			foreach ($facts as $fact) {
127				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
128					$this->facts[] = $fact;
129				}
130			}
131			Functions::sortFacts($this->facts);
132		}
133
134		return $this->facts;
135	}
136
137	/** {@inheritdoc} */
138	public function canLoadAjax() {
139		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
140	}
141
142	/** {@inheritdoc} */
143	public function getPreLoadContent() {
144		return '';
145	}
146}
147