xref: /webtrees/app/Services/ModuleService.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
14ca7e03cSGreg Roach<?php
24ca7e03cSGreg Roach/**
34ca7e03cSGreg Roach * webtrees: online genealogy
44ca7e03cSGreg Roach * Copyright (C) 2019 webtrees development team
54ca7e03cSGreg Roach * This program is free software: you can redistribute it and/or modify
64ca7e03cSGreg Roach * it under the terms of the GNU General Public License as published by
74ca7e03cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ca7e03cSGreg Roach * (at your option) any later version.
94ca7e03cSGreg Roach * This program is distributed in the hope that it will be useful,
104ca7e03cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ca7e03cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ca7e03cSGreg Roach * GNU General Public License for more details.
134ca7e03cSGreg Roach * You should have received a copy of the GNU General Public License
144ca7e03cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ca7e03cSGreg Roach */
164ca7e03cSGreg Roachdeclare(strict_types=1);
174ca7e03cSGreg Roach
184ca7e03cSGreg Roachnamespace Fisharebest\Webtrees\Services;
194ca7e03cSGreg Roach
204ca7e03cSGreg Roachuse Closure;
214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Auth;
22e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
244ca7e03cSGreg Roachuse Fisharebest\Webtrees\I18N;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AhnentafelReportModule;
264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AlbumModule;
274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\AncestorsChartModule;
284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BatchUpdateModule;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BingWebmasterToolsModule;
304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule;
314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\BirthReportModule;
3267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\BranchesListModule;
334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CalendarMenuModule;
344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CemeteryReportModule;
354ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CensusAssistantModule;
364ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChangeReportModule;
374ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsBlockModule;
384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ChartsMenuModule;
394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CkeditorModule;
404ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ClippingsCartModule;
414ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CloudsTheme;
424ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ColorsTheme;
434ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CompactTreeChartModule;
444ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ContactsFooterModule;
454ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\CookieWarningModule;
464ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DeathReportModule;
474ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyChartModule;
484ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyModule;
494ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyReportModule;
508eaf8709SGreg Roachuse Fisharebest\Webtrees\Module\IndividualMetadataModule;
514ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FabTheme;
524ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FactSourcesReportModule;
534ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyBookChartModule;
544ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyGroupReportModule;
5567992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\FamilyListModule;
564ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyNavigatorModule;
574ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule;
584ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeNewsModule;
594ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule;
604ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FanChartModule;
614ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule;
624ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleAnalyticsModule;
634ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule;
644ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HitCountFooterModule;
654ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HourglassChartModule;
664ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\HtmlBlockModule;
674ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFactsTabModule;
684ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualFamiliesReportModule;
6967992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\IndividualListModule;
704ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\IndividualReportModule;
714ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTreeModule;
724ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LifespansChartModule;
734ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ListsMenuModule;
744ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoggedInUsersModule;
754ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\LoginBlockModule;
764ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MarriageReportModule;
774ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MatomoAnalyticsModule;
7867992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\MediaListModule;
794ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MediaTabModule;
804ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MinimalTheme;
814ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\MissingFactsReportModule;
82bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
834ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
844ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
85bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleConfigInterface;
864ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleCustomInterface;
874ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleFooterInterface;
88bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
894ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
90bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleLanguageInterface;
9167992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\ModuleListInterface;
924ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
934ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
944ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
954ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
96bf57b580SGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
9767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\NoteListModule;
984ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\NotesTabModule;
994ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OccupationReportModule;
1004ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\OnThisDayModule;
1014ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeChartModule;
1024ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeMapModule;
1034ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PedigreeReportModule;
10467992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
1054ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PlacesModule;
1064ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\PoweredByWebtreesModule;
1074ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RecentChangesModule;
1084ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelatedIndividualsReportModule;
1094ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelationshipsChartModule;
1104ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\RelativesTabModule;
1114ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReportsMenuModule;
11267992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\RepositoryListModule;
1134ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ResearchTaskModule;
1144ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ReviewChangesModule;
1154ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SearchMenuModule;
1164ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SiteMapModule;
1174ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SlideShowModule;
11867992b6aSRichard Cisseeuse Fisharebest\Webtrees\Module\SourceListModule;
1194ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\SourcesTabModule;
1204ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatcounterModule;
1214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StatisticsChartModule;
1224ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\StoriesModule;
1234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ThemeSelectModule;
1244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TimelineChartModule;
1254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopGivenNamesModule;
1264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopPageViewsModule;
1274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TopSurnamesModule;
1284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TreesMenuModule;
1294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UpcomingAnniversariesModule;
1304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserFavoritesModule;
1314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserJournalModule;
1324ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserMessagesModule;
1334ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\UserWelcomeModule;
1344ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WebtreesTheme;
1354ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\WelcomeBlockModule;
1364ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\XeneaTheme;
1374ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\YahrzeitModule;
1384ca7e03cSGreg Roachuse Fisharebest\Webtrees\Tree;
1394ca7e03cSGreg Roachuse Fisharebest\Webtrees\Webtrees;
1404ca7e03cSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
1414ca7e03cSGreg Roachuse Illuminate\Support\Collection;
1424ca7e03cSGreg Roachuse Illuminate\Support\Str;
1434ca7e03cSGreg Roachuse stdClass;
1444ca7e03cSGreg Roachuse Throwable;
1454ca7e03cSGreg Roach
1464ca7e03cSGreg Roach/**
1474ca7e03cSGreg Roach * Functions for managing and maintaining modules.
1484ca7e03cSGreg Roach */
1494ca7e03cSGreg Roachclass ModuleService
1504ca7e03cSGreg Roach{
151bf57b580SGreg Roach    // Components are managed together in the control panel.
1524ca7e03cSGreg Roach    private const COMPONENTS = [
153*87cca37cSGreg Roach        ModuleAnalyticsInterface::class,
154*87cca37cSGreg Roach        ModuleBlockInterface::class,
155*87cca37cSGreg Roach        ModuleChartInterface::class,
156*87cca37cSGreg Roach        ModuleFooterInterface::class,
157*87cca37cSGreg Roach        ModuleHistoricEventsInterface::class,
158*87cca37cSGreg Roach        ModuleLanguageInterface::class,
159*87cca37cSGreg Roach        ModuleListInterface::class,
160*87cca37cSGreg Roach        ModuleMenuInterface::class,
161*87cca37cSGreg Roach        ModuleReportInterface::class,
162*87cca37cSGreg Roach        ModuleSidebarInterface::class,
163*87cca37cSGreg Roach        ModuleTabInterface::class,
164*87cca37cSGreg Roach        ModuleThemeInterface::class,
1654ca7e03cSGreg Roach    ];
1664ca7e03cSGreg Roach
1674ca7e03cSGreg Roach    // Array keys are module names, and should match module names from earlier versions of webtrees.
1684ca7e03cSGreg Roach    private const CORE_MODULES = [
1694ca7e03cSGreg Roach        'GEDFact_assistant'      => CensusAssistantModule::class,
1704ca7e03cSGreg Roach        'ahnentafel_report'      => AhnentafelReportModule::class,
1714ca7e03cSGreg Roach        'ancestors_chart'        => AncestorsChartModule::class,
1724ca7e03cSGreg Roach        'batch_update'           => BatchUpdateModule::class,
1734ca7e03cSGreg Roach        'bdm_report'             => BirthDeathMarriageReportModule::class,
1744ca7e03cSGreg Roach        'bing-webmaster-tools'   => BingWebmasterToolsModule::class,
1754ca7e03cSGreg Roach        'birth_report'           => BirthReportModule::class,
17667992b6aSRichard Cissee        'branches_list'          => BranchesListModule::class,
1774ca7e03cSGreg Roach        'calendar-menu'          => CalendarMenuModule::class,
1784ca7e03cSGreg Roach        'cemetery_report'        => CemeteryReportModule::class,
1794ca7e03cSGreg Roach        'change_report'          => ChangeReportModule::class,
1804ca7e03cSGreg Roach        'charts'                 => ChartsBlockModule::class,
1814ca7e03cSGreg Roach        'charts-menu'            => ChartsMenuModule::class,
1824ca7e03cSGreg Roach        'ckeditor'               => CkeditorModule::class,
1834ca7e03cSGreg Roach        'clippings'              => ClippingsCartModule::class,
1844ca7e03cSGreg Roach        'clouds'                 => CloudsTheme::class,
1854ca7e03cSGreg Roach        'colors'                 => ColorsTheme::class,
1864ca7e03cSGreg Roach        'compact-chart'          => CompactTreeChartModule::class,
1874ca7e03cSGreg Roach        'contact-links'          => ContactsFooterModule::class,
1884ca7e03cSGreg Roach        'cookie-warning'         => CookieWarningModule::class,
1894ca7e03cSGreg Roach        'death_report'           => DeathReportModule::class,
1904ca7e03cSGreg Roach        'descendancy'            => DescendancyModule::class,
1914ca7e03cSGreg Roach        'descendancy_chart'      => DescendancyChartModule::class,
1924ca7e03cSGreg Roach        'descendancy_report'     => DescendancyReportModule::class,
1938eaf8709SGreg Roach        'extra_info'             => IndividualMetadataModule::class,
1944ca7e03cSGreg Roach        'fab'                    => FabTheme::class,
1954ca7e03cSGreg Roach        'fact_sources'           => FactSourcesReportModule::class,
1964ca7e03cSGreg Roach        'family_book_chart'      => FamilyBookChartModule::class,
1974ca7e03cSGreg Roach        'family_group_report'    => FamilyGroupReportModule::class,
19867992b6aSRichard Cissee        'family_list'            => FamilyListModule::class,
1994ca7e03cSGreg Roach        'family_nav'             => FamilyNavigatorModule::class,
2004ca7e03cSGreg Roach        'fan_chart'              => FanChartModule::class,
2014ca7e03cSGreg Roach        'faq'                    => FrequentlyAskedQuestionsModule::class,
2024ca7e03cSGreg Roach        'gedcom_block'           => WelcomeBlockModule::class,
2034ca7e03cSGreg Roach        'gedcom_favorites'       => FamilyTreeFavoritesModule::class,
2044ca7e03cSGreg Roach        'gedcom_news'            => FamilyTreeNewsModule::class,
2054ca7e03cSGreg Roach        'gedcom_stats'           => FamilyTreeStatisticsModule::class,
2064ca7e03cSGreg Roach        'google-analytics'       => GoogleAnalyticsModule::class,
2074ca7e03cSGreg Roach        'google-webmaster-tools' => GoogleWebmasterToolsModule::class,
2084ca7e03cSGreg Roach        'hit-counter'            => HitCountFooterModule::class,
2094ca7e03cSGreg Roach        'hourglass_chart'        => HourglassChartModule::class,
2104ca7e03cSGreg Roach        'html'                   => HtmlBlockModule::class,
2114ca7e03cSGreg Roach        'individual_ext_report'  => IndividualFamiliesReportModule::class,
21267992b6aSRichard Cissee        'individual_list'        => IndividualListModule::class,
2134ca7e03cSGreg Roach        'individual_report'      => IndividualReportModule::class,
2144ca7e03cSGreg Roach        'lifespans_chart'        => LifespansChartModule::class,
2154ca7e03cSGreg Roach        'lightbox'               => AlbumModule::class,
2164ca7e03cSGreg Roach        'lists-menu'             => ListsMenuModule::class,
2174ca7e03cSGreg Roach        'logged_in'              => LoggedInUsersModule::class,
2184ca7e03cSGreg Roach        'login_block'            => LoginBlockModule::class,
2194ca7e03cSGreg Roach        'marriage_report'        => MarriageReportModule::class,
2204ca7e03cSGreg Roach        'matomo-analytics'       => MatomoAnalyticsModule::class,
2214ca7e03cSGreg Roach        'media'                  => MediaTabModule::class,
22267992b6aSRichard Cissee        'media_list'             => MediaListModule::class,
2234ca7e03cSGreg Roach        'minimal'                => MinimalTheme::class,
2244ca7e03cSGreg Roach        'missing_facts_report'   => MissingFactsReportModule::class,
2254ca7e03cSGreg Roach        'notes'                  => NotesTabModule::class,
22667992b6aSRichard Cissee        'note_list'              => NoteListModule::class,
2274ca7e03cSGreg Roach        'occupation_report'      => OccupationReportModule::class,
2284ca7e03cSGreg Roach        'pedigree-map'           => PedigreeMapModule::class,
2294ca7e03cSGreg Roach        'pedigree_chart'         => PedigreeChartModule::class,
2304ca7e03cSGreg Roach        'pedigree_report'        => PedigreeReportModule::class,
2314ca7e03cSGreg Roach        'personal_facts'         => IndividualFactsTabModule::class,
2324ca7e03cSGreg Roach        'places'                 => PlacesModule::class,
23367992b6aSRichard Cissee        'places_list'            => PlaceHierarchyListModule::class,
2344ca7e03cSGreg Roach        'powered-by-webtrees'    => PoweredByWebtreesModule::class,
2354ca7e03cSGreg Roach        'random_media'           => SlideShowModule::class,
2364ca7e03cSGreg Roach        'recent_changes'         => RecentChangesModule::class,
2374ca7e03cSGreg Roach        'relationships_chart'    => RelationshipsChartModule::class,
2384ca7e03cSGreg Roach        'relative_ext_report'    => RelatedIndividualsReportModule::class,
2394ca7e03cSGreg Roach        'relatives'              => RelativesTabModule::class,
2404ca7e03cSGreg Roach        'reports-menu'           => ReportsMenuModule::class,
24167992b6aSRichard Cissee        'repository_list'        => RepositoryListModule::class,
2424ca7e03cSGreg Roach        'review_changes'         => ReviewChangesModule::class,
2434ca7e03cSGreg Roach        'search-menu'            => SearchMenuModule::class,
2444ca7e03cSGreg Roach        'sitemap'                => SiteMapModule::class,
24567992b6aSRichard Cissee        'source_list'            => SourceListModule::class,
2464ca7e03cSGreg Roach        'sources_tab'            => SourcesTabModule::class,
2474ca7e03cSGreg Roach        'statcounter'            => StatcounterModule::class,
2484ca7e03cSGreg Roach        'statistics_chart'       => StatisticsChartModule::class,
2494ca7e03cSGreg Roach        'stories'                => StoriesModule::class,
2504ca7e03cSGreg Roach        'theme_select'           => ThemeSelectModule::class,
2514ca7e03cSGreg Roach        'timeline_chart'         => TimelineChartModule::class,
2524ca7e03cSGreg Roach        'todays_events'          => OnThisDayModule::class,
2534ca7e03cSGreg Roach        'todo'                   => ResearchTaskModule::class,
2544ca7e03cSGreg Roach        'top10_givnnames'        => TopGivenNamesModule::class,
2554ca7e03cSGreg Roach        'top10_pageviews'        => TopPageViewsModule::class,
2564ca7e03cSGreg Roach        'top10_surnames'         => TopSurnamesModule::class,
2574ca7e03cSGreg Roach        'tree'                   => InteractiveTreeModule::class,
2584ca7e03cSGreg Roach        'trees-menu'             => TreesMenuModule::class,
2594ca7e03cSGreg Roach        'upcoming_events'        => UpcomingAnniversariesModule::class,
2604ca7e03cSGreg Roach        'user_blog'              => UserJournalModule::class,
2614ca7e03cSGreg Roach        'user_favorites'         => UserFavoritesModule::class,
2624ca7e03cSGreg Roach        'user_messages'          => UserMessagesModule::class,
2634ca7e03cSGreg Roach        'user_welcome'           => UserWelcomeModule::class,
2644ca7e03cSGreg Roach        'webtrees'               => WebtreesTheme::class,
2654ca7e03cSGreg Roach        'xenea'                  => XeneaTheme::class,
2664ca7e03cSGreg Roach        'yahrzeit'               => YahrzeitModule::class,
2674ca7e03cSGreg Roach    ];
2684ca7e03cSGreg Roach
2694ca7e03cSGreg Roach    /**
2704ca7e03cSGreg Roach     * All core modules in the system.
2714ca7e03cSGreg Roach     *
2724ca7e03cSGreg Roach     * @return Collection
2734ca7e03cSGreg Roach     */
2744ca7e03cSGreg Roach    private function coreModules(): Collection
2754ca7e03cSGreg Roach    {
2764ca7e03cSGreg Roach        $modules = new Collection(self::CORE_MODULES);
2774ca7e03cSGreg Roach
2784ca7e03cSGreg Roach        return $modules->map(function (string $class, string $name): ModuleInterface {
2794ca7e03cSGreg Roach            $module = app()->make($class);
2804ca7e03cSGreg Roach
2814ca7e03cSGreg Roach            $module->setName($name);
2824ca7e03cSGreg Roach
2834ca7e03cSGreg Roach            return $module;
2844ca7e03cSGreg Roach        });
2854ca7e03cSGreg Roach    }
2864ca7e03cSGreg Roach
2874ca7e03cSGreg Roach    /**
2884ca7e03cSGreg Roach     * All custom modules in the system.  Custom modules are defined in modules_v4/
2894ca7e03cSGreg Roach     *
2904ca7e03cSGreg Roach     * @return Collection
2914ca7e03cSGreg Roach     */
2924ca7e03cSGreg Roach    private function customModules(): Collection
2934ca7e03cSGreg Roach    {
2944ca7e03cSGreg Roach        $pattern   = WT_ROOT . Webtrees::MODULES_PATH . '*/module.php';
2954ca7e03cSGreg Roach        $filenames = glob($pattern);
2964ca7e03cSGreg Roach
2974ca7e03cSGreg Roach        return (new Collection($filenames))
2984ca7e03cSGreg Roach            ->filter(function (string $filename): bool {
2994ca7e03cSGreg Roach                // Special characters will break PHP variable names.
3004ca7e03cSGreg Roach                // This also allows us to ignore modules called "foo.example" and "foo.disable"
3014ca7e03cSGreg Roach                $module_name = basename(dirname($filename));
3024ca7e03cSGreg Roach
3034ca7e03cSGreg Roach                return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30;
3044ca7e03cSGreg Roach            })
3054ca7e03cSGreg Roach            ->map(function (string $filename): ?ModuleCustomInterface {
3064ca7e03cSGreg Roach                try {
3074ca7e03cSGreg Roach                    $module = self::load($filename);
3084ca7e03cSGreg Roach
3094ca7e03cSGreg Roach                    if ($module instanceof ModuleCustomInterface) {
3104ca7e03cSGreg Roach                        $module_name = '_' . basename(dirname($filename)) . '_';
3114ca7e03cSGreg Roach
3124ca7e03cSGreg Roach                        $module->setName($module_name);
3134ca7e03cSGreg Roach                    } else {
3144ca7e03cSGreg Roach                        return null;
3154ca7e03cSGreg Roach                    }
3164ca7e03cSGreg Roach
3174ca7e03cSGreg Roach                    return $module;
3184ca7e03cSGreg Roach                } catch (Throwable $ex) {
3194ca7e03cSGreg Roach                    $message = '<pre>' . e($ex->getMessage()) . "\n" . e($ex->getTraceAsString()) . '</pre>';
3204ca7e03cSGreg Roach                    FlashMessages::addMessage($message, 'danger');
3214ca7e03cSGreg Roach
3224ca7e03cSGreg Roach                    return null;
3234ca7e03cSGreg Roach                }
3244ca7e03cSGreg Roach            })
3254ca7e03cSGreg Roach            ->filter();
3264ca7e03cSGreg Roach    }
3274ca7e03cSGreg Roach
3284ca7e03cSGreg Roach    /**
3294ca7e03cSGreg Roach     * All modules.
3304ca7e03cSGreg Roach     *
3314ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
3324ca7e03cSGreg Roach     */
3334ca7e03cSGreg Roach    public function all(): Collection
3344ca7e03cSGreg Roach    {
3354ca7e03cSGreg Roach        return app('cache.array')->rememberForever('all_modules', function (): Collection {
3364ca7e03cSGreg Roach            // Modules have a default status, order etc.
3374ca7e03cSGreg Roach            // We can override these from database settings.
3384ca7e03cSGreg Roach            $module_info = DB::table('module')
3394ca7e03cSGreg Roach                ->get()
3404ca7e03cSGreg Roach                ->mapWithKeys(function (stdClass $row): array {
3414ca7e03cSGreg Roach                    return [$row->module_name => $row];
3424ca7e03cSGreg Roach                });
3434ca7e03cSGreg Roach
344bf57b580SGreg Roach            return $this->coreModules()
345bf57b580SGreg Roach                ->merge($this->customModules())
3464ca7e03cSGreg Roach                ->map(function (ModuleInterface $module) use ($module_info): ModuleInterface {
3474ca7e03cSGreg Roach                    $info = $module_info->get($module->name());
3484ca7e03cSGreg Roach
3494ca7e03cSGreg Roach                    if ($info instanceof stdClass) {
3504ca7e03cSGreg Roach                        $module->setEnabled($info->status === 'enabled');
3514ca7e03cSGreg Roach
3524ca7e03cSGreg Roach                        if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) {
3534ca7e03cSGreg Roach                            $module->setFooterOrder((int) $info->footer_order);
3544ca7e03cSGreg Roach                        }
3554ca7e03cSGreg Roach
3564ca7e03cSGreg Roach                        if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) {
3574ca7e03cSGreg Roach                            $module->setMenuOrder((int) $info->menu_order);
3584ca7e03cSGreg Roach                        }
3594ca7e03cSGreg Roach
3604ca7e03cSGreg Roach                        if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) {
3614ca7e03cSGreg Roach                            $module->setSidebarOrder((int) $info->sidebar_order);
3624ca7e03cSGreg Roach                        }
3634ca7e03cSGreg Roach
3644ca7e03cSGreg Roach                        if ($module instanceof ModuleTabInterface && $info->tab_order !== null) {
3654ca7e03cSGreg Roach                            $module->setTabOrder((int) $info->tab_order);
3664ca7e03cSGreg Roach                        }
3674ca7e03cSGreg Roach                    } else {
368888ddf4fSGreg Roach                        $module->setEnabled($module->isEnabledByDefault());
369888ddf4fSGreg Roach
370888ddf4fSGreg Roach                        DB::table('module')->insert([
371888ddf4fSGreg Roach                            'module_name' => $module->name(),
372888ddf4fSGreg Roach                            'status'      => $module->isEnabled() ? 'enabled' : 'disabled',
373888ddf4fSGreg Roach                        ]);
3744ca7e03cSGreg Roach                    }
3754ca7e03cSGreg Roach
3764ca7e03cSGreg Roach                    return $module;
3774ca7e03cSGreg Roach                })
378bf57b580SGreg Roach                ->sort($this->moduleSorter());
3794ca7e03cSGreg Roach        });
3804ca7e03cSGreg Roach    }
3814ca7e03cSGreg Roach
3824ca7e03cSGreg Roach    /**
383a8fbc491SGreg Roach     * Load a module in a static scope, to prevent it from modifying local or object variables.
3844ca7e03cSGreg Roach     *
3854ca7e03cSGreg Roach     * @param string $filename
3864ca7e03cSGreg Roach     *
3874ca7e03cSGreg Roach     * @return mixed
3884ca7e03cSGreg Roach     */
389a8fbc491SGreg Roach    private static function load(string $filename)
3904ca7e03cSGreg Roach    {
3914ca7e03cSGreg Roach        return include $filename;
3924ca7e03cSGreg Roach    }
3934ca7e03cSGreg Roach
3944ca7e03cSGreg Roach    /**
3954ca7e03cSGreg Roach     * A function to sort modules by name
3964ca7e03cSGreg Roach     *
3974ca7e03cSGreg Roach     * @return Closure
3984ca7e03cSGreg Roach     */
3994ca7e03cSGreg Roach    private function moduleSorter(): Closure
4004ca7e03cSGreg Roach    {
4014ca7e03cSGreg Roach        return function (ModuleInterface $x, ModuleInterface $y): int {
4024ca7e03cSGreg Roach            return I18N::strcasecmp($x->title(), $y->title());
4034ca7e03cSGreg Roach        };
4044ca7e03cSGreg Roach    }
4054ca7e03cSGreg Roach
4064ca7e03cSGreg Roach    /**
4074ca7e03cSGreg Roach     * A function to sort footers
4084ca7e03cSGreg Roach     *
4094ca7e03cSGreg Roach     * @return Closure
4104ca7e03cSGreg Roach     */
4114ca7e03cSGreg Roach    private function footerSorter(): Closure
4124ca7e03cSGreg Roach    {
4134ca7e03cSGreg Roach        return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int {
4144ca7e03cSGreg Roach            return $x->getFooterOrder() <=> $y->getFooterOrder();
4154ca7e03cSGreg Roach        };
4164ca7e03cSGreg Roach    }
4174ca7e03cSGreg Roach
4184ca7e03cSGreg Roach    /**
4194ca7e03cSGreg Roach     * A function to sort menus
4204ca7e03cSGreg Roach     *
4214ca7e03cSGreg Roach     * @return Closure
4224ca7e03cSGreg Roach     */
4234ca7e03cSGreg Roach    private function menuSorter(): Closure
4244ca7e03cSGreg Roach    {
4254ca7e03cSGreg Roach        return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int {
4264ca7e03cSGreg Roach            return $x->getMenuOrder() <=> $y->getMenuOrder();
4274ca7e03cSGreg Roach        };
4284ca7e03cSGreg Roach    }
4294ca7e03cSGreg Roach
4304ca7e03cSGreg Roach    /**
4314ca7e03cSGreg Roach     * A function to sort menus
4324ca7e03cSGreg Roach     *
4334ca7e03cSGreg Roach     * @return Closure
4344ca7e03cSGreg Roach     */
4354ca7e03cSGreg Roach    private function sidebarSorter(): Closure
4364ca7e03cSGreg Roach    {
4374ca7e03cSGreg Roach        return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int {
4384ca7e03cSGreg Roach            return $x->getSidebarOrder() <=> $y->getSidebarOrder();
4394ca7e03cSGreg Roach        };
4404ca7e03cSGreg Roach    }
4414ca7e03cSGreg Roach
4424ca7e03cSGreg Roach    /**
4434ca7e03cSGreg Roach     * A function to sort menus
4444ca7e03cSGreg Roach     *
4454ca7e03cSGreg Roach     * @return Closure
4464ca7e03cSGreg Roach     */
4474ca7e03cSGreg Roach    private function tabSorter(): Closure
4484ca7e03cSGreg Roach    {
4494ca7e03cSGreg Roach        return function (ModuleTabInterface $x, ModuleTabInterface $y): int {
4504ca7e03cSGreg Roach            return $x->getTabOrder() <=> $y->getTabOrder();
4514ca7e03cSGreg Roach        };
4524ca7e03cSGreg Roach    }
4534ca7e03cSGreg Roach
4544ca7e03cSGreg Roach    /**
455dedcf571SGreg Roach     * A function to convert modules into their titles - to create option lists, etc.
456dedcf571SGreg Roach     *
457dedcf571SGreg Roach     * @return Closure
458dedcf571SGreg Roach     */
459dedcf571SGreg Roach    public function titleMapper(): Closure
460dedcf571SGreg Roach    {
461dedcf571SGreg Roach        return function (ModuleInterface $module): string {
462dedcf571SGreg Roach            return $module->title();
463dedcf571SGreg Roach        };
464dedcf571SGreg Roach    }
465dedcf571SGreg Roach
466dedcf571SGreg Roach    /**
4674ca7e03cSGreg Roach     * Modules which (a) provide a specific function and (b) we have permission to see.
4684ca7e03cSGreg Roach     *
469*87cca37cSGreg Roach     * @param string        $interface
4704ca7e03cSGreg Roach     * @param Tree          $tree
471e5a6b4d4SGreg Roach     * @param UserInterface $user
4724ca7e03cSGreg Roach     *
4734ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
4744ca7e03cSGreg Roach     */
475*87cca37cSGreg Roach    public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection
4764ca7e03cSGreg Roach    {
477bf57b580SGreg Roach        return $this->findByInterface($interface)
478*87cca37cSGreg Roach            ->filter(function (ModuleInterface $module) use ($interface, $tree, $user): bool {
479*87cca37cSGreg Roach                return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user);
4804ca7e03cSGreg Roach            });
4814ca7e03cSGreg Roach    }
4824ca7e03cSGreg Roach
4834ca7e03cSGreg Roach    /**
4844ca7e03cSGreg Roach     * All modules which provide a specific function.
4854ca7e03cSGreg Roach     *
4864ca7e03cSGreg Roach     * @param string $interface
4874ca7e03cSGreg Roach     * @param bool   $include_disabled
4884ca7e03cSGreg Roach     *
4894ca7e03cSGreg Roach     * @return Collection|ModuleInterface[]
4904ca7e03cSGreg Roach     */
4914ca7e03cSGreg Roach    public function findByInterface(string $interface, $include_disabled = false): Collection
4924ca7e03cSGreg Roach    {
493bf57b580SGreg Roach        $modules = $this->all()
4944ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($interface): bool {
4954ca7e03cSGreg Roach                return $module instanceof $interface;
4964ca7e03cSGreg Roach            })
4974ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($include_disabled): bool {
4984ca7e03cSGreg Roach                return $include_disabled || $module->isEnabled();
4994ca7e03cSGreg Roach            });
5004ca7e03cSGreg Roach
5014ca7e03cSGreg Roach        switch ($interface) {
5024ca7e03cSGreg Roach            case ModuleFooterInterface::class:
503bf57b580SGreg Roach                return $modules->sort($this->footerSorter());
5044ca7e03cSGreg Roach
5054ca7e03cSGreg Roach            case ModuleMenuInterface::class:
506bf57b580SGreg Roach                return $modules->sort($this->menuSorter());
5074ca7e03cSGreg Roach
5084ca7e03cSGreg Roach            case ModuleSidebarInterface::class:
509bf57b580SGreg Roach                return $modules->sort($this->sidebarSorter());
5104ca7e03cSGreg Roach
5114ca7e03cSGreg Roach            case ModuleTabInterface::class:
512bf57b580SGreg Roach                return $modules->sort($this->tabSorter());
5134ca7e03cSGreg Roach        }
5144ca7e03cSGreg Roach
5154ca7e03cSGreg Roach        return $modules;
5164ca7e03cSGreg Roach    }
5174ca7e03cSGreg Roach
5184ca7e03cSGreg Roach    /**
5194ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
5204ca7e03cSGreg Roach     *
5214ca7e03cSGreg Roach     * @param string $module_name
5224ca7e03cSGreg Roach     *
5234ca7e03cSGreg Roach     * @return ModuleInterface|null
5244ca7e03cSGreg Roach     */
5254ca7e03cSGreg Roach    public function findByName(string $module_name): ?ModuleInterface
5264ca7e03cSGreg Roach    {
527bf57b580SGreg Roach        return $this->all()
5284ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($module_name): bool {
5294ca7e03cSGreg Roach                return $module->isEnabled() && $module->name() === $module_name;
5304ca7e03cSGreg Roach            })
5314ca7e03cSGreg Roach            ->first();
5324ca7e03cSGreg Roach    }
5334ca7e03cSGreg Roach
5344ca7e03cSGreg Roach    /**
5354ca7e03cSGreg Roach     * Find a specified module, if it is currently active.
5364ca7e03cSGreg Roach     *
5374ca7e03cSGreg Roach     * @param string $class_name
5384ca7e03cSGreg Roach     *
5394ca7e03cSGreg Roach     * @return ModuleInterface|null
5404ca7e03cSGreg Roach     */
5414ca7e03cSGreg Roach    public function findByClass(string $class_name): ?ModuleInterface
5424ca7e03cSGreg Roach    {
543bf57b580SGreg Roach        return $this->all()
5444ca7e03cSGreg Roach            ->filter(function (ModuleInterface $module) use ($class_name): bool {
5454ca7e03cSGreg Roach                return $module->isEnabled() && $module instanceof $class_name;
5464ca7e03cSGreg Roach            })
5474ca7e03cSGreg Roach            ->first();
5484ca7e03cSGreg Roach    }
549bf57b580SGreg Roach
550bf57b580SGreg Roach    /**
551bf57b580SGreg Roach     * Configuration settings are available through the various "module component" pages.
552bf57b580SGreg Roach     * For modules that do not provide a component, we need to list them separately.
553bf57b580SGreg Roach     *
554bf57b580SGreg Roach     * @return Collection|ModuleConfigInterface[]
555bf57b580SGreg Roach     */
556bf57b580SGreg Roach    public function configOnlyModules(): Collection
557bf57b580SGreg Roach    {
558bf57b580SGreg Roach        return $this->findByInterface(ModuleConfigInterface::class)
559bf57b580SGreg Roach            ->filter(function (ModuleConfigInterface $module): bool {
560bf57b580SGreg Roach                foreach (self::COMPONENTS as $interface) {
561bf57b580SGreg Roach                    if ($module instanceof $interface) {
562bf57b580SGreg Roach                        return false;
563bf57b580SGreg Roach                    }
564bf57b580SGreg Roach                }
565bf57b580SGreg Roach
566bf57b580SGreg Roach                return true;
567bf57b580SGreg Roach            });
568bf57b580SGreg Roach    }
569bf57b580SGreg Roach
570bf57b580SGreg Roach    /**
571bf57b580SGreg Roach     * Generate a list of module names which exist in the database but not on disk.
572bf57b580SGreg Roach     *
573bf57b580SGreg Roach     * @return Collection|string[]
574bf57b580SGreg Roach     */
575bf57b580SGreg Roach    public function deletedModules(): Collection
576bf57b580SGreg Roach    {
577bf57b580SGreg Roach        $database_modules = DB::table('module')->pluck('module_name');
578bf57b580SGreg Roach
579bf57b580SGreg Roach        $disk_modules = $this->all()
580bf57b580SGreg Roach            ->map(function (ModuleInterface $module): string {
581bf57b580SGreg Roach                return $module->name();
582bf57b580SGreg Roach            });
583bf57b580SGreg Roach
584bf57b580SGreg Roach        return $database_modules->diff($disk_modules);
585bf57b580SGreg Roach    }
5864ca7e03cSGreg Roach}
587