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