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