. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Carbon; use Fisharebest\Webtrees\Family; use Fisharebest\Webtrees\Gedcom; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Media; use Fisharebest\Webtrees\Note; use Fisharebest\Webtrees\Repository; use Fisharebest\Webtrees\Services\PendingChangesService; use Fisharebest\Webtrees\Services\TreeService; use Fisharebest\Webtrees\Source; use Fisharebest\Webtrees\Tree; use Illuminate\Database\Capsule\Manager as DB; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; use function key; use function preg_match; use function reset; use function route; /** * Show all pending changes. */ class PendingChanges implements RequestHandlerInterface { use ViewResponseTrait; /** @var PendingChangesService */ private $pending_changes_service; /** @var TreeService */ private $tree_service; /** * @param PendingChangesService $pending_changes_service * @param TreeService $tree_service */ public function __construct(PendingChangesService $pending_changes_service, TreeService $tree_service) { $this->pending_changes_service = $pending_changes_service; $this->tree_service = $tree_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree, new InvalidArgumentException()); $url = $request->getQueryParams()['url'] ?? route('tree-page', ['tree' => $tree->name()]); $rows = DB::table('change') ->join('user', 'user.user_id', '=', 'change.user_id') ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id') ->where('status', '=', 'pending') ->orderBy('change.gedcom_id') ->orderBy('change.xref') ->orderBy('change.change_id') ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name']) ->get(); $changes = []; foreach ($rows as $row) { $row->change_time = Carbon::make($row->change_time); $change_tree = $this->tree_service->all()->get($row->gedcom_name); preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match); switch ($match[1]) { case 'INDI': $row->record = new Individual($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; case 'FAM': $row->record = new Family($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; case 'SOUR': $row->record = new Source($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; case 'REPO': $row->record = new Repository($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; case 'OBJE': $row->record = new Media($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; case 'NOTE': $row->record = new Note($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; default: $row->record = new GedcomRecord($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree); break; } $changes[$row->gedcom_id][$row->xref][] = $row; } $title = I18N::translate('Pending changes'); // If the current tree has changes, activate that tab. Otherwise activate the first tab. if (($changes[$tree->id()] ?? []) === []) { reset($changes); $active_tree_id = key($changes); } else { $active_tree_id = $tree->id(); } return $this->viewResponse('pending-changes-page', [ 'active_tree_id' => $active_tree_id, 'changes' => $changes, 'title' => $title, 'tree' => $tree, 'url' => $url, ]); } }