xref: /webtrees/app/Services/TreeService.php (revision a4e402eb603d108dd04b9d4be757e6dd2ae9ded0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Localization\Locale\LocaleInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Functions\FunctionsImport;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\User;
29use Illuminate\Database\Capsule\Manager as DB;
30use Illuminate\Database\Query\Builder;
31use Illuminate\Database\Query\Expression;
32use Illuminate\Database\Query\JoinClause;
33use Illuminate\Support\Collection;
34use Psr\Http\Message\ServerRequestInterface;
35use RuntimeException;
36use stdClass;
37
38use function app;
39use function assert;
40
41/**
42 * Tree management and queries.
43 */
44class TreeService
45{
46    // The most likely surname tradition for a given language.
47    private const DEFAULT_SURNAME_TRADITIONS = [
48        'es'    => 'spanish',
49        'is'    => 'icelandic',
50        'lt'    => 'lithuanian',
51        'pl'    => 'polish',
52        'pt'    => 'portuguese',
53        'pt-BR' => 'portuguese',
54    ];
55
56    /**
57     * All the trees that the current user has permission to access.
58     *
59     * @return Collection
60     */
61    public function all(): Collection
62    {
63        return app('cache.array')->rememberForever(__CLASS__ . __METHOD__, static function (): Collection {
64            // All trees
65            $query = DB::table('gedcom')
66                ->leftJoin('gedcom_setting', static function (JoinClause $join): void {
67                    $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
68                        ->where('gedcom_setting.setting_name', '=', 'title');
69                })
70                ->where('gedcom.gedcom_id', '>', 0)
71                ->select([
72                    'gedcom.gedcom_id AS tree_id',
73                    'gedcom.gedcom_name AS tree_name',
74                    'gedcom_setting.setting_value AS tree_title',
75                ])
76                ->orderBy('gedcom.sort_order')
77                ->orderBy('gedcom_setting.setting_value');
78
79            // Non-admins may not see all trees
80            if (!Auth::isAdmin()) {
81                $query
82                    ->join('gedcom_setting AS gs2', static function (JoinClause $join): void {
83                        $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id')
84                            ->where('gs2.setting_name', '=', 'imported');
85                    })
86                    ->join('gedcom_setting AS gs3', static function (JoinClause $join): void {
87                        $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id')
88                            ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION');
89                    })
90                    ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void {
91                        $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id')
92                            ->where('user_gedcom_setting.user_id', '=', Auth::id())
93                            ->where('user_gedcom_setting.setting_name', '=', User::PREF_TREE_ROLE);
94                    })
95                    ->where(static function (Builder $query): void {
96                        $query
97                            // Managers
98                            ->where('user_gedcom_setting.setting_value', '=', User::ROLE_MANAGER)
99                            // Members
100                            ->orWhere(static function (Builder $query): void {
101                                $query
102                                    ->where('gs2.setting_value', '=', '1')
103                                    ->where('gs3.setting_value', '=', '1')
104                                    ->where('user_gedcom_setting.setting_value', '<>', User::ROLE_VISITOR);
105                            })
106                            // Public trees
107                            ->orWhere(static function (Builder $query): void {
108                                $query
109                                    ->where('gs2.setting_value', '=', '1')
110                                    ->where('gs3.setting_value', '<>', '1');
111                            });
112                    });
113            }
114
115            return $query
116                ->get()
117                ->mapWithKeys(static function (stdClass $row): array {
118                    return [$row->tree_name => Tree::rowMapper()($row)];
119                });
120        });
121    }
122
123    /**
124     * Find a tree by its ID.
125     *
126     * @param int $id
127     *
128     * @return Tree
129     */
130    public function find(int $id): Tree
131    {
132        $tree = $this->all()->first(static function (Tree $tree) use ($id): bool {
133            return $tree->id() === $id;
134        });
135
136        assert($tree instanceof Tree, new RuntimeException());
137
138        return $tree;
139    }
140
141    /**
142     * All trees, name => title
143     *
144     * @return string[]
145     */
146    public function titles(): array
147    {
148        return $this->all()->map(static function (Tree $tree): string {
149            return $tree->title();
150        })->all();
151    }
152
153    /**
154     * @param string $name
155     * @param string $title
156     *
157     * @return Tree
158     */
159    public function create(string $name, string $title): Tree
160    {
161        $locale = app(ServerRequestInterface::class)->getAttribute('locale');
162        assert($locale instanceof LocaleInterface);
163
164        DB::table('gedcom')->insert([
165            'gedcom_name' => $name,
166        ]);
167
168        $tree_id = (int) DB::connection()->getPdo()->lastInsertId();
169
170        $tree = new Tree($tree_id, $name, $title);
171
172        $tree->setPreference('imported', '1');
173        $tree->setPreference('title', $title);
174
175        // Set preferences from default tree
176        (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing(
177            ['gedcom_id', 'setting_name', 'setting_value'],
178            static function (Builder $query) use ($tree_id): void {
179                $query
180                    ->select([new Expression($tree_id), 'setting_name', 'setting_value'])
181                    ->from('gedcom_setting')
182                    ->where('gedcom_id', '=', -1);
183            }
184        );
185
186        (new Builder(DB::connection()))->from('default_resn')->insertUsing(
187            ['gedcom_id', 'tag_type', 'resn'],
188            static function (Builder $query) use ($tree_id): void {
189                $query
190                    ->select([new Expression($tree_id), 'tag_type', 'resn'])
191                    ->from('default_resn')
192                    ->where('gedcom_id', '=', -1);
193            }
194        );
195
196        // Gedcom and privacy settings
197        $tree->setPreference('CONTACT_USER_ID', (string) Auth::id());
198        $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id());
199        $tree->setPreference('LANGUAGE', $locale->languageTag()); // Default to the current admin’s language
200        $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[$locale->languageTag()] ?? 'paternal');
201
202        // A tree needs at least one record.
203        $head = "0 HEAD\n1 SOUR webtrees\n2 DEST webtrees\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED\n1 CHAR UTF-8";
204        FunctionsImport::importRecord($head, $tree, true);
205
206        // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname.
207        $name = I18N::translate('John /DOE/');
208        $note = I18N::translate('Edit this individual and replace their details with your own.');
209        $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note;
210        FunctionsImport::importRecord($indi, $tree, true);
211
212        return $tree;
213    }
214
215    /**
216     * @param Tree $tree
217     */
218    public function delete(Tree $tree): void
219    {
220        // If this is the default tree, then unset it
221        if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) {
222            Site::setPreference('DEFAULT_GEDCOM', '');
223        }
224
225        $tree->deleteGenealogyData(false);
226
227        DB::table('block_setting')
228            ->join('block', 'block.block_id', '=', 'block_setting.block_id')
229            ->where('gedcom_id', '=', $tree->id())
230            ->delete();
231        DB::table('block')->where('gedcom_id', '=', $tree->id())->delete();
232        DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
233        DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete();
234        DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete();
235        DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete();
236        DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete();
237        DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete();
238        DB::table('log')->where('gedcom_id', '=', $tree->id())->delete();
239        DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete();
240    }
241
242    /**
243     * Generate a unique name for a new tree.
244     *
245     * @return string
246     */
247    public function uniqueTreeName(): string
248    {
249        $name   = 'tree';
250        $number = 1;
251
252        while ($this->all()->get($name . $number) instanceof Tree) {
253            $number++;
254        }
255
256        return $name . $number;
257    }
258}
259