xref: /webtrees/app/Services/ModuleService.php (revision 076996987b136698e1f11dd9419e2fb115233c3b)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Closure;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\FlashMessages;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Module\AhnentafelReportModule;
28use Fisharebest\Webtrees\Module\AlbumModule;
29use Fisharebest\Webtrees\Module\AncestorsChartModule;
30use Fisharebest\Webtrees\Module\BatchUpdateModule;
31use Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
32use Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
33use Fisharebest\Webtrees\Module\BirthReportModule;
34use Fisharebest\Webtrees\Module\BranchesListModule;
35use Fisharebest\Webtrees\Module\BritishMonarchs;
36use Fisharebest\Webtrees\Module\BritishPrimeMinisters;
37use Fisharebest\Webtrees\Module\BritishSocialHistory;
38use Fisharebest\Webtrees\Module\CalendarMenuModule;
39use Fisharebest\Webtrees\Module\CemeteryReportModule;
40use Fisharebest\Webtrees\Module\CensusAssistantModule;
41use Fisharebest\Webtrees\Module\ChangeReportModule;
42use Fisharebest\Webtrees\Module\ChartsBlockModule;
43use Fisharebest\Webtrees\Module\ChartsMenuModule;
44use Fisharebest\Webtrees\Module\CkeditorModule;
45use Fisharebest\Webtrees\Module\ClippingsCartModule;
46use Fisharebest\Webtrees\Module\CloudsTheme;
47use Fisharebest\Webtrees\Module\ColorsTheme;
48use Fisharebest\Webtrees\Module\CompactTreeChartModule;
49use Fisharebest\Webtrees\Module\ContactsFooterModule;
50use Fisharebest\Webtrees\Module\CustomCssJsModule;
51use Fisharebest\Webtrees\Module\CzechMonarchsAndPresidents;
52use Fisharebest\Webtrees\Module\DeathReportModule;
53use Fisharebest\Webtrees\Module\DescendancyChartModule;
54use Fisharebest\Webtrees\Module\DescendancyModule;
55use Fisharebest\Webtrees\Module\DescendancyReportModule;
56use Fisharebest\Webtrees\Module\FabTheme;
57use Fisharebest\Webtrees\Module\FactSourcesReportModule;
58use Fisharebest\Webtrees\Module\FamilyBookChartModule;
59use Fisharebest\Webtrees\Module\FamilyGroupReportModule;
60use Fisharebest\Webtrees\Module\FamilyListModule;
61use Fisharebest\Webtrees\Module\FamilyNavigatorModule;
62use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
63use Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
64use Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
65use Fisharebest\Webtrees\Module\FanChartModule;
66use Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
67use Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
68use Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
69use Fisharebest\Webtrees\Module\HitCountFooterModule;
70use Fisharebest\Webtrees\Module\HourglassChartModule;
71use Fisharebest\Webtrees\Module\HtmlBlockModule;
72use Fisharebest\Webtrees\Module\IndividualFactsTabModule;
73use Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
74use Fisharebest\Webtrees\Module\IndividualListModule;
75use Fisharebest\Webtrees\Module\IndividualMetadataModule;
76use Fisharebest\Webtrees\Module\IndividualReportModule;
77use Fisharebest\Webtrees\Module\InteractiveTreeModule;
78use Fisharebest\Webtrees\Module\LanguageAfrikaans;
79use Fisharebest\Webtrees\Module\LanguageAlbanian;
80use Fisharebest\Webtrees\Module\LanguageArabic;
81use Fisharebest\Webtrees\Module\LanguageBosnian;
82use Fisharebest\Webtrees\Module\LanguageBulgarian;
83use Fisharebest\Webtrees\Module\LanguageCatalan;
84use Fisharebest\Webtrees\Module\LanguageChineseSimplified;
85use Fisharebest\Webtrees\Module\LanguageChineseTraditional;
86use Fisharebest\Webtrees\Module\LanguageCroatian;
87use Fisharebest\Webtrees\Module\LanguageCzech;
88use Fisharebest\Webtrees\Module\LanguageDanish;
89use Fisharebest\Webtrees\Module\LanguageDivehi;
90use Fisharebest\Webtrees\Module\LanguageDutch;
91use Fisharebest\Webtrees\Module\LanguageEnglishAustralia;
92use Fisharebest\Webtrees\Module\LanguageEnglishGreatBritain;
93use Fisharebest\Webtrees\Module\LanguageEnglishUnitedStates;
94use Fisharebest\Webtrees\Module\LanguageEstonian;
95use Fisharebest\Webtrees\Module\LanguageFaroese;
96use Fisharebest\Webtrees\Module\LanguageFarsi;
97use Fisharebest\Webtrees\Module\LanguageFinnish;
98use Fisharebest\Webtrees\Module\LanguageFrench;
99use Fisharebest\Webtrees\Module\LanguageFrenchCanada;
100use Fisharebest\Webtrees\Module\LanguageGalician;
101use Fisharebest\Webtrees\Module\LanguageGeorgian;
102use Fisharebest\Webtrees\Module\LanguageGerman;
103use Fisharebest\Webtrees\Module\LanguageGreek;
104use Fisharebest\Webtrees\Module\LanguageHebrew;
105use Fisharebest\Webtrees\Module\LanguageHindi;
106use Fisharebest\Webtrees\Module\LanguageHungarian;
107use Fisharebest\Webtrees\Module\LanguageIcelandic;
108use Fisharebest\Webtrees\Module\LanguageIndonesian;
109use Fisharebest\Webtrees\Module\LanguageItalian;
110use Fisharebest\Webtrees\Module\LanguageJapanese;
111use Fisharebest\Webtrees\Module\LanguageJavanese;
112use Fisharebest\Webtrees\Module\LanguageKazhak;
113use Fisharebest\Webtrees\Module\LanguageKorean;
114use Fisharebest\Webtrees\Module\LanguageKurdish;
115use Fisharebest\Webtrees\Module\LanguageLatvian;
116use Fisharebest\Webtrees\Module\LanguageLingala;
117use Fisharebest\Webtrees\Module\LanguageLithuanian;
118use Fisharebest\Webtrees\Module\LanguageMalay;
119use Fisharebest\Webtrees\Module\LanguageMaori;
120use Fisharebest\Webtrees\Module\LanguageMarathi;
121use Fisharebest\Webtrees\Module\LanguageNepalese;
122use Fisharebest\Webtrees\Module\LanguageNorwegianBokmal;
123use Fisharebest\Webtrees\Module\LanguageNorwegianNynorsk;
124use Fisharebest\Webtrees\Module\LanguageOccitan;
125use Fisharebest\Webtrees\Module\LanguagePolish;
126use Fisharebest\Webtrees\Module\LanguagePortuguese;
127use Fisharebest\Webtrees\Module\LanguagePortugueseBrazil;
128use Fisharebest\Webtrees\Module\LanguageRomanian;
129use Fisharebest\Webtrees\Module\LanguageRussian;
130use Fisharebest\Webtrees\Module\LanguageSerbian;
131use Fisharebest\Webtrees\Module\LanguageSerbianLatin;
132use Fisharebest\Webtrees\Module\LanguageSlovakian;
133use Fisharebest\Webtrees\Module\LanguageSlovenian;
134use Fisharebest\Webtrees\Module\LanguageSpanish;
135use Fisharebest\Webtrees\Module\LanguageSundanese;
136use Fisharebest\Webtrees\Module\LanguageSwahili;
137use Fisharebest\Webtrees\Module\LanguageSwedish;
138use Fisharebest\Webtrees\Module\LanguageTamil;
139use Fisharebest\Webtrees\Module\LanguageTatar;
140use Fisharebest\Webtrees\Module\LanguageThai;
141use Fisharebest\Webtrees\Module\LanguageTurkish;
142use Fisharebest\Webtrees\Module\LanguageUkranian;
143use Fisharebest\Webtrees\Module\LanguageVietnamese;
144use Fisharebest\Webtrees\Module\LanguageYiddish;
145use Fisharebest\Webtrees\Module\LifespansChartModule;
146use Fisharebest\Webtrees\Module\ListsMenuModule;
147use Fisharebest\Webtrees\Module\LoggedInUsersModule;
148use Fisharebest\Webtrees\Module\LoginBlockModule;
149use Fisharebest\Webtrees\Module\MarriageReportModule;
150use Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
151use Fisharebest\Webtrees\Module\MediaListModule;
152use Fisharebest\Webtrees\Module\MediaTabModule;
153use Fisharebest\Webtrees\Module\MinimalTheme;
154use Fisharebest\Webtrees\Module\MissingFactsReportModule;
155use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
156use Fisharebest\Webtrees\Module\ModuleBlockInterface;
157use Fisharebest\Webtrees\Module\ModuleChartInterface;
158use Fisharebest\Webtrees\Module\ModuleCustomInterface;
159use Fisharebest\Webtrees\Module\ModuleFooterInterface;
160use Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
161use Fisharebest\Webtrees\Module\ModuleInterface;
162use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
163use Fisharebest\Webtrees\Module\ModuleListInterface;
164use Fisharebest\Webtrees\Module\ModuleMenuInterface;
165use Fisharebest\Webtrees\Module\ModuleReportInterface;
166use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
167use Fisharebest\Webtrees\Module\ModuleTabInterface;
168use Fisharebest\Webtrees\Module\ModuleThemeInterface;
169use Fisharebest\Webtrees\Module\NoteListModule;
170use Fisharebest\Webtrees\Module\NotesTabModule;
171use Fisharebest\Webtrees\Module\OccupationReportModule;
172use Fisharebest\Webtrees\Module\OnThisDayModule;
173use Fisharebest\Webtrees\Module\PedigreeChartModule;
174use Fisharebest\Webtrees\Module\PedigreeMapModule;
175use Fisharebest\Webtrees\Module\PedigreeReportModule;
176use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
177use Fisharebest\Webtrees\Module\PlacesModule;
178use Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
179use Fisharebest\Webtrees\Module\PrivacyPolicy;
180use Fisharebest\Webtrees\Module\RecentChangesModule;
181use Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
182use Fisharebest\Webtrees\Module\RelationshipsChartModule;
183use Fisharebest\Webtrees\Module\RelativesTabModule;
184use Fisharebest\Webtrees\Module\ReportsMenuModule;
185use Fisharebest\Webtrees\Module\RepositoryListModule;
186use Fisharebest\Webtrees\Module\ResearchTaskModule;
187use Fisharebest\Webtrees\Module\ReviewChangesModule;
188use Fisharebest\Webtrees\Module\SearchMenuModule;
189use Fisharebest\Webtrees\Module\SiteMapModule;
190use Fisharebest\Webtrees\Module\SlideShowModule;
191use Fisharebest\Webtrees\Module\SourceListModule;
192use Fisharebest\Webtrees\Module\SourcesTabModule;
193use Fisharebest\Webtrees\Module\StatcounterModule;
194use Fisharebest\Webtrees\Module\StatisticsChartModule;
195use Fisharebest\Webtrees\Module\StoriesModule;
196use Fisharebest\Webtrees\Module\SubmitterListModule;
197use Fisharebest\Webtrees\Module\ThemeSelectModule;
198use Fisharebest\Webtrees\Module\TimelineChartModule;
199use Fisharebest\Webtrees\Module\TopGivenNamesModule;
200use Fisharebest\Webtrees\Module\TopPageViewsModule;
201use Fisharebest\Webtrees\Module\TopSurnamesModule;
202use Fisharebest\Webtrees\Module\TreesMenuModule;
203use Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
204use Fisharebest\Webtrees\Module\UserFavoritesModule;
205use Fisharebest\Webtrees\Module\UserJournalModule;
206use Fisharebest\Webtrees\Module\UserMessagesModule;
207use Fisharebest\Webtrees\Module\UserWelcomeModule;
208use Fisharebest\Webtrees\Module\USPresidents;
209use Fisharebest\Webtrees\Module\WebtreesTheme;
210use Fisharebest\Webtrees\Module\WelcomeBlockModule;
211use Fisharebest\Webtrees\Module\XeneaTheme;
212use Fisharebest\Webtrees\Module\YahrzeitModule;
213use Fisharebest\Webtrees\Tree;
214use Fisharebest\Webtrees\Webtrees;
215use Illuminate\Database\Capsule\Manager as DB;
216use Illuminate\Support\Collection;
217use Illuminate\Support\Str;
218use stdClass;
219use Throwable;
220
221use function app;
222
223/**
224 * Functions for managing and maintaining modules.
225 */
226class ModuleService
227{
228    // Components are pieces of user-facing functionality, are managed together in the control panel.
229    private const COMPONENTS = [
230        ModuleAnalyticsInterface::class,
231        ModuleBlockInterface::class,
232        ModuleChartInterface::class,
233        ModuleFooterInterface::class,
234        ModuleHistoricEventsInterface::class,
235        ModuleLanguageInterface::class,
236        ModuleListInterface::class,
237        ModuleMenuInterface::class,
238        ModuleReportInterface::class,
239        ModuleSidebarInterface::class,
240        ModuleTabInterface::class,
241        ModuleThemeInterface::class,
242    ];
243
244    // Components that have access levels.
245    private const COMPONENTS_WITH_ACCESS = [
246        ModuleBlockInterface::class,
247        ModuleChartInterface::class,
248        ModuleListInterface::class,
249        ModuleMenuInterface::class,
250        ModuleReportInterface::class,
251        ModuleSidebarInterface::class,
252        ModuleTabInterface::class,
253    ];
254
255    // Components that are displayed in a particular order
256    private const COMPONENTS_WITH_SORT = [
257        ModuleFooterInterface::class,
258        ModuleMenuInterface::class,
259        ModuleSidebarInterface::class,
260        ModuleTabInterface::class,
261    ];
262
263    // Array keys are module names, and should match module names from earlier versions of webtrees.
264    private const CORE_MODULES = [
265        'GEDFact_assistant'       => CensusAssistantModule::class,
266        'ahnentafel_report'       => AhnentafelReportModule::class,
267        'ancestors_chart'         => AncestorsChartModule::class,
268        'batch_update'            => BatchUpdateModule::class,
269        'bdm_report'              => BirthDeathMarriageReportModule::class,
270        'bing-webmaster-tools'    => BingWebmasterToolsModule::class,
271        'birth_report'            => BirthReportModule::class,
272        'branches_list'           => BranchesListModule::class,
273        'british-monarchs'        => BritishMonarchs::class,
274        'british-prime-ministers' => BritishPrimeMinisters::class,
275        'british-social-history'  => BritishSocialHistory::class,
276        'calendar-menu'           => CalendarMenuModule::class,
277        'cemetery_report'         => CemeteryReportModule::class,
278        'change_report'           => ChangeReportModule::class,
279        'charts'                  => ChartsBlockModule::class,
280        'charts-menu'             => ChartsMenuModule::class,
281        'ckeditor'                => CkeditorModule::class,
282        'clippings'               => ClippingsCartModule::class,
283        'clouds'                  => CloudsTheme::class,
284        'colors'                  => ColorsTheme::class,
285        'compact-chart'           => CompactTreeChartModule::class,
286        'contact-links'           => ContactsFooterModule::class,
287        'czech-leaders'           => CzechMonarchsAndPresidents::class,
288        'custom-css-js'           => CustomCssJsModule::class,
289        'death_report'            => DeathReportModule::class,
290        'descendancy'             => DescendancyModule::class,
291        'descendancy_chart'       => DescendancyChartModule::class,
292        'descendancy_report'      => DescendancyReportModule::class,
293        'extra_info'              => IndividualMetadataModule::class,
294        'fab'                     => FabTheme::class,
295        'fact_sources'            => FactSourcesReportModule::class,
296        'family_book_chart'       => FamilyBookChartModule::class,
297        'family_group_report'     => FamilyGroupReportModule::class,
298        'family_list'             => FamilyListModule::class,
299        'family_nav'              => FamilyNavigatorModule::class,
300        'fan_chart'               => FanChartModule::class,
301        'faq'                     => FrequentlyAskedQuestionsModule::class,
302        'gedcom_block'            => WelcomeBlockModule::class,
303        'gedcom_favorites'        => FamilyTreeFavoritesModule::class,
304        'gedcom_news'             => FamilyTreeNewsModule::class,
305        'gedcom_stats'            => FamilyTreeStatisticsModule::class,
306        'google-analytics'        => GoogleAnalyticsModule::class,
307        'google-webmaster-tools'  => GoogleWebmasterToolsModule::class,
308        'hit-counter'             => HitCountFooterModule::class,
309        'hourglass_chart'         => HourglassChartModule::class,
310        'html'                    => HtmlBlockModule::class,
311        'individual_ext_report'   => IndividualFamiliesReportModule::class,
312        'individual_list'         => IndividualListModule::class,
313        'individual_report'       => IndividualReportModule::class,
314        'language-af'             => LanguageAfrikaans::class,
315        'language-ar'             => LanguageArabic::class,
316        'language-bs'             => LanguageBosnian::class,
317        'language-bu'             => LanguageBulgarian::class,
318        'language-ca'             => LanguageCatalan::class,
319        'language-cs'             => LanguageCzech::class,
320        'language-da'             => LanguageDanish::class,
321        'language-de'             => LanguageGerman::class,
322        'language-dv'             => LanguageDivehi::class,
323        'language-el'             => LanguageGreek::class,
324        'language-en-AU'          => LanguageEnglishAustralia::class,
325        'language-en-GB'          => LanguageEnglishGreatBritain::class,
326        'language-en-US'          => LanguageEnglishUnitedStates::class,
327        'language-es'             => LanguageSpanish::class,
328        'language-et'             => LanguageEstonian::class,
329        'language-fa'             => LanguageFarsi::class,
330        'language-fi'             => LanguageFinnish::class,
331        'language-fo'             => LanguageFaroese::class,
332        'language-fr'             => LanguageFrench::class,
333        'language-fr-CA'          => LanguageFrenchCanada::class,
334        'language-gl'             => LanguageGalician::class,
335        'language-he'             => LanguageHebrew::class,
336        'language-hi'             => LanguageHindi::class,
337        'language-hr'             => LanguageCroatian::class,
338        'language-hu'             => LanguageHungarian::class,
339        'language-id'             => LanguageIndonesian::class,
340        'language-is'             => LanguageIcelandic::class,
341        'language-it'             => LanguageItalian::class,
342        'language-ja'             => LanguageJapanese::class,
343        'language-jv'             => LanguageJavanese::class,
344        'language-ka'             => LanguageGeorgian::class,
345        'language-kk'             => LanguageKazhak::class,
346        'language-ko'             => LanguageKorean::class,
347        'language-ku'             => LanguageKurdish::class,
348        'language-ln'             => LanguageLingala::class,
349        'language-lt'             => LanguageLithuanian::class,
350        'language-lv'             => LanguageLatvian::class,
351        'language-mi'             => LanguageMaori::class,
352        'language-mr'             => LanguageMarathi::class,
353        'language-ms'             => LanguageMalay::class,
354        'language-nb'             => LanguageNorwegianBokmal::class,
355        'language-ne'             => LanguageNepalese::class,
356        'language-nl'             => LanguageDutch::class,
357        'language-nn'             => LanguageNorwegianNynorsk::class,
358        'language-oc'             => LanguageOccitan::class,
359        'language-pl'             => LanguagePolish::class,
360        'language-pt'             => LanguagePortuguese::class,
361        'language-pt-BR'          => LanguagePortugueseBrazil::class,
362        'language-ro'             => LanguageRomanian::class,
363        'language-ru'             => LanguageRussian::class,
364        'language-sk'             => LanguageSlovakian::class,
365        'language-sl'             => LanguageSlovenian::class,
366        'language-sq'             => LanguageAlbanian::class,
367        'language-sr'             => LanguageSerbian::class,
368        'language-sr-Latn'        => LanguageSerbianLatin::class,
369        'language-su'             => LanguageSundanese::class,
370        'language-sv'             => LanguageSwedish::class,
371        'language-sw'             => LanguageSwahili::class,
372        'language-ta'             => LanguageTamil::class,
373        'language-th'             => LanguageThai::class,
374        'language-tr'             => LanguageTurkish::class,
375        'language-tt'             => LanguageTatar::class,
376        'language-uk'             => LanguageUkranian::class,
377        'language-vi'             => LanguageVietnamese::class,
378        'language-yi'             => LanguageYiddish::class,
379        'language-zh-Hans'        => LanguageChineseSimplified::class,
380        'language-zh-Hant'        => LanguageChineseTraditional::class,
381        'lifespans_chart'         => LifespansChartModule::class,
382        'lightbox'                => AlbumModule::class,
383        'lists-menu'              => ListsMenuModule::class,
384        'logged_in'               => LoggedInUsersModule::class,
385        'login_block'             => LoginBlockModule::class,
386        'marriage_report'         => MarriageReportModule::class,
387        'matomo-analytics'        => MatomoAnalyticsModule::class,
388        'media'                   => MediaTabModule::class,
389        'media_list'              => MediaListModule::class,
390        'minimal'                 => MinimalTheme::class,
391        'missing_facts_report'    => MissingFactsReportModule::class,
392        'notes'                   => NotesTabModule::class,
393        'note_list'               => NoteListModule::class,
394        'occupation_report'       => OccupationReportModule::class,
395        'pedigree-map'            => PedigreeMapModule::class,
396        'pedigree_chart'          => PedigreeChartModule::class,
397        'pedigree_report'         => PedigreeReportModule::class,
398        'personal_facts'          => IndividualFactsTabModule::class,
399        'places'                  => PlacesModule::class,
400        'places_list'             => PlaceHierarchyListModule::class,
401        'powered-by-webtrees'     => PoweredByWebtreesModule::class,
402        'privacy-policy'          => PrivacyPolicy::class,
403        'random_media'            => SlideShowModule::class,
404        'recent_changes'          => RecentChangesModule::class,
405        'relationships_chart'     => RelationshipsChartModule::class,
406        'relative_ext_report'     => RelatedIndividualsReportModule::class,
407        'relatives'               => RelativesTabModule::class,
408        'reports-menu'            => ReportsMenuModule::class,
409        'repository_list'         => RepositoryListModule::class,
410        'review_changes'          => ReviewChangesModule::class,
411        'search-menu'             => SearchMenuModule::class,
412        'sitemap'                 => SiteMapModule::class,
413        'source_list'             => SourceListModule::class,
414        'sources_tab'             => SourcesTabModule::class,
415        'statcounter'             => StatcounterModule::class,
416        'statistics_chart'        => StatisticsChartModule::class,
417        'stories'                 => StoriesModule::class,
418        'submitter_list'          => SubmitterListModule::class,
419        'theme_select'            => ThemeSelectModule::class,
420        'timeline_chart'          => TimelineChartModule::class,
421        'todays_events'           => OnThisDayModule::class,
422        'todo'                    => ResearchTaskModule::class,
423        'top10_givnnames'         => TopGivenNamesModule::class,
424        'top10_pageviews'         => TopPageViewsModule::class,
425        'top10_surnames'          => TopSurnamesModule::class,
426        'tree'                    => InteractiveTreeModule::class,
427        'trees-menu'              => TreesMenuModule::class,
428        'upcoming_events'         => UpcomingAnniversariesModule::class,
429        'us-presidents'           => USPresidents::class,
430        'user_blog'               => UserJournalModule::class,
431        'user_favorites'          => UserFavoritesModule::class,
432        'user_messages'           => UserMessagesModule::class,
433        'user_welcome'            => UserWelcomeModule::class,
434        'webtrees'                => WebtreesTheme::class,
435        'xenea'                   => XeneaTheme::class,
436        'yahrzeit'                => YahrzeitModule::class,
437    ];
438
439    /**
440     * A function to convert modules into their titles - to create option lists, etc.
441     *
442     * @return Closure
443     */
444    public function titleMapper(): Closure
445    {
446        return static function (ModuleInterface $module): string {
447            return $module->title();
448        };
449    }
450
451    /**
452     * Modules which (a) provide a specific function and (b) we have permission to see.
453     *
454     * @param string        $interface
455     * @param Tree          $tree
456     * @param UserInterface $user
457     *
458     * @return Collection<ModuleInterface>
459     */
460    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
461    {
462        return $this->findByInterface($interface, false, true)
463            ->filter(static function (ModuleInterface $module) use ($interface, $tree, $user): bool {
464                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
465            });
466    }
467
468    /**
469     * All modules which provide a specific function.
470     *
471     * @param string $interface
472     * @param bool   $include_disabled
473     * @param bool   $sort
474     *
475     * @return Collection<ModuleInterface>
476     */
477    public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection
478    {
479        $modules = $this->all($include_disabled)
480            ->filter($this->interfaceFilter($interface));
481
482        switch ($interface) {
483            case ModuleFooterInterface::class:
484                return $modules->sort($this->footerSorter());
485
486            case ModuleMenuInterface::class:
487                return $modules->sort($this->menuSorter());
488
489            case ModuleSidebarInterface::class:
490                return $modules->sort($this->sidebarSorter());
491
492            case ModuleTabInterface::class:
493                return $modules->sort($this->tabSorter());
494
495            default:
496                if ($sort) {
497                    return $modules->sort($this->moduleSorter());
498                }
499
500                return $modules;
501        }
502    }
503
504    /**
505     * All modules.
506     *
507     * @param bool $include_disabled
508     *
509     * @return Collection<ModuleInterface>
510     */
511    public function all(bool $include_disabled = false): Collection
512    {
513        return app('cache.array')->remember('all-modules', function (): Collection {
514            // Modules have a default status, order etc.
515            // We can override these from database settings.
516            $module_info = DB::table('module')
517                ->get()
518                ->mapWithKeys(static function (stdClass $row): array {
519                    return [$row->module_name => $row];
520                });
521
522            return $this->coreModules()
523                ->merge($this->customModules())
524                ->map(static function (ModuleInterface $module) use ($module_info): ModuleInterface {
525                    $info = $module_info->get($module->name());
526
527                    if ($info instanceof stdClass) {
528                        $module->setEnabled($info->status === 'enabled');
529
530                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
531                            $module->setFooterOrder((int) $info->footer_order);
532                        }
533
534                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
535                            $module->setMenuOrder((int) $info->menu_order);
536                        }
537
538                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
539                            $module->setSidebarOrder((int) $info->sidebar_order);
540                        }
541
542                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
543                            $module->setTabOrder((int) $info->tab_order);
544                        }
545                    } else {
546                        $module->setEnabled($module->isEnabledByDefault());
547
548                        DB::table('module')->insert([
549                            'module_name' => $module->name(),
550                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
551                        ]);
552                    }
553
554                    return $module;
555                });
556        })->filter($this->enabledFilter($include_disabled));
557    }
558
559    /**
560     * All core modules in the system.
561     *
562     * @return Collection<ModuleInterface>
563     */
564    private function coreModules(): Collection
565    {
566        return Collection::make(self::CORE_MODULES)
567            ->map(static function (string $class, string $name): ModuleInterface {
568                $module = app($class);
569
570                $module->setName($name);
571
572                return $module;
573            });
574    }
575
576    /**
577     * All custom modules in the system.  Custom modules are defined in modules_v4/
578     *
579     * @return Collection<ModuleCustomInterface>
580     */
581    private function customModules(): Collection
582    {
583        $pattern   = Webtrees::MODULES_DIR . '*/module.php';
584        $filenames = glob($pattern, GLOB_NOSORT);
585
586        return Collection::make($filenames)
587            ->filter(static function (string $filename): bool {
588                // Special characters will break PHP variable names.
589                // This also allows us to ignore modules called "foo.example" and "foo.disable"
590                $module_name = basename(dirname($filename));
591
592                return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30;
593            })
594            ->map(static function (string $filename): ?ModuleCustomInterface {
595                try {
596                    $module = self::load($filename);
597
598                    if ($module instanceof ModuleCustomInterface) {
599                        $module_name = '_' . basename(dirname($filename)) . '_';
600
601                        $module->setName($module_name);
602                    } else {
603                        return null;
604                    }
605
606                    return $module;
607                } catch (Throwable $ex) {
608                    // It would be nice to show this error in a flash-message or similar, but the framework
609                    // has not yet been initialised so we have no themes, languages, sessions, etc.
610                    throw $ex;
611                }
612            })
613            ->filter()
614            ->mapWithKeys(static function (ModuleCustomInterface $module): array {
615                return [$module->name() => $module];
616            });
617    }
618
619    /**
620     * During setup, we'll need access to some languages.
621     *
622     * @return Collection<ModuleLanguageInterface>
623     */
624    public function setupLanguages(): Collection
625    {
626        return $this->coreModules()
627            ->filter(static function (ModuleInterface $module): bool {
628                return $module instanceof ModuleLanguageInterface && $module->isEnabledByDefault();
629            })
630            ->sort(static function (ModuleLanguageInterface $x, ModuleLanguageInterface $y): int {
631                return $x->locale()->endonymSortable() <=> $y->locale()->endonymSortable();
632            });
633    }
634
635    /**
636     * Load a custom module in a static scope, to prevent it from modifying local or object variables.
637     *
638     * @param string $filename
639     *
640     * @return mixed
641     */
642    private static function load(string $filename)
643    {
644        try {
645            return include $filename;
646        } catch (Throwable $exception) {
647            $module_name = basename(dirname($filename));
648            $message = 'Fatal error in module: ' . $module_name;
649            $message .= '<br>' . $exception;
650            FlashMessages::addMessage($message, 'danger');
651        }
652        return null;
653    }
654
655    /**
656     * A function filter modules by enabled/disabled
657     *
658     * @param bool $include_disabled
659     *
660     * @return Closure
661     */
662    private function enabledFilter(bool $include_disabled): Closure
663    {
664        return static function (ModuleInterface $module) use ($include_disabled): bool {
665            return $include_disabled || $module->isEnabled();
666        };
667    }
668
669    /**
670     * A function filter modules by type
671     *
672     * @param string $interface
673     *
674     * @return Closure
675     */
676    private function interfaceFilter(string $interface): Closure
677    {
678        return static function (ModuleInterface $module) use ($interface): bool {
679            return $module instanceof $interface;
680        };
681    }
682
683    /**
684     * A function to sort footers
685     *
686     * @return Closure
687     */
688    private function footerSorter(): Closure
689    {
690        return static function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
691            return $x->getFooterOrder() <=> $y->getFooterOrder();
692        };
693    }
694
695    /**
696     * A function to sort menus
697     *
698     * @return Closure
699     */
700    private function menuSorter(): Closure
701    {
702        return static function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
703            return $x->getMenuOrder() <=> $y->getMenuOrder();
704        };
705    }
706
707    /**
708     * A function to sort menus
709     *
710     * @return Closure
711     */
712    private function sidebarSorter(): Closure
713    {
714        return static function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
715            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
716        };
717    }
718
719    /**
720     * A function to sort menus
721     *
722     * @return Closure
723     */
724    private function tabSorter(): Closure
725    {
726        return static function (ModuleTabInterface $x, ModuleTabInterface $y): int {
727            return $x->getTabOrder() <=> $y->getTabOrder();
728        };
729    }
730
731    /**
732     * A function to sort modules by name.
733     *
734     * Languages have a "sortable" name, so that "British English" sorts as "English, British".
735     * This provides a more natural order in the language menu.
736     *
737     * @return Closure
738     */
739    private function moduleSorter(): Closure
740    {
741        return static function (ModuleInterface $x, ModuleInterface $y): int {
742            $title1 = $x instanceof ModuleLanguageInterface ? $x->locale()->endonymSortable() : $x->title();
743            $title2 = $y instanceof ModuleLanguageInterface ? $y->locale()->endonymSortable() : $y->title();
744
745            return I18N::strcasecmp($title1, $title2);
746        };
747    }
748
749    /**
750     * Find a specified module, if it is currently active.
751     *
752     * @param string $module_name
753     * @param bool   $include_disabled
754     *
755     * @return ModuleInterface|null
756     */
757    public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface
758    {
759        return $this->all($include_disabled)
760            ->first(static function (ModuleInterface $module) use ($module_name): bool {
761                return $module->name() === $module_name;
762            });
763    }
764
765    /**
766     * Configuration settings are available through the various "module component" pages.
767     * For modules that do not provide a component, we need to list them separately.
768     *
769     * @param bool $include_disabled
770     *
771     * @return Collection<ModuleInterface>
772     */
773    public function otherModules(bool $include_disabled = false): Collection
774    {
775        return $this->findByInterface(ModuleInterface::class, $include_disabled, true)
776            ->filter(static function (ModuleInterface $module): bool {
777                foreach (self::COMPONENTS as $interface) {
778                    if ($module instanceof $interface) {
779                        return false;
780                    }
781                }
782
783                return true;
784            });
785    }
786
787    /**
788     * Generate a list of module names which exist in the database but not on disk.
789     *
790     * @return Collection<string>
791     */
792    public function deletedModules(): Collection
793    {
794        $database_modules = DB::table('module')->pluck('module_name');
795
796        $disk_modules = $this->all(true)
797            ->map(static function (ModuleInterface $module): string {
798                return $module->name();
799            });
800
801        return $database_modules->diff($disk_modules);
802    }
803
804    /**
805     * Boot all the modules.
806     *
807     * @param ModuleThemeInterface $current_theme
808     */
809    public function bootModules(ModuleThemeInterface $current_theme): void
810    {
811        foreach ($this->all() as $module) {
812            // Only bootstrap the current theme.
813            if ($module instanceof ModuleThemeInterface && $module !== $current_theme) {
814                continue;
815            }
816
817            $module->boot();
818        }
819    }
820
821    /**
822     * @return string[]
823     */
824    public function componentsWithAccess(): array
825    {
826        return self::COMPONENTS_WITH_ACCESS;
827    }
828
829    /**
830     * @return string[]
831     */
832    public function componentsWithOrder(): array
833    {
834        return self::COMPONENTS_WITH_SORT;
835    }
836}
837