xref: /webtrees/app/Services/TreeService.php (revision 7c4add84379afdbaa7c4c272763673edc20fb830)
15afbc57aSGreg Roach<?php
25afbc57aSGreg Roach
35afbc57aSGreg Roach/**
45afbc57aSGreg Roach * webtrees: online genealogy
55afbc57aSGreg Roach * Copyright (C) 2019 webtrees development team
65afbc57aSGreg Roach * This program is free software: you can redistribute it and/or modify
75afbc57aSGreg Roach * it under the terms of the GNU General Public License as published by
85afbc57aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
95afbc57aSGreg Roach * (at your option) any later version.
105afbc57aSGreg Roach * This program is distributed in the hope that it will be useful,
115afbc57aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
125afbc57aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135afbc57aSGreg Roach * GNU General Public License for more details.
145afbc57aSGreg Roach * You should have received a copy of the GNU General Public License
155afbc57aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
165afbc57aSGreg Roach */
17fcfa147eSGreg Roach
185afbc57aSGreg Roachdeclare(strict_types=1);
195afbc57aSGreg Roach
205afbc57aSGreg Roachnamespace Fisharebest\Webtrees\Services;
215afbc57aSGreg Roach
2290a2f718SGreg Roachuse Fisharebest\Localization\Locale\LocaleInterface;
235afbc57aSGreg Roachuse Fisharebest\Webtrees\Auth;
245afbc57aSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport;
255afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N;
265afbc57aSGreg Roachuse Fisharebest\Webtrees\Site;
275afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree;
28*7c4add84SGreg Roachuse Fisharebest\Webtrees\User;
295afbc57aSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
305afbc57aSGreg Roachuse Illuminate\Database\Query\Builder;
315afbc57aSGreg Roachuse Illuminate\Database\Query\Expression;
325afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause;
335afbc57aSGreg Roachuse Illuminate\Support\Collection;
3490a2f718SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
351e653452SGreg Roachuse RuntimeException;
365afbc57aSGreg Roachuse stdClass;
375afbc57aSGreg Roach
385afbc57aSGreg Roachuse function app;
3990a2f718SGreg Roachuse function assert;
405afbc57aSGreg Roach
415afbc57aSGreg Roach/**
425afbc57aSGreg Roach * Tree management and queries.
435afbc57aSGreg Roach */
445afbc57aSGreg Roachclass TreeService
455afbc57aSGreg Roach{
465afbc57aSGreg Roach    // The most likely surname tradition for a given language.
475afbc57aSGreg Roach    private const DEFAULT_SURNAME_TRADITIONS = [
485afbc57aSGreg Roach        'es'    => 'spanish',
495afbc57aSGreg Roach        'is'    => 'icelandic',
505afbc57aSGreg Roach        'lt'    => 'lithuanian',
515afbc57aSGreg Roach        'pl'    => 'polish',
525afbc57aSGreg Roach        'pt'    => 'portuguese',
535afbc57aSGreg Roach        'pt-BR' => 'portuguese',
545afbc57aSGreg Roach    ];
555afbc57aSGreg Roach
565afbc57aSGreg Roach    /**
575afbc57aSGreg Roach     * All the trees that the current user has permission to access.
585afbc57aSGreg Roach     *
595afbc57aSGreg Roach     * @return Collection
605afbc57aSGreg Roach     */
615afbc57aSGreg Roach    public function all(): Collection
625afbc57aSGreg Roach    {
635afbc57aSGreg Roach        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__, static function (): Collection {
645afbc57aSGreg Roach            // All trees
655afbc57aSGreg Roach            $query = DB::table('gedcom')
665afbc57aSGreg Roach                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
675afbc57aSGreg Roach                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
685afbc57aSGreg Roach                        ->where('gedcom_setting.setting_name', '=', 'title');
695afbc57aSGreg Roach                })
705afbc57aSGreg Roach                ->where('gedcom.gedcom_id', '>', 0)
715afbc57aSGreg Roach                ->select([
725afbc57aSGreg Roach                    'gedcom.gedcom_id AS tree_id',
735afbc57aSGreg Roach                    'gedcom.gedcom_name AS tree_name',
745afbc57aSGreg Roach                    'gedcom_setting.setting_value AS tree_title',
755afbc57aSGreg Roach                ])
765afbc57aSGreg Roach                ->orderBy('gedcom.sort_order')
775afbc57aSGreg Roach                ->orderBy('gedcom_setting.setting_value');
785afbc57aSGreg Roach
795afbc57aSGreg Roach            // Non-admins may not see all trees
805afbc57aSGreg Roach            if (!Auth::isAdmin()) {
815afbc57aSGreg Roach                $query
825afbc57aSGreg Roach                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
835afbc57aSGreg Roach                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
845afbc57aSGreg Roach                            ->where('gs2.setting_name', '=', 'imported');
855afbc57aSGreg Roach                    })
865afbc57aSGreg Roach                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
875afbc57aSGreg Roach                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
885afbc57aSGreg Roach                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
895afbc57aSGreg Roach                    })
905afbc57aSGreg Roach                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
915afbc57aSGreg Roach                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
925afbc57aSGreg Roach                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
93*7c4add84SGreg Roach                            ->where('user_gedcom_setting.setting_name', '=', User::PREF_TREE_ROLE);
945afbc57aSGreg Roach                    })
955afbc57aSGreg Roach                    ->where(static function (Builder $query): void {
965afbc57aSGreg Roach                        $query
975afbc57aSGreg Roach                            // Managers
98*7c4add84SGreg Roach                            ->where('user_gedcom_setting.setting_value', '=', User::ROLE_MANAGER)
995afbc57aSGreg Roach                            // Members
1005afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1015afbc57aSGreg Roach                                $query
1025afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1035afbc57aSGreg Roach                                    ->where('gs3.setting_value', '=', '1')
104*7c4add84SGreg Roach                                    ->where('user_gedcom_setting.setting_value', '<>', User::ROLE_VISITOR);
1055afbc57aSGreg Roach                            })
1065afbc57aSGreg Roach                            // Public trees
1075afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1085afbc57aSGreg Roach                                $query
1095afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1105afbc57aSGreg Roach                                    ->where('gs3.setting_value', '<>', '1');
1115afbc57aSGreg Roach                            });
1125afbc57aSGreg Roach                    });
1135afbc57aSGreg Roach            }
1145afbc57aSGreg Roach
1155afbc57aSGreg Roach            return $query
1165afbc57aSGreg Roach                ->get()
1175afbc57aSGreg Roach                ->mapWithKeys(static function (stdClass $row): array {
1181e653452SGreg Roach                    return [$row->tree_name => Tree::rowMapper()($row)];
1195afbc57aSGreg Roach                });
1205afbc57aSGreg Roach        });
1215afbc57aSGreg Roach    }
1225afbc57aSGreg Roach
1235afbc57aSGreg Roach    /**
1241e653452SGreg Roach     * Find a tree by its ID.
1255afbc57aSGreg Roach     *
1261e653452SGreg Roach     * @param int $id
1275afbc57aSGreg Roach     *
1281e653452SGreg Roach     * @return Tree
1295afbc57aSGreg Roach     */
1301e653452SGreg Roach    public function find(int $id): Tree
1315afbc57aSGreg Roach    {
1321e653452SGreg Roach        $tree = $this->all()->first(static function (Tree $tree) use ($id): bool {
1331e653452SGreg Roach            return $tree->id() === $id;
1345afbc57aSGreg Roach        });
1351e653452SGreg Roach
1361e653452SGreg Roach        assert($tree instanceof Tree, new RuntimeException());
1371e653452SGreg Roach
1381e653452SGreg Roach        return $tree;
1391e653452SGreg Roach    }
1401e653452SGreg Roach
1411e653452SGreg Roach    /**
1421e653452SGreg Roach     * All trees, name => title
1431e653452SGreg Roach     *
1441e653452SGreg Roach     * @return string[]
1451e653452SGreg Roach     */
1461e653452SGreg Roach    public function titles(): array
1471e653452SGreg Roach    {
1481e653452SGreg Roach        return $this->all()->map(static function (Tree $tree): string {
1491e653452SGreg Roach            return $tree->title();
1501e653452SGreg Roach        })->all();
1515afbc57aSGreg Roach    }
1525afbc57aSGreg Roach
1535afbc57aSGreg Roach    /**
1545afbc57aSGreg Roach     * @param string $name
1555afbc57aSGreg Roach     * @param string $title
1565afbc57aSGreg Roach     *
1575afbc57aSGreg Roach     * @return Tree
1585afbc57aSGreg Roach     */
1595afbc57aSGreg Roach    public function create(string $name, string $title): Tree
1605afbc57aSGreg Roach    {
16190a2f718SGreg Roach        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
16290a2f718SGreg Roach        assert($locale instanceof LocaleInterface);
16390a2f718SGreg Roach
1645afbc57aSGreg Roach        DB::table('gedcom')->insert([
1655afbc57aSGreg Roach            'gedcom_name' => $name,
1665afbc57aSGreg Roach        ]);
1675afbc57aSGreg Roach
1685afbc57aSGreg Roach        $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
1695afbc57aSGreg Roach
1705afbc57aSGreg Roach        $tree = new Tree($tree_id, $name, $title);
1715afbc57aSGreg Roach
1725afbc57aSGreg Roach        $tree->setPreference('imported', '1');
1735afbc57aSGreg Roach        $tree->setPreference('title', $title);
1745afbc57aSGreg Roach
1755afbc57aSGreg Roach        // Set preferences from default tree
1765afbc57aSGreg Roach        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
1775afbc57aSGreg Roach            ['gedcom_id', 'setting_name', 'setting_value'],
1785afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1795afbc57aSGreg Roach                $query
1805afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
1815afbc57aSGreg Roach                    ->from('gedcom_setting')
1825afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
1835afbc57aSGreg Roach            }
1845afbc57aSGreg Roach        );
1855afbc57aSGreg Roach
1865afbc57aSGreg Roach        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
1875afbc57aSGreg Roach            ['gedcom_id', 'tag_type', 'resn'],
1885afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1895afbc57aSGreg Roach                $query
1905afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
1915afbc57aSGreg Roach                    ->from('default_resn')
1925afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
1935afbc57aSGreg Roach            }
1945afbc57aSGreg Roach        );
1955afbc57aSGreg Roach
1965afbc57aSGreg Roach        // Gedcom and privacy settings
1975afbc57aSGreg Roach        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
1985afbc57aSGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
19990a2f718SGreg Roach        $tree->setPreference('LANGUAGE', $locale->languageTag()); // Default to the current admin’s language
20090a2f718SGreg Roach        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[$locale->languageTag()] ?? 'paternal');
2015afbc57aSGreg Roach
2025afbc57aSGreg Roach        // A tree needs at least one record.
2039b5c9597SGreg Roach        $head = "0 HEAD\n1 SOUR webtrees\n2 DEST webtrees\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED\n1 CHAR UTF-8";
2049b5c9597SGreg Roach        FunctionsImport::importRecord($head, $tree, true);
2055afbc57aSGreg Roach
2065afbc57aSGreg Roach        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
2075afbc57aSGreg Roach        $name = I18N::translate('John /DOE/');
2085afbc57aSGreg Roach        $note = I18N::translate('Edit this individual and replace their details with your own.');
2099b5c9597SGreg Roach        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
2109b5c9597SGreg Roach        FunctionsImport::importRecord($indi, $tree, true);
2115afbc57aSGreg Roach
2125afbc57aSGreg Roach        return $tree;
2135afbc57aSGreg Roach    }
2145afbc57aSGreg Roach
2155afbc57aSGreg Roach    /**
2165afbc57aSGreg Roach     * @param Tree $tree
2175afbc57aSGreg Roach     */
2185afbc57aSGreg Roach    public function delete(Tree $tree): void
2195afbc57aSGreg Roach    {
2205afbc57aSGreg Roach        // If this is the default tree, then unset it
2215afbc57aSGreg Roach        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
2225afbc57aSGreg Roach            Site::setPreference('DEFAULT_GEDCOM', '');
2235afbc57aSGreg Roach        }
2245afbc57aSGreg Roach
2255afbc57aSGreg Roach        $tree->deleteGenealogyData(false);
2265afbc57aSGreg Roach
2275afbc57aSGreg Roach        DB::table('block_setting')
2285afbc57aSGreg Roach            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
2295afbc57aSGreg Roach            ->where('gedcom_id', '=', $tree->id())
2305afbc57aSGreg Roach            ->delete();
2315afbc57aSGreg Roach        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
2325afbc57aSGreg Roach        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
2335afbc57aSGreg Roach        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
2345afbc57aSGreg Roach        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
2355afbc57aSGreg Roach        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
2365afbc57aSGreg Roach        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
2375afbc57aSGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
2385afbc57aSGreg Roach        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
2395afbc57aSGreg Roach        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
2405afbc57aSGreg Roach    }
2415afbc57aSGreg Roach
2425afbc57aSGreg Roach    /**
2435afbc57aSGreg Roach     * Generate a unique name for a new tree.
2445afbc57aSGreg Roach     *
2455afbc57aSGreg Roach     * @return string
2465afbc57aSGreg Roach     */
2475afbc57aSGreg Roach    public function uniqueTreeName(): string
2485afbc57aSGreg Roach    {
2495afbc57aSGreg Roach        $name   = 'tree';
2505afbc57aSGreg Roach        $number = 1;
2515afbc57aSGreg Roach
2521e653452SGreg Roach        while ($this->all()->get($name . $number) instanceof Tree) {
2535afbc57aSGreg Roach            $number++;
2545afbc57aSGreg Roach        }
2555afbc57aSGreg Roach
2565afbc57aSGreg Roach        return $name . $number;
2575afbc57aSGreg Roach    }
2585afbc57aSGreg Roach}
259