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