xref: /webtrees/app/Module/IndividualMetadataModule.php (revision 54c7f8df0fc7bddb0bcc1441937c30320ca68563)
18eaf8709SGreg Roach<?php
28eaf8709SGreg Roach/**
38eaf8709SGreg Roach * webtrees: online genealogy
48eaf8709SGreg Roach * Copyright (C) 2019 webtrees development team
58eaf8709SGreg Roach * This program is free software: you can redistribute it and/or modify
68eaf8709SGreg Roach * it under the terms of the GNU General Public License as published by
78eaf8709SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88eaf8709SGreg Roach * (at your option) any later version.
98eaf8709SGreg Roach * This program is distributed in the hope that it will be useful,
108eaf8709SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118eaf8709SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128eaf8709SGreg Roach * GNU General Public License for more details.
138eaf8709SGreg Roach * You should have received a copy of the GNU General Public License
148eaf8709SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158eaf8709SGreg Roach */
168eaf8709SGreg Roachdeclare(strict_types=1);
178eaf8709SGreg Roach
188eaf8709SGreg Roachnamespace Fisharebest\Webtrees\Module;
198eaf8709SGreg Roach
208eaf8709SGreg Roachuse Fisharebest\Webtrees\Fact;
218eaf8709SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrintFacts;
228eaf8709SGreg Roachuse Fisharebest\Webtrees\I18N;
238eaf8709SGreg Roachuse Fisharebest\Webtrees\Individual;
248eaf8709SGreg Roachuse Illuminate\Support\Collection;
258eaf8709SGreg Roach
268eaf8709SGreg Roach/**
278eaf8709SGreg Roach * Class ExtraInformationModule
288eaf8709SGreg Roach * A sidebar to show non-genealogy information about an individual
298eaf8709SGreg Roach */
308eaf8709SGreg Roachclass IndividualMetadataModule extends AbstractModule implements ModuleSidebarInterface
318eaf8709SGreg Roach{
328eaf8709SGreg Roach    use ModuleSidebarTrait;
338eaf8709SGreg Roach
348eaf8709SGreg Roach    // A list of facts that are handled by this module.
358eaf8709SGreg Roach    protected const HANDLED_FACTS = [
368eaf8709SGreg Roach        'AFN',
378eaf8709SGreg Roach        'CHAN',
388eaf8709SGreg Roach        'IDNO',
398eaf8709SGreg Roach        'REFN',
408eaf8709SGreg Roach        'RESN',
418eaf8709SGreg Roach        'RFN',
428eaf8709SGreg Roach        'RIN',
438eaf8709SGreg Roach        'SSN',
448eaf8709SGreg Roach        '_UID',
458eaf8709SGreg Roach    ];
468eaf8709SGreg Roach
478eaf8709SGreg Roach    /**
480cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
498eaf8709SGreg Roach     *
508eaf8709SGreg Roach     * @return string
518eaf8709SGreg Roach     */
528eaf8709SGreg Roach    public function title(): string
538eaf8709SGreg Roach    {
548eaf8709SGreg Roach        /* I18N: Name of a module/sidebar */
558eaf8709SGreg Roach        return I18N::translate('Extra information');
568eaf8709SGreg Roach    }
578eaf8709SGreg Roach
588eaf8709SGreg Roach    /**
598eaf8709SGreg Roach     * A sentence describing what this module does.
608eaf8709SGreg Roach     *
618eaf8709SGreg Roach     * @return string
628eaf8709SGreg Roach     */
638eaf8709SGreg Roach    public function description(): string
648eaf8709SGreg Roach    {
658eaf8709SGreg Roach        /* I18N: Description of the “Extra information” module */
668eaf8709SGreg Roach        return I18N::translate('A sidebar showing non-genealogy information about an individual.');
678eaf8709SGreg Roach    }
688eaf8709SGreg Roach
698eaf8709SGreg Roach    /**
708eaf8709SGreg Roach     * The default position for this sidebar.  It can be changed in the control panel.
718eaf8709SGreg Roach     *
728eaf8709SGreg Roach     * @return int
738eaf8709SGreg Roach     */
748eaf8709SGreg Roach    public function defaultSidebarOrder(): int
758eaf8709SGreg Roach    {
768eaf8709SGreg Roach        return 1;
778eaf8709SGreg Roach    }
788eaf8709SGreg Roach
798eaf8709SGreg Roach    /** {@inheritdoc} */
808eaf8709SGreg Roach    public function hasSidebarContent(Individual $individual): bool
818eaf8709SGreg Roach    {
828eaf8709SGreg Roach        return true;
838eaf8709SGreg Roach    }
848eaf8709SGreg Roach
858eaf8709SGreg Roach    /**
868eaf8709SGreg Roach     * Load this sidebar synchronously.
878eaf8709SGreg Roach     *
888eaf8709SGreg Roach     * @param Individual $individual
898eaf8709SGreg Roach     *
908eaf8709SGreg Roach     * @return string
918eaf8709SGreg Roach     */
928eaf8709SGreg Roach    public function getSidebarContent(Individual $individual): string
938eaf8709SGreg Roach    {
948eaf8709SGreg Roach        $indifacts = [];
958eaf8709SGreg Roach        // The individual’s own facts
968eaf8709SGreg Roach        foreach ($individual->facts() as $fact) {
978eaf8709SGreg Roach            if ($this->showFact($fact)) {
988eaf8709SGreg Roach                $indifacts[] = $fact;
998eaf8709SGreg Roach            }
1008eaf8709SGreg Roach        }
1018eaf8709SGreg Roach
1028eaf8709SGreg Roach        ob_start();
1038eaf8709SGreg Roach        if (!$indifacts) {
1048eaf8709SGreg Roach            echo I18N::translate('There are no facts for this individual.');
1058eaf8709SGreg Roach        } else {
1068eaf8709SGreg Roach            foreach ($indifacts as $fact) {
1078eaf8709SGreg Roach                FunctionsPrintFacts::printFact($fact, $individual);
1088eaf8709SGreg Roach            }
1098eaf8709SGreg Roach        }
1108eaf8709SGreg Roach
1118eaf8709SGreg Roach        return strip_tags(ob_get_clean(), '<a><div><span>');
1128eaf8709SGreg Roach    }
1138eaf8709SGreg Roach
1148eaf8709SGreg Roach    /**
1158eaf8709SGreg Roach     * Does this module display a particular fact
1168eaf8709SGreg Roach     *
1178eaf8709SGreg Roach     * @param Fact $fact
1188eaf8709SGreg Roach     *
1198eaf8709SGreg Roach     * @return bool
1208eaf8709SGreg Roach     */
1218eaf8709SGreg Roach    public function showFact(Fact $fact)
1228eaf8709SGreg Roach    {
1238eaf8709SGreg Roach        return in_array($fact->getTag(), static::HANDLED_FACTS);
1248eaf8709SGreg Roach    }
1258eaf8709SGreg Roach
1268eaf8709SGreg Roach    /**
1278eaf8709SGreg Roach     * This module handles the following facts - so don't show them on the "Facts and events" tab.
1288eaf8709SGreg Roach     *
129*54c7f8dfSGreg Roach     * @return Collection
130*54c7f8dfSGreg Roach     * @return string[]
1318eaf8709SGreg Roach     */
1328eaf8709SGreg Roach    public function supportedFacts(): Collection
1338eaf8709SGreg Roach    {
1348eaf8709SGreg Roach        return new Collection(static::HANDLED_FACTS);
1358eaf8709SGreg Roach    }
1368eaf8709SGreg Roach}
137