xref: /webtrees/app/Module/ModuleHistoricEventsTrait.php (revision 0b5fd0a636fa959f5279ee28ebd2f27e921c091e)
117c50b57SGreg Roach<?php
217c50b57SGreg Roach/**
317c50b57SGreg Roach * webtrees: online genealogy
417c50b57SGreg Roach * Copyright (C) 2019 webtrees development team
517c50b57SGreg Roach * This program is free software: you can redistribute it and/or modify
617c50b57SGreg Roach * it under the terms of the GNU General Public License as published by
717c50b57SGreg Roach * the Free Software Foundation, either version 3 of the License, or
817c50b57SGreg Roach * (at your option) any later version.
917c50b57SGreg Roach * This program is distributed in the hope that it will be useful,
1017c50b57SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1117c50b57SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1217c50b57SGreg Roach * GNU General Public License for more details.
1317c50b57SGreg Roach * You should have received a copy of the GNU General Public License
1417c50b57SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1517c50b57SGreg Roach */
1617c50b57SGreg Roachdeclare(strict_types=1);
1717c50b57SGreg Roach
1817c50b57SGreg Roachnamespace Fisharebest\Webtrees\Module;
1917c50b57SGreg Roach
2017c50b57SGreg Roachuse Fisharebest\Webtrees\Date;
2117c50b57SGreg Roachuse Fisharebest\Webtrees\Fact;
2217c50b57SGreg Roachuse Fisharebest\Webtrees\I18N;
2317c50b57SGreg Roachuse Fisharebest\Webtrees\Individual;
2417c50b57SGreg Roachuse Illuminate\Support\Collection;
2517c50b57SGreg Roach
2617c50b57SGreg Roach/**
2717c50b57SGreg Roach * Trait ModuleHistoricEventsTrait - Show historic events on an individual‘s page
2817c50b57SGreg Roach */
2917c50b57SGreg Roachtrait ModuleHistoricEventsTrait
3017c50b57SGreg Roach{
3117c50b57SGreg Roach    /**
3217c50b57SGreg Roach     * A sentence describing what this module does.
3317c50b57SGreg Roach     *
3417c50b57SGreg Roach     * @return string
3517c50b57SGreg Roach     */
3617c50b57SGreg Roach    public function description(): string
3717c50b57SGreg Roach    {
3817c50b57SGreg Roach        return I18N::translate('Add historic events to an individual‘s page.');
3917c50b57SGreg Roach    }
4017c50b57SGreg Roach
4117c50b57SGreg Roach    /**
4217c50b57SGreg Roach     * All events provided by this module.
4317c50b57SGreg Roach     *
4417c50b57SGreg Roach     * @return string[]
4517c50b57SGreg Roach     */
4617c50b57SGreg Roach    public function historicEventsAll(): array
4717c50b57SGreg Roach    {
4817c50b57SGreg Roach        return [
4917c50b57SGreg Roach            "1 EVEN foo\n2 TYPE bar\n2 DATE FROM 6 FEB 1952"
5017c50b57SGreg Roach        ];
5117c50b57SGreg Roach    }
5217c50b57SGreg Roach
5317c50b57SGreg Roach    /**
5417c50b57SGreg Roach     * Which events should we show for an individual?
5517c50b57SGreg Roach     *
5617c50b57SGreg Roach     * @param Individual $individual
5717c50b57SGreg Roach     *
5817c50b57SGreg Roach     * @return Collection
5917c50b57SGreg Roach     */
6017c50b57SGreg Roach    public function historicEventsForIndividual(Individual $individual): Collection
6117c50b57SGreg Roach    {
6217c50b57SGreg Roach        $min_date = $individual->getEstimatedBirthDate();
6317c50b57SGreg Roach        $max_date = $individual->getEstimatedDeathDate();
6417c50b57SGreg Roach
6517c50b57SGreg Roach        return (new Collection($this->historicEventsAll()))
66*0b5fd0a6SGreg Roach            ->map(static function (string $gedcom) use ($individual): Fact {
6717c50b57SGreg Roach                return new Fact($gedcom, $individual, 'histo');
6817c50b57SGreg Roach            })
69*0b5fd0a6SGreg Roach            ->filter(static function (Fact $fact) use ($min_date, $max_date): bool {
7017c50b57SGreg Roach                return Date::compare($fact->date(), $min_date) >= 0 && Date::compare($fact->date(), $max_date) <= 0;
7117c50b57SGreg Roach            });
7217c50b57SGreg Roach    }
7317c50b57SGreg Roach}
74