xref: /webtrees/app/Module/NotesTabModule.php (revision 895230eed7521b5cd885b90d4f5310405ff0b69a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 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 ModuleInterface, ModuleTabInterface
29{
30    use ModuleTabTrait;
31
32    /** @var Fact[] A list facts for this note. */
33    private $facts;
34
35    /** {@inheritdoc} */
36    public function title(): string
37    {
38        /* I18N: Name of a module */
39        return I18N::translate('Notes');
40    }
41
42    /** {@inheritdoc} */
43    public function description(): string
44    {
45        /* I18N: Description of the “Notes” module */
46        return I18N::translate('A tab showing the notes attached to an individual.');
47    }
48
49    /**
50     * The default position for this tab.  It can be changed in the control panel.
51     *
52     * @return int
53     */
54    public function defaultTabOrder(): int
55    {
56        return 50;
57    }
58
59    /** {@inheritdoc} */
60    public function hasTabContent(Individual $individual): bool
61    {
62        return $individual->canEdit() || $this->getFactsWithNotes($individual);
63    }
64
65    /** {@inheritdoc} */
66    public function isGrayedOut(Individual $individual): bool
67    {
68        return !$this->getFactsWithNotes($individual);
69    }
70
71    /** {@inheritdoc} */
72    public function getTabContent(Individual $individual): string
73    {
74        return view('modules/notes/tab', [
75            'can_edit'   => $individual->canEdit(),
76            'individual' => $individual,
77            'facts'      => $this->getFactsWithNotes($individual),
78        ]);
79    }
80
81    /**
82     * Get all the facts for an individual which contain notes.
83     *
84     * @param Individual $individual
85     *
86     * @return Fact[]
87     */
88    private function getFactsWithNotes(Individual $individual): array
89    {
90        if ($this->facts === null) {
91            $facts = $individual->facts();
92            foreach ($individual->getSpouseFamilies() as $family) {
93                if ($family->canShow()) {
94                    foreach ($family->facts() as $fact) {
95                        $facts[] = $fact;
96                    }
97                }
98            }
99            $this->facts = [];
100            foreach ($facts as $fact) {
101                if (preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom())) {
102                    $this->facts[] = $fact;
103                }
104            }
105            Functions::sortFacts($this->facts);
106        }
107
108        return $this->facts;
109    }
110
111    /** {@inheritdoc} */
112    public function canLoadAjax(): bool
113    {
114        return false;
115    }
116}
117