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