Issues (2559)

app/Module/NotesTabModule.php (4 issues)

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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;
0 ignored issues
show
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Fisharebest\Webtrees\Individual;
0 ignored issues
show
The type Fisharebest\Webtrees\Individual was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\Services\ClipboardService;
0 ignored issues
show
The type Fisharebest\Webtrees\Services\ClipboardService was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Illuminate\Support\Collection;
27
28
use function preg_match;
29
30
class NotesTabModule extends AbstractModule implements ModuleTabInterface
31
{
32
    use ModuleTabTrait;
33
34
    /** @var Collection<array-key,Fact>|null  */
35
    private Collection|null $facts = null;
36
37
    private ClipboardService $clipboard_service;
38
39
    /**
40
     * @param ClipboardService $clipboard_service
41
     */
42
    public function __construct(ClipboardService $clipboard_service)
43
    {
44
        $this->clipboard_service = $clipboard_service;
45
    }
46
47
    public function title(): string
48
    {
49
        /* I18N: Name of a module */
50
        return I18N::translate('Notes');
51
    }
52
53
    public function description(): string
54
    {
55
        /* I18N: Description of the “Notes” module */
56
        return I18N::translate('A tab showing the notes attached to an individual.');
57
    }
58
59
    /**
60
     * The default position for this tab.  It can be changed in the control panel.
61
     *
62
     * @return int
63
     */
64
    public function defaultTabOrder(): int
65
    {
66
        return 4;
67
    }
68
69
    /**
70
     * Is this tab empty? If so, we don't always need to display it.
71
     *
72
     * @param Individual $individual
73
     *
74
     * @return bool
75
     */
76
    public function hasTabContent(Individual $individual): bool
77
    {
78
        return $individual->canEdit() || $this->getFactsWithNotes($individual)->isNotEmpty();
79
    }
80
81
    /**
82
     * A greyed out tab has no actual content, but may perhaps have
83
     * options to create content.
84
     *
85
     * @param Individual $individual
86
     *
87
     * @return bool
88
     */
89
    public function isGrayedOut(Individual $individual): bool
90
    {
91
        return $this->getFactsWithNotes($individual)->isEmpty();
92
    }
93
94
    /**
95
     * Generate the HTML content of this tab.
96
     *
97
     * @param Individual $individual
98
     *
99
     * @return string
100
     */
101
    public function getTabContent(Individual $individual): string
102
    {
103
        return view('modules/notes/tab', [
104
            'can_edit'        => $individual->canEdit(),
105
            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
106
            'individual'      => $individual,
107
            'facts'           => $this->getFactsWithNotes($individual),
108
        ]);
109
    }
110
111
    /**
112
     * Get all the facts for an individual which contain notes.
113
     *
114
     * @param Individual $individual
115
     *
116
     * @return Collection<int,Fact>
117
     */
118
    private function getFactsWithNotes(Individual $individual): Collection
119
    {
120
        if ($this->facts === null) {
121
            $facts = $individual->facts();
122
123
            foreach ($individual->spouseFamilies() as $family) {
124
                if ($family->canShow()) {
125
                    foreach ($family->facts() as $fact) {
126
                        $facts->push($fact);
127
                    }
128
                }
129
            }
130
131
            $callback = static fn (Fact $fact): bool => preg_match('/(?:^1|\n\d) NOTE/', $fact->gedcom()) === 1;
132
133
            $this->facts = $facts->filter($callback);
134
135
            $this->facts = Fact::sortFacts($this->facts);
136
        }
137
138
        return $this->facts;
139
    }
140
141
    /**
142
     * Can this tab load asynchronously?
143
     *
144
     * @return bool
145
     */
146
    public function canLoadAjax(): bool
147
    {
148
        return false;
149
    }
150
151
    /**
152
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
153
     *
154
     * @return Collection<int,string>
155
     */
156
    public function supportedFacts(): Collection
157
    {
158
        return new Collection(['INDI:NOTE', 'FAM:NOTE']);
0 ignored issues
show
array('INDI:NOTE', 'FAM:NOTE') of type array<integer,string> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

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

158
        return new Collection(/** @scrutinizer ignore-type */ ['INDI:NOTE', 'FAM:NOTE']);
Loading history...
159
    }
160
}
161