xref: /webtrees/app/Http/RequestHandlers/ControlPanel.php (revision 774eb6bada09fdd46a086fd31e84fd157a6351c4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Module\FamilyListModule;
26use Fisharebest\Webtrees\Module\IndividualListModule;
27use Fisharebest\Webtrees\Module\MediaListModule;
28use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
29use Fisharebest\Webtrees\Module\ModuleBlockInterface;
30use Fisharebest\Webtrees\Module\ModuleChartInterface;
31use Fisharebest\Webtrees\Module\ModuleCustomInterface;
32use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
33use Fisharebest\Webtrees\Module\ModuleFooterInterface;
34use Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
35use Fisharebest\Webtrees\Module\ModuleLanguageInterface;
36use Fisharebest\Webtrees\Module\ModuleListInterface;
37use Fisharebest\Webtrees\Module\ModuleMapAutocompleteInterface;
38use Fisharebest\Webtrees\Module\ModuleMapGeoLocationInterface;
39use Fisharebest\Webtrees\Module\ModuleMapLinkInterface;
40use Fisharebest\Webtrees\Module\ModuleMapProviderInterface;
41use Fisharebest\Webtrees\Module\ModuleMenuInterface;
42use Fisharebest\Webtrees\Module\ModuleReportInterface;
43use Fisharebest\Webtrees\Module\ModuleShareInterface;
44use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
45use Fisharebest\Webtrees\Module\ModuleTabInterface;
46use Fisharebest\Webtrees\Module\ModuleThemeInterface;
47use Fisharebest\Webtrees\Module\NoteListModule;
48use Fisharebest\Webtrees\Module\RepositoryListModule;
49use Fisharebest\Webtrees\Module\SourceListModule;
50use Fisharebest\Webtrees\Module\SubmitterListModule;
51use Fisharebest\Webtrees\Note;
52use Fisharebest\Webtrees\Registry;
53use Fisharebest\Webtrees\Repository;
54use Fisharebest\Webtrees\Services\AdminService;
55use Fisharebest\Webtrees\Services\HousekeepingService;
56use Fisharebest\Webtrees\Services\MessageService;
57use Fisharebest\Webtrees\Services\ModuleService;
58use Fisharebest\Webtrees\Services\ServerCheckService;
59use Fisharebest\Webtrees\Services\TreeService;
60use Fisharebest\Webtrees\Services\UpgradeService;
61use Fisharebest\Webtrees\Services\UserService;
62use Fisharebest\Webtrees\Submitter;
63use Fisharebest\Webtrees\Webtrees;
64use Illuminate\Database\Query\Expression;
65use Illuminate\Database\Query\JoinClause;
66use Illuminate\Support\Collection;
67use League\Flysystem\Filesystem;
68use League\Flysystem\Local\LocalFilesystemAdapter;
69use Psr\Http\Message\ResponseInterface;
70use Psr\Http\Message\ServerRequestInterface;
71use Psr\Http\Server\RequestHandlerInterface;
72
73/**
74 * The control panel shows a summary of the site and links to admin functions.
75 */
76class ControlPanel implements RequestHandlerInterface
77{
78    use ViewResponseTrait;
79
80    private AdminService $admin_service;
81
82    private HousekeepingService $housekeeping_service;
83
84    private MessageService $message_service;
85
86    private ModuleService $module_service;
87
88    private ServerCheckService $server_check_service;
89
90    private TreeService $tree_service;
91
92    private UpgradeService $upgrade_service;
93
94    private UserService $user_service;
95
96    /**
97     * @param AdminService        $admin_service
98     * @param HousekeepingService $housekeeping_service
99     * @param MessageService      $message_service
100     * @param ModuleService       $module_service
101     * @param ServerCheckService  $server_check_service
102     * @param TreeService         $tree_service
103     * @param UpgradeService      $upgrade_service
104     * @param UserService         $user_service
105     */
106    public function __construct(
107        AdminService $admin_service,
108        HousekeepingService $housekeeping_service,
109        MessageService $message_service,
110        ModuleService $module_service,
111        ServerCheckService $server_check_service,
112        TreeService $tree_service,
113        UpgradeService $upgrade_service,
114        UserService $user_service
115    ) {
116        $this->admin_service        = $admin_service;
117        $this->housekeeping_service = $housekeeping_service;
118        $this->message_service      = $message_service;
119        $this->module_service       = $module_service;
120        $this->server_check_service = $server_check_service;
121        $this->tree_service         = $tree_service;
122        $this->upgrade_service      = $upgrade_service;
123        $this->user_service         = $user_service;
124    }
125
126    /**
127     * @param ServerRequestInterface $request
128     *
129     * @return ResponseInterface
130     */
131    public function handle(ServerRequestInterface $request): ResponseInterface
132    {
133        $this->layout = 'layouts/administration';
134
135        $filesystem      = new Filesystem(new LocalFilesystemAdapter(Webtrees::ROOT_DIR));
136        $files_to_delete = $this->housekeeping_service->deleteOldWebtreesFiles($filesystem);
137
138        $custom_updates = $this->module_service
139            ->findByInterface(ModuleCustomInterface::class)
140            ->filter(static fn (ModuleCustomInterface $module): bool => version_compare($module->customModuleLatestVersion(), $module->customModuleVersion()) > 0);
141
142        $multiple_tree_threshold = $this->admin_service->multipleTreeThreshold();
143        $gedcom_file_count       = $this->admin_service->gedcomFiles(Registry::filesystem()->data())->count();
144
145        return $this->viewResponse('admin/control-panel', [
146            'title'                             => I18N::translate('Control panel'),
147            'server_errors'                     => $this->server_check_service->serverErrors(),
148            'server_warnings'                   => $this->server_check_service->serverWarnings(),
149            'latest_version'                    => $this->upgrade_service->latestVersion(),
150            'latest_version_error'              => $this->upgrade_service->latestVersionError(),
151            'latest_version_timestamp'          => $this->upgrade_service->latestVersionTimestamp(),
152            'all_users'                         => $this->user_service->all(),
153            'administrators'                    => $this->user_service->administrators(),
154            'managers'                          => $this->user_service->managers(),
155            'moderators'                        => $this->user_service->moderators(),
156            'unapproved'                        => $this->user_service->unapproved(),
157            'unverified'                        => $this->user_service->unverified(),
158            'recipients'                        => $this->message_service->recipientTypes(),
159            'all_trees'                         => $this->tree_service->all(),
160            'changes'                           => $this->totalChanges(),
161            'individuals'                       => $this->totalIndividuals(),
162            'families'                          => $this->totalFamilies(),
163            'sources'                           => $this->totalSources(),
164            'media'                             => $this->totalMediaObjects(),
165            'repositories'                      => $this->totalRepositories(),
166            'notes'                             => $this->totalNotes(),
167            'submitters'                        => $this->totalSubmitters(),
168            'individual_list_module'            => $this->module_service->findByInterface(IndividualListModule::class)->first(),
169            'family_list_module'                => $this->module_service->findByInterface(FamilyListModule::class)->first(),
170            'media_list_module'                 => $this->module_service->findByInterface(MediaListModule::class)->first(),
171            'note_list_module'                  => $this->module_service->findByInterface(NoteListModule::class)->first(),
172            'repository_list_module'            => $this->module_service->findByInterface(RepositoryListModule::class)->first(),
173            'source_list_module'                => $this->module_service->findByInterface(SourceListModule::class)->first(),
174            'submitter_list_module'             => $this->module_service->findByInterface(SubmitterListModule::class)->first(),
175            'files_to_delete'                   => $files_to_delete,
176            'all_modules_disabled'              => $this->module_service->all(true),
177            'all_modules_enabled'               => $this->module_service->all(),
178            'deleted_modules'                   => $this->module_service->deletedModules(),
179            'analytics_modules_disabled'        => $this->module_service->findByInterface(ModuleAnalyticsInterface::class, true),
180            'analytics_modules_enabled'         => $this->module_service->findByInterface(ModuleAnalyticsInterface::class),
181            'block_modules_disabled'            => $this->module_service->findByInterface(ModuleBlockInterface::class, true),
182            'block_modules_enabled'             => $this->module_service->findByInterface(ModuleBlockInterface::class),
183            'chart_modules_disabled'            => $this->module_service->findByInterface(ModuleChartInterface::class, true),
184            'chart_modules_enabled'             => $this->module_service->findByInterface(ModuleChartInterface::class),
185            'custom_updates'                    => $custom_updates,
186            'data_fix_modules_disabled'         => $this->module_service->findByInterface(ModuleDataFixInterface::class, true),
187            'data_fix_modules_enabled'          => $this->module_service->findByInterface(ModuleDataFixInterface::class),
188            'other_modules'                     => $this->module_service->otherModules(true),
189            'footer_modules_disabled'           => $this->module_service->findByInterface(ModuleFooterInterface::class, true),
190            'footer_modules_enabled'            => $this->module_service->findByInterface(ModuleFooterInterface::class),
191            'history_modules_disabled'          => $this->module_service->findByInterface(ModuleHistoricEventsInterface::class, true),
192            'history_modules_enabled'           => $this->module_service->findByInterface(ModuleHistoricEventsInterface::class),
193            'language_modules_disabled'         => $this->module_service->findByInterface(ModuleLanguageInterface::class, true),
194            'language_modules_enabled'          => $this->module_service->findByInterface(ModuleLanguageInterface::class),
195            'list_modules_disabled'             => $this->module_service->findByInterface(ModuleListInterface::class, true),
196            'list_modules_enabled'              => $this->module_service->findByInterface(ModuleListInterface::class),
197            'map_autocomplete_modules_disabled' => $this->module_service->findByInterface(ModuleMapAutocompleteInterface::class, true),
198            'map_autocomplete_modules_enabled'  => $this->module_service->findByInterface(ModuleMapAutocompleteInterface::class),
199            'map_link_modules_disabled'         => $this->module_service->findByInterface(ModuleMapLinkInterface::class, true),
200            'map_link_modules_enabled'          => $this->module_service->findByInterface(ModuleMapLinkInterface::class),
201            'map_provider_modules_disabled'     => $this->module_service->findByInterface(ModuleMapProviderInterface::class, true),
202            'map_provider_modules_enabled'      => $this->module_service->findByInterface(ModuleMapProviderInterface::class),
203            'map_search_modules_disabled'       => $this->module_service->findByInterface(ModuleMapGeoLocationInterface::class, true),
204            'map_search_modules_enabled'        => $this->module_service->findByInterface(ModuleMapGeoLocationInterface::class),
205            'menu_modules_disabled'             => $this->module_service->findByInterface(ModuleMenuInterface::class, true),
206            'menu_modules_enabled'              => $this->module_service->findByInterface(ModuleMenuInterface::class),
207            'report_modules_disabled'           => $this->module_service->findByInterface(ModuleReportInterface::class, true),
208            'report_modules_enabled'            => $this->module_service->findByInterface(ModuleReportInterface::class),
209            'share_modules_disabled'            => $this->module_service->findByInterface(ModuleShareInterface::class, true),
210            'share_modules_enabled'             => $this->module_service->findByInterface(ModuleShareInterface::class),
211            'sidebar_modules_disabled'          => $this->module_service->findByInterface(ModuleSidebarInterface::class, true),
212            'sidebar_modules_enabled'           => $this->module_service->findByInterface(ModuleSidebarInterface::class),
213            'tab_modules_disabled'              => $this->module_service->findByInterface(ModuleTabInterface::class, true),
214            'tab_modules_enabled'               => $this->module_service->findByInterface(ModuleTabInterface::class),
215            'theme_modules_disabled'            => $this->module_service->findByInterface(ModuleThemeInterface::class, true),
216            'theme_modules_enabled'             => $this->module_service->findByInterface(ModuleThemeInterface::class),
217            'show_synchronize'                  => $gedcom_file_count >= $multiple_tree_threshold,
218        ]);
219    }
220
221    /**
222     * Count the number of pending changes in each tree.
223     *
224     * @return array<int>
225     */
226    private function totalChanges(): array
227    {
228        return DB::table('gedcom')
229            ->leftJoin('change', static function (JoinClause $join): void {
230                $join
231                    ->on('change.gedcom_id', '=', 'gedcom.gedcom_id')
232                    ->where('change.status', '=', 'pending');
233            })
234            ->groupBy(['gedcom.gedcom_id'])
235            ->pluck(new Expression('COUNT(change_id) AS aggregate'), 'gedcom.gedcom_id')
236            ->map(static fn (string $count): int => (int) $count)
237            ->all();
238    }
239
240    /**
241     * Count the number of individuals in each tree.
242     *
243     * @return Collection<int,int>
244     */
245    private function totalIndividuals(): Collection
246    {
247        return DB::table('gedcom')
248            ->leftJoin('individuals', 'i_file', '=', 'gedcom_id')
249            ->groupBy(['gedcom_id'])
250            ->pluck(new Expression('COUNT(i_id) AS aggregate'), 'gedcom_id')
251            ->map(static fn (string $count): int => (int) $count);
252    }
253
254    /**
255     * Count the number of families in each tree.
256     *
257     * @return Collection<int,int>
258     */
259    private function totalFamilies(): Collection
260    {
261        return DB::table('gedcom')
262            ->leftJoin('families', 'f_file', '=', 'gedcom_id')
263            ->groupBy(['gedcom_id'])
264            ->pluck(new Expression('COUNT(f_id) AS aggregate'), 'gedcom_id')
265            ->map(static fn (string $count): int => (int) $count);
266    }
267
268    /**
269     * Count the number of sources in each tree.
270     *
271     * @return Collection<int,int>
272     */
273    private function totalSources(): Collection
274    {
275        return DB::table('gedcom')
276            ->leftJoin('sources', 's_file', '=', 'gedcom_id')
277            ->groupBy(['gedcom_id'])
278            ->pluck(new Expression('COUNT(s_id) AS aggregate'), 'gedcom_id')
279            ->map(static fn (string $count): int => (int) $count);
280    }
281
282    /**
283     * Count the number of media objects in each tree.
284     *
285     * @return Collection<int,int>
286     */
287    private function totalMediaObjects(): Collection
288    {
289        return DB::table('gedcom')
290            ->leftJoin('media', 'm_file', '=', 'gedcom_id')
291            ->groupBy(['gedcom_id'])
292            ->pluck(new Expression('COUNT(m_id) AS aggregate'), 'gedcom_id')
293            ->map(static fn (string $count): int => (int) $count);
294    }
295
296    /**
297     * Count the number of repositories in each tree.
298     *
299     * @return Collection<int,int>
300     */
301    private function totalRepositories(): Collection
302    {
303        return DB::table('gedcom')
304            ->leftJoin('other', static function (JoinClause $join): void {
305                $join
306                    ->on('o_file', '=', 'gedcom_id')
307                    ->where('o_type', '=', Repository::RECORD_TYPE);
308            })
309            ->groupBy(['gedcom_id'])
310            ->pluck(new Expression('COUNT(o_id) AS aggregate'), 'gedcom_id')
311            ->map(static fn (string $count): int => (int) $count);
312    }
313
314    /**
315     * Count the number of notes in each tree.
316     *
317     * @return Collection<int,int>
318     */
319    private function totalNotes(): Collection
320    {
321        return DB::table('gedcom')
322            ->leftJoin('other', static function (JoinClause $join): void {
323                $join
324                    ->on('o_file', '=', 'gedcom_id')
325                    ->where('o_type', '=', Note::RECORD_TYPE);
326            })
327            ->groupBy(['gedcom_id'])
328            ->pluck(new Expression('COUNT(o_id) AS aggregate'), 'gedcom_id')
329            ->map(static fn (string $count): int => (int) $count);
330    }
331
332    /**
333     * Count the number of submitters in each tree.
334     *
335     * @return Collection<int,int>
336     */
337    private function totalSubmitters(): Collection
338    {
339        return DB::table('gedcom')
340            ->leftJoin('other', static function (JoinClause $join): void {
341                $join
342                    ->on('o_file', '=', 'gedcom_id')
343                    ->where('o_type', '=', Submitter::RECORD_TYPE);
344            })
345            ->groupBy(['gedcom_id'])
346            ->pluck(new Expression('COUNT(o_id) AS aggregate'), 'gedcom_id')
347            ->map(static fn (string $count): int => (int) $count);
348    }
349}
350