Passed
Push — main ( aab1cd...10d6d9 )
by Greg
06:18
created

IndividualFactsTabModule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
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\Date;
24
use Fisharebest\Webtrees\Elements\CustomElement;
25
use Fisharebest\Webtrees\Fact;
26
use Fisharebest\Webtrees\Family;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\Individual;
29
use Fisharebest\Webtrees\Registry;
30
use Fisharebest\Webtrees\Services\ClipboardService;
31
use Fisharebest\Webtrees\Services\IndividualFactsService;
32
use Fisharebest\Webtrees\Services\LinkedRecordService;
33
use Fisharebest\Webtrees\Services\ModuleService;
34
use Illuminate\Support\Collection;
35
36
use function explode;
37
use function preg_match;
38
use function preg_replace;
39
use function str_contains;
40
use function str_replace;
41
use function view;
42
43
/**
44
 * Class IndividualFactsTabModule
45
 */
46
class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterface
47
{
48
    use ModuleTabTrait;
49
50
    private ClipboardService $clipboard_service;
51
52
    private LinkedRecordService $linked_record_service;
53
54
    private ModuleService $module_service;
55
56
    /**
57
     * @param ClipboardService       $clipboard_service
58
     * @param IndividualFactsService $individual_facts_service
59
     * @param LinkedRecordService    $linked_record_service
60
     * @param ModuleService          $module_service
61
     */
62
    public function __construct(
63
        ClipboardService $clipboard_service,
64
        IndividualFactsService $individual_facts_service,
65
        LinkedRecordService $linked_record_service,
66
        ModuleService $module_service
67
    ) {
68
        $this->clipboard_service        = $clipboard_service;
69
        $this->individual_facts_service = $individual_facts_service;
0 ignored issues
show
Bug Best Practice introduced by
The property individual_facts_service does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        $this->linked_record_service    = $linked_record_service;
71
        $this->module_service           = $module_service;
72
    }
73
74
    /**
75
     * How should this module be identified in the control panel, etc.?
76
     *
77
     * @return string
78
     */
79
    public function title(): string
80
    {
81
        /* I18N: Name of a module/tab on the individual page. */
82
        return I18N::translate('Facts and events');
83
    }
84
85
    /**
86
     * A sentence describing what this module does.
87
     *
88
     * @return string
89
     */
90
    public function description(): string
91
    {
92
        /* I18N: Description of the “Facts and events” module */
93
        return I18N::translate('A tab showing the facts and events of an individual.');
94
    }
95
96
    /**
97
     * The default position for this tab.  It can be changed in the control panel.
98
     *
99
     * @return int
100
     */
101
    public function defaultTabOrder(): int
102
    {
103
        return 1;
104
    }
105
106
    /**
107
     * A greyed out tab has no actual content, but may perhaps have
108
     * options to create content.
109
     *
110
     * @param Individual $individual
111
     *
112
     * @return bool
113
     */
114
    public function isGrayedOut(Individual $individual): bool
115
    {
116
        return false;
117
    }
118
119
    /**
120
     * Generate the HTML content of this tab.
121
     *
122
     * @param Individual $individual
123
     *
124
     * @return string
125
     */
126
    public function getTabContent(Individual $individual): string
127
    {
128
        // Which facts and events are handled by other modules?
129
        $sidebar_facts = $this->module_service
130
            ->findByComponent(ModuleSidebarInterface::class, $individual->tree(), Auth::user())
131
            ->map(fn (ModuleSidebarInterface $sidebar): Collection => $sidebar->supportedFacts())
132
            ->flatten();
133
134
        $tab_facts = $this->module_service
135
            ->findByComponent(ModuleTabInterface::class, $individual->tree(), Auth::user())
136
            ->map(fn (ModuleTabInterface $tab): Collection => $tab->supportedFacts())
137
            ->flatten();
138
139
        $indi_exclude_facts = $sidebar_facts->merge($tab_facts);
140
        $fam_exclude_facts  = new Collection(['FAM:CHAN', 'FAM:_UID']);
141
142
        $individual_facts = $this->individual_facts_service->individualFacts($individual, $indi_exclude_facts);
143
        $family_facts     = $this->individual_facts_service->familyFacts($individual, $fam_exclude_facts);
144
        $relative_facts   = $this->individual_facts_service->relativeFacts($individual);
145
        $associate_facts  = $this->individual_facts_service->associateFacts($individual);
146
        $historic_facts   = $this->individual_facts_service->historicFacts($individual);
147
148
        $individual_facts = $individual_facts
149
            ->merge($family_facts)
150
            ->merge($relative_facts)
151
            ->merge($associate_facts)
152
            ->merge($historic_facts);
153
154
        $individual_facts = Fact::sortFacts($individual_facts);
155
156
        // Facts of relatives take the form 1 EVEN / 2 TYPE Event of Individual
157
        // Ensure custom tags from there are recognised
158
        Registry::elementFactory()->registerTags(['INDI:EVEN:CEME' => new CustomElement('Cemetery')]);
159
160
        return view('modules/personal_facts/tab', [
161
            'can_edit'            => $individual->canEdit(),
162
            'clipboard_facts'     => $this->clipboard_service->pastableFacts($individual),
163
            'has_associate_facts' => $associate_facts->isNotEmpty(),
164
            'has_historic_facts'  => $historic_facts->isNotEmpty(),
165
            'has_relative_facts'  => $relative_facts->isNotEmpty(),
166
            'individual'          => $individual,
167
            'facts'               => $individual_facts,
168
        ]);
169
    }
170
171
    /**
172
     * Is this tab empty? If so, we don't always need to display it.
173
     *
174
     * @param Individual $individual
175
     *
176
     * @return bool
177
     */
178
    public function hasTabContent(Individual $individual): bool
179
    {
180
        return true;
181
    }
182
183
    /**
184
     * Can this tab load asynchronously?
185
     *
186
     * @return bool
187
     */
188
    public function canLoadAjax(): bool
189
    {
190
        return false;
191
    }
192
193
    /**
194
     * This module handles the following facts - so don't show them on the "Facts and events" tab.
195
     *
196
     * @return Collection<int,string>
197
     */
198
    public function supportedFacts(): Collection
199
    {
200
        // We don't actually displaye these facts, but they are displayed
201
        // outside the tabs/sidebar systems. This just forces them to be excluded here.
202
        return new Collection(['INDI:NAME', 'INDI:SEX', 'INDI:OBJE']);
203
    }
204
}
205