diff options
| author | Greg Roach <fisharebest@webtrees.net> | 2019-01-26 12:55:50 +0000 |
|---|---|---|
| committer | Greg Roach <fisharebest@webtrees.net> | 2019-01-26 12:55:50 +0000 |
| commit | 17c50b57b14adeaa197eed2530436177c4db8f6b (patch) | |
| tree | feae904dbad2443ad6696946202868bcf78f76b1 | |
| parent | 9156780be8836f84cbba0ceaf5f52fd1086855e0 (diff) | |
| download | webtrees-17c50b57b14adeaa197eed2530436177c4db8f6b.tar.gz webtrees-17c50b57b14adeaa197eed2530436177c4db8f6b.tar.bz2 webtrees-17c50b57b14adeaa197eed2530436177c4db8f6b.zip | |
Create module interface for historic events
| -rw-r--r-- | app/Module.php | 6 | ||||
| -rw-r--r-- | app/Module/IndividualFactsTabModule.php | 30 | ||||
| -rw-r--r-- | app/Module/ModuleAnalyticsInterface.php | 2 | ||||
| -rw-r--r-- | app/Module/ModuleHistoricEventsInterface.php | 45 | ||||
| -rw-r--r-- | app/Module/ModuleHistoricEventsTrait.php | 75 |
5 files changed, 134 insertions, 24 deletions
diff --git a/app/Module.php b/app/Module.php index 7db9ad73f3..76dd1ed764 100644 --- a/app/Module.php +++ b/app/Module.php @@ -243,14 +243,16 @@ class Module ->filter(function (string $filename): bool { // Special characters will break PHP variable names. // This also allows us to ignore modules called "foo.example" and "foo.disable" - return !Str::contains(basename(dirname($filename)), ['.', ' ', '[', ']']); + $module_name = basename(dirname($filename)); + + return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30; }) ->map(function (string $filename): ?ModuleCustomInterface { try { $module = self::load($filename); if ($module instanceof ModuleCustomInterface) { - $module_name = 'custom-' . basename(dirname($filename)); + $module_name = '_' . basename(dirname($filename)) . '_'; $module->setName($module_name); } else { diff --git a/app/Module/IndividualFactsTabModule.php b/app/Module/IndividualFactsTabModule.php index 29ee5e120c..50b6cc740c 100644 --- a/app/Module/IndividualFactsTabModule.php +++ b/app/Module/IndividualFactsTabModule.php @@ -27,6 +27,7 @@ use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Module; use Fisharebest\Webtrees\Site; +use Illuminate\Support\Collection; /** * Class IndividualFactsTabModule @@ -139,7 +140,7 @@ class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterf $parent_facts = self::parentFacts($individual, 1, $min_date, $max_date); $associate_facts = self::associateFacts($individual); - $historical_facts = self::historicalFacts($individual, $min_date, $max_date); + $historical_facts = self::historicalFacts($individual); $indifacts = array_merge($indifacts, $parent_facts, $associate_facts, $historical_facts); @@ -440,29 +441,18 @@ class IndividualFactsTabModule extends AbstractModule implements ModuleTabInterf /** * Get any historical events. * - * @param Individual $person - * @param Date $min_date - * @param Date $max_date + * @param Individual $individual * * @return Fact[] */ - private static function historicalFacts(Individual $person, Date $min_date, Date $max_date): array + private static function historicalFacts(Individual $individual): array { - $facts = []; - - if (file_exists(Site::getPreference('INDEX_DIRECTORY') . 'histo.' . WT_LOCALE . '.php')) { - $histo = []; - require Site::getPreference('INDEX_DIRECTORY') . 'histo.' . WT_LOCALE . '.php'; - foreach ($histo as $hist) { - $fact = new Fact($hist, $person, 'histo'); - - if (self::includeFact($fact, $min_date, $max_date)) { - $facts[] = $fact; - } - } - } - - return $facts; + return Module::findByInterface(ModuleHistoricEventsInterface::class) + ->map(function (ModuleHistoricEventsInterface $module) use ($individual): Collection { + return $module->historicEventsForIndividual($individual); + }) + ->flatten() + ->all(); } /** diff --git a/app/Module/ModuleAnalyticsInterface.php b/app/Module/ModuleAnalyticsInterface.php index dbd16cb11b..f943b5ce8c 100644 --- a/app/Module/ModuleAnalyticsInterface.php +++ b/app/Module/ModuleAnalyticsInterface.php @@ -17,8 +17,6 @@ declare(strict_types=1); namespace Fisharebest\Webtrees\Module; -use Symfony\Component\HttpFoundation\Request; - /** * Interface ModuleAnalyticsInterface - Classes and libraries for module system */ diff --git a/app/Module/ModuleHistoricEventsInterface.php b/app/Module/ModuleHistoricEventsInterface.php new file mode 100644 index 0000000000..e6d5545868 --- /dev/null +++ b/app/Module/ModuleHistoricEventsInterface.php @@ -0,0 +1,45 @@ +<?php +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Module; + +use Fisharebest\Webtrees\Individual; +use Illuminate\Support\Collection; + +/** + * Interface ModuleHistoricEventsInterface - Show historic facts on an individual‘s page + */ +interface ModuleHistoricEventsInterface extends ModuleInterface +{ + /** + * All events provided by this module. + * + * @param Individual $individual + * + * @return string[] + */ + public function historicEventsAll(): array; + + /** + * Which events should we show for an individual? + * + * @param Individual $individual + * + * @return Collection + */ + public function historicEventsForIndividual(Individual $individual): Collection; +} diff --git a/app/Module/ModuleHistoricEventsTrait.php b/app/Module/ModuleHistoricEventsTrait.php new file mode 100644 index 0000000000..490217fdde --- /dev/null +++ b/app/Module/ModuleHistoricEventsTrait.php @@ -0,0 +1,75 @@ +<?php +/** + * webtrees: online genealogy + * Copyright (C) 2019 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +declare(strict_types=1); + +namespace Fisharebest\Webtrees\Module; + +use Fisharebest\Webtrees\Date; +use Fisharebest\Webtrees\Fact; +use Fisharebest\Webtrees\I18N; +use Fisharebest\Webtrees\Individual; +use Illuminate\Support\Collection; + +/** + * Trait ModuleHistoricEventsTrait - Show historic events on an individual‘s page + */ +trait ModuleHistoricEventsTrait +{ + /** + * A sentence describing what this module does. + * + * @return string + */ + public function description(): string + { + return I18N::translate('Add historic events to an individual‘s page.'); + } + + /** + * All events provided by this module. + * + * @param Individual $individual + * + * @return string[] + */ + public function historicEventsAll(): array + { + return [ + "1 EVEN foo\n2 TYPE bar\n2 DATE FROM 6 FEB 1952" + ]; + } + + /** + * Which events should we show for an individual? + * + * @param Individual $individual + * + * @return Collection + */ + public function historicEventsForIndividual(Individual $individual): Collection + { + $min_date = $individual->getEstimatedBirthDate(); + $max_date = $individual->getEstimatedDeathDate(); + + return (new Collection($this->historicEventsAll())) + ->map(function (string $gedcom) use ($individual): Fact { + return new Fact($gedcom, $individual, 'histo'); + }) + ->filter(function (Fact $fact) use ($min_date, $max_date): bool { + return Date::compare($fact->date(), $min_date) >= 0 && Date::compare($fact->date(), $max_date) <= 0; + }); + } +} |
