Passed
Push — develop ( e50e9c...6c809f )
by Greg
20:25 queued 05:22
created

IndividualFactsTabModule::historicFacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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\Auth;
23
use Fisharebest\Webtrees\Elements\CustomElement;
24
use Fisharebest\Webtrees\Fact;
25
use Fisharebest\Webtrees\I18N;
26
use Fisharebest\Webtrees\Individual;
27
use Fisharebest\Webtrees\Registry;
28
use Fisharebest\Webtrees\Services\ClipboardService;
29
use Fisharebest\Webtrees\Services\IndividualFactsService;
30
use Fisharebest\Webtrees\Services\ModuleService;
31
use Illuminate\Support\Collection;
32
33
use function view;
34
35
/**
36
 * Class IndividualFactsTabModule
37
 */
38
class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
39
{
40
    use ModuleTabTrait;
41
42
    private ClipboardService $clipboard_service;
43
44
    private IndividualFactsService $individual_facts_service;
45
46
    private ModuleService $module_service;
47
48
    /**
49
     * @param ClipboardService       $clipboard_service
50
     * @param IndividualFactsService $individual_facts_service
51
     * @param ModuleService          $module_service
52
     */
53
    public function __construct(
54
        ClipboardService $clipboard_service,
55
        IndividualFactsService $individual_facts_service,
56
        ModuleService $module_service
57
    ) {
58
        $this->clipboard_service        = $clipboard_service;
59
        $this->individual_facts_service = $individual_facts_service;
60
        $this->module_service           = $module_service;
61
    }
62
63
    /**
64
     * How should this module be identified in the control panel, etc.?
65
     *
66
     * @return string
67
     */
68
    public function title(): string
69
    {
70
        /* I18N: Name of a module/tab on the individual page. */
71
        return I18N::translate('Facts and events');
72
    }
73
74
    /**
75
     * A sentence describing what this module does.
76
     *
77
     * @return string
78
     */
79
    public function description(): string
80
    {
81
        /* I18N: Description of the “Facts and events” module */
82
        return I18N::translate('A tab showing the facts and events of an individual.');
83
    }
84
85
    /**
86
     * The default position for this tab.  It can be changed in the control panel.
87
     *
88
     * @return int
89
     */
90
    public function defaultTabOrder(): int
91
    {
92
        return 1;
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 false;
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
        // Which facts and events are handled by other modules?
118
        $sidebar_facts = $this->module_service
119
            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
120
            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts())
121
            ->flatten();
122
123
        $tab_facts = $this->module_service
124
            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
125
            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts())
126
            ->flatten();
127
128
        // Don't show family meta-data tags
129
        $exclude_facts  = new Collection(['FAM:CHAN', 'FAM:_UID']);
0 ignored issues
show
Bug introduced by
array('FAM:CHAN', 'FAM:_UID') 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

129
        $exclude_facts  = new Collection(/** @scrutinizer ignore-type */ ['FAM:CHAN', 'FAM:_UID']);
Loading history...
130
        // Don't show tags that are shown in tabs or sidebars
131
        $exclude_facts = $exclude_facts->merge($sidebar_facts)->merge($tab_facts);
132
133
        $individual_facts = $this->individual_facts_service->individualFacts($individual, $exclude_facts);
134
        $family_facts     = $this->individual_facts_service->familyFacts($individual, $exclude_facts);
135
        $relative_facts   = $this->individual_facts_service->relativeFacts($individual);
136
        $associate_facts  = $this->individual_facts_service->associateFacts($individual);
137
        $historic_facts   = $this->individual_facts_service->historicFacts($individual);
138
139
        $individual_facts = $individual_facts
140
            ->merge($family_facts)
141
            ->merge($relative_facts)
142
            ->merge($associate_facts)
143
            ->merge($historic_facts);
144
145
        $individual_facts = Fact::sortFacts($individual_facts);
146
147
        // Facts of relatives take the form 1 EVEN / 2 TYPE Event of Individual
148
        // Ensure custom tags from there are recognised
149
        Registry::elementFactory()->registerTags(['INDI:EVEN:CEME' => new CustomElement('Cemetery')]);
150
151
        return view('modules/personal_facts/tab', [
152
            'can_edit'            => $individual->canEdit(),
153
            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
154
            'has_associate_facts' => $associate_facts->isNotEmpty(),
155
            'has_historic_facts'  => $historic_facts->isNotEmpty(),
156
            'has_relative_facts'  => $relative_facts->isNotEmpty(),
157
            'individual'          => $individual,
158
            'facts'               => $individual_facts,
159
        ]);
160
    }
161
162
    /**
163
     * Is this tab empty? If so, we don't always need to display it.
164
     *
165
     * @param Individual $individual
166
     *
167
     * @return bool
168
     */
169
    public function hasTabContent(Individual $individual): bool
170
    {
171
        return true;
172
    }
173
174
    /**
175
     * Can this tab load asynchronously?
176
     *
177
     * @return bool
178
     */
179
    public function canLoadAjax(): bool
180
    {
181
        return false;
182
    }
183
184
    /**
185
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
186
     *
187
     * @return Collection<int,string>
188
     */
189
    public function supportedFacts(): Collection
190
    {
191
        // We don't actually displaye these facts, but they are displayed
192
        // outside the tabs/sidebar systems. This just forces them to be excluded here.
193
        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
0 ignored issues
show
Bug introduced by
array('INDI:NAME', 'INDI:SEX', 'INDI:OBJE') 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

193
        return new Collection(/** @scrutinizer ignore-type */ ['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
Loading history...
194
    }
195
}
196