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