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