xref: /webtrees/app/Module/SourcesTabModule.php (revision 15d603e7c7c15d20f055d3d9c38d6b133453c5be)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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\I18N;
23
24/**
25 * Class SourcesTabModule
26 */
27class SourcesTabModule extends AbstractModule implements ModuleTabInterface {
28	/** @var Fact[] All facts belonging to this source. */
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;
61
62		ob_start();
63		?>
64		<table class="facts_table">
65			<tr>
66				<td colspan="2" class="descriptionbox rela">
67					<label>
68						<input id="show-level-2-sources" type="checkbox">
69						<?= I18N::translate('Show all sources') ?>
70					</label>
71				</td>
72			</tr>
73			<?php
74			foreach ($this->getFactsWithSources() as $fact) {
75				if ($fact->getTag() == 'SOUR') {
76					FunctionsPrintFacts::printMainSources($fact, 1);
77				} else {
78					FunctionsPrintFacts::printMainSources($fact, 2);
79				}
80			}
81			if (!$this->getFactsWithSources()) {
82				echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no source citations for this individual.'), '</td></tr>';
83			}
84
85			// New Source Link
86			if ($controller->record->canEdit()) {
87				?>
88				<tr>
89					<td class="facts_label">
90						<?= I18N::translate('Source') ?>
91					</td>
92					<td class="facts_value">
93						<a href="edit_interface.php?action=add&amp;ged=<?= $controller->record->getTree()->getNameHtml() ?>&amp;xref=<?= $controller->record->getXref() ?>&amp;fact=SOUR">
94							<?= I18N::translate('Add a source citation') ?>
95						</a>
96					</td>
97				</tr>
98			<?php
99			}
100			?>
101		</table>
102		<script>
103			//persistent_toggle("show-level-2-sources", ".row_sour2");
104		</script>
105		<?php
106
107		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
108	}
109
110	/**
111	 * Get all the facts for an individual which contain sources.
112	 *
113	 * @return Fact[]
114	 */
115	private function getFactsWithSources() {
116		global $controller;
117
118		if ($this->facts === null) {
119			$facts = $controller->record->getFacts();
120			foreach ($controller->record->getSpouseFamilies() as $family) {
121				if ($family->canShow()) {
122					foreach ($family->getFacts() as $fact) {
123						$facts[] = $fact;
124					}
125				}
126			}
127			$this->facts = [];
128			foreach ($facts as $fact) {
129				if (preg_match('/(?:^1|\n\d) SOUR/', $fact->getGedcom())) {
130					$this->facts[] = $fact;
131				}
132			}
133			Functions::sortFacts($this->facts);
134		}
135
136		return $this->facts;
137	}
138
139	/** {@inheritdoc} */
140	public function canLoadAjax() {
141		return false;
142	}
143
144	/** {@inheritdoc} */
145	public function getPreLoadContent() {
146		return '';
147	}
148}
149