xref: /webtrees/app/Module/NotesTabModule.php (revision b0f03dfe90252b729e1ddaafe0b37dc485b3c221)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Fact;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Individual;
25use Fisharebest\Webtrees\Services\ClipboardService;
26use Illuminate\Support\Collection;
27
28use function preg_match;
29
30/**
31 * Class NotesTabModule
32 */
33class NotesTabModule extends AbstractModule implements ModuleTabInterface
34{
35    use ModuleTabTrait;
36
37    /** @var Collection<array-key,Fact>|null  */
38    private Collection|null $facts = null;
39
40    private ClipboardService $clipboard_service;
41
42    /**
43     * @param ClipboardService $clipboard_service
44     */
45    public function __construct(ClipboardService $clipboard_service)
46    {
47        $this->clipboard_service = $clipboard_service;
48    }
49
50    /**
51     * How should this module be identified in the control panel, etc.?
52     *
53     * @return string
54     */
55    public function title(): string
56    {
57        /* I18N: Name of a module */
58        return I18N::translate('Notes');
59    }
60
61    public function description(): string
62    {
63        /* I18N: Description of the “Notes” module */
64        return I18N::translate('A tab showing the notes attached to an individual.');
65    }
66
67    /**
68     * The default position for this tab.  It can be changed in the control panel.
69     *
70     * @return int
71     */
72    public function defaultTabOrder(): int
73    {
74        return 4;
75    }
76
77    /**
78     * Is this tab empty? If so, we don't always need to display it.
79     *
80     * @param Individual $individual
81     *
82     * @return bool
83     */
84    public function hasTabContent(Individual $individual): bool
85    {
86        return $individual->canEdit() || $this->getFactsWithNotes($individual)->isNotEmpty();
87    }
88
89    /**
90     * A greyed out tab has no actual content, but may perhaps have
91     * options to create content.
92     *
93     * @param Individual $individual
94     *
95     * @return bool
96     */
97    public function isGrayedOut(Individual $individual): bool
98    {
99        return $this->getFactsWithNotes($individual)->isEmpty();
100    }
101
102    /**
103     * Generate the HTML content of this tab.
104     *
105     * @param Individual $individual
106     *
107     * @return string
108     */
109    public function getTabContent(Individual $individual): string
110    {
111        return view('modules/notes/tab', [
112            'can_edit'        => $individual->canEdit(),
113            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
114            'individual'      => $individual,
115            'facts'           => $this->getFactsWithNotes($individual),
116        ]);
117    }
118
119    /**
120     * Get all the facts for an individual which contain notes.
121     *
122     * @param Individual $individual
123     *
124     * @return Collection<int,Fact>
125     */
126    private function getFactsWithNotes(Individual $individual): Collection
127    {
128        if ($this->facts === null) {
129            $facts = $individual->facts();
130
131            foreach ($individual->spouseFamilies() as $family) {
132                if ($family->canShow()) {
133                    foreach ($family->facts() as $fact) {
134                        $facts->push($fact);
135                    }
136                }
137            }
138
139            $callback = static fn (Fact $fact): bool => preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom()) === 1;
140
141            $this->facts = $facts->filter($callback);
142
143            $this->facts = Fact::sortFacts($this->facts);
144        }
145
146        return $this->facts;
147    }
148
149    /**
150     * Can this tab load asynchronously?
151     *
152     * @return bool
153     */
154    public function canLoadAjax(): bool
155    {
156        return false;
157    }
158
159    /**
160     * This module handles the following facts - so don't show them on the "Facts and events" tab.
161     *
162     * @return Collection<int,string>
163     */
164    public function supportedFacts(): Collection
165    {
166        return new Collection(['INDI:NOTE', 'FAM:NOTE']);
167    }
168}
169