Issues (2559)

app/Module/SourcesTabModule.php (5 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
class SourcesTabModule extends AbstractModule implements ModuleTabInterface
29
{
30
    use ModuleTabTrait;
31
32
    /** @var Collection<array-key,Fact>|null  */
33
    private Collection|null $facts = null;
34
35
    private ClipboardService $clipboard_service;
36
37
    /**
38
     * @param ClipboardService $clipboard_service
39
     */
40
    public function __construct(ClipboardService $clipboard_service)
41
    {
42
        $this->clipboard_service = $clipboard_service;
43
    }
44
45
    public function title(): string
46
    {
47
        /* I18N: Name of a module */
48
        return I18N::translate('Sources');
49
    }
50
51
    public function description(): string
52
    {
53
        /* I18N: Description of the “Sources” module */
54
        return I18N::translate('A tab showing the sources linked to an individual.');
55
    }
56
57
    /**
58
     * The default position for this tab.  It can be changed in the control panel.
59
     *
60
     * @return int
61
     */
62
    public function defaultTabOrder(): int
63
    {
64
        return 3;
65
    }
66
67
    /**
68
     * Is this tab empty? If so, we don't always need to display it.
69
     *
70
     * @param Individual $individual
71
     *
72
     * @return bool
73
     */
74
    public function hasTabContent(Individual $individual): bool
75
    {
76
        return $individual->canEdit() || $this->getFactsWithSources($individual)->isNotEmpty();
77
    }
78
79
    /**
80
     * A greyed out tab has no actual content, but may perhaps have
81
     * options to create content.
82
     *
83
     * @param Individual $individual
84
     *
85
     * @return bool
86
     */
87
    public function isGrayedOut(Individual $individual): bool
88
    {
89
        return $this->getFactsWithSources($individual)->isEmpty();
90
    }
91
92
    /**
93
     * Generate the HTML content of this tab.
94
     *
95
     * @param Individual $individual
96
     *
97
     * @return string
98
     */
99
    public function getTabContent(Individual $individual): string
100
    {
101
        return view('modules/sources_tab/tab', [
102
            'can_edit'        => $individual->canEdit(),
103
            'clipboard_facts' => $this->clipboard_service->pastableFactsOfType($individual, $this->supportedFacts()),
104
            'individual'      => $individual,
105
            'facts'           => $this->getFactsWithSources($individual),
106
        ]);
107
    }
108
109
    /**
110
     * Get all the facts for an individual which contain sources.
111
     *
112
     * @param Individual $individual
113
     *
114
     * @return Collection<int,Fact>
115
     */
116
    private function getFactsWithSources(Individual $individual): Collection
117
    {
118
        if ($this->facts === null) {
119
            $facts = $individual->facts();
120
121
            foreach ($individual->spouseFamilies() as $family) {
122
                if ($family->canShow()) {
123
                    foreach ($family->facts() as $fact) {
124
                        $facts->push($fact);
125
                    }
126
                }
127
            }
128
129
            $this->facts = new Collection();
130
131
            foreach ($facts as $fact) {
132
                if (preg_match('/(?:^1|\n\d) SOUR/', $fact->gedcom())) {
133
                    $this->facts->push($fact);
0 ignored issues
show
The method push() does not exist on null. ( Ignorable by Annotation )

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

133
                    $this->facts->/** @scrutinizer ignore-call */ 
134
                                  push($fact);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
                }
135
            }
136
137
            $this->facts = Fact::sortFacts($this->facts);
138
        }
139
140
        return $this->facts;
141
    }
142
143
    /**
144
     * Can this tab load asynchronously?
145
     *
146
     * @return bool
147
     */
148
    public function canLoadAjax(): bool
149
    {
150
        return false;
151
    }
152
153
    /**
154
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
155
     *
156
     * @return Collection<int,string>
157
     */
158
    public function supportedFacts(): Collection
159
    {
160
        return new Collection(['INDI:SOUR', 'FAM:SOUR']);
0 ignored issues
show
array('INDI:SOUR', 'FAM:SOUR') 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

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