xref: /webtrees/app/Http/RequestHandlers/ControlPanel.php (revision edf6ae5b0d35e10e7e82da63bf2168c85a8df026)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\ModuleMapLinkInterface;
37use Fisharebest\Webtrees\Module\ModuleMenuInterface;
38use Fisharebest\Webtrees\Module\ModuleReportInterface;
39use Fisharebest\Webtrees\Module\ModuleShareInterface;
40use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
41use Fisharebest\Webtrees\Module\ModuleTabInterface;
42use Fisharebest\Webtrees\Module\ModuleThemeInterface;
43use Fisharebest\Webtrees\Module\NoteListModule;
44use Fisharebest\Webtrees\Module\RepositoryListModule;
45use Fisharebest\Webtrees\Module\SourceListModule;
46use Fisharebest\Webtrees\Module\SubmitterListModule;
47use Fisharebest\Webtrees\Note;
48use Fisharebest\Webtrees\Registry;
49use Fisharebest\Webtrees\Repository;
50use Fisharebest\Webtrees\Services\AdminService;
51use Fisharebest\Webtrees\Services\HousekeepingService;
52use Fisharebest\Webtrees\Services\ModuleService;
53use Fisharebest\Webtrees\Services\ServerCheckService;
54use Fisharebest\Webtrees\Services\TreeService;
55use Fisharebest\Webtrees\Services\UpgradeService;
56use Fisharebest\Webtrees\Services\UserService;
57use Fisharebest\Webtrees\Submitter;
58use Fisharebest\Webtrees\Webtrees;
59use Illuminate\Database\Capsule\Manager as DB;
60use Illuminate\Database\Query\Expression;
61use Illuminate\Database\Query\JoinClause;
62use Illuminate\Support\Collection;
63use League\Flysystem\Filesystem;
64use League\Flysystem\Local\LocalFilesystemAdapter;
65use Psr\Http\Message\ResponseInterface;
66use Psr\Http\Message\ServerRequestInterface;
67use Psr\Http\Server\RequestHandlerInterface;
68
69/**
70 * The control panel shows a summary of the site and links to admin functions.
71 */
72class ControlPanel implements RequestHandlerInterface
73{
74    use ViewResponseTrait;
75
76    /** @var AdminService */
77    private $admin_service;
78
79    /** @var ModuleService */
80    private $module_service;
81
82    /** @var HousekeepingService */
83    private $housekeeping_service;
84
85    /** @var ServerCheckService */
86    private $server_check_service;
87
88    /** @var TreeService */
89    private $tree_service;
90
91    /** @var UpgradeService */
92    private $upgrade_service;
93
94    /** @var UserService */
95    private $user_service;
96
97    /**
98     * ControlPanel constructor.
99     *
100     * @param AdminService        $admin_service
101     * @param HousekeepingService $housekeeping_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        ModuleService $module_service,
112        ServerCheckService $server_check_service,
113        TreeService $tree_service,
114        UpgradeService $upgrade_service,
115        UserService $user_service
116    ) {
117        $this->admin_service        = $admin_service;
118        $this->housekeeping_service = $housekeeping_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 function (ModuleCustomInterface $module): bool {
141                return version_compare($module->customModuleLatestVersion(), $module->customModuleVersion()) > 0;
142            });
143
144        $multiple_tree_threshold = $this->admin_service->multipleTreeThreshold();
145        $gedcom_file_count       = $this->admin_service->gedcomFiles(Registry::filesystem()->data())->count();
146
147        return $this->viewResponse('admin/control-panel', [
148            'title'                      => I18N::translate('Control panel'),
149            'server_errors'              => $this->server_check_service->serverErrors(),
150            'server_warnings'            => $this->server_check_service->serverWarnings(),
151            'latest_version'             => $this->upgrade_service->latestVersion(),
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            'all_trees'                  => $this->tree_service->all(),
159            'changes'                    => $this->totalChanges(),
160            'individuals'                => $this->totalIndividuals(),
161            'families'                   => $this->totalFamilies(),
162            'sources'                    => $this->totalSources(),
163            'media'                      => $this->totalMediaObjects(),
164            'repositories'               => $this->totalRepositories(),
165            'notes'                      => $this->totalNotes(),
166            'submitters'                 => $this->totalSubmitters(),
167            'individual_list_module'     => $this->module_service->findByInterface(IndividualListModule::class)->last(),
168            'family_list_module'         => $this->module_service->findByInterface(FamilyListModule::class)->first(),
169            'media_list_module'          => $this->module_service->findByInterface(MediaListModule::class)->first(),
170            'note_list_module'           => $this->module_service->findByInterface(NoteListModule::class)->first(),
171            'repository_list_module'     => $this->module_service->findByInterface(RepositoryListModule::class)->first(),
172            'source_list_module'         => $this->module_service->findByInterface(SourceListModule::class)->first(),
173            'submitter_list_module'      => $this->module_service->findByInterface(SubmitterListModule::class)->first(),
174            'files_to_delete'            => $files_to_delete,
175            'all_modules_disabled'       => $this->module_service->all(true),
176            'all_modules_enabled'        => $this->module_service->all(),
177            'deleted_modules'            => $this->module_service->deletedModules(),
178            'analytics_modules_disabled' => $this->module_service->findByInterface(ModuleAnalyticsInterface::class, true),
179            'analytics_modules_enabled'  => $this->module_service->findByInterface(ModuleAnalyticsInterface::class),
180            'block_modules_disabled'     => $this->module_service->findByInterface(ModuleBlockInterface::class, true),
181            'block_modules_enabled'      => $this->module_service->findByInterface(ModuleBlockInterface::class),
182            'chart_modules_disabled'     => $this->module_service->findByInterface(ModuleChartInterface::class, true),
183            'chart_modules_enabled'      => $this->module_service->findByInterface(ModuleChartInterface::class),
184            'custom_updates'             => $custom_updates,
185            'data_fix_modules_disabled'  => $this->module_service->findByInterface(ModuleDataFixInterface::class, true),
186            'data_fix_modules_enabled'   => $this->module_service->findByInterface(ModuleDataFixInterface::class),
187            'other_modules'              => $this->module_service->otherModules(true),
188            'footer_modules_disabled'    => $this->module_service->findByInterface(ModuleFooterInterface::class, true),
189            'footer_modules_enabled'     => $this->module_service->findByInterface(ModuleFooterInterface::class),
190            'history_modules_disabled'   => $this->module_service->findByInterface(ModuleHistoricEventsInterface::class, true),
191            'history_modules_enabled'    => $this->module_service->findByInterface(ModuleHistoricEventsInterface::class),
192            'language_modules_disabled'  => $this->module_service->findByInterface(ModuleLanguageInterface::class, true),
193            'language_modules_enabled'   => $this->module_service->findByInterface(ModuleLanguageInterface::class),
194            'list_modules_disabled'      => $this->module_service->findByInterface(ModuleListInterface::class, true),
195            'list_modules_enabled'       => $this->module_service->findByInterface(ModuleListInterface::class),
196            'map_link_modules_disabled'  => $this->module_service->findByInterface(ModuleMapLinkInterface::class, true),
197            'map_link_modules_enabled'   => $this->module_service->findByInterface(ModuleMapLinkInterface::class),
198            'menu_modules_disabled'      => $this->module_service->findByInterface(ModuleMenuInterface::class, true),
199            'menu_modules_enabled'       => $this->module_service->findByInterface(ModuleMenuInterface::class),
200            'report_modules_disabled'    => $this->module_service->findByInterface(ModuleReportInterface::class, true),
201            'report_modules_enabled'     => $this->module_service->findByInterface(ModuleReportInterface::class),
202            'share_modules_disabled'     => $this->module_service->findByInterface(ModuleShareInterface::class, true),
203            'share_modules_enabled'      => $this->module_service->findByInterface(ModuleShareInterface::class),
204            'sidebar_modules_disabled'   => $this->module_service->findByInterface(ModuleSidebarInterface::class, true),
205            'sidebar_modules_enabled'    => $this->module_service->findByInterface(ModuleSidebarInterface::class),
206            'tab_modules_disabled'       => $this->module_service->findByInterface(ModuleTabInterface::class, true),
207            'tab_modules_enabled'        => $this->module_service->findByInterface(ModuleTabInterface::class),
208            'theme_modules_disabled'     => $this->module_service->findByInterface(ModuleThemeInterface::class, true),
209            'theme_modules_enabled'      => $this->module_service->findByInterface(ModuleThemeInterface::class),
210            'show_synchronize'           => $gedcom_file_count >= $multiple_tree_threshold,
211        ]);
212    }
213
214    /**
215     * Count the number of pending changes in each tree.
216     *
217     * @return array<string>
218     */
219    private function totalChanges(): array
220    {
221        return DB::table('gedcom')
222            ->leftJoin('change', static function (JoinClause $join): void {
223                $join
224                    ->on('change.gedcom_id', '=', 'gedcom.gedcom_id')
225                    ->where('change.status', '=', 'pending');
226            })
227            ->groupBy(['gedcom.gedcom_id'])
228            ->pluck(new Expression('COUNT(change_id) AS aggregate'), 'gedcom.gedcom_id')
229            ->all();
230    }
231
232    /**
233     * Count the number of individuals in each tree.
234     *
235     * @return Collection<string,int>
236     */
237    private function totalIndividuals(): Collection
238    {
239        return DB::table('gedcom')
240            ->leftJoin('individuals', 'i_file', '=', 'gedcom_id')
241            ->groupBy(['gedcom_id'])
242            ->pluck(new Expression('COUNT(i_id) AS aggregate'), 'gedcom_id')
243            ->map(static function (string $count) {
244                return (int) $count;
245            });
246    }
247
248    /**
249     * Count the number of families in each tree.
250     *
251     * @return Collection<string,int>
252     */
253    private function totalFamilies(): Collection
254    {
255        return DB::table('gedcom')
256            ->leftJoin('families', 'f_file', '=', 'gedcom_id')
257            ->groupBy(['gedcom_id'])
258            ->pluck(new Expression('COUNT(f_id) AS aggregate'), 'gedcom_id')
259            ->map(static function (string $count) {
260                return (int) $count;
261            });
262    }
263
264    /**
265     * Count the number of sources in each tree.
266     *
267     * @return Collection<string,int>
268     */
269    private function totalSources(): Collection
270    {
271        return DB::table('gedcom')
272            ->leftJoin('sources', 's_file', '=', 'gedcom_id')
273            ->groupBy(['gedcom_id'])
274            ->pluck(new Expression('COUNT(s_id) AS aggregate'), 'gedcom_id')
275            ->map(static function (string $count) {
276                return (int) $count;
277            });
278    }
279
280    /**
281     * Count the number of media objects in each tree.
282     *
283     * @return Collection<string,int>
284     */
285    private function totalMediaObjects(): Collection
286    {
287        return DB::table('gedcom')
288            ->leftJoin('media', 'm_file', '=', 'gedcom_id')
289            ->groupBy(['gedcom_id'])
290            ->pluck(new Expression('COUNT(m_id) AS aggregate'), 'gedcom_id')
291            ->map(static function (string $count) {
292                return (int) $count;
293            });
294    }
295
296    /**
297     * Count the number of repositorie in each tree.
298     *
299     * @return Collection<string,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 function (string $count) {
312                return (int) $count;
313            });
314    }
315
316    /**
317     * Count the number of notes in each tree.
318     *
319     * @return Collection<string,int>
320     */
321    private function totalNotes(): Collection
322    {
323        return DB::table('gedcom')
324            ->leftJoin('other', static function (JoinClause $join): void {
325                $join
326                    ->on('o_file', '=', 'gedcom_id')
327                    ->where('o_type', '=', Note::RECORD_TYPE);
328            })
329            ->groupBy(['gedcom_id'])
330            ->pluck(new Expression('COUNT(o_id) AS aggregate'), 'gedcom_id')
331            ->map(static function (string $count) {
332                return (int) $count;
333            });
334    }
335
336    /**
337     * Count the number of submitters in each tree.
338     *
339     * @return Collection<string,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 function (string $count) {
352                return (int) $count;
353            });
354    }
355}
356