xref: /webtrees/app/Services/ModuleService.php (revision 8d6560c40d2d2d26dd23f877bd58f736e0388d8f)
14ca7e03cSGreg Roach<?php
24ca7e03cSGreg Roach/**
34ca7e03cSGreg Roach * webtrees: online genealogy
44ca7e03cSGreg Roach * Copyright (C) 2019 webtrees development team
54ca7e03cSGreg Roach * This program is free software: you can redistribute it and/or modify
64ca7e03cSGreg Roach * it under the terms of the GNU General Public License as published by
74ca7e03cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ca7e03cSGreg Roach * (at your option) any later version.
94ca7e03cSGreg Roach * This program is distributed in the hope that it will be useful,
104ca7e03cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ca7e03cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ca7e03cSGreg Roach * GNU General Public License for more details.
134ca7e03cSGreg Roach * You should have received a copy of the GNU General Public License
144ca7e03cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ca7e03cSGreg Roach */
164ca7e03cSGreg Roachdeclare(strict_types=1);
174ca7e03cSGreg Roach
184ca7e03cSGreg Roachnamespace Fisharebest\Webtrees\Services;
194ca7e03cSGreg Roach
204ca7e03cSGreg Roachuse Closure;
214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Auth;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\I18N;
244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AhnentafelReportModule;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AlbumModule;
264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AncestorsChartModule;
274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BatchUpdateModule;
284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthReportModule;
3167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\BranchesListModule;
3209fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishMonarchs;
3309fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishPrimeMinisters;
3409fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishSocialHistory;
354ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CalendarMenuModule;
364ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CemeteryReportModule;
374ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CensusAssistantModule;
384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChangeReportModule;
394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsBlockModule;
404ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsMenuModule;
414ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CkeditorModule;
424ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ClippingsCartModule;
434ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CloudsTheme;
444ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ColorsTheme;
454ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CompactTreeChartModule;
464ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ContactsFooterModule;
474ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CookieWarningModule;
48*8d6560c4SGreg Roachuse Fisharebest\Webtrees\Module\CustomCssJsModule;
494ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DeathReportModule;
504ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyChartModule;
514ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyModule;
524ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyReportModule;
534ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FabTheme;
544ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FactSourcesReportModule;
554ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyBookChartModule;
564ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyGroupReportModule;
5767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\FamilyListModule;
584ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyNavigatorModule;
594ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
604ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
614ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
624ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FanChartModule;
634ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
644ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
654ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
664ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HitCountFooterModule;
674ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HourglassChartModule;
684ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HtmlBlockModule;
694ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFactsTabModule;
704ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
7167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\IndividualListModule;
7209fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\IndividualMetadataModule;
734ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualReportModule;
744ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTreeModule;
754ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LifespansChartModule;
764ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ListsMenuModule;
774ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoggedInUsersModule;
784ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoginBlockModule;
794ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MarriageReportModule;
804ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
8167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\MediaListModule;
824ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MediaTabModule;
834ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MinimalTheme;
844ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MissingFactsReportModule;
85bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
864ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
874ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
88bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleConfigInterface;
894ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface;
904ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleFooterInterface;
91bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
924ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
93bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
9467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleListInterface;
954ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
964ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
974ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
984ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
99bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
10067992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\NoteListModule;
1014ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\NotesTabModule;
1024ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OccupationReportModule;
1034ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OnThisDayModule;
1044ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeChartModule;
1054ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeMapModule;
1064ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeReportModule;
10767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
1084ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PlacesModule;
1094ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
1104ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RecentChangesModule;
1114ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
1124ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelationshipsChartModule;
1134ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelativesTabModule;
1144ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReportsMenuModule;
11567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\RepositoryListModule;
1164ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ResearchTaskModule;
1174ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReviewChangesModule;
1184ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SearchMenuModule;
1194ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule;
1204ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SlideShowModule;
12167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\SourceListModule;
1224ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SourcesTabModule;
1234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatcounterModule;
1244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatisticsChartModule;
1254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StoriesModule;
1264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ThemeSelectModule;
1274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TimelineChartModule;
1284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopGivenNamesModule;
1294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopPageViewsModule;
1304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopSurnamesModule;
1314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TreesMenuModule;
1324ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
1334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserFavoritesModule;
1344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserJournalModule;
1354ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserMessagesModule;
1364ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserWelcomeModule;
13709fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\USPresidents;
1384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
1394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WelcomeBlockModule;
1404ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\XeneaTheme;
1414ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\YahrzeitModule;
1424ca7e03cSGreg Roachuse Fisharebest\Webtrees\Tree;
1434ca7e03cSGreg Roachuse Fisharebest\Webtrees\Webtrees;
1444ca7e03cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
1454ca7e03cSGreg Roachuse Illuminate\Support\Collection;
1464ca7e03cSGreg Roachuse Illuminate\Support\Str;
1474ca7e03cSGreg Roachuse stdClass;
1484ca7e03cSGreg Roachuse Throwable;
1494ca7e03cSGreg Roach
1504ca7e03cSGreg Roach/**
1514ca7e03cSGreg Roach * Functions for managing and maintaining modules.
1524ca7e03cSGreg Roach */
1534ca7e03cSGreg Roachclass ModuleService
1544ca7e03cSGreg Roach{
155362b8464SGreg Roach    // Components are pieces of user-facing functionality, are managed together in the control panel.
1564ca7e03cSGreg Roach    private const COMPONENTS = [
15787cca37cSGreg Roach        ModuleAnalyticsInterface::class,
15887cca37cSGreg Roach        ModuleBlockInterface::class,
15987cca37cSGreg Roach        ModuleChartInterface::class,
16087cca37cSGreg Roach        ModuleFooterInterface::class,
16187cca37cSGreg Roach        ModuleHistoricEventsInterface::class,
16287cca37cSGreg Roach        ModuleLanguageInterface::class,
16387cca37cSGreg Roach        ModuleListInterface::class,
16487cca37cSGreg Roach        ModuleMenuInterface::class,
16587cca37cSGreg Roach        ModuleReportInterface::class,
16687cca37cSGreg Roach        ModuleSidebarInterface::class,
16787cca37cSGreg Roach        ModuleTabInterface::class,
16887cca37cSGreg Roach        ModuleThemeInterface::class,
1694ca7e03cSGreg Roach    ];
1704ca7e03cSGreg Roach
1714ca7e03cSGreg Roach    // Array keys are module names, and should match module names from earlier versions of webtrees.
1724ca7e03cSGreg Roach    private const CORE_MODULES = [
1734ca7e03cSGreg Roach        'GEDFact_assistant'       => CensusAssistantModule::class,
1744ca7e03cSGreg Roach        'ahnentafel_report'       => AhnentafelReportModule::class,
1754ca7e03cSGreg Roach        'ancestors_chart'         => AncestorsChartModule::class,
1764ca7e03cSGreg Roach        'batch_update'            => BatchUpdateModule::class,
1774ca7e03cSGreg Roach        'bdm_report'              => BirthDeathMarriageReportModule::class,
1784ca7e03cSGreg Roach        'bing-webmaster-tools'    => BingWebmasterToolsModule::class,
1794ca7e03cSGreg Roach        'birth_report'            => BirthReportModule::class,
18067992b6aSRichard Cissee        'branches_list'           => BranchesListModule::class,
18109fbf3e5SGreg Roach        'british-monarchs'        => BritishMonarchs::class,
18209fbf3e5SGreg Roach        'british-prime-ministers' => BritishPrimeMinisters::class,
18309fbf3e5SGreg Roach        'british-social-history'  => BritishSocialHistory::class,
1844ca7e03cSGreg Roach        'calendar-menu'           => CalendarMenuModule::class,
1854ca7e03cSGreg Roach        'cemetery_report'         => CemeteryReportModule::class,
1864ca7e03cSGreg Roach        'change_report'           => ChangeReportModule::class,
1874ca7e03cSGreg Roach        'charts'                  => ChartsBlockModule::class,
1884ca7e03cSGreg Roach        'charts-menu'             => ChartsMenuModule::class,
1894ca7e03cSGreg Roach        'ckeditor'                => CkeditorModule::class,
1904ca7e03cSGreg Roach        'clippings'               => ClippingsCartModule::class,
1914ca7e03cSGreg Roach        'clouds'                  => CloudsTheme::class,
1924ca7e03cSGreg Roach        'colors'                  => ColorsTheme::class,
1934ca7e03cSGreg Roach        'compact-chart'           => CompactTreeChartModule::class,
1944ca7e03cSGreg Roach        'contact-links'           => ContactsFooterModule::class,
1954ca7e03cSGreg Roach        'cookie-warning'          => CookieWarningModule::class,
196*8d6560c4SGreg Roach        'custom-css-js'           => CustomCssJsModule::class,
1974ca7e03cSGreg Roach        'death_report'            => DeathReportModule::class,
1984ca7e03cSGreg Roach        'descendancy'             => DescendancyModule::class,
1994ca7e03cSGreg Roach        'descendancy_chart'       => DescendancyChartModule::class,
2004ca7e03cSGreg Roach        'descendancy_report'      => DescendancyReportModule::class,
2018eaf8709SGreg Roach        'extra_info'              => IndividualMetadataModule::class,
2024ca7e03cSGreg Roach        'fab'                     => FabTheme::class,
2034ca7e03cSGreg Roach        'fact_sources'            => FactSourcesReportModule::class,
2044ca7e03cSGreg Roach        'family_book_chart'       => FamilyBookChartModule::class,
2054ca7e03cSGreg Roach        'family_group_report'     => FamilyGroupReportModule::class,
20667992b6aSRichard Cissee        'family_list'             => FamilyListModule::class,
2074ca7e03cSGreg Roach        'family_nav'              => FamilyNavigatorModule::class,
2084ca7e03cSGreg Roach        'fan_chart'               => FanChartModule::class,
2094ca7e03cSGreg Roach        'faq'                     => FrequentlyAskedQuestionsModule::class,
2104ca7e03cSGreg Roach        'gedcom_block'            => WelcomeBlockModule::class,
2114ca7e03cSGreg Roach        'gedcom_favorites'        => FamilyTreeFavoritesModule::class,
2124ca7e03cSGreg Roach        'gedcom_news'             => FamilyTreeNewsModule::class,
2134ca7e03cSGreg Roach        'gedcom_stats'            => FamilyTreeStatisticsModule::class,
2144ca7e03cSGreg Roach        'google-analytics'        => GoogleAnalyticsModule::class,
2154ca7e03cSGreg Roach        'google-webmaster-tools'  => GoogleWebmasterToolsModule::class,
2164ca7e03cSGreg Roach        'hit-counter'             => HitCountFooterModule::class,
2174ca7e03cSGreg Roach        'hourglass_chart'         => HourglassChartModule::class,
2184ca7e03cSGreg Roach        'html'                    => HtmlBlockModule::class,
2194ca7e03cSGreg Roach        'individual_ext_report'   => IndividualFamiliesReportModule::class,
22067992b6aSRichard Cissee        'individual_list'         => IndividualListModule::class,
2214ca7e03cSGreg Roach        'individual_report'       => IndividualReportModule::class,
2224ca7e03cSGreg Roach        'lifespans_chart'         => LifespansChartModule::class,
2234ca7e03cSGreg Roach        'lightbox'                => AlbumModule::class,
2244ca7e03cSGreg Roach        'lists-menu'              => ListsMenuModule::class,
2254ca7e03cSGreg Roach        'logged_in'               => LoggedInUsersModule::class,
2264ca7e03cSGreg Roach        'login_block'             => LoginBlockModule::class,
2274ca7e03cSGreg Roach        'marriage_report'         => MarriageReportModule::class,
2284ca7e03cSGreg Roach        'matomo-analytics'        => MatomoAnalyticsModule::class,
2294ca7e03cSGreg Roach        'media'                   => MediaTabModule::class,
23067992b6aSRichard Cissee        'media_list'              => MediaListModule::class,
2314ca7e03cSGreg Roach        'minimal'                 => MinimalTheme::class,
2324ca7e03cSGreg Roach        'missing_facts_report'    => MissingFactsReportModule::class,
2334ca7e03cSGreg Roach        'notes'                   => NotesTabModule::class,
23467992b6aSRichard Cissee        'note_list'               => NoteListModule::class,
2354ca7e03cSGreg Roach        'occupation_report'       => OccupationReportModule::class,
2364ca7e03cSGreg Roach        'pedigree-map'            => PedigreeMapModule::class,
2374ca7e03cSGreg Roach        'pedigree_chart'          => PedigreeChartModule::class,
2384ca7e03cSGreg Roach        'pedigree_report'         => PedigreeReportModule::class,
2394ca7e03cSGreg Roach        'personal_facts'          => IndividualFactsTabModule::class,
2404ca7e03cSGreg Roach        'places'                  => PlacesModule::class,
24167992b6aSRichard Cissee        'places_list'             => PlaceHierarchyListModule::class,
2424ca7e03cSGreg Roach        'powered-by-webtrees'     => PoweredByWebtreesModule::class,
2434ca7e03cSGreg Roach        'random_media'            => SlideShowModule::class,
2444ca7e03cSGreg Roach        'recent_changes'          => RecentChangesModule::class,
2454ca7e03cSGreg Roach        'relationships_chart'     => RelationshipsChartModule::class,
2464ca7e03cSGreg Roach        'relative_ext_report'     => RelatedIndividualsReportModule::class,
2474ca7e03cSGreg Roach        'relatives'               => RelativesTabModule::class,
2484ca7e03cSGreg Roach        'reports-menu'            => ReportsMenuModule::class,
24967992b6aSRichard Cissee        'repository_list'         => RepositoryListModule::class,
2504ca7e03cSGreg Roach        'review_changes'          => ReviewChangesModule::class,
2514ca7e03cSGreg Roach        'search-menu'             => SearchMenuModule::class,
2524ca7e03cSGreg Roach        'sitemap'                 => SiteMapModule::class,
25367992b6aSRichard Cissee        'source_list'             => SourceListModule::class,
2544ca7e03cSGreg Roach        'sources_tab'             => SourcesTabModule::class,
2554ca7e03cSGreg Roach        'statcounter'             => StatcounterModule::class,
2564ca7e03cSGreg Roach        'statistics_chart'        => StatisticsChartModule::class,
2574ca7e03cSGreg Roach        'stories'                 => StoriesModule::class,
2584ca7e03cSGreg Roach        'theme_select'            => ThemeSelectModule::class,
2594ca7e03cSGreg Roach        'timeline_chart'          => TimelineChartModule::class,
2604ca7e03cSGreg Roach        'todays_events'           => OnThisDayModule::class,
2614ca7e03cSGreg Roach        'todo'                    => ResearchTaskModule::class,
2624ca7e03cSGreg Roach        'top10_givnnames'         => TopGivenNamesModule::class,
2634ca7e03cSGreg Roach        'top10_pageviews'         => TopPageViewsModule::class,
2644ca7e03cSGreg Roach        'top10_surnames'          => TopSurnamesModule::class,
2654ca7e03cSGreg Roach        'tree'                    => InteractiveTreeModule::class,
2664ca7e03cSGreg Roach        'trees-menu'              => TreesMenuModule::class,
2674ca7e03cSGreg Roach        'upcoming_events'         => UpcomingAnniversariesModule::class,
26809fbf3e5SGreg Roach        'us-presidents'           => USPresidents::class,
2694ca7e03cSGreg Roach        'user_blog'               => UserJournalModule::class,
2704ca7e03cSGreg Roach        'user_favorites'          => UserFavoritesModule::class,
2714ca7e03cSGreg Roach        'user_messages'           => UserMessagesModule::class,
2724ca7e03cSGreg Roach        'user_welcome'            => UserWelcomeModule::class,
2734ca7e03cSGreg Roach        'webtrees'                => WebtreesTheme::class,
2744ca7e03cSGreg Roach        'xenea'                   => XeneaTheme::class,
2754ca7e03cSGreg Roach        'yahrzeit'                => YahrzeitModule::class,
2764ca7e03cSGreg Roach    ];
2774ca7e03cSGreg Roach
2784ca7e03cSGreg Roach    /**
279dedcf571SGreg Roach     * A function to convert modules into their titles - to create option lists, etc.
280dedcf571SGreg Roach     *
281dedcf571SGreg Roach     * @return Closure
282dedcf571SGreg Roach     */
283dedcf571SGreg Roach    public function titleMapper(): Closure
284dedcf571SGreg Roach    {
285dedcf571SGreg Roach        return function (ModuleInterface $module): string {
286dedcf571SGreg Roach            return $module->title();
287dedcf571SGreg Roach        };
288dedcf571SGreg Roach    }
289dedcf571SGreg Roach
290dedcf571SGreg Roach    /**
2914ca7e03cSGreg Roach     * Modules which (a) provide a specific function and (b) we have permission to see.
2924ca7e03cSGreg Roach     *
29387cca37cSGreg Roach     * @param string        $interface
2944ca7e03cSGreg Roach     * @param Tree          $tree
295e5a6b4d4SGreg Roach     * @param UserInterface $user
2964ca7e03cSGreg Roach     *
29754c7f8dfSGreg Roach     * @return Collection
29854c7f8dfSGreg Roach     * @return ModuleInterface[]
2994ca7e03cSGreg Roach     */
30087cca37cSGreg Roach    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
3014ca7e03cSGreg Roach    {
302b5979037SGreg Roach        return $this->findByInterface($interface, false, true)
30387cca37cSGreg Roach            ->filter(function (ModuleInterface $module) use ($interface, $tree, $user): bool {
30487cca37cSGreg Roach                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
3054ca7e03cSGreg Roach            });
3064ca7e03cSGreg Roach    }
3074ca7e03cSGreg Roach
3084ca7e03cSGreg Roach    /**
3094ca7e03cSGreg Roach     * All modules which provide a specific function.
3104ca7e03cSGreg Roach     *
3114ca7e03cSGreg Roach     * @param string $interface
3124ca7e03cSGreg Roach     * @param bool   $include_disabled
313b5979037SGreg Roach     * @param bool   $sort
3144ca7e03cSGreg Roach     *
31554c7f8dfSGreg Roach     * @return Collection
31654c7f8dfSGreg Roach     * @return ModuleInterface[]
3174ca7e03cSGreg Roach     */
318b5979037SGreg Roach    public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection
3194ca7e03cSGreg Roach    {
3209d627a9eSGreg Roach        $modules = $this->all($include_disabled)
3219d627a9eSGreg Roach            ->filter($this->interfaceFilter($interface));
3224ca7e03cSGreg Roach
3234ca7e03cSGreg Roach        switch ($interface) {
3244ca7e03cSGreg Roach            case ModuleFooterInterface::class:
325bf57b580SGreg Roach                return $modules->sort($this->footerSorter());
3264ca7e03cSGreg Roach
3274ca7e03cSGreg Roach            case ModuleMenuInterface::class:
328bf57b580SGreg Roach                return $modules->sort($this->menuSorter());
3294ca7e03cSGreg Roach
3304ca7e03cSGreg Roach            case ModuleSidebarInterface::class:
331bf57b580SGreg Roach                return $modules->sort($this->sidebarSorter());
3324ca7e03cSGreg Roach
3334ca7e03cSGreg Roach            case ModuleTabInterface::class:
334bf57b580SGreg Roach                return $modules->sort($this->tabSorter());
335b5979037SGreg Roach
336b5979037SGreg Roach            default:
337b5979037SGreg Roach                if ($sort) {
338b5979037SGreg Roach                    return $modules->sort($this->moduleSorter());
3394ca7e03cSGreg Roach                }
3404ca7e03cSGreg Roach
3414ca7e03cSGreg Roach                return $modules;
3424ca7e03cSGreg Roach        }
343b5979037SGreg Roach    }
3444ca7e03cSGreg Roach
3454ca7e03cSGreg Roach    /**
346*8d6560c4SGreg Roach     * All modules.
347*8d6560c4SGreg Roach     *
348*8d6560c4SGreg Roach     * @param bool $include_disabled
349*8d6560c4SGreg Roach     *
350*8d6560c4SGreg Roach     * @return Collection
351*8d6560c4SGreg Roach     * @return ModuleInterface[]
352*8d6560c4SGreg Roach     */
353*8d6560c4SGreg Roach    public function all(bool $include_disabled = false): Collection
354*8d6560c4SGreg Roach    {
355*8d6560c4SGreg Roach        return app('cache.array')->rememberForever('all_modules', function (): Collection {
356*8d6560c4SGreg Roach            // Modules have a default status, order etc.
357*8d6560c4SGreg Roach            // We can override these from database settings.
358*8d6560c4SGreg Roach            $module_info = DB::table('module')
359*8d6560c4SGreg Roach                ->get()
360*8d6560c4SGreg Roach                ->mapWithKeys(function (stdClass $row): array {
361*8d6560c4SGreg Roach                    return [$row->module_name => $row];
362*8d6560c4SGreg Roach                });
363*8d6560c4SGreg Roach
364*8d6560c4SGreg Roach            return $this->coreModules()
365*8d6560c4SGreg Roach                ->merge($this->customModules())
366*8d6560c4SGreg Roach                ->map(function (ModuleInterface $module) use ($module_info): ModuleInterface {
367*8d6560c4SGreg Roach                    $info = $module_info->get($module->name());
368*8d6560c4SGreg Roach
369*8d6560c4SGreg Roach                    if ($info instanceof stdClass) {
370*8d6560c4SGreg Roach                        $module->setEnabled($info->status === 'enabled');
371*8d6560c4SGreg Roach
372*8d6560c4SGreg Roach                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
373*8d6560c4SGreg Roach                            $module->setFooterOrder((int) $info->footer_order);
374*8d6560c4SGreg Roach                        }
375*8d6560c4SGreg Roach
376*8d6560c4SGreg Roach                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
377*8d6560c4SGreg Roach                            $module->setMenuOrder((int) $info->menu_order);
378*8d6560c4SGreg Roach                        }
379*8d6560c4SGreg Roach
380*8d6560c4SGreg Roach                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
381*8d6560c4SGreg Roach                            $module->setSidebarOrder((int) $info->sidebar_order);
382*8d6560c4SGreg Roach                        }
383*8d6560c4SGreg Roach
384*8d6560c4SGreg Roach                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
385*8d6560c4SGreg Roach                            $module->setTabOrder((int) $info->tab_order);
386*8d6560c4SGreg Roach                        }
387*8d6560c4SGreg Roach                    } else {
388*8d6560c4SGreg Roach                        $module->setEnabled($module->isEnabledByDefault());
389*8d6560c4SGreg Roach
390*8d6560c4SGreg Roach                        DB::table('module')->insert([
391*8d6560c4SGreg Roach                            'module_name' => $module->name(),
392*8d6560c4SGreg Roach                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
393*8d6560c4SGreg Roach                        ]);
394*8d6560c4SGreg Roach                    }
395*8d6560c4SGreg Roach
396*8d6560c4SGreg Roach                    return $module;
397*8d6560c4SGreg Roach                });
398*8d6560c4SGreg Roach        })->filter($this->enabledFilter($include_disabled));
399*8d6560c4SGreg Roach    }
400*8d6560c4SGreg Roach
401*8d6560c4SGreg Roach    /**
402*8d6560c4SGreg Roach     * All core modules in the system.
403*8d6560c4SGreg Roach     *
404*8d6560c4SGreg Roach     * @return Collection
405*8d6560c4SGreg Roach     */
406*8d6560c4SGreg Roach    private function coreModules(): Collection
407*8d6560c4SGreg Roach    {
408*8d6560c4SGreg Roach        return Collection::make(self::CORE_MODULES)
409*8d6560c4SGreg Roach            ->map(function (string $class, string $name): ModuleInterface {
410*8d6560c4SGreg Roach                $module = app($class);
411*8d6560c4SGreg Roach
412*8d6560c4SGreg Roach                $module->setName($name);
413*8d6560c4SGreg Roach
414*8d6560c4SGreg Roach                return $module;
415*8d6560c4SGreg Roach            });
416*8d6560c4SGreg Roach    }
417*8d6560c4SGreg Roach
418*8d6560c4SGreg Roach    /**
419*8d6560c4SGreg Roach     * All custom modules in the system.  Custom modules are defined in modules_v4/
420*8d6560c4SGreg Roach     *
421*8d6560c4SGreg Roach     * @return Collection
422*8d6560c4SGreg Roach     */
423*8d6560c4SGreg Roach    private function customModules(): Collection
424*8d6560c4SGreg Roach    {
425*8d6560c4SGreg Roach        $pattern   = WT_ROOT . Webtrees::MODULES_PATH . '*/module.php';
426*8d6560c4SGreg Roach        $filenames = glob($pattern, GLOB_NOSORT);
427*8d6560c4SGreg Roach
428*8d6560c4SGreg Roach        return Collection::make($filenames)
429*8d6560c4SGreg Roach            ->filter(function (string $filename): bool {
430*8d6560c4SGreg Roach                // Special characters will break PHP variable names.
431*8d6560c4SGreg Roach                // This also allows us to ignore modules called "foo.example" and "foo.disable"
432*8d6560c4SGreg Roach                $module_name = basename(dirname($filename));
433*8d6560c4SGreg Roach
434*8d6560c4SGreg Roach                return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30;
435*8d6560c4SGreg Roach            })
436*8d6560c4SGreg Roach            ->map(function (string $filename): ?ModuleCustomInterface {
437*8d6560c4SGreg Roach                try {
438*8d6560c4SGreg Roach                    $module = self::load($filename);
439*8d6560c4SGreg Roach
440*8d6560c4SGreg Roach                    if ($module instanceof ModuleCustomInterface) {
441*8d6560c4SGreg Roach                        $module_name = '_' . basename(dirname($filename)) . '_';
442*8d6560c4SGreg Roach
443*8d6560c4SGreg Roach                        $module->setName($module_name);
444*8d6560c4SGreg Roach                    } else {
445*8d6560c4SGreg Roach                        return null;
446*8d6560c4SGreg Roach                    }
447*8d6560c4SGreg Roach
448*8d6560c4SGreg Roach                    return $module;
449*8d6560c4SGreg Roach                } catch (Throwable $ex) {
450*8d6560c4SGreg Roach                    // It would be nice to show this error in a flash-message or similar, but the framework
451*8d6560c4SGreg Roach                    // has not yet been initialised so we have no themes, languages, sessions, etc.
452*8d6560c4SGreg Roach                    throw $ex;
453*8d6560c4SGreg Roach                }
454*8d6560c4SGreg Roach            })
455*8d6560c4SGreg Roach            ->filter()
456*8d6560c4SGreg Roach            ->mapWithKeys(function (ModuleCustomInterface $module): array {
457*8d6560c4SGreg Roach                return [$module->name() => $module];
458*8d6560c4SGreg Roach            });
459*8d6560c4SGreg Roach    }
460*8d6560c4SGreg Roach
461*8d6560c4SGreg Roach    /**
462*8d6560c4SGreg Roach     * Load a module in a static scope, to prevent it from modifying local or object variables.
463*8d6560c4SGreg Roach     *
464*8d6560c4SGreg Roach     * @param string $filename
465*8d6560c4SGreg Roach     *
466*8d6560c4SGreg Roach     * @return mixed
467*8d6560c4SGreg Roach     */
468*8d6560c4SGreg Roach    private static function load(string $filename)
469*8d6560c4SGreg Roach    {
470*8d6560c4SGreg Roach        return include $filename;
471*8d6560c4SGreg Roach    }
472*8d6560c4SGreg Roach
473*8d6560c4SGreg Roach    /**
474*8d6560c4SGreg Roach     * A function filter modules by enabled/disabled
475*8d6560c4SGreg Roach     *
476*8d6560c4SGreg Roach     * @param bool $include_disabled
477*8d6560c4SGreg Roach     *
478*8d6560c4SGreg Roach     * @return Closure
479*8d6560c4SGreg Roach     */
480*8d6560c4SGreg Roach    private function enabledFilter(bool $include_disabled): Closure
481*8d6560c4SGreg Roach    {
482*8d6560c4SGreg Roach        return function (ModuleInterface $module) use ($include_disabled): bool {
483*8d6560c4SGreg Roach            return $include_disabled || $module->isEnabled();
484*8d6560c4SGreg Roach        };
485*8d6560c4SGreg Roach    }
486*8d6560c4SGreg Roach
487*8d6560c4SGreg Roach    /**
488*8d6560c4SGreg Roach     * A function filter modules by type
489*8d6560c4SGreg Roach     *
490*8d6560c4SGreg Roach     * @param string $interface
491*8d6560c4SGreg Roach     *
492*8d6560c4SGreg Roach     * @return Closure
493*8d6560c4SGreg Roach     */
494*8d6560c4SGreg Roach    private function interfaceFilter(string $interface): Closure
495*8d6560c4SGreg Roach    {
496*8d6560c4SGreg Roach        return function (ModuleInterface $module) use ($interface): bool {
497*8d6560c4SGreg Roach            return $module instanceof $interface;
498*8d6560c4SGreg Roach        };
499*8d6560c4SGreg Roach    }
500*8d6560c4SGreg Roach
501*8d6560c4SGreg Roach    /**
502*8d6560c4SGreg Roach     * A function to sort footers
503*8d6560c4SGreg Roach     *
504*8d6560c4SGreg Roach     * @return Closure
505*8d6560c4SGreg Roach     */
506*8d6560c4SGreg Roach    private function footerSorter(): Closure
507*8d6560c4SGreg Roach    {
508*8d6560c4SGreg Roach        return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
509*8d6560c4SGreg Roach            return $x->getFooterOrder() <=> $y->getFooterOrder();
510*8d6560c4SGreg Roach        };
511*8d6560c4SGreg Roach    }
512*8d6560c4SGreg Roach
513*8d6560c4SGreg Roach    /**
514*8d6560c4SGreg Roach     * A function to sort menus
515*8d6560c4SGreg Roach     *
516*8d6560c4SGreg Roach     * @return Closure
517*8d6560c4SGreg Roach     */
518*8d6560c4SGreg Roach    private function menuSorter(): Closure
519*8d6560c4SGreg Roach    {
520*8d6560c4SGreg Roach        return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
521*8d6560c4SGreg Roach            return $x->getMenuOrder() <=> $y->getMenuOrder();
522*8d6560c4SGreg Roach        };
523*8d6560c4SGreg Roach    }
524*8d6560c4SGreg Roach
525*8d6560c4SGreg Roach    /**
526*8d6560c4SGreg Roach     * A function to sort menus
527*8d6560c4SGreg Roach     *
528*8d6560c4SGreg Roach     * @return Closure
529*8d6560c4SGreg Roach     */
530*8d6560c4SGreg Roach    private function sidebarSorter(): Closure
531*8d6560c4SGreg Roach    {
532*8d6560c4SGreg Roach        return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
533*8d6560c4SGreg Roach            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
534*8d6560c4SGreg Roach        };
535*8d6560c4SGreg Roach    }
536*8d6560c4SGreg Roach
537*8d6560c4SGreg Roach    /**
538*8d6560c4SGreg Roach     * A function to sort menus
539*8d6560c4SGreg Roach     *
540*8d6560c4SGreg Roach     * @return Closure
541*8d6560c4SGreg Roach     */
542*8d6560c4SGreg Roach    private function tabSorter(): Closure
543*8d6560c4SGreg Roach    {
544*8d6560c4SGreg Roach        return function (ModuleTabInterface $x, ModuleTabInterface $y): int {
545*8d6560c4SGreg Roach            return $x->getTabOrder() <=> $y->getTabOrder();
546*8d6560c4SGreg Roach        };
547*8d6560c4SGreg Roach    }
548*8d6560c4SGreg Roach
549*8d6560c4SGreg Roach    /**
550*8d6560c4SGreg Roach     * A function to sort modules by name
551*8d6560c4SGreg Roach     *
552*8d6560c4SGreg Roach     * @return Closure
553*8d6560c4SGreg Roach     */
554*8d6560c4SGreg Roach    private function moduleSorter(): Closure
555*8d6560c4SGreg Roach    {
556*8d6560c4SGreg Roach        return function (ModuleInterface $x, ModuleInterface $y): int {
557*8d6560c4SGreg Roach            return I18N::strcasecmp($x->title(), $y->title());
558*8d6560c4SGreg Roach        };
559*8d6560c4SGreg Roach    }
560*8d6560c4SGreg Roach
561*8d6560c4SGreg Roach    /**
5624ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
5634ca7e03cSGreg Roach     *
5644ca7e03cSGreg Roach     * @param string $module_name
565321a89daSGreg Roach     * @param bool   $include_disabled
5664ca7e03cSGreg Roach     *
5674ca7e03cSGreg Roach     * @return ModuleInterface|null
5684ca7e03cSGreg Roach     */
569321a89daSGreg Roach    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
5704ca7e03cSGreg Roach    {
5719d627a9eSGreg Roach        return $this->all($include_disabled)
5724ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($module_name): bool {
573321a89daSGreg Roach                return $module->name() === $module_name;
5744ca7e03cSGreg Roach            })
5754ca7e03cSGreg Roach            ->first();
5764ca7e03cSGreg Roach    }
577bf57b580SGreg Roach
578bf57b580SGreg Roach    /**
579bf57b580SGreg Roach     * Configuration settings are available through the various "module component" pages.
580bf57b580SGreg Roach     * For modules that do not provide a component, we need to list them separately.
581bf57b580SGreg Roach     *
58209fbf3e5SGreg Roach     * @param bool $include_disabled
58309fbf3e5SGreg Roach     *
58454c7f8dfSGreg Roach     * @return Collection
58554c7f8dfSGreg Roach     * @return ModuleConfigInterface[]
586bf57b580SGreg Roach     */
58709fbf3e5SGreg Roach    public function otherModules(bool $include_disabled = false): Collection
588bf57b580SGreg Roach    {
589abafa13cSGreg Roach        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
59009fbf3e5SGreg Roach            ->filter(function (ModuleInterface $module): bool {
591bf57b580SGreg Roach                foreach (self::COMPONENTS as $interface) {
592bf57b580SGreg Roach                    if ($module instanceof $interface) {
593bf57b580SGreg Roach                        return false;
594bf57b580SGreg Roach                    }
595bf57b580SGreg Roach                }
596bf57b580SGreg Roach
597bf57b580SGreg Roach                return true;
598bf57b580SGreg Roach            });
599bf57b580SGreg Roach    }
600bf57b580SGreg Roach
601bf57b580SGreg Roach    /**
602bf57b580SGreg Roach     * Generate a list of module names which exist in the database but not on disk.
603bf57b580SGreg Roach     *
60454c7f8dfSGreg Roach     * @return Collection
60554c7f8dfSGreg Roach     * @return string[]
606bf57b580SGreg Roach     */
607bf57b580SGreg Roach    public function deletedModules(): Collection
608bf57b580SGreg Roach    {
609bf57b580SGreg Roach        $database_modules = DB::table('module')->pluck('module_name');
610bf57b580SGreg Roach
611d5d58984SGreg Roach        $disk_modules = $this->all(true)
612bf57b580SGreg Roach            ->map(function (ModuleInterface $module): string {
613bf57b580SGreg Roach                return $module->name();
614bf57b580SGreg Roach            });
615bf57b580SGreg Roach
616bf57b580SGreg Roach        return $database_modules->diff($disk_modules);
617bf57b580SGreg Roach    }
6184ca7e03cSGreg Roach}
619