1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Carbon; 23use Fisharebest\Webtrees\Factory; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\Gedcom; 26use Fisharebest\Webtrees\Header; 27use Fisharebest\Webtrees\Http\ViewResponseTrait; 28use Fisharebest\Webtrees\I18N; 29use Fisharebest\Webtrees\Individual; 30use Fisharebest\Webtrees\Media; 31use Fisharebest\Webtrees\Note; 32use Fisharebest\Webtrees\Repository; 33use Fisharebest\Webtrees\Services\TreeService; 34use Fisharebest\Webtrees\Source; 35use Fisharebest\Webtrees\Submission; 36use Fisharebest\Webtrees\Submitter; 37use Fisharebest\Webtrees\Tree; 38use Illuminate\Database\Capsule\Manager as DB; 39use Psr\Http\Message\ResponseInterface; 40use Psr\Http\Message\ServerRequestInterface; 41use Psr\Http\Server\RequestHandlerInterface; 42 43use function assert; 44use function key; 45use function preg_match; 46use function reset; 47use function route; 48 49/** 50 * Show all pending changes. 51 */ 52class PendingChanges implements RequestHandlerInterface 53{ 54 use ViewResponseTrait; 55 56 /** @var TreeService */ 57 private $tree_service; 58 59 /** 60 * @param TreeService $tree_service 61 */ 62 public function __construct(TreeService $tree_service) 63 { 64 $this->tree_service = $tree_service; 65 } 66 67 /** 68 * @param ServerRequestInterface $request 69 * 70 * @return ResponseInterface 71 */ 72 public function handle(ServerRequestInterface $request): ResponseInterface 73 { 74 $tree = $request->getAttribute('tree'); 75 assert($tree instanceof Tree); 76 77 $url = $request->getQueryParams()['url'] ?? route(TreePage::class, ['tree' => $tree->name()]); 78 79 $rows = DB::table('change') 80 ->join('user', 'user.user_id', '=', 'change.user_id') 81 ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id') 82 ->where('status', '=', 'pending') 83 ->orderBy('change.gedcom_id') 84 ->orderBy('change.xref') 85 ->orderBy('change.change_id') 86 ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name']) 87 ->get(); 88 89 $changes = []; 90 foreach ($rows as $row) { 91 $row->change_time = Carbon::make($row->change_time); 92 93 $change_tree = $this->tree_service->all()->get($row->gedcom_name); 94 95 preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match); 96 97 switch ($match[1]) { 98 case Individual::RECORD_TYPE: 99 $row->record = Factory::individual()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 100 break; 101 case Family::RECORD_TYPE: 102 $row->record = Factory::family()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 103 break; 104 case Source::RECORD_TYPE: 105 $row->record = Factory::source()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 106 break; 107 case Repository::RECORD_TYPE: 108 $row->record = Factory::repository()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 109 break; 110 case Media::RECORD_TYPE: 111 $row->record = Factory::media()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 112 break; 113 case Note::RECORD_TYPE: 114 $row->record = Factory::note()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 115 break; 116 case Submitter::RECORD_TYPE: 117 $row->record = Factory::submitter()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 118 break; 119 case Submission::RECORD_TYPE: 120 $row->record = Factory::submission()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 121 break; 122 case Header::RECORD_TYPE: 123 $row->record = Factory::header()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 124 break; 125 default: 126 $row->record = Factory::gedcomRecord()->new($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); 127 break; 128 } 129 130 $changes[$row->gedcom_name][$row->xref][] = $row; 131 } 132 133 $title = I18N::translate('Pending changes'); 134 135 // If the current tree has changes, activate that tab. Otherwise activate the first tab. 136 if (($changes[$tree->id()] ?? []) === []) { 137 reset($changes); 138 $active_tree_name = key($changes); 139 } else { 140 $active_tree_name = $tree->name(); 141 } 142 143 return $this->viewResponse('pending-changes-page', [ 144 'active_tree_name' => $active_tree_name, 145 'changes' => $changes, 146 'title' => $title, 147 'tree' => $tree, 148 'trees' => $this->tree_service->all(), 149 'url' => $url, 150 ]); 151 } 152} 153