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