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