xref: /webtrees/app/Http/RequestHandlers/PendingChanges.php (revision dce3fc17a77de429d654952a4a15ef60b3ec9fec)
122e73debSGreg Roach<?php
222e73debSGreg Roach
322e73debSGreg Roach/**
422e73debSGreg Roach * webtrees: online genealogy
522e73debSGreg Roach * Copyright (C) 2019 webtrees development team
622e73debSGreg Roach * This program is free software: you can redistribute it and/or modify
722e73debSGreg Roach * it under the terms of the GNU General Public License as published by
822e73debSGreg Roach * the Free Software Foundation, either version 3 of the License, or
922e73debSGreg Roach * (at your option) any later version.
1022e73debSGreg Roach * This program is distributed in the hope that it will be useful,
1122e73debSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1222e73debSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1322e73debSGreg Roach * GNU General Public License for more details.
1422e73debSGreg Roach * You should have received a copy of the GNU General Public License
1522e73debSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1622e73debSGreg Roach */
1722e73debSGreg Roach
1822e73debSGreg Roachdeclare(strict_types=1);
1922e73debSGreg Roach
2022e73debSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2122e73debSGreg Roach
2222e73debSGreg Roachuse Fisharebest\Webtrees\Carbon;
2322e73debSGreg Roachuse Fisharebest\Webtrees\Family;
2422e73debSGreg Roachuse Fisharebest\Webtrees\Gedcom;
2522e73debSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2622e73debSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
2722e73debSGreg Roachuse Fisharebest\Webtrees\I18N;
2822e73debSGreg Roachuse Fisharebest\Webtrees\Individual;
2922e73debSGreg Roachuse Fisharebest\Webtrees\Media;
3022e73debSGreg Roachuse Fisharebest\Webtrees\Note;
3122e73debSGreg Roachuse Fisharebest\Webtrees\Repository;
3222e73debSGreg Roachuse Fisharebest\Webtrees\Services\PendingChangesService;
3322e73debSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
3422e73debSGreg Roachuse Fisharebest\Webtrees\Source;
3522e73debSGreg Roachuse Fisharebest\Webtrees\Tree;
3622e73debSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3722e73debSGreg Roachuse InvalidArgumentException;
3822e73debSGreg Roachuse Psr\Http\Message\ResponseInterface;
3922e73debSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
4022e73debSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
4122e73debSGreg Roach
4222e73debSGreg Roachuse function assert;
4322e73debSGreg Roachuse function key;
4422e73debSGreg Roachuse function preg_match;
4522e73debSGreg Roachuse function reset;
4622e73debSGreg Roachuse function route;
4722e73debSGreg Roach
4822e73debSGreg Roach/**
4922e73debSGreg Roach * Show all pending changes.
5022e73debSGreg Roach */
5122e73debSGreg Roachclass PendingChanges implements RequestHandlerInterface
5222e73debSGreg Roach{
5322e73debSGreg Roach    use ViewResponseTrait;
5422e73debSGreg Roach
5522e73debSGreg Roach    /** @var PendingChangesService */
5622e73debSGreg Roach    private $pending_changes_service;
5722e73debSGreg Roach
5822e73debSGreg Roach    /**
5922e73debSGreg Roach     * @param TreeService $tree_service
6022e73debSGreg Roach     */
61*dce3fc17SGreg Roach    public function __construct(TreeService $tree_service)
6222e73debSGreg Roach    {
6322e73debSGreg Roach        $this->tree_service = $tree_service;
6422e73debSGreg Roach    }
6522e73debSGreg Roach
6622e73debSGreg Roach    /**
6722e73debSGreg Roach     * @param ServerRequestInterface $request
6822e73debSGreg Roach     *
6922e73debSGreg Roach     * @return ResponseInterface
7022e73debSGreg Roach     */
7122e73debSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
7222e73debSGreg Roach    {
7322e73debSGreg Roach        $tree = $request->getAttribute('tree');
7422e73debSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
7522e73debSGreg Roach
7622e73debSGreg Roach        $url = $request->getQueryParams()['url'] ?? route('tree-page', ['tree' => $tree->name()]);
7722e73debSGreg Roach
7822e73debSGreg Roach        $rows = DB::table('change')
7922e73debSGreg Roach            ->join('user', 'user.user_id', '=', 'change.user_id')
8022e73debSGreg Roach            ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id')
8122e73debSGreg Roach            ->where('status', '=', 'pending')
8222e73debSGreg Roach            ->orderBy('change.gedcom_id')
8322e73debSGreg Roach            ->orderBy('change.xref')
8422e73debSGreg Roach            ->orderBy('change.change_id')
8522e73debSGreg Roach            ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name'])
8622e73debSGreg Roach            ->get();
8722e73debSGreg Roach
8822e73debSGreg Roach        $changes = [];
8922e73debSGreg Roach        foreach ($rows as $row) {
9022e73debSGreg Roach            $row->change_time = Carbon::make($row->change_time);
9122e73debSGreg Roach
9222e73debSGreg Roach            $change_tree = $this->tree_service->all()->get($row->gedcom_name);
9322e73debSGreg Roach
9422e73debSGreg Roach            preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match);
9522e73debSGreg Roach
9622e73debSGreg Roach            switch ($match[1]) {
9722e73debSGreg Roach                case 'INDI':
9822e73debSGreg Roach                    $row->record = new Individual($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
9922e73debSGreg Roach                    break;
10022e73debSGreg Roach                case 'FAM':
10122e73debSGreg Roach                    $row->record = new Family($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10222e73debSGreg Roach                    break;
10322e73debSGreg Roach                case 'SOUR':
10422e73debSGreg Roach                    $row->record = new Source($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10522e73debSGreg Roach                    break;
10622e73debSGreg Roach                case 'REPO':
10722e73debSGreg Roach                    $row->record = new Repository($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10822e73debSGreg Roach                    break;
10922e73debSGreg Roach                case 'OBJE':
11022e73debSGreg Roach                    $row->record = new Media($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
11122e73debSGreg Roach                    break;
11222e73debSGreg Roach                case 'NOTE':
11322e73debSGreg Roach                    $row->record = new Note($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
11422e73debSGreg Roach                    break;
11522e73debSGreg Roach                default:
11622e73debSGreg Roach                    $row->record = new GedcomRecord($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
11722e73debSGreg Roach                    break;
11822e73debSGreg Roach            }
11922e73debSGreg Roach
12022e73debSGreg Roach            $changes[$row->gedcom_id][$row->xref][] = $row;
12122e73debSGreg Roach        }
12222e73debSGreg Roach
12322e73debSGreg Roach        $title = I18N::translate('Pending changes');
12422e73debSGreg Roach
12522e73debSGreg Roach        // If the current tree has changes, activate that tab.  Otherwise activate the first tab.
12622e73debSGreg Roach        if (($changes[$tree->id()] ?? []) === []) {
12722e73debSGreg Roach            reset($changes);
12822e73debSGreg Roach            $active_tree_id = key($changes);
12922e73debSGreg Roach        } else {
13022e73debSGreg Roach            $active_tree_id = $tree->id();
13122e73debSGreg Roach        }
13222e73debSGreg Roach
13322e73debSGreg Roach        return $this->viewResponse('pending-changes-page', [
13422e73debSGreg Roach            'active_tree_id' => $active_tree_id,
13522e73debSGreg Roach            'changes'        => $changes,
13622e73debSGreg Roach            'title'          => $title,
13722e73debSGreg Roach            'tree'           => $tree,
13822e73debSGreg Roach            'url'            => $url,
13922e73debSGreg Roach        ]);
14022e73debSGreg Roach    }
14122e73debSGreg Roach}
142