xref: /webtrees/app/Module/NotesTabModule.php (revision 16e7dcbf4079d7ea9a84244c8f3c93e4df7f69a9)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 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;
24use Fisharebest\Webtrees\Individual;
25
26/**
27 * Class NotesTabModule
28 */
29class NotesTabModule extends AbstractModule implements ModuleTabInterface {
30	/** @var Fact[] A list facts for this note. */
31	private $facts;
32
33	/** {@inheritdoc} */
34	public function getTitle() {
35		return /* I18N: Name of a module */ I18N::translate('Notes');
36	}
37
38	/** {@inheritdoc} */
39	public function getDescription() {
40		return /* I18N: Description of the “Notes” module */ I18N::translate('A tab showing the notes attached to an individual.');
41	}
42
43	/** {@inheritdoc} */
44	public function defaultTabOrder() {
45		return 40;
46	}
47
48	/** {@inheritdoc} */
49	public function hasTabContent(Individual $individual) {
50		return $individual->canEdit() || $this->getFactsWithNotes($individual);
51	}
52
53	/** {@inheritdoc} */
54	public function isGrayedOut(Individual $individual) {
55		return !$this->getFactsWithNotes($individual);
56	}
57
58	/** {@inheritdoc} */
59	public function getTabContent(Individual $individual) {
60		return view('modules/notes/tab', [
61			'can_edit'   => $individual->canEdit(),
62			'individual' => $individual,
63			'facts'      => $this->getFactsWithNotes($individual),
64		]);
65	}
66
67	/**
68	 * Get all the facts for an individual which contain notes.
69	 *
70	 * @param Individual $individual
71	 *
72	 * @return Fact[]
73	 */
74	private function getFactsWithNotes(Individual $individual) {
75		if ($this->facts === null) {
76			$facts = $individual->getFacts();
77			foreach ($individual->getSpouseFamilies() as $family) {
78				if ($family->canShow()) {
79					foreach ($family->getFacts() as $fact) {
80						$facts[] = $fact;
81					}
82				}
83			}
84			$this->facts = [];
85			foreach ($facts as $fact) {
86				if (preg_match('/(?:^1|\n\d) NOTE/', $fact->getGedcom())) {
87					$this->facts[] = $fact;
88				}
89			}
90			Functions::sortFacts($this->facts);
91		}
92
93		return $this->facts;
94	}
95
96	/** {@inheritdoc} */
97	public function canLoadAjax() {
98		return false;
99	}
100}
101