xref: /webtrees/app/Services/TreeService.php (revision 90a2f71801fb87cd8a5109b7c84d162b5549b38c)
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
22*90a2f718SGreg 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;
285afbc57aSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
295afbc57aSGreg Roachuse Illuminate\Database\Query\Builder;
305afbc57aSGreg Roachuse Illuminate\Database\Query\Expression;
315afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause;
325afbc57aSGreg Roachuse Illuminate\Support\Collection;
33*90a2f718SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
341e653452SGreg Roachuse RuntimeException;
355afbc57aSGreg Roachuse stdClass;
365afbc57aSGreg Roach
375afbc57aSGreg Roachuse function app;
38*90a2f718SGreg Roachuse function assert;
395afbc57aSGreg Roach
405afbc57aSGreg Roach/**
415afbc57aSGreg Roach * Tree management and queries.
425afbc57aSGreg Roach */
435afbc57aSGreg Roachclass TreeService
445afbc57aSGreg Roach{
455afbc57aSGreg Roach    // The most likely surname tradition for a given language.
465afbc57aSGreg Roach    private const DEFAULT_SURNAME_TRADITIONS = [
475afbc57aSGreg Roach        'es'    => 'spanish',
485afbc57aSGreg Roach        'is'    => 'icelandic',
495afbc57aSGreg Roach        'lt'    => 'lithuanian',
505afbc57aSGreg Roach        'pl'    => 'polish',
515afbc57aSGreg Roach        'pt'    => 'portuguese',
525afbc57aSGreg Roach        'pt-BR' => 'portuguese',
535afbc57aSGreg Roach    ];
545afbc57aSGreg Roach
555afbc57aSGreg Roach    /**
565afbc57aSGreg Roach     * All the trees that the current user has permission to access.
575afbc57aSGreg Roach     *
585afbc57aSGreg Roach     * @return Collection
595afbc57aSGreg Roach     */
605afbc57aSGreg Roach    public function all(): Collection
615afbc57aSGreg Roach    {
625afbc57aSGreg Roach        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__, static function (): Collection {
635afbc57aSGreg Roach            // All trees
645afbc57aSGreg Roach            $query = DB::table('gedcom')
655afbc57aSGreg Roach                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
665afbc57aSGreg Roach                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
675afbc57aSGreg Roach                        ->where('gedcom_setting.setting_name', '=', 'title');
685afbc57aSGreg Roach                })
695afbc57aSGreg Roach                ->where('gedcom.gedcom_id', '>', 0)
705afbc57aSGreg Roach                ->select([
715afbc57aSGreg Roach                    'gedcom.gedcom_id AS tree_id',
725afbc57aSGreg Roach                    'gedcom.gedcom_name AS tree_name',
735afbc57aSGreg Roach                    'gedcom_setting.setting_value AS tree_title',
745afbc57aSGreg Roach                ])
755afbc57aSGreg Roach                ->orderBy('gedcom.sort_order')
765afbc57aSGreg Roach                ->orderBy('gedcom_setting.setting_value');
775afbc57aSGreg Roach
785afbc57aSGreg Roach            // Non-admins may not see all trees
795afbc57aSGreg Roach            if (!Auth::isAdmin()) {
805afbc57aSGreg Roach                $query
815afbc57aSGreg Roach                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
825afbc57aSGreg Roach                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
835afbc57aSGreg Roach                            ->where('gs2.setting_name', '=', 'imported');
845afbc57aSGreg Roach                    })
855afbc57aSGreg Roach                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
865afbc57aSGreg Roach                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
875afbc57aSGreg Roach                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
885afbc57aSGreg Roach                    })
895afbc57aSGreg Roach                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
905afbc57aSGreg Roach                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
915afbc57aSGreg Roach                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
925afbc57aSGreg Roach                            ->where('user_gedcom_setting.setting_name', '=', 'canedit');
935afbc57aSGreg Roach                    })
945afbc57aSGreg Roach                    ->where(static function (Builder $query): void {
955afbc57aSGreg Roach                        $query
965afbc57aSGreg Roach                            // Managers
975afbc57aSGreg Roach                            ->where('user_gedcom_setting.setting_value', '=', 'admin')
985afbc57aSGreg Roach                            // Members
995afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1005afbc57aSGreg Roach                                $query
1015afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1025afbc57aSGreg Roach                                    ->where('gs3.setting_value', '=', '1')
1035afbc57aSGreg Roach                                    ->where('user_gedcom_setting.setting_value', '<>', 'none');
1045afbc57aSGreg Roach                            })
1055afbc57aSGreg Roach                            // Public trees
1065afbc57aSGreg Roach                            ->orWhere(static function (Builder $query): void {
1075afbc57aSGreg Roach                                $query
1085afbc57aSGreg Roach                                    ->where('gs2.setting_value', '=', '1')
1095afbc57aSGreg Roach                                    ->where('gs3.setting_value', '<>', '1');
1105afbc57aSGreg Roach                            });
1115afbc57aSGreg Roach                    });
1125afbc57aSGreg Roach            }
1135afbc57aSGreg Roach
1145afbc57aSGreg Roach            return $query
1155afbc57aSGreg Roach                ->get()
1165afbc57aSGreg Roach                ->mapWithKeys(static function (stdClass $row): array {
1171e653452SGreg Roach                    return [$row->tree_name => Tree::rowMapper()($row)];
1185afbc57aSGreg Roach                });
1195afbc57aSGreg Roach        });
1205afbc57aSGreg Roach    }
1215afbc57aSGreg Roach
1225afbc57aSGreg Roach    /**
1231e653452SGreg Roach     * Find a tree by its ID.
1245afbc57aSGreg Roach     *
1251e653452SGreg Roach     * @param int $id
1265afbc57aSGreg Roach     *
1271e653452SGreg Roach     * @return Tree
1285afbc57aSGreg Roach     */
1291e653452SGreg Roach    public function find(int $id): Tree
1305afbc57aSGreg Roach    {
1311e653452SGreg Roach        $tree = $this->all()->first(static function (Tree $tree) use ($id): bool {
1321e653452SGreg Roach            return $tree->id() === $id;
1335afbc57aSGreg Roach        });
1341e653452SGreg Roach
1351e653452SGreg Roach        assert($tree instanceof Tree, new RuntimeException());
1361e653452SGreg Roach
1371e653452SGreg Roach        return $tree;
1381e653452SGreg Roach    }
1391e653452SGreg Roach
1401e653452SGreg Roach    /**
1411e653452SGreg Roach     * All trees, name => title
1421e653452SGreg Roach     *
1431e653452SGreg Roach     * @return string[]
1441e653452SGreg Roach     */
1451e653452SGreg Roach    public function titles(): array
1461e653452SGreg Roach    {
1471e653452SGreg Roach        return $this->all()->map(static function (Tree $tree): string {
1481e653452SGreg Roach            return $tree->title();
1491e653452SGreg Roach        })->all();
1505afbc57aSGreg Roach    }
1515afbc57aSGreg Roach
1525afbc57aSGreg Roach    /**
1535afbc57aSGreg Roach     * @param string $name
1545afbc57aSGreg Roach     * @param string $title
1555afbc57aSGreg Roach     *
1565afbc57aSGreg Roach     * @return Tree
1575afbc57aSGreg Roach     */
1585afbc57aSGreg Roach    public function create(string $name, string $title): Tree
1595afbc57aSGreg Roach    {
160*90a2f718SGreg Roach        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
161*90a2f718SGreg Roach        assert($locale instanceof LocaleInterface);
162*90a2f718SGreg Roach
1635afbc57aSGreg Roach        DB::table('gedcom')->insert([
1645afbc57aSGreg Roach            'gedcom_name' => $name,
1655afbc57aSGreg Roach        ]);
1665afbc57aSGreg Roach
1675afbc57aSGreg Roach        $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
1685afbc57aSGreg Roach
1695afbc57aSGreg Roach        $tree = new Tree($tree_id, $name, $title);
1705afbc57aSGreg Roach
1715afbc57aSGreg Roach        $tree->setPreference('imported', '1');
1725afbc57aSGreg Roach        $tree->setPreference('title', $title);
1735afbc57aSGreg Roach
1745afbc57aSGreg Roach        // Set preferences from default tree
1755afbc57aSGreg Roach        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
1765afbc57aSGreg Roach            ['gedcom_id', 'setting_name', 'setting_value'],
1775afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1785afbc57aSGreg Roach                $query
1795afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
1805afbc57aSGreg Roach                    ->from('gedcom_setting')
1815afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
1825afbc57aSGreg Roach            }
1835afbc57aSGreg Roach        );
1845afbc57aSGreg Roach
1855afbc57aSGreg Roach        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
1865afbc57aSGreg Roach            ['gedcom_id', 'tag_type', 'resn'],
1875afbc57aSGreg Roach            static function (Builder $query) use ($tree_id): void {
1885afbc57aSGreg Roach                $query
1895afbc57aSGreg Roach                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
1905afbc57aSGreg Roach                    ->from('default_resn')
1915afbc57aSGreg Roach                    ->where('gedcom_id', '=', -1);
1925afbc57aSGreg Roach            }
1935afbc57aSGreg Roach        );
1945afbc57aSGreg Roach
1955afbc57aSGreg Roach        // Gedcom and privacy settings
1965afbc57aSGreg Roach        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
1975afbc57aSGreg Roach        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
198*90a2f718SGreg Roach        $tree->setPreference('LANGUAGE', $locale->languageTag()); // Default to the current admin’s language
199*90a2f718SGreg Roach        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[$locale->languageTag()] ?? 'paternal');
2005afbc57aSGreg Roach
2015afbc57aSGreg Roach        // A tree needs at least one record.
2029b5c9597SGreg 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";
2039b5c9597SGreg Roach        FunctionsImport::importRecord($head, $tree, true);
2045afbc57aSGreg Roach
2055afbc57aSGreg Roach        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
2065afbc57aSGreg Roach        $name = I18N::translate('John /DOE/');
2075afbc57aSGreg Roach        $note = I18N::translate('Edit this individual and replace their details with your own.');
2089b5c9597SGreg Roach        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
2099b5c9597SGreg Roach        FunctionsImport::importRecord($indi, $tree, true);
2105afbc57aSGreg Roach
2115afbc57aSGreg Roach        return $tree;
2125afbc57aSGreg Roach    }
2135afbc57aSGreg Roach
2145afbc57aSGreg Roach    /**
2155afbc57aSGreg Roach     * @param Tree $tree
2165afbc57aSGreg Roach     */
2175afbc57aSGreg Roach    public function delete(Tree $tree): void
2185afbc57aSGreg Roach    {
2195afbc57aSGreg Roach        // If this is the default tree, then unset it
2205afbc57aSGreg Roach        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
2215afbc57aSGreg Roach            Site::setPreference('DEFAULT_GEDCOM', '');
2225afbc57aSGreg Roach        }
2235afbc57aSGreg Roach
2245afbc57aSGreg Roach        $tree->deleteGenealogyData(false);
2255afbc57aSGreg Roach
2265afbc57aSGreg Roach        DB::table('block_setting')
2275afbc57aSGreg Roach            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
2285afbc57aSGreg Roach            ->where('gedcom_id', '=', $tree->id())
2295afbc57aSGreg Roach            ->delete();
2305afbc57aSGreg Roach        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
2315afbc57aSGreg Roach        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
2325afbc57aSGreg Roach        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
2335afbc57aSGreg Roach        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
2345afbc57aSGreg Roach        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
2355afbc57aSGreg Roach        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
2365afbc57aSGreg Roach        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
2375afbc57aSGreg Roach        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
2385afbc57aSGreg Roach        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
2395afbc57aSGreg Roach    }
2405afbc57aSGreg Roach
2415afbc57aSGreg Roach    /**
2425afbc57aSGreg Roach     * Generate a unique name for a new tree.
2435afbc57aSGreg Roach     *
2445afbc57aSGreg Roach     * @return string
2455afbc57aSGreg Roach     */
2465afbc57aSGreg Roach    public function uniqueTreeName(): string
2475afbc57aSGreg Roach    {
2485afbc57aSGreg Roach        $name   = 'tree';
2495afbc57aSGreg Roach        $number = 1;
2505afbc57aSGreg Roach
2511e653452SGreg Roach        while ($this->all()->get($name . $number) instanceof Tree) {
2525afbc57aSGreg Roach            $number++;
2535afbc57aSGreg Roach        }
2545afbc57aSGreg Roach
2555afbc57aSGreg Roach        return $name . $number;
2565afbc57aSGreg Roach    }
2575afbc57aSGreg Roach}
258