xref: /webtrees/app/Http/RequestHandlers/MergeTreesAction.php (revision 52550490b7095dd69811f3ec21ed5a3ca1a8968d)
16fd01894SGreg Roach<?php
26fd01894SGreg Roach
36fd01894SGreg Roach/**
46fd01894SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
66fd01894SGreg Roach * This program is free software: you can redistribute it and/or modify
76fd01894SGreg Roach * it under the terms of the GNU General Public License as published by
86fd01894SGreg Roach * the Free Software Foundation, either version 3 of the License, or
96fd01894SGreg Roach * (at your option) any later version.
106fd01894SGreg Roach * This program is distributed in the hope that it will be useful,
116fd01894SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
126fd01894SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136fd01894SGreg Roach * GNU General Public License for more details.
146fd01894SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
166fd01894SGreg Roach */
176fd01894SGreg Roach
186fd01894SGreg Roachdeclare(strict_types=1);
196fd01894SGreg Roach
206fd01894SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
216fd01894SGreg Roach
226f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
236fd01894SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
246fd01894SGreg Roachuse Fisharebest\Webtrees\Header;
256fd01894SGreg Roachuse Fisharebest\Webtrees\I18N;
266fd01894SGreg Roachuse Fisharebest\Webtrees\Services\AdminService;
276fd01894SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
286fd01894SGreg Roachuse Fisharebest\Webtrees\Tree;
29748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
306fd01894SGreg Roachuse Illuminate\Database\Query\Builder;
316fd01894SGreg Roachuse Illuminate\Database\Query\Expression;
326fd01894SGreg Roachuse Psr\Http\Message\ResponseInterface;
336fd01894SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
346fd01894SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
356fd01894SGreg Roach
366fd01894SGreg Roachuse function redirect;
376fd01894SGreg Roachuse function route;
386fd01894SGreg Roach
396fd01894SGreg Roach/**
406fd01894SGreg Roach * Merge two family trees.
416fd01894SGreg Roach */
426fd01894SGreg Roachclass MergeTreesAction implements RequestHandlerInterface
436fd01894SGreg Roach{
44c4943cffSGreg Roach    private AdminService $admin_service;
456fd01894SGreg Roach
46c4943cffSGreg Roach    private TreeService $tree_service;
476fd01894SGreg Roach
486fd01894SGreg Roach    /**
496fd01894SGreg Roach     * @param AdminService $admin_service
506fd01894SGreg Roach     * @param TreeService  $tree_service
516fd01894SGreg Roach     */
526fd01894SGreg Roach    public function __construct(AdminService $admin_service, TreeService $tree_service)
536fd01894SGreg Roach    {
546fd01894SGreg Roach        $this->admin_service = $admin_service;
556fd01894SGreg Roach        $this->tree_service  = $tree_service;
566fd01894SGreg Roach    }
576fd01894SGreg Roach
586fd01894SGreg Roach    /**
596fd01894SGreg Roach     * @param ServerRequestInterface $request
606fd01894SGreg Roach     *
616fd01894SGreg Roach     * @return ResponseInterface
626fd01894SGreg Roach     */
636fd01894SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
646fd01894SGreg Roach    {
65748dbe15SGreg Roach        $tree1_name = Validator::parsedBody($request)->string('tree1_name');
66748dbe15SGreg Roach        $tree2_name = Validator::parsedBody($request)->string('tree2_name');
676fd01894SGreg Roach
686fd01894SGreg Roach        $tree1 = $this->tree_service->all()->get($tree1_name);
696fd01894SGreg Roach        $tree2 = $this->tree_service->all()->get($tree2_name);
706fd01894SGreg Roach
716fd01894SGreg Roach        if ($tree1 instanceof Tree && $tree2 instanceof Tree && $tree1 !== $tree2 && $this->admin_service->countCommonXrefs($tree1, $tree2) === 0) {
72*52550490SGreg Roach            DB::query()->from('individuals')->insertUsing([
736fd01894SGreg Roach                'i_file',
746fd01894SGreg Roach                'i_id',
756fd01894SGreg Roach                'i_rin',
766fd01894SGreg Roach                'i_sex',
776fd01894SGreg Roach                'i_gedcom',
786fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
796fd01894SGreg Roach                $query->select([
806fd01894SGreg Roach                    new Expression($tree2->id()),
816fd01894SGreg Roach                    'i_id',
826fd01894SGreg Roach                    'i_rin',
836fd01894SGreg Roach                    'i_sex',
846fd01894SGreg Roach                    'i_gedcom',
856fd01894SGreg Roach                ])->from('individuals')
866fd01894SGreg Roach                    ->where('i_file', '=', $tree1->id());
876fd01894SGreg Roach            });
886fd01894SGreg Roach
89*52550490SGreg Roach            DB::query()->from('families')->insertUsing([
906fd01894SGreg Roach                'f_file',
916fd01894SGreg Roach                'f_id',
926fd01894SGreg Roach                'f_husb',
936fd01894SGreg Roach                'f_wife',
946fd01894SGreg Roach                'f_gedcom',
956fd01894SGreg Roach                'f_numchil',
966fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
976fd01894SGreg Roach                $query->select([
986fd01894SGreg Roach                    new Expression($tree2->id()),
996fd01894SGreg Roach                    'f_id',
1006fd01894SGreg Roach                    'f_husb',
1016fd01894SGreg Roach                    'f_wife',
1026fd01894SGreg Roach                    'f_gedcom',
1036fd01894SGreg Roach                    'f_numchil',
1046fd01894SGreg Roach                ])->from('families')
1056fd01894SGreg Roach                    ->where('f_file', '=', $tree1->id());
1066fd01894SGreg Roach            });
1076fd01894SGreg Roach
108*52550490SGreg Roach            DB::query()->from('sources')->insertUsing([
1096fd01894SGreg Roach                's_file',
1106fd01894SGreg Roach                's_id',
1116fd01894SGreg Roach                's_name',
1126fd01894SGreg Roach                's_gedcom',
1136fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
1146fd01894SGreg Roach                $query->select([
1156fd01894SGreg Roach                    new Expression($tree2->id()),
1166fd01894SGreg Roach                    's_id',
1176fd01894SGreg Roach                    's_name',
1186fd01894SGreg Roach                    's_gedcom',
1196fd01894SGreg Roach                ])->from('sources')
1206fd01894SGreg Roach                    ->where('s_file', '=', $tree1->id());
1216fd01894SGreg Roach            });
1226fd01894SGreg Roach
123*52550490SGreg Roach            DB::query()->from('media')->insertUsing([
1246fd01894SGreg Roach                'm_file',
1256fd01894SGreg Roach                'm_id',
1266fd01894SGreg Roach                'm_gedcom',
1276fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
1286fd01894SGreg Roach                $query->select([
1296fd01894SGreg Roach                    new Expression($tree2->id()),
1306fd01894SGreg Roach                    'm_id',
1316fd01894SGreg Roach                    'm_gedcom',
1326fd01894SGreg Roach                ])->from('media')
1336fd01894SGreg Roach                    ->where('m_file', '=', $tree1->id());
1346fd01894SGreg Roach            });
1356fd01894SGreg Roach
136*52550490SGreg Roach            DB::query()->from('media_file')->insertUsing([
1376fd01894SGreg Roach                'm_file',
1386fd01894SGreg Roach                'm_id',
1396fd01894SGreg Roach                'multimedia_file_refn',
1406fd01894SGreg Roach                'multimedia_format',
1416fd01894SGreg Roach                'source_media_type',
1426fd01894SGreg Roach                'descriptive_title',
1436fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
1446fd01894SGreg Roach                $query->select([
1456fd01894SGreg Roach                    new Expression($tree2->id()),
1466fd01894SGreg Roach                    'm_id',
1476fd01894SGreg Roach                    'multimedia_file_refn',
1486fd01894SGreg Roach                    'multimedia_format',
1496fd01894SGreg Roach                    'source_media_type',
1506fd01894SGreg Roach                    'descriptive_title',
1516fd01894SGreg Roach                ])->from('media_file')
1526fd01894SGreg Roach                    ->where('m_file', '=', $tree1->id());
1536fd01894SGreg Roach            });
1546fd01894SGreg Roach
155*52550490SGreg Roach            DB::query()->from('other')->insertUsing([
1566fd01894SGreg Roach                'o_file',
1576fd01894SGreg Roach                'o_id',
1586fd01894SGreg Roach                'o_type',
1596fd01894SGreg Roach                'o_gedcom',
1606fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
1616fd01894SGreg Roach                $query->select([
1626fd01894SGreg Roach                    new Expression($tree2->id()),
1636fd01894SGreg Roach                    'o_id',
1646fd01894SGreg Roach                    'o_type',
1656fd01894SGreg Roach                    'o_gedcom',
1666fd01894SGreg Roach                ])->from('other')
1676fd01894SGreg Roach                    ->whereNotIn('o_type', [Header::RECORD_TYPE, 'TRLR'])
1686fd01894SGreg Roach                    ->where('o_file', '=', $tree1->id());
1696fd01894SGreg Roach            });
1706fd01894SGreg Roach
171*52550490SGreg Roach            DB::query()->from('name')->insertUsing([
1726fd01894SGreg Roach                'n_file',
1736fd01894SGreg Roach                'n_id',
1746fd01894SGreg Roach                'n_num',
1756fd01894SGreg Roach                'n_type',
1766fd01894SGreg Roach                'n_sort',
1776fd01894SGreg Roach                'n_full',
1786fd01894SGreg Roach                'n_surname',
1796fd01894SGreg Roach                'n_surn',
1806fd01894SGreg Roach                'n_givn',
1816fd01894SGreg Roach                'n_soundex_givn_std',
1826fd01894SGreg Roach                'n_soundex_surn_std',
1836fd01894SGreg Roach                'n_soundex_givn_dm',
1846fd01894SGreg Roach                'n_soundex_surn_dm',
1856fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
1866fd01894SGreg Roach                $query->select([
1876fd01894SGreg Roach                    new Expression($tree2->id()),
1886fd01894SGreg Roach                    'n_id',
1896fd01894SGreg Roach                    'n_num',
1906fd01894SGreg Roach                    'n_type',
1916fd01894SGreg Roach                    'n_sort',
1926fd01894SGreg Roach                    'n_full',
1936fd01894SGreg Roach                    'n_surname',
1946fd01894SGreg Roach                    'n_surn',
1956fd01894SGreg Roach                    'n_givn',
1966fd01894SGreg Roach                    'n_soundex_givn_std',
1976fd01894SGreg Roach                    'n_soundex_surn_std',
1986fd01894SGreg Roach                    'n_soundex_givn_dm',
1996fd01894SGreg Roach                    'n_soundex_surn_dm',
2006fd01894SGreg Roach                ])->from('name')
2016fd01894SGreg Roach                    ->where('n_file', '=', $tree1->id());
2026fd01894SGreg Roach            });
2036fd01894SGreg Roach
204*52550490SGreg Roach            DB::query()->from('dates')->insertUsing([
2056fd01894SGreg Roach                'd_file',
2066fd01894SGreg Roach                'd_gid',
2076fd01894SGreg Roach                'd_day',
2086fd01894SGreg Roach                'd_month',
2096fd01894SGreg Roach                'd_mon',
2106fd01894SGreg Roach                'd_year',
2116fd01894SGreg Roach                'd_julianday1',
2126fd01894SGreg Roach                'd_julianday2',
2136fd01894SGreg Roach                'd_fact',
2146fd01894SGreg Roach                'd_type',
2156fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
2166fd01894SGreg Roach                $query->select([
2176fd01894SGreg Roach                    new Expression($tree2->id()),
2186fd01894SGreg Roach                    'd_gid',
2196fd01894SGreg Roach                    'd_day',
2206fd01894SGreg Roach                    'd_month',
2216fd01894SGreg Roach                    'd_mon',
2226fd01894SGreg Roach                    'd_year',
2236fd01894SGreg Roach                    'd_julianday1',
2246fd01894SGreg Roach                    'd_julianday2',
2256fd01894SGreg Roach                    'd_fact',
2266fd01894SGreg Roach                    'd_type',
2276fd01894SGreg Roach                ])->from('dates')
2286fd01894SGreg Roach                    ->where('d_file', '=', $tree1->id());
2296fd01894SGreg Roach            });
2306fd01894SGreg Roach
231*52550490SGreg Roach            DB::query()->from('link')->insertUsing([
2326fd01894SGreg Roach                'l_file',
2336fd01894SGreg Roach                'l_from',
2346fd01894SGreg Roach                'l_type',
2356fd01894SGreg Roach                'l_to',
2366fd01894SGreg Roach            ], static function (Builder $query) use ($tree1, $tree2): void {
2376fd01894SGreg Roach                $query->select([
2386fd01894SGreg Roach                    new Expression($tree2->id()),
2396fd01894SGreg Roach                    'l_from',
2406fd01894SGreg Roach                    'l_type',
2416fd01894SGreg Roach                    'l_to',
2426fd01894SGreg Roach                ])->from('link')
2436fd01894SGreg Roach                    ->whereNotIn('l_from', [Header::RECORD_TYPE, 'TRLR'])
2446fd01894SGreg Roach                    ->where('l_file', '=', $tree1->id());
2456fd01894SGreg Roach            });
2466fd01894SGreg Roach
2476fd01894SGreg Roach            FlashMessages::addMessage(I18N::translate('The family trees have been merged successfully.'), 'success');
2486fd01894SGreg Roach
2496fd01894SGreg Roach            $url = route(ManageTrees::class, ['tree' => $tree2->name()]);
2506fd01894SGreg Roach        } else {
2516fd01894SGreg Roach            $url = route(MergeTreesPage::class, [
2526fd01894SGreg Roach                'tree1_name' => $tree1->name(),
2536fd01894SGreg Roach                'tree2_name' => $tree2->name(),
2546fd01894SGreg Roach            ]);
2556fd01894SGreg Roach        }
2566fd01894SGreg Roach
2576fd01894SGreg Roach        return redirect($url);
2586fd01894SGreg Roach    }
2596fd01894SGreg Roach}
260