xref: /webtrees/app/Services/ModuleService.php (revision 82be4a69c30dc2bee49c743493b9126499ad6d81)
14ca7e03cSGreg Roach<?php
23976b470SGreg Roach
34ca7e03cSGreg Roach/**
44ca7e03cSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
64ca7e03cSGreg Roach * This program is free software: you can redistribute it and/or modify
74ca7e03cSGreg Roach * it under the terms of the GNU General Public License as published by
84ca7e03cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
94ca7e03cSGreg Roach * (at your option) any later version.
104ca7e03cSGreg Roach * This program is distributed in the hope that it will be useful,
114ca7e03cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124ca7e03cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134ca7e03cSGreg Roach * GNU General Public License for more details.
144ca7e03cSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164ca7e03cSGreg Roach */
17fcfa147eSGreg Roach
184ca7e03cSGreg Roachdeclare(strict_types=1);
194ca7e03cSGreg Roach
204ca7e03cSGreg Roachnamespace Fisharebest\Webtrees\Services;
214ca7e03cSGreg Roach
224ca7e03cSGreg Roachuse Closure;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Auth;
24e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
25*82be4a69SGreg Roachuse Fisharebest\Webtrees\Module\DutchMonarchs;
26*82be4a69SGreg Roachuse Fisharebest\Webtrees\Module\DutchPrimeMinisters;
27e8ded2caSGreg Roachuse Fisharebest\Webtrees\Module\LocationListModule;
28*82be4a69SGreg Roachuse Fisharebest\Webtrees\Module\LowCountriesRulers;
296b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
305bc7939cSBenuse Fisharebest\Webtrees\FlashMessages;
319e18e23bSGreg Roachuse Fisharebest\Webtrees\I18N;
324ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AhnentafelReportModule;
334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AlbumModule;
344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AncestorsChartModule;
35fa24d895SGreg Roachuse Fisharebest\Webtrees\Module\AustrianHistoricEvents;
363b387b05SGreg Roachuse Fisharebest\Webtrees\Module\AustrianPresidents;
374ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthReportModule;
4067992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\BranchesListModule;
4109fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishMonarchs;
4209fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishPrimeMinisters;
4309fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\BritishSocialHistory;
444ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CalendarMenuModule;
454ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CemeteryReportModule;
464ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CensusAssistantModule;
474ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChangeReportModule;
484ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsBlockModule;
494ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsMenuModule;
504ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CkeditorModule;
514ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ClippingsCartModule;
524ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CloudsTheme;
534ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ColorsTheme;
544ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CompactTreeChartModule;
554ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ContactsFooterModule;
568d6560c4SGreg Roachuse Fisharebest\Webtrees\Module\CustomCssJsModule;
57c2ee636cSGreg Roachuse Fisharebest\Webtrees\Module\CzechMonarchsAndPresidents;
584ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DeathReportModule;
594ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyChartModule;
604ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyModule;
614ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyReportModule;
624ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FabTheme;
634ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FactSourcesReportModule;
644ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyBookChartModule;
654ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyGroupReportModule;
6667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\FamilyListModule;
674ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyNavigatorModule;
684ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
694ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
704ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
714ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FanChartModule;
72ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixCemeteryTag;
73ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixDuplicateLinks;
74ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixMissingDeaths;
75ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixMissingMarriedNames;
76ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixNameSlashesAndSpaces;
77a50fee42SGreg Roachuse Fisharebest\Webtrees\Module\FixNameTags;
78ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixPlaceNames;
79ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixPrimaryTag;
80ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\FixSearchAndReplace;
81a50fee42SGreg Roachuse Fisharebest\Webtrees\Module\FrenchHistory;
824ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
834ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
844ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
854ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HitCountFooterModule;
864ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HourglassChartModule;
874ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HtmlBlockModule;
884ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFactsTabModule;
894ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
9067992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\IndividualListModule;
9109fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\IndividualMetadataModule;
924ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualReportModule;
934ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTreeModule;
9402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageAfrikaans;
9502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageAlbanian;
9602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageArabic;
9702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageBosnian;
9802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageBulgarian;
9902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageCatalan;
10002086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageChineseSimplified;
10102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageChineseTraditional;
10202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageCroatian;
10302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageCzech;
10402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageDanish;
10502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageDivehi;
10602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageDutch;
10702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageEnglishAustralia;
10802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageEnglishGreatBritain;
10902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
11002086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageEstonian;
11102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageFaroese;
11202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageFarsi;
11302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageFinnish;
11402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageFrench;
11502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageFrenchCanada;
11602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageGalician;
11702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageGeorgian;
11802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageGerman;
11902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageGreek;
12002086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageHebrew;
1210f180719SGreg Roachuse Fisharebest\Webtrees\Module\LanguageHindi;
12202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageHungarian;
12302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageIcelandic;
12402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageIndonesian;
12502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageItalian;
12602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageJapanese;
12702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageJavanese;
12802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageKazhak;
12902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageKorean;
130015d9fbcSGreg Roachuse Fisharebest\Webtrees\Module\LanguageKurdish;
13102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageLatvian;
13202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageLingala;
13302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageLithuanian;
13402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageMalay;
13502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageMaori;
13602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageMarathi;
13702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageNepalese;
13802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageNorwegianBokmal;
13902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageNorwegianNynorsk;
14002086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageOccitan;
14102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguagePolish;
14202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguagePortuguese;
14302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguagePortugueseBrazil;
14402086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageRomanian;
14502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageRussian;
14602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSerbian;
14702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSerbianLatin;
14802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSlovakian;
14902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSlovenian;
15002086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSpanish;
15102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSundanese;
15202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSwahili;
15302086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageSwedish;
154d7e19759SGreg Roachuse Fisharebest\Webtrees\Module\LanguageTagalog;
15502086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageTamil;
15602086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageTatar;
15702086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageThai;
15802086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageTurkish;
15902086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageUkranian;
1603ca31ad9SGreg Roachuse Fisharebest\Webtrees\Module\LanguageUrdu;
16102086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageVietnamese;
16202086832SGreg Roachuse Fisharebest\Webtrees\Module\LanguageYiddish;
16355134edcSGreg Roachuse Fisharebest\Webtrees\Module\RedirectLegacyUrlsModule;
1644ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LifespansChartModule;
1654ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ListsMenuModule;
1664ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoggedInUsersModule;
1674ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoginBlockModule;
1684ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MarriageReportModule;
1694ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
17067992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\MediaListModule;
1714ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MediaTabModule;
1724ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MinimalTheme;
1734ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MissingFactsReportModule;
174bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
1754ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
1764ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
1774ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface;
178ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
1794ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleFooterInterface;
180bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
1814ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
182bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
18367992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleListInterface;
1844ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
1854ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
1864ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
1874ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
188bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
18967992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\NoteListModule;
1904ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\NotesTabModule;
1914ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OccupationReportModule;
1924ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OnThisDayModule;
1934ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeChartModule;
1944ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeMapModule;
1954ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeReportModule;
19667992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
1974ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PlacesModule;
1984ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
199b3a775f6SGreg Roachuse Fisharebest\Webtrees\Module\PrivacyPolicy;
2004ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RecentChangesModule;
2014ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
2024ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelationshipsChartModule;
2034ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelativesTabModule;
2044ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReportsMenuModule;
20567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\RepositoryListModule;
2064ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ResearchTaskModule;
2074ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReviewChangesModule;
2084ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SearchMenuModule;
2094ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule;
2104ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SlideShowModule;
21167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\SourceListModule;
2124ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SourcesTabModule;
2134ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatcounterModule;
2144ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatisticsChartModule;
2154ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StoriesModule;
216e72c24d6SGreg Roachuse Fisharebest\Webtrees\Module\SubmitterListModule;
2174ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ThemeSelectModule;
2184ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TimelineChartModule;
2194ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopGivenNamesModule;
2204ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopPageViewsModule;
2214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopSurnamesModule;
2224ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TreesMenuModule;
2234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
2244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserFavoritesModule;
2254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserJournalModule;
2264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserMessagesModule;
2274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserWelcomeModule;
22809fbf3e5SGreg Roachuse Fisharebest\Webtrees\Module\USPresidents;
2294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
2304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WelcomeBlockModule;
2314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\XeneaTheme;
2324ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\YahrzeitModule;
2334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Tree;
2344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Webtrees;
2354ca7e03cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2364ca7e03cSGreg Roachuse Illuminate\Support\Collection;
2374ca7e03cSGreg Roachuse stdClass;
2384ca7e03cSGreg Roachuse Throwable;
2394ca7e03cSGreg Roach
24071378461SGreg Roachuse function app;
241dec352c1SGreg Roachuse function str_contains;
242dec352c1SGreg Roachuse function strlen;
24371378461SGreg Roach
2444ca7e03cSGreg Roach/**
2454ca7e03cSGreg Roach * Functions for managing and maintaining modules.
2464ca7e03cSGreg Roach */
2474ca7e03cSGreg Roachclass ModuleService
2484ca7e03cSGreg Roach{
249362b8464SGreg Roach    // Components are pieces of user-facing functionality, are managed together in the control panel.
2504ca7e03cSGreg Roach    private const COMPONENTS = [
25187cca37cSGreg Roach        ModuleAnalyticsInterface::class,
25287cca37cSGreg Roach        ModuleBlockInterface::class,
25387cca37cSGreg Roach        ModuleChartInterface::class,
254ce42304aSGreg Roach        ModuleDataFixInterface::class,
25587cca37cSGreg Roach        ModuleFooterInterface::class,
25687cca37cSGreg Roach        ModuleHistoricEventsInterface::class,
25787cca37cSGreg Roach        ModuleLanguageInterface::class,
25887cca37cSGreg Roach        ModuleListInterface::class,
25987cca37cSGreg Roach        ModuleMenuInterface::class,
26087cca37cSGreg Roach        ModuleReportInterface::class,
26187cca37cSGreg Roach        ModuleSidebarInterface::class,
26287cca37cSGreg Roach        ModuleTabInterface::class,
26387cca37cSGreg Roach        ModuleThemeInterface::class,
2644ca7e03cSGreg Roach    ];
2654ca7e03cSGreg Roach
266745968f0SGreg Roach    // Components that have access levels.
267745968f0SGreg Roach    private const COMPONENTS_WITH_ACCESS = [
268745968f0SGreg Roach        ModuleBlockInterface::class,
269745968f0SGreg Roach        ModuleChartInterface::class,
270745968f0SGreg Roach        ModuleListInterface::class,
271745968f0SGreg Roach        ModuleMenuInterface::class,
272745968f0SGreg Roach        ModuleReportInterface::class,
273745968f0SGreg Roach        ModuleSidebarInterface::class,
274745968f0SGreg Roach        ModuleTabInterface::class,
275745968f0SGreg Roach    ];
276745968f0SGreg Roach
277745968f0SGreg Roach    // Components that are displayed in a particular order
278745968f0SGreg Roach    private const COMPONENTS_WITH_SORT = [
279745968f0SGreg Roach        ModuleFooterInterface::class,
280745968f0SGreg Roach        ModuleMenuInterface::class,
281745968f0SGreg Roach        ModuleSidebarInterface::class,
282745968f0SGreg Roach        ModuleTabInterface::class,
283745968f0SGreg Roach    ];
284745968f0SGreg Roach
2854ca7e03cSGreg Roach    // Array keys are module names, and should match module names from earlier versions of webtrees.
2864ca7e03cSGreg Roach    private const CORE_MODULES = [
2874ca7e03cSGreg Roach        'GEDFact_assistant'       => CensusAssistantModule::class,
2884ca7e03cSGreg Roach        'ahnentafel_report'       => AhnentafelReportModule::class,
2894ca7e03cSGreg Roach        'ancestors_chart'         => AncestorsChartModule::class,
290fa24d895SGreg Roach        'austrian-history'        => AustrianHistoricEvents::class,
2913b387b05SGreg Roach        'austrian-presidents'     => AustrianPresidents::class,
2924ca7e03cSGreg Roach        'bdm_report'              => BirthDeathMarriageReportModule::class,
2934ca7e03cSGreg Roach        'bing-webmaster-tools'    => BingWebmasterToolsModule::class,
2944ca7e03cSGreg Roach        'birth_report'            => BirthReportModule::class,
29567992b6aSRichard Cissee        'branches_list'           => BranchesListModule::class,
29609fbf3e5SGreg Roach        'british-monarchs'        => BritishMonarchs::class,
29709fbf3e5SGreg Roach        'british-prime-ministers' => BritishPrimeMinisters::class,
29809fbf3e5SGreg Roach        'british-social-history'  => BritishSocialHistory::class,
2994ca7e03cSGreg Roach        'calendar-menu'           => CalendarMenuModule::class,
3004ca7e03cSGreg Roach        'cemetery_report'         => CemeteryReportModule::class,
3014ca7e03cSGreg Roach        'change_report'           => ChangeReportModule::class,
3024ca7e03cSGreg Roach        'charts'                  => ChartsBlockModule::class,
3034ca7e03cSGreg Roach        'charts-menu'             => ChartsMenuModule::class,
3044ca7e03cSGreg Roach        'ckeditor'                => CkeditorModule::class,
3054ca7e03cSGreg Roach        'clippings'               => ClippingsCartModule::class,
3064ca7e03cSGreg Roach        'clouds'                  => CloudsTheme::class,
3074ca7e03cSGreg Roach        'colors'                  => ColorsTheme::class,
3084ca7e03cSGreg Roach        'compact-chart'           => CompactTreeChartModule::class,
3094ca7e03cSGreg Roach        'contact-links'           => ContactsFooterModule::class,
310c2ee636cSGreg Roach        'czech-leaders'           => CzechMonarchsAndPresidents::class,
3118d6560c4SGreg Roach        'custom-css-js'           => CustomCssJsModule::class,
3124ca7e03cSGreg Roach        'death_report'            => DeathReportModule::class,
3134ca7e03cSGreg Roach        'descendancy'             => DescendancyModule::class,
3144ca7e03cSGreg Roach        'descendancy_chart'       => DescendancyChartModule::class,
3154ca7e03cSGreg Roach        'descendancy_report'      => DescendancyReportModule::class,
316*82be4a69SGreg Roach        'dutch_monarchs'          => DutchMonarchs::class,
317*82be4a69SGreg Roach        'dutch_prime_ministers'   => DutchPrimeMinisters::class,
3188eaf8709SGreg Roach        'extra_info'              => IndividualMetadataModule::class,
3194ca7e03cSGreg Roach        'fab'                     => FabTheme::class,
3204ca7e03cSGreg Roach        'fact_sources'            => FactSourcesReportModule::class,
3214ca7e03cSGreg Roach        'family_book_chart'       => FamilyBookChartModule::class,
3224ca7e03cSGreg Roach        'family_group_report'     => FamilyGroupReportModule::class,
32367992b6aSRichard Cissee        'family_list'             => FamilyListModule::class,
3244ca7e03cSGreg Roach        'family_nav'              => FamilyNavigatorModule::class,
3254ca7e03cSGreg Roach        'fan_chart'               => FanChartModule::class,
3264ca7e03cSGreg Roach        'faq'                     => FrequentlyAskedQuestionsModule::class,
327f466a2a5SGreg Roach        'french-history'          => FrenchHistory::class,
328ce42304aSGreg Roach        'fix-add-death'           => FixMissingDeaths::class,
329ce42304aSGreg Roach        'fix-add-marr-names'      => FixMissingMarriedNames::class,
33076937c18SGreg Roach        'fix-ceme-tag'            => FixCemeteryTag::class,
331ce42304aSGreg Roach        'fix-duplicate-links'     => FixDuplicateLinks::class,
332ce42304aSGreg Roach        'fix-name-slashes-spaces' => FixNameSlashesAndSpaces::class,
333042e1af7SGreg Roach        'fix-name-tags'           => FixNameTags::class,
33429a51599SGreg Roach        'fix-place-names'         => FixPlaceNames::class,
335ce42304aSGreg Roach        'fix-prim-tag'            => FixPrimaryTag::class,
336ce42304aSGreg Roach        'fix-search-and-replace'  => FixSearchAndReplace::class,
3374ca7e03cSGreg Roach        'gedcom_block'            => WelcomeBlockModule::class,
3384ca7e03cSGreg Roach        'gedcom_favorites'        => FamilyTreeFavoritesModule::class,
3394ca7e03cSGreg Roach        'gedcom_news'             => FamilyTreeNewsModule::class,
3404ca7e03cSGreg Roach        'gedcom_stats'            => FamilyTreeStatisticsModule::class,
3414ca7e03cSGreg Roach        'google-analytics'        => GoogleAnalyticsModule::class,
3424ca7e03cSGreg Roach        'google-webmaster-tools'  => GoogleWebmasterToolsModule::class,
3434ca7e03cSGreg Roach        'hit-counter'             => HitCountFooterModule::class,
3444ca7e03cSGreg Roach        'hourglass_chart'         => HourglassChartModule::class,
3454ca7e03cSGreg Roach        'html'                    => HtmlBlockModule::class,
3464ca7e03cSGreg Roach        'individual_ext_report'   => IndividualFamiliesReportModule::class,
34767992b6aSRichard Cissee        'individual_list'         => IndividualListModule::class,
3484ca7e03cSGreg Roach        'individual_report'       => IndividualReportModule::class,
34902086832SGreg Roach        'language-af'             => LanguageAfrikaans::class,
35002086832SGreg Roach        'language-ar'             => LanguageArabic::class,
351899f7f5fSGreg Roach        'language-bg'             => LanguageBulgarian::class,
35202086832SGreg Roach        'language-bs'             => LanguageBosnian::class,
35302086832SGreg Roach        'language-ca'             => LanguageCatalan::class,
35402086832SGreg Roach        'language-cs'             => LanguageCzech::class,
35502086832SGreg Roach        'language-da'             => LanguageDanish::class,
35602086832SGreg Roach        'language-de'             => LanguageGerman::class,
35702086832SGreg Roach        'language-dv'             => LanguageDivehi::class,
35802086832SGreg Roach        'language-el'             => LanguageGreek::class,
35902086832SGreg Roach        'language-en-AU'          => LanguageEnglishAustralia::class,
36002086832SGreg Roach        'language-en-GB'          => LanguageEnglishGreatBritain::class,
36102086832SGreg Roach        'language-en-US'          => LanguageEnglishUnitedStates::class,
36202086832SGreg Roach        'language-es'             => LanguageSpanish::class,
36302086832SGreg Roach        'language-et'             => LanguageEstonian::class,
36402086832SGreg Roach        'language-fa'             => LanguageFarsi::class,
36502086832SGreg Roach        'language-fi'             => LanguageFinnish::class,
36602086832SGreg Roach        'language-fo'             => LanguageFaroese::class,
36702086832SGreg Roach        'language-fr'             => LanguageFrench::class,
36802086832SGreg Roach        'language-fr-CA'          => LanguageFrenchCanada::class,
36902086832SGreg Roach        'language-gl'             => LanguageGalician::class,
37002086832SGreg Roach        'language-he'             => LanguageHebrew::class,
3710f180719SGreg Roach        'language-hi'             => LanguageHindi::class,
37202086832SGreg Roach        'language-hr'             => LanguageCroatian::class,
37302086832SGreg Roach        'language-hu'             => LanguageHungarian::class,
37402086832SGreg Roach        'language-id'             => LanguageIndonesian::class,
37502086832SGreg Roach        'language-is'             => LanguageIcelandic::class,
37602086832SGreg Roach        'language-it'             => LanguageItalian::class,
37702086832SGreg Roach        'language-ja'             => LanguageJapanese::class,
37802086832SGreg Roach        'language-jv'             => LanguageJavanese::class,
37902086832SGreg Roach        'language-ka'             => LanguageGeorgian::class,
38002086832SGreg Roach        'language-kk'             => LanguageKazhak::class,
38102086832SGreg Roach        'language-ko'             => LanguageKorean::class,
382015d9fbcSGreg Roach        'language-ku'             => LanguageKurdish::class,
38302086832SGreg Roach        'language-ln'             => LanguageLingala::class,
38402086832SGreg Roach        'language-lt'             => LanguageLithuanian::class,
38502086832SGreg Roach        'language-lv'             => LanguageLatvian::class,
38602086832SGreg Roach        'language-mi'             => LanguageMaori::class,
38702086832SGreg Roach        'language-mr'             => LanguageMarathi::class,
38802086832SGreg Roach        'language-ms'             => LanguageMalay::class,
38902086832SGreg Roach        'language-nb'             => LanguageNorwegianBokmal::class,
39002086832SGreg Roach        'language-ne'             => LanguageNepalese::class,
39102086832SGreg Roach        'language-nl'             => LanguageDutch::class,
39202086832SGreg Roach        'language-nn'             => LanguageNorwegianNynorsk::class,
39302086832SGreg Roach        'language-oc'             => LanguageOccitan::class,
39402086832SGreg Roach        'language-pl'             => LanguagePolish::class,
39502086832SGreg Roach        'language-pt'             => LanguagePortuguese::class,
39602086832SGreg Roach        'language-pt-BR'          => LanguagePortugueseBrazil::class,
39702086832SGreg Roach        'language-ro'             => LanguageRomanian::class,
39802086832SGreg Roach        'language-ru'             => LanguageRussian::class,
39902086832SGreg Roach        'language-sk'             => LanguageSlovakian::class,
40002086832SGreg Roach        'language-sl'             => LanguageSlovenian::class,
40102086832SGreg Roach        'language-sq'             => LanguageAlbanian::class,
40202086832SGreg Roach        'language-sr'             => LanguageSerbian::class,
40302086832SGreg Roach        'language-sr-Latn'        => LanguageSerbianLatin::class,
40402086832SGreg Roach        'language-su'             => LanguageSundanese::class,
40502086832SGreg Roach        'language-sv'             => LanguageSwedish::class,
40602086832SGreg Roach        'language-sw'             => LanguageSwahili::class,
40702086832SGreg Roach        'language-ta'             => LanguageTamil::class,
40802086832SGreg Roach        'language-th'             => LanguageThai::class,
409d7e19759SGreg Roach        'language-tl'             => LanguageTagalog::class,
41002086832SGreg Roach        'language-tr'             => LanguageTurkish::class,
41102086832SGreg Roach        'language-tt'             => LanguageTatar::class,
41202086832SGreg Roach        'language-uk'             => LanguageUkranian::class,
4133ca31ad9SGreg Roach        'language-ur'             => LanguageUrdu::class,
41402086832SGreg Roach        'language-vi'             => LanguageVietnamese::class,
41502086832SGreg Roach        'language-yi'             => LanguageYiddish::class,
41602086832SGreg Roach        'language-zh-Hans'        => LanguageChineseSimplified::class,
41702086832SGreg Roach        'language-zh-Hant'        => LanguageChineseTraditional::class,
41855134edcSGreg Roach        'legacy-urls'             => RedirectLegacyUrlsModule::class,
4194ca7e03cSGreg Roach        'lifespans_chart'         => LifespansChartModule::class,
4204ca7e03cSGreg Roach        'lightbox'                => AlbumModule::class,
4214ca7e03cSGreg Roach        'lists-menu'              => ListsMenuModule::class,
422e8ded2caSGreg Roach        'location_list'           => LocationListModule::class,
4234ca7e03cSGreg Roach        'logged_in'               => LoggedInUsersModule::class,
4244ca7e03cSGreg Roach        'login_block'             => LoginBlockModule::class,
425*82be4a69SGreg Roach        'low_countries_rulers'    => LowCountriesRulers::class,
4264ca7e03cSGreg Roach        'marriage_report'         => MarriageReportModule::class,
4274ca7e03cSGreg Roach        'matomo-analytics'        => MatomoAnalyticsModule::class,
4284ca7e03cSGreg Roach        'media'                   => MediaTabModule::class,
42967992b6aSRichard Cissee        'media_list'              => MediaListModule::class,
4304ca7e03cSGreg Roach        'minimal'                 => MinimalTheme::class,
4314ca7e03cSGreg Roach        'missing_facts_report'    => MissingFactsReportModule::class,
4324ca7e03cSGreg Roach        'notes'                   => NotesTabModule::class,
43367992b6aSRichard Cissee        'note_list'               => NoteListModule::class,
4344ca7e03cSGreg Roach        'occupation_report'       => OccupationReportModule::class,
4354ca7e03cSGreg Roach        'pedigree-map'            => PedigreeMapModule::class,
4364ca7e03cSGreg Roach        'pedigree_chart'          => PedigreeChartModule::class,
4374ca7e03cSGreg Roach        'pedigree_report'         => PedigreeReportModule::class,
4384ca7e03cSGreg Roach        'personal_facts'          => IndividualFactsTabModule::class,
4394ca7e03cSGreg Roach        'places'                  => PlacesModule::class,
44067992b6aSRichard Cissee        'places_list'             => PlaceHierarchyListModule::class,
4414ca7e03cSGreg Roach        'powered-by-webtrees'     => PoweredByWebtreesModule::class,
442b3a775f6SGreg Roach        'privacy-policy'          => PrivacyPolicy::class,
4434ca7e03cSGreg Roach        'random_media'            => SlideShowModule::class,
4444ca7e03cSGreg Roach        'recent_changes'          => RecentChangesModule::class,
4454ca7e03cSGreg Roach        'relationships_chart'     => RelationshipsChartModule::class,
4464ca7e03cSGreg Roach        'relative_ext_report'     => RelatedIndividualsReportModule::class,
4474ca7e03cSGreg Roach        'relatives'               => RelativesTabModule::class,
4484ca7e03cSGreg Roach        'reports-menu'            => ReportsMenuModule::class,
44967992b6aSRichard Cissee        'repository_list'         => RepositoryListModule::class,
4504ca7e03cSGreg Roach        'review_changes'          => ReviewChangesModule::class,
4514ca7e03cSGreg Roach        'search-menu'             => SearchMenuModule::class,
4524ca7e03cSGreg Roach        'sitemap'                 => SiteMapModule::class,
45367992b6aSRichard Cissee        'source_list'             => SourceListModule::class,
4544ca7e03cSGreg Roach        'sources_tab'             => SourcesTabModule::class,
4554ca7e03cSGreg Roach        'statcounter'             => StatcounterModule::class,
4564ca7e03cSGreg Roach        'statistics_chart'        => StatisticsChartModule::class,
4574ca7e03cSGreg Roach        'stories'                 => StoriesModule::class,
458e72c24d6SGreg Roach        'submitter_list'          => SubmitterListModule::class,
4594ca7e03cSGreg Roach        'theme_select'            => ThemeSelectModule::class,
4604ca7e03cSGreg Roach        'timeline_chart'          => TimelineChartModule::class,
4614ca7e03cSGreg Roach        'todays_events'           => OnThisDayModule::class,
4624ca7e03cSGreg Roach        'todo'                    => ResearchTaskModule::class,
4634ca7e03cSGreg Roach        'top10_givnnames'         => TopGivenNamesModule::class,
4644ca7e03cSGreg Roach        'top10_pageviews'         => TopPageViewsModule::class,
4654ca7e03cSGreg Roach        'top10_surnames'          => TopSurnamesModule::class,
4664ca7e03cSGreg Roach        'tree'                    => InteractiveTreeModule::class,
4674ca7e03cSGreg Roach        'trees-menu'              => TreesMenuModule::class,
4684ca7e03cSGreg Roach        'upcoming_events'         => UpcomingAnniversariesModule::class,
46909fbf3e5SGreg Roach        'us-presidents'           => USPresidents::class,
4704ca7e03cSGreg Roach        'user_blog'               => UserJournalModule::class,
4714ca7e03cSGreg Roach        'user_favorites'          => UserFavoritesModule::class,
4724ca7e03cSGreg Roach        'user_messages'           => UserMessagesModule::class,
4734ca7e03cSGreg Roach        'user_welcome'            => UserWelcomeModule::class,
4744ca7e03cSGreg Roach        'webtrees'                => WebtreesTheme::class,
4754ca7e03cSGreg Roach        'xenea'                   => XeneaTheme::class,
4764ca7e03cSGreg Roach        'yahrzeit'                => YahrzeitModule::class,
4774ca7e03cSGreg Roach    ];
4784ca7e03cSGreg Roach
4794ca7e03cSGreg Roach    /**
480dedcf571SGreg Roach     * A function to convert modules into their titles - to create option lists, etc.
481dedcf571SGreg Roach     *
482dedcf571SGreg Roach     * @return Closure
483dedcf571SGreg Roach     */
484dedcf571SGreg Roach    public function titleMapper(): Closure
485dedcf571SGreg Roach    {
4866c2179e2SGreg Roach        return static function (ModuleInterface $module): string {
487dedcf571SGreg Roach            return $module->title();
488dedcf571SGreg Roach        };
489dedcf571SGreg Roach    }
490dedcf571SGreg Roach
491dedcf571SGreg Roach    /**
4924ca7e03cSGreg Roach     * Modules which (a) provide a specific function and (b) we have permission to see.
4934ca7e03cSGreg Roach     *
49487cca37cSGreg Roach     * @param string        $interface
4954ca7e03cSGreg Roach     * @param Tree          $tree
496e5a6b4d4SGreg Roach     * @param UserInterface $user
4974ca7e03cSGreg Roach     *
498b5c8fd7eSGreg Roach     * @return Collection<ModuleInterface>
4994ca7e03cSGreg Roach     */
50087cca37cSGreg Roach    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
5014ca7e03cSGreg Roach    {
502b5979037SGreg Roach        return $this->findByInterface($interface, false, true)
5030b5fd0a6SGreg Roach            ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool {
50487cca37cSGreg Roach                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
5054ca7e03cSGreg Roach            });
5064ca7e03cSGreg Roach    }
5074ca7e03cSGreg Roach
5084ca7e03cSGreg Roach    /**
5094ca7e03cSGreg Roach     * All modules which provide a specific function.
5104ca7e03cSGreg Roach     *
5114ca7e03cSGreg Roach     * @param string $interface
5124ca7e03cSGreg Roach     * @param bool   $include_disabled
513b5979037SGreg Roach     * @param bool   $sort
5144ca7e03cSGreg Roach     *
515b5c8fd7eSGreg Roach     * @return Collection<ModuleInterface>
5164ca7e03cSGreg Roach     */
517b5979037SGreg Roach    public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection
5184ca7e03cSGreg Roach    {
5199d627a9eSGreg Roach        $modules = $this->all($include_disabled)
5209d627a9eSGreg Roach            ->filter($this->interfaceFilter($interface));
5214ca7e03cSGreg Roach
5224ca7e03cSGreg Roach        switch ($interface) {
5234ca7e03cSGreg Roach            case ModuleFooterInterface::class:
52437646143SGreg Roach                return $modules->sort($this->footerComparator());
5254ca7e03cSGreg Roach
5264ca7e03cSGreg Roach            case ModuleMenuInterface::class:
52737646143SGreg Roach                return $modules->sort($this->menuComparator());
5284ca7e03cSGreg Roach
5294ca7e03cSGreg Roach            case ModuleSidebarInterface::class:
53037646143SGreg Roach                return $modules->sort($this->sidebarComparator());
5314ca7e03cSGreg Roach
5324ca7e03cSGreg Roach            case ModuleTabInterface::class:
53337646143SGreg Roach                return $modules->sort($this->tabComparator());
534b5979037SGreg Roach
535b5979037SGreg Roach            default:
536b5979037SGreg Roach                if ($sort) {
53737646143SGreg Roach                    return $modules->sort($this->moduleComparator());
5384ca7e03cSGreg Roach                }
5394ca7e03cSGreg Roach
5404ca7e03cSGreg Roach                return $modules;
5414ca7e03cSGreg Roach        }
542b5979037SGreg Roach    }
5434ca7e03cSGreg Roach
5444ca7e03cSGreg Roach    /**
5458d6560c4SGreg Roach     * All modules.
5468d6560c4SGreg Roach     *
5478d6560c4SGreg Roach     * @param bool $include_disabled
5488d6560c4SGreg Roach     *
549b5c8fd7eSGreg Roach     * @return Collection<ModuleInterface>
5508d6560c4SGreg Roach     */
5518d6560c4SGreg Roach    public function all(bool $include_disabled = false): Collection
5528d6560c4SGreg Roach    {
5536b9cb339SGreg Roach        return Registry::cache()->array()->remember('all-modules', function (): Collection {
5548d6560c4SGreg Roach            // Modules have a default status, order etc.
5558d6560c4SGreg Roach            // We can override these from database settings.
5568d6560c4SGreg Roach            $module_info = DB::table('module')
5578d6560c4SGreg Roach                ->get()
5580b5fd0a6SGreg Roach                ->mapWithKeys(static function (stdClass $row): array {
5598d6560c4SGreg Roach                    return [$row->module_name => $row];
5608d6560c4SGreg Roach                });
5618d6560c4SGreg Roach
5628d6560c4SGreg Roach            return $this->coreModules()
5638d6560c4SGreg Roach                ->merge($this->customModules())
5640b5fd0a6SGreg Roach                ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface {
5658d6560c4SGreg Roach                    $info = $module_info->get($module->name());
5668d6560c4SGreg Roach
5678d6560c4SGreg Roach                    if ($info instanceof stdClass) {
5688d6560c4SGreg Roach                        $module->setEnabled($info->status === 'enabled');
5698d6560c4SGreg Roach
5708d6560c4SGreg Roach                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
5718d6560c4SGreg Roach                            $module->setFooterOrder((int) $info->footer_order);
5728d6560c4SGreg Roach                        }
5738d6560c4SGreg Roach
5748d6560c4SGreg Roach                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
5758d6560c4SGreg Roach                            $module->setMenuOrder((int) $info->menu_order);
5768d6560c4SGreg Roach                        }
5778d6560c4SGreg Roach
5788d6560c4SGreg Roach                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
5798d6560c4SGreg Roach                            $module->setSidebarOrder((int) $info->sidebar_order);
5808d6560c4SGreg Roach                        }
5818d6560c4SGreg Roach
5828d6560c4SGreg Roach                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
5838d6560c4SGreg Roach                            $module->setTabOrder((int) $info->tab_order);
5848d6560c4SGreg Roach                        }
5858d6560c4SGreg Roach                    } else {
5868d6560c4SGreg Roach                        $module->setEnabled($module->isEnabledByDefault());
5878d6560c4SGreg Roach
5888d6560c4SGreg Roach                        DB::table('module')->insert([
5898d6560c4SGreg Roach                            'module_name' => $module->name(),
5908d6560c4SGreg Roach                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
5918d6560c4SGreg Roach                        ]);
5928d6560c4SGreg Roach                    }
5938d6560c4SGreg Roach
5948d6560c4SGreg Roach                    return $module;
5958d6560c4SGreg Roach                });
5968d6560c4SGreg Roach        })->filter($this->enabledFilter($include_disabled));
5978d6560c4SGreg Roach    }
5988d6560c4SGreg Roach
5998d6560c4SGreg Roach    /**
6008d6560c4SGreg Roach     * All core modules in the system.
6018d6560c4SGreg Roach     *
602b5c8fd7eSGreg Roach     * @return Collection<ModuleInterface>
6038d6560c4SGreg Roach     */
6048d6560c4SGreg Roach    private function coreModules(): Collection
6058d6560c4SGreg Roach    {
6068d6560c4SGreg Roach        return Collection::make(self::CORE_MODULES)
6070b5fd0a6SGreg Roach            ->map(static function (string $class, string $name): ModuleInterface {
6088d6560c4SGreg Roach                $module = app($class);
6098d6560c4SGreg Roach
6108d6560c4SGreg Roach                $module->setName($name);
6118d6560c4SGreg Roach
6128d6560c4SGreg Roach                return $module;
6138d6560c4SGreg Roach            });
6148d6560c4SGreg Roach    }
6158d6560c4SGreg Roach
6168d6560c4SGreg Roach    /**
6178d6560c4SGreg Roach     * All custom modules in the system.  Custom modules are defined in modules_v4/
6188d6560c4SGreg Roach     *
619b5c8fd7eSGreg Roach     * @return Collection<ModuleCustomInterface>
6208d6560c4SGreg Roach     */
6218d6560c4SGreg Roach    private function customModules(): Collection
6228d6560c4SGreg Roach    {
623f397d0fdSGreg Roach        $pattern   = Webtrees::MODULES_DIR . '*/module.php';
6248d6560c4SGreg Roach        $filenames = glob($pattern, GLOB_NOSORT);
6258d6560c4SGreg Roach
6268d6560c4SGreg Roach        return Collection::make($filenames)
6270b5fd0a6SGreg Roach            ->filter(static function (string $filename): bool {
6288d6560c4SGreg Roach                // Special characters will break PHP variable names.
6298d6560c4SGreg Roach                // This also allows us to ignore modules called "foo.example" and "foo.disable"
6308d6560c4SGreg Roach                $module_name = basename(dirname($filename));
6318d6560c4SGreg Roach
632dec352c1SGreg Roach                foreach (['.', ' ', '[', ']'] as $character) {
633dec352c1SGreg Roach                    if (str_contains($module_name, $character)) {
634dec352c1SGreg Roach                        return false;
635dec352c1SGreg Roach                    }
636dec352c1SGreg Roach                }
637dec352c1SGreg Roach
638dec352c1SGreg Roach                return strlen($module_name) <= 30;
6398d6560c4SGreg Roach            })
6400b5fd0a6SGreg Roach            ->map(static function (string $filename): ?ModuleCustomInterface {
6418d6560c4SGreg Roach                try {
6428d6560c4SGreg Roach                    $module = self::load($filename);
6438d6560c4SGreg Roach
6448d6560c4SGreg Roach                    if ($module instanceof ModuleCustomInterface) {
6458d6560c4SGreg Roach                        $module_name = '_' . basename(dirname($filename)) . '_';
6468d6560c4SGreg Roach
6478d6560c4SGreg Roach                        $module->setName($module_name);
6488d6560c4SGreg Roach                    } else {
6498d6560c4SGreg Roach                        return null;
6508d6560c4SGreg Roach                    }
6518d6560c4SGreg Roach
6528d6560c4SGreg Roach                    return $module;
6538d6560c4SGreg Roach                } catch (Throwable $ex) {
6548d6560c4SGreg Roach                    // It would be nice to show this error in a flash-message or similar, but the framework
6558d6560c4SGreg Roach                    // has not yet been initialised so we have no themes, languages, sessions, etc.
6568d6560c4SGreg Roach                    throw $ex;
6578d6560c4SGreg Roach                }
6588d6560c4SGreg Roach            })
6598d6560c4SGreg Roach            ->filter()
6600b5fd0a6SGreg Roach            ->mapWithKeys(static function (ModuleCustomInterface $module): array {
6618d6560c4SGreg Roach                return [$module->name() => $module];
6628d6560c4SGreg Roach            });
6638d6560c4SGreg Roach    }
6648d6560c4SGreg Roach
6658d6560c4SGreg Roach    /**
66602086832SGreg Roach     * During setup, we'll need access to some languages.
66702086832SGreg Roach     *
668b5c8fd7eSGreg Roach     * @return Collection<ModuleLanguageInterface>
66902086832SGreg Roach     */
67002086832SGreg Roach    public function setupLanguages(): Collection
67102086832SGreg Roach    {
67202086832SGreg Roach        return $this->coreModules()
6730b93976aSGreg Roach            ->filter(static function (ModuleInterface $module): bool {
67402086832SGreg Roach                return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();
67502086832SGreg Roach            })
6760b93976aSGreg Roach            ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int {
677c116a5ccSGreg Roach                return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();
678c116a5ccSGreg Roach            });
67902086832SGreg Roach    }
68002086832SGreg Roach
68102086832SGreg Roach    /**
6825bc7939cSBen     * Load a custom module in a static scope, to prevent it from modifying local or object variables.
6838d6560c4SGreg Roach     *
6848d6560c4SGreg Roach     * @param string $filename
6858d6560c4SGreg Roach     *
6868d6560c4SGreg Roach     * @return mixed
6878d6560c4SGreg Roach     */
6888d6560c4SGreg Roach    private static function load(string $filename)
6898d6560c4SGreg Roach    {
6905bc7939cSBen        try {
6918d6560c4SGreg Roach            return include $filename;
692ad602e4bSGreg Roach        } catch (Throwable $exception) {
6935bc7939cSBen            $module_name = basename(dirname($filename));
694ad602e4bSGreg Roach            $message = 'Fatal error in module: ' . $module_name;
695ad602e4bSGreg Roach            $message .= '<br>' . $exception;
696ad602e4bSGreg Roach            FlashMessages::addMessage($message, 'danger');
6975bc7939cSBen        }
6985bc7939cSBen        return null;
6998d6560c4SGreg Roach    }
7008d6560c4SGreg Roach
7018d6560c4SGreg Roach    /**
7028d6560c4SGreg Roach     * A function filter modules by enabled/disabled
7038d6560c4SGreg Roach     *
7048d6560c4SGreg Roach     * @param bool $include_disabled
7058d6560c4SGreg Roach     *
7068d6560c4SGreg Roach     * @return Closure
7078d6560c4SGreg Roach     */
7088d6560c4SGreg Roach    private function enabledFilter(bool $include_disabled): Closure
7098d6560c4SGreg Roach    {
7106c2179e2SGreg Roach        return static function (ModuleInterface $module) use ($include_disabled): bool {
7118d6560c4SGreg Roach            return $include_disabled || $module->isEnabled();
7128d6560c4SGreg Roach        };
7138d6560c4SGreg Roach    }
7148d6560c4SGreg Roach
7158d6560c4SGreg Roach    /**
7168d6560c4SGreg Roach     * A function filter modules by type
7178d6560c4SGreg Roach     *
7188d6560c4SGreg Roach     * @param string $interface
7198d6560c4SGreg Roach     *
7208d6560c4SGreg Roach     * @return Closure
7218d6560c4SGreg Roach     */
7228d6560c4SGreg Roach    private function interfaceFilter(string $interface): Closure
7238d6560c4SGreg Roach    {
7246c2179e2SGreg Roach        return static function (ModuleInterface $module) use ($interface): bool {
7258d6560c4SGreg Roach            return $module instanceof $interface;
7268d6560c4SGreg Roach        };
7278d6560c4SGreg Roach    }
7288d6560c4SGreg Roach
7298d6560c4SGreg Roach    /**
7308d6560c4SGreg Roach     * A function to sort footers
7318d6560c4SGreg Roach     *
7328d6560c4SGreg Roach     * @return Closure
7338d6560c4SGreg Roach     */
73437646143SGreg Roach    private function footerComparator(): Closure
7358d6560c4SGreg Roach    {
7366c2179e2SGreg Roach        return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
7378d6560c4SGreg Roach            return $x->getFooterOrder() <=> $y->getFooterOrder();
7388d6560c4SGreg Roach        };
7398d6560c4SGreg Roach    }
7408d6560c4SGreg Roach
7418d6560c4SGreg Roach    /**
7428d6560c4SGreg Roach     * A function to sort menus
7438d6560c4SGreg Roach     *
7448d6560c4SGreg Roach     * @return Closure
7458d6560c4SGreg Roach     */
74637646143SGreg Roach    private function menuComparator(): Closure
7478d6560c4SGreg Roach    {
7486c2179e2SGreg Roach        return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
7498d6560c4SGreg Roach            return $x->getMenuOrder() <=> $y->getMenuOrder();
7508d6560c4SGreg Roach        };
7518d6560c4SGreg Roach    }
7528d6560c4SGreg Roach
7538d6560c4SGreg Roach    /**
7548d6560c4SGreg Roach     * A function to sort menus
7558d6560c4SGreg Roach     *
7568d6560c4SGreg Roach     * @return Closure
7578d6560c4SGreg Roach     */
75837646143SGreg Roach    private function sidebarComparator(): Closure
7598d6560c4SGreg Roach    {
7606c2179e2SGreg Roach        return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
7618d6560c4SGreg Roach            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
7628d6560c4SGreg Roach        };
7638d6560c4SGreg Roach    }
7648d6560c4SGreg Roach
7658d6560c4SGreg Roach    /**
7668d6560c4SGreg Roach     * A function to sort menus
7678d6560c4SGreg Roach     *
7688d6560c4SGreg Roach     * @return Closure
7698d6560c4SGreg Roach     */
77037646143SGreg Roach    private function tabComparator(): Closure
7718d6560c4SGreg Roach    {
7726c2179e2SGreg Roach        return static function (ModuleTabInterface $x, ModuleTabInterface $y): int {
7738d6560c4SGreg Roach            return $x->getTabOrder() <=> $y->getTabOrder();
7748d6560c4SGreg Roach        };
7758d6560c4SGreg Roach    }
7768d6560c4SGreg Roach
7778d6560c4SGreg Roach    /**
77802086832SGreg Roach     * A function to sort modules by name.
77902086832SGreg Roach     *
78002086832SGreg Roach     * Languages have a "sortable" name, so that "British English" sorts as "English, British".
78102086832SGreg Roach     * This provides a more natural order in the language menu.
7828d6560c4SGreg Roach     *
7838d6560c4SGreg Roach     * @return Closure
7848d6560c4SGreg Roach     */
78537646143SGreg Roach    private function moduleComparator(): Closure
7868d6560c4SGreg Roach    {
7876c2179e2SGreg Roach        return static function (ModuleInterface $x, ModuleInterface $y): int {
78802086832SGreg Roach            $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();
78902086832SGreg Roach            $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();
79002086832SGreg Roach
79137646143SGreg Roach            return I18N::comparator()($title1, $title2);
7928d6560c4SGreg Roach        };
7938d6560c4SGreg Roach    }
7948d6560c4SGreg Roach
7958d6560c4SGreg Roach    /**
7964ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
7974ca7e03cSGreg Roach     *
7984ca7e03cSGreg Roach     * @param string $module_name
799321a89daSGreg Roach     * @param bool   $include_disabled
8004ca7e03cSGreg Roach     *
8014ca7e03cSGreg Roach     * @return ModuleInterface|null
8024ca7e03cSGreg Roach     */
803321a89daSGreg Roach    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
8044ca7e03cSGreg Roach    {
8059d627a9eSGreg Roach        return $this->all($include_disabled)
8060797053bSGreg Roach            ->first(static function (ModuleInterface $module) use ($module_name): bool {
807321a89daSGreg Roach                return $module->name() === $module_name;
8080797053bSGreg Roach            });
8094ca7e03cSGreg Roach    }
810bf57b580SGreg Roach
811bf57b580SGreg Roach    /**
812bf57b580SGreg Roach     * Configuration settings are available through the various "module component" pages.
813bf57b580SGreg Roach     * For modules that do not provide a component, we need to list them separately.
814bf57b580SGreg Roach     *
81509fbf3e5SGreg Roach     * @param bool $include_disabled
81609fbf3e5SGreg Roach     *
817b5c8fd7eSGreg Roach     * @return Collection<ModuleInterface>
818bf57b580SGreg Roach     */
81909fbf3e5SGreg Roach    public function otherModules(bool $include_disabled = false): Collection
820bf57b580SGreg Roach    {
821abafa13cSGreg Roach        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
8220b5fd0a6SGreg Roach            ->filter(static function (ModuleInterface $module): bool {
823bf57b580SGreg Roach                foreach (self::COMPONENTS as $interface) {
824bf57b580SGreg Roach                    if ($module instanceof $interface) {
825bf57b580SGreg Roach                        return false;
826bf57b580SGreg Roach                    }
827bf57b580SGreg Roach                }
828bf57b580SGreg Roach
829bf57b580SGreg Roach                return true;
830bf57b580SGreg Roach            });
831bf57b580SGreg Roach    }
832bf57b580SGreg Roach
833bf57b580SGreg Roach    /**
834bf57b580SGreg Roach     * Generate a list of module names which exist in the database but not on disk.
835bf57b580SGreg Roach     *
836b5c8fd7eSGreg Roach     * @return Collection<string>
837bf57b580SGreg Roach     */
838bf57b580SGreg Roach    public function deletedModules(): Collection
839bf57b580SGreg Roach    {
840bf57b580SGreg Roach        $database_modules = DB::table('module')->pluck('module_name');
841bf57b580SGreg Roach
842d5d58984SGreg Roach        $disk_modules = $this->all(true)
8430b5fd0a6SGreg Roach            ->map(static function (ModuleInterface $module): string {
844bf57b580SGreg Roach                return $module->name();
845bf57b580SGreg Roach            });
846bf57b580SGreg Roach
847bf57b580SGreg Roach        return $database_modules->diff($disk_modules);
848bf57b580SGreg Roach    }
84971378461SGreg Roach
85071378461SGreg Roach    /**
85171378461SGreg Roach     * Boot all the modules.
85271378461SGreg Roach     *
85371378461SGreg Roach     * @param ModuleThemeInterface $current_theme
85471378461SGreg Roach     */
855c81b7bf1SGreg Roach    public function bootModules(ModuleThemeInterface $current_theme): void
856c81b7bf1SGreg Roach    {
8579e18e23bSGreg Roach        foreach ($this->all() as $module) {
85871378461SGreg Roach            // Only bootstrap the current theme.
85971378461SGreg Roach            if ($module instanceof ModuleThemeInterface && $module !== $current_theme) {
86071378461SGreg Roach                continue;
86171378461SGreg Roach            }
86271378461SGreg Roach
8639e18e23bSGreg Roach            $module->boot();
86471378461SGreg Roach        }
86571378461SGreg Roach    }
866745968f0SGreg Roach
867745968f0SGreg Roach    /**
8689f667ff2SGreg Roach     * @return Collection<string>
869745968f0SGreg Roach     */
8709f667ff2SGreg Roach    public function componentsWithAccess(): Collection
871745968f0SGreg Roach    {
8729f667ff2SGreg Roach        return new Collection(self::COMPONENTS_WITH_ACCESS);
873745968f0SGreg Roach    }
874745968f0SGreg Roach
875745968f0SGreg Roach    /**
8769f667ff2SGreg Roach     * @return Collection<string>
877745968f0SGreg Roach     */
8789f667ff2SGreg Roach    public function componentsWithOrder(): Collection
879745968f0SGreg Roach    {
8809f667ff2SGreg Roach        return new Collection(self::COMPONENTS_WITH_SORT);
881745968f0SGreg Roach    }
8824ca7e03cSGreg Roach}
883