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