xref: /webtrees/app/Module/NotesTabModule.php (revision 8d897fd11a9a8845d8456cb9eaad33b9c9d5a225)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Module;
20
21use Fisharebest\Webtrees\Fact;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Services\ClipboardService;
25use Illuminate\Support\Collection;
26
27/**
28 * Class NotesTabModule
29 */
30class NotesTabModule extends AbstractModule implements ModuleTabInterface
31{
32    use ModuleTabTrait;
33
34    /** @var Collection A list facts for this note. */
35    private $facts;
36
37    /** @var ClipboardService */
38    private $clipboard_service;
39
40    /**
41     * NotesTabModule constructor.
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    /**
62     * A sentence describing what this module does.
63     *
64     * @return string
65     */
66    public function description(): string
67    {
68        /* I18N: Description of the “Notes” module */
69        return I18N::translate('A tab showing the notes attached to an individual.');
70    }
71
72    /**
73     * The default position for this tab.  It can be changed in the control panel.
74     *
75     * @return int
76     */
77    public function defaultTabOrder(): int
78    {
79        return 4;
80    }
81
82    /**
83     * Is this tab empty? If so, we don't always need to display it.
84     *
85     * @param Individual $individual
86     *
87     * @return bool
88     */
89    public function hasTabContent(Individual $individual): bool
90    {
91        return $individual->canEdit() || $this->getFactsWithNotes($individual)->isNotEmpty();
92    }
93
94    /**
95     * A greyed out tab has no actual content, but may perhaps have
96     * options to create content.
97     *
98     * @param Individual $individual
99     *
100     * @return bool
101     */
102    public function isGrayedOut(Individual $individual): bool
103    {
104        return $this->getFactsWithNotes($individual)->isEmpty();
105    }
106
107    /**
108     * Generate the HTML content of this tab.
109     *
110     * @param Individual $individual
111     *
112     * @return string
113     */
114    public function getTabContent(Individual $individual): string
115    {
116        return view('modules/notes/tab', [
117            'can_edit'        => $individual->canEdit(),
118            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
119            'individual'      => $individual,
120            'facts'           => $this->getFactsWithNotes($individual),
121        ]);
122    }
123
124    /**
125     * Get all the facts for an individual which contain notes.
126     *
127     * @param Individual $individual
128     *
129     * @return Collection
130     */
131    private function getFactsWithNotes(Individual $individual): Collection
132    {
133        if ($this->facts === null) {
134            $facts = $individual->facts();
135
136            foreach ($individual->spouseFamilies() as $family) {
137                if ($family->canShow()) {
138                    foreach ($family->facts() as $fact) {
139                        $facts->push($fact);
140                    }
141                }
142            }
143
144            $this->facts = new Collection();
145
146            foreach ($facts as $fact) {
147                if (preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom())) {
148                    $this->facts->push($fact);
149                }
150            }
151            $this->facts = Fact::sortFacts($this->facts);
152        }
153
154        return $this->facts;
155    }
156
157    /**
158     * Can this tab load asynchronously?
159     *
160     * @return bool
161     */
162    public function canLoadAjax(): bool
163    {
164        return false;
165    }
166
167    /**
168     * This module handles the following facts - so don't show them on the "Facts and events" tab.
169     *
170     * @return Collection
171     */
172    public function supportedFacts(): Collection
173    {
174        return new Collection(['NOTE']);
175    }
176}
177