Passed
Push — dev ( 8976f1...f79383 )
by Greg
07:34
created

NotesTabModule   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 140
rs 10
c 0
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A description() 0 4 1
A getTabContent() 0 7 1
A __construct() 0 3 1
A isGrayedOut() 0 3 1
A defaultTabOrder() 0 3 1
A hasTabContent() 0 3 2
A title() 0 4 1
A supportedFacts() 0 3 1
A getFactsWithNotes() 0 21 5
A canLoadAjax() 0 3 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Module;
21
22
use Fisharebest\Webtrees\Fact;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Individual;
25
use Fisharebest\Webtrees\Services\ClipboardService;
26
use Illuminate\Support\Collection;
27
28
use function preg_match;
29
30
/**
31
 * Class NotesTabModule
32
 */
33
class NotesTabModule extends AbstractModule implements ModuleTabInterface
34
{
35
    use ModuleTabTrait;
36
37
    private ?Collection $facts = null;
38
39
    private ClipboardService $clipboard_service;
40
41
    /**
42
     * NotesTabModule constructor.
43
     *
44
     * @param ClipboardService $clipboard_service
45
     */
46
    public function __construct(ClipboardService $clipboard_service)
47
    {
48
        $this->clipboard_service = $clipboard_service;
49
    }
50
51
    /**
52
     * How should this module be identified in the control panel, etc.?
53
     *
54
     * @return string
55
     */
56
    public function title(): string
57
    {
58
        /* I18N: Name of a module */
59
        return I18N::translate('Notes');
60
    }
61
62
    /**
63
     * A sentence describing what this module does.
64
     *
65
     * @return string
66
     */
67
    public function description(): string
68
    {
69
        /* I18N: Description of the “Notes” module */
70
        return I18N::translate('A tab showing the notes attached to an individual.');
71
    }
72
73
    /**
74
     * The default position for this tab.  It can be changed in the control panel.
75
     *
76
     * @return int
77
     */
78
    public function defaultTabOrder(): int
79
    {
80
        return 4;
81
    }
82
83
    /**
84
     * Is this tab empty? If so, we don't always need to display it.
85
     *
86
     * @param Individual $individual
87
     *
88
     * @return bool
89
     */
90
    public function hasTabContent(Individual $individual): bool
91
    {
92
        return $individual->canEdit() || $this->getFactsWithNotes($individual)->isNotEmpty();
93
    }
94
95
    /**
96
     * A greyed out tab has no actual content, but may perhaps have
97
     * options to create content.
98
     *
99
     * @param Individual $individual
100
     *
101
     * @return bool
102
     */
103
    public function isGrayedOut(Individual $individual): bool
104
    {
105
        return $this->getFactsWithNotes($individual)->isEmpty();
106
    }
107
108
    /**
109
     * Generate the HTML content of this tab.
110
     *
111
     * @param Individual $individual
112
     *
113
     * @return string
114
     */
115
    public function getTabContent(Individual $individual): string
116
    {
117
        return view('modules/notes/tab', [
118
            'can_edit'        => $individual->canEdit(),
119
            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
120
            'individual'      => $individual,
121
            'facts'           => $this->getFactsWithNotes($individual),
122
        ]);
123
    }
124
125
    /**
126
     * Get all the facts for an individual which contain notes.
127
     *
128
     * @param Individual $individual
129
     *
130
     * @return Collection<Fact>
131
     */
132
    private function getFactsWithNotes(Individual $individual): Collection
133
    {
134
        if ($this->facts === null) {
135
            $facts = $individual->facts();
136
137
            foreach ($individual->spouseFamilies() as $family) {
138
                if ($family->canShow()) {
139
                    foreach ($family->facts() as $fact) {
140
                        $facts->push($fact);
141
                    }
142
                }
143
            }
144
145
            $callback = static fn (Fact $fact): bool => preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom()) === 1;
146
147
            $this->facts = $facts->filter($callback);
148
149
            $this->facts = Fact::sortFacts($this->facts);
0 ignored issues
show
Bug introduced by
$this->facts of type null is incompatible with the type Illuminate\Support\Collection expected by parameter $unsorted of Fisharebest\Webtrees\Fact::sortFacts(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            $this->facts = Fact::sortFacts(/** @scrutinizer ignore-type */ $this->facts);
Loading history...
150
        }
151
152
        return $this->facts;
153
    }
154
155
    /**
156
     * Can this tab load asynchronously?
157
     *
158
     * @return bool
159
     */
160
    public function canLoadAjax(): bool
161
    {
162
        return false;
163
    }
164
165
    /**
166
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
167
     *
168
     * @return Collection<string>
169
     */
170
    public function supportedFacts(): Collection
171
    {
172
        return new Collection(['INDI:NOTE', 'FAM:NOTE']);
173
    }
174
}
175