xref: /webtrees/app/Module/IndividualMetadataModule.php (revision ad7270802ba57a28d195fc58fcc55f26458a779a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Fact;
21use Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Illuminate\Support\Collection;
25
26/**
27 * Class ExtraInformationModule
28 * A sidebar to show non-genealogy information about an individual
29 */
30class IndividualMetadataModule extends AbstractModule implements ModuleSidebarInterface
31{
32    use ModuleSidebarTrait;
33
34    // A list of facts that are handled by this module.
35    protected const HANDLED_FACTS = [
36        'AFN',
37        'CHAN',
38        'IDNO',
39        'REFN',
40        'RESN',
41        'RFN',
42        'RIN',
43        'SSN',
44        '_UID',
45    ];
46
47    /**
48     * How should this module be labelled on tabs, menus, etc.?
49     *
50     * @return string
51     */
52    public function title(): string
53    {
54        /* I18N: Name of a module/sidebar */
55        return I18N::translate('Extra information');
56    }
57
58    /**
59     * A sentence describing what this module does.
60     *
61     * @return string
62     */
63    public function description(): string
64    {
65        /* I18N: Description of the “Extra information” module */
66        return I18N::translate('A sidebar showing non-genealogy information about an individual.');
67    }
68
69    /**
70     * The default position for this sidebar.  It can be changed in the control panel.
71     *
72     * @return int
73     */
74    public function defaultSidebarOrder(): int
75    {
76        return 1;
77    }
78
79    /** {@inheritdoc} */
80    public function hasSidebarContent(Individual $individual): bool
81    {
82        return true;
83    }
84
85    /**
86     * Load this sidebar synchronously.
87     *
88     * @param Individual $individual
89     *
90     * @return string
91     */
92    public function getSidebarContent(Individual $individual): string
93    {
94        $indifacts = [];
95        // The individual’s own facts
96        foreach ($individual->facts() as $fact) {
97            if ($this->showFact($fact)) {
98                $indifacts[] = $fact;
99            }
100        }
101
102        ob_start();
103        if (!$indifacts) {
104            echo I18N::translate('There are no facts for this individual.');
105        } else {
106            foreach ($indifacts as $fact) {
107                FunctionsPrintFacts::printFact($fact, $individual);
108            }
109        }
110
111        return strip_tags(ob_get_clean(), '<a><div><span>');
112    }
113
114    /**
115     * Does this module display a particular fact
116     *
117     * @param Fact $fact
118     *
119     * @return bool
120     */
121    public function showFact(Fact $fact)
122    {
123        return in_array($fact->getTag(), static::HANDLED_FACTS);
124    }
125
126    /**
127     * This module handles the following facts - so don't show them on the "Facts and events" tab.
128     *
129     * @return Collection|string[]
130     */
131    public function supportedFacts(): Collection
132    {
133        return new Collection(static::HANDLED_FACTS);
134    }
135}
136