xref: /webtrees/app/Http/RequestHandlers/PendingChanges.php (revision 75964c75fd01f8e12e2a313cc89fc2222413a03f)
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\TreeService;
3322e73debSGreg Roachuse Fisharebest\Webtrees\Source;
3422e73debSGreg Roachuse Fisharebest\Webtrees\Tree;
3522e73debSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3622e73debSGreg Roachuse Psr\Http\Message\ResponseInterface;
3722e73debSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3822e73debSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3922e73debSGreg Roach
4022e73debSGreg Roachuse function assert;
4122e73debSGreg Roachuse function key;
4222e73debSGreg Roachuse function preg_match;
4322e73debSGreg Roachuse function reset;
4422e73debSGreg Roachuse function route;
4522e73debSGreg Roach
4622e73debSGreg Roach/**
4722e73debSGreg Roach * Show all pending changes.
4822e73debSGreg Roach */
4922e73debSGreg Roachclass PendingChanges implements RequestHandlerInterface
5022e73debSGreg Roach{
5122e73debSGreg Roach    use ViewResponseTrait;
5222e73debSGreg Roach
5357bfa969SGreg Roach    /** @var TreeService */
5457bfa969SGreg Roach    private $tree_service;
5522e73debSGreg Roach
5622e73debSGreg Roach    /**
5722e73debSGreg Roach     * @param TreeService $tree_service
5822e73debSGreg Roach     */
59dce3fc17SGreg Roach    public function __construct(TreeService $tree_service)
6022e73debSGreg Roach    {
6122e73debSGreg Roach        $this->tree_service = $tree_service;
6222e73debSGreg Roach    }
6322e73debSGreg Roach
6422e73debSGreg Roach    /**
6522e73debSGreg Roach     * @param ServerRequestInterface $request
6622e73debSGreg Roach     *
6722e73debSGreg Roach     * @return ResponseInterface
6822e73debSGreg Roach     */
6922e73debSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
7022e73debSGreg Roach    {
7122e73debSGreg Roach        $tree = $request->getAttribute('tree');
72*75964c75SGreg Roach        assert($tree instanceof Tree);
7322e73debSGreg Roach
7422e73debSGreg Roach        $url = $request->getQueryParams()['url'] ?? route('tree-page', ['tree' => $tree->name()]);
7522e73debSGreg Roach
7622e73debSGreg Roach        $rows = DB::table('change')
7722e73debSGreg Roach            ->join('user', 'user.user_id', '=', 'change.user_id')
7822e73debSGreg Roach            ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id')
7922e73debSGreg Roach            ->where('status', '=', 'pending')
8022e73debSGreg Roach            ->orderBy('change.gedcom_id')
8122e73debSGreg Roach            ->orderBy('change.xref')
8222e73debSGreg Roach            ->orderBy('change.change_id')
8322e73debSGreg Roach            ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name'])
8422e73debSGreg Roach            ->get();
8522e73debSGreg Roach
8622e73debSGreg Roach        $changes = [];
8722e73debSGreg Roach        foreach ($rows as $row) {
8822e73debSGreg Roach            $row->change_time = Carbon::make($row->change_time);
8922e73debSGreg Roach
9022e73debSGreg Roach            $change_tree = $this->tree_service->all()->get($row->gedcom_name);
9122e73debSGreg Roach
9222e73debSGreg Roach            preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match);
9322e73debSGreg Roach
9422e73debSGreg Roach            switch ($match[1]) {
9522e73debSGreg Roach                case 'INDI':
9622e73debSGreg Roach                    $row->record = new Individual($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
9722e73debSGreg Roach                    break;
9822e73debSGreg Roach                case 'FAM':
9922e73debSGreg Roach                    $row->record = new Family($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10022e73debSGreg Roach                    break;
10122e73debSGreg Roach                case 'SOUR':
10222e73debSGreg Roach                    $row->record = new Source($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10322e73debSGreg Roach                    break;
10422e73debSGreg Roach                case 'REPO':
10522e73debSGreg Roach                    $row->record = new Repository($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10622e73debSGreg Roach                    break;
10722e73debSGreg Roach                case 'OBJE':
10822e73debSGreg Roach                    $row->record = new Media($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
10922e73debSGreg Roach                    break;
11022e73debSGreg Roach                case 'NOTE':
11122e73debSGreg Roach                    $row->record = new Note($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
11222e73debSGreg Roach                    break;
11322e73debSGreg Roach                default:
11422e73debSGreg Roach                    $row->record = new GedcomRecord($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
11522e73debSGreg Roach                    break;
11622e73debSGreg Roach            }
11722e73debSGreg Roach
11822e73debSGreg Roach            $changes[$row->gedcom_id][$row->xref][] = $row;
11922e73debSGreg Roach        }
12022e73debSGreg Roach
12122e73debSGreg Roach        $title = I18N::translate('Pending changes');
12222e73debSGreg Roach
12322e73debSGreg Roach        // If the current tree has changes, activate that tab.  Otherwise activate the first tab.
12422e73debSGreg Roach        if (($changes[$tree->id()] ?? []) === []) {
12522e73debSGreg Roach            reset($changes);
12622e73debSGreg Roach            $active_tree_id = key($changes);
12722e73debSGreg Roach        } else {
12822e73debSGreg Roach            $active_tree_id = $tree->id();
12922e73debSGreg Roach        }
13022e73debSGreg Roach
13122e73debSGreg Roach        return $this->viewResponse('pending-changes-page', [
13222e73debSGreg Roach            'active_tree_id' => $active_tree_id,
13322e73debSGreg Roach            'changes'        => $changes,
13422e73debSGreg Roach            'title'          => $title,
13522e73debSGreg Roach            'tree'           => $tree,
13622e73debSGreg Roach            'url'            => $url,
13722e73debSGreg Roach        ]);
13822e73debSGreg Roach    }
13922e73debSGreg Roach}
140