xref: /webtrees/app/Module/NotesTabModule.php (revision 8593c473faa31f2615cc145c51982b372d724614)
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 NotesTabModule
27 */
28class NotesTabModule extends AbstractModule implements ModuleTabInterface {
29	/** @var Fact[] A list facts for this note. */
30	private $facts;
31
32	/** {@inheritdoc} */
33	public function getTitle() {
34		return /* I18N: Name of a module */ I18N::translate('Notes');
35	}
36
37	/** {@inheritdoc} */
38	public function getDescription() {
39		return /* I18N: Description of the “Notes” module */ I18N::translate('A tab showing the notes attached to an individual.');
40	}
41
42	/** {@inheritdoc} */
43	public function defaultTabOrder() {
44		return 40;
45	}
46
47	/** {@inheritdoc} */
48	public function hasTabContent() {
49		global $WT_TREE;
50
51		return Auth::isEditor($WT_TREE) || $this->getFactsWithNotes();
52	}
53
54	/** {@inheritdoc} */
55	public function isGrayedOut() {
56		return !$this->getFactsWithNotes();
57	}
58
59	/** {@inheritdoc} */
60	public function getTabContent() {
61		global $WT_TREE, $controller;
62
63		ob_start();
64		?>
65		<table class="facts_table">
66			<colgroup>
67				<col class="width20">
68				<col class="width80">
69			</colgroup>
70			<tr class="noprint">
71				<td colspan="2" class="descriptionbox rela">
72					<input id="checkbox_note2" type="checkbox" <?php echo $WT_TREE->getPreference('SHOW_LEVEL2_NOTES') ? 'checked' : ''; ?> onclick="jQuery('tr.row_note2').toggle();">
73					<label for="checkbox_note2"><?php echo I18N::translate('Show all notes'); ?></label>
74				</td>
75			</tr>
76
77		<?php
78		foreach ($this->getFactsWithNotes() as $fact) {
79			if ($fact->getTag() == 'NOTE') {
80				FunctionsPrintFacts::printMainNotes($fact, 1);
81			} else {
82				for ($i = 2; $i < 4; ++$i) {
83					FunctionsPrintFacts::printMainNotes($fact, $i);
84				}
85			}
86		}
87		if (!$this->getFactsWithNotes()) {
88			echo '<tr><td id="no_tab4" colspan="2" class="facts_value">', I18N::translate('There are no notes for this individual.'), '</td></tr>';
89		}
90
91		// New note link
92		if ($controller->record->canEdit()) {
93			?>
94			<tr class="noprint">
95				<td class="facts_label">
96					<?php echo GedcomTag::getLabel('NOTE'); ?>
97				</td>
98				<td class="facts_value">
99					<a href="#" onclick="add_new_record('<?php echo $controller->record->getXref(); ?>','NOTE'); return false;">
100						<?php echo I18N::translate('Add a note'); ?>
101					</a>
102				</td>
103			</tr>
104			<tr class="noprint">
105				<td class="facts_label">
106					<?php echo GedcomTag::getLabel('SHARED_NOTE'); ?>
107				</td>
108				<td class="facts_value">
109					<a href="#" onclick="add_new_record('<?php echo $controller->record->getXref(); ?>','SHARED_NOTE'); return false;">
110						<?php echo I18N::translate('Add a shared note'); ?>
111					</a>
112				</td>
113			</tr>
114		<?php
115		}
116		?>
117		</table>
118		<?php
119		if (!$WT_TREE->getPreference('SHOW_LEVEL2_NOTES')) {
120			echo '<script>jQuery("tr.row_note2").toggle();</script>';
121		}
122
123		return '<div id="' . $this->getName() . '_content">' . ob_get_clean() . '</div>';
124	}
125
126	/**
127	 * Get all the facts for an individual which contain notes.
128	 *
129	 * @return Fact[]
130	 */
131	private function getFactsWithNotes() {
132		global $controller;
133
134		if ($this->facts === null) {
135			$facts = $controller->record->getFacts();
136			foreach ($controller->record->getSpouseFamilies() as $family) {
137				if ($family->canShow()) {
138					foreach ($family->getFacts() as $fact) {
139						$facts[] = $fact;
140					}
141				}
142			}
143			$this->facts = array();
144			foreach ($facts as $fact) {
145				if (preg_match('/(?:^1|\n\d) NOTE/', $fact->getGedcom())) {
146					$this->facts[] = $fact;
147				}
148			}
149			Functions::sortFacts($this->facts);
150		}
151
152		return $this->facts;
153	}
154
155	/** {@inheritdoc} */
156	public function canLoadAjax() {
157		return !Auth::isSearchEngine(); // Search engines cannot use AJAX
158	}
159
160	/** {@inheritdoc} */
161	public function getPreLoadContent() {
162		return '';
163	}
164}
165