15afbc57aSGreg Roach<?php 25afbc57aSGreg Roach 35afbc57aSGreg Roach/** 45afbc57aSGreg Roach * webtrees: online genealogy 51fe542e9SGreg Roach * Copyright (C) 2021 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 15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 165afbc57aSGreg Roach */ 17fcfa147eSGreg Roach 185afbc57aSGreg Roachdeclare(strict_types=1); 195afbc57aSGreg Roach 205afbc57aSGreg Roachnamespace Fisharebest\Webtrees\Services; 215afbc57aSGreg Roach 225afbc57aSGreg Roachuse Fisharebest\Webtrees\Auth; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 246b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 255afbc57aSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 265afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N; 275afbc57aSGreg Roachuse Fisharebest\Webtrees\Site; 285afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree; 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; 341e653452SGreg Roachuse RuntimeException; 355afbc57aSGreg Roachuse stdClass; 365afbc57aSGreg Roach 3790a2f718SGreg Roachuse function assert; 385afbc57aSGreg Roach 395afbc57aSGreg Roach/** 405afbc57aSGreg Roach * Tree management and queries. 415afbc57aSGreg Roach */ 425afbc57aSGreg Roachclass TreeService 435afbc57aSGreg Roach{ 445afbc57aSGreg Roach // The most likely surname tradition for a given language. 455afbc57aSGreg Roach private const DEFAULT_SURNAME_TRADITIONS = [ 465afbc57aSGreg Roach 'es' => 'spanish', 475afbc57aSGreg Roach 'is' => 'icelandic', 485afbc57aSGreg Roach 'lt' => 'lithuanian', 495afbc57aSGreg Roach 'pl' => 'polish', 505afbc57aSGreg Roach 'pt' => 'portuguese', 515afbc57aSGreg Roach 'pt-BR' => 'portuguese', 525afbc57aSGreg Roach ]; 535afbc57aSGreg Roach 545afbc57aSGreg Roach /** 555afbc57aSGreg Roach * All the trees that the current user has permission to access. 565afbc57aSGreg Roach * 57b5c8fd7eSGreg Roach * @return Collection<Tree> 585afbc57aSGreg Roach */ 595afbc57aSGreg Roach public function all(): Collection 605afbc57aSGreg Roach { 616b9cb339SGreg Roach return Registry::cache()->array()->remember('all-trees', static function (): Collection { 625afbc57aSGreg Roach // All trees 635afbc57aSGreg Roach $query = DB::table('gedcom') 645afbc57aSGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 655afbc57aSGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 665afbc57aSGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 675afbc57aSGreg Roach }) 685afbc57aSGreg Roach ->where('gedcom.gedcom_id', '>', 0) 695afbc57aSGreg Roach ->select([ 705afbc57aSGreg Roach 'gedcom.gedcom_id AS tree_id', 715afbc57aSGreg Roach 'gedcom.gedcom_name AS tree_name', 725afbc57aSGreg Roach 'gedcom_setting.setting_value AS tree_title', 735afbc57aSGreg Roach ]) 745afbc57aSGreg Roach ->orderBy('gedcom.sort_order') 755afbc57aSGreg Roach ->orderBy('gedcom_setting.setting_value'); 765afbc57aSGreg Roach 775afbc57aSGreg Roach // Non-admins may not see all trees 785afbc57aSGreg Roach if (!Auth::isAdmin()) { 795afbc57aSGreg Roach $query 805afbc57aSGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 815afbc57aSGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 825afbc57aSGreg Roach ->where('gs2.setting_name', '=', 'imported'); 835afbc57aSGreg Roach }) 845afbc57aSGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 855afbc57aSGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 865afbc57aSGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 875afbc57aSGreg Roach }) 885afbc57aSGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 895afbc57aSGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 905afbc57aSGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 911fe542e9SGreg Roach ->where('user_gedcom_setting.setting_name', '=', UserInterface::PREF_TREE_ROLE); 925afbc57aSGreg Roach }) 935afbc57aSGreg Roach ->where(static function (Builder $query): void { 945afbc57aSGreg Roach $query 955afbc57aSGreg Roach // Managers 961fe542e9SGreg Roach ->where('user_gedcom_setting.setting_value', '=', UserInterface::ROLE_MANAGER) 975afbc57aSGreg Roach // Members 985afbc57aSGreg Roach ->orWhere(static function (Builder $query): void { 995afbc57aSGreg Roach $query 1005afbc57aSGreg Roach ->where('gs2.setting_value', '=', '1') 1015afbc57aSGreg Roach ->where('gs3.setting_value', '=', '1') 1021fe542e9SGreg Roach ->where('user_gedcom_setting.setting_value', '<>', UserInterface::ROLE_VISITOR); 1035afbc57aSGreg Roach }) 1045afbc57aSGreg Roach // Public trees 1055afbc57aSGreg Roach ->orWhere(static function (Builder $query): void { 1065afbc57aSGreg Roach $query 1075afbc57aSGreg Roach ->where('gs2.setting_value', '=', '1') 1085afbc57aSGreg Roach ->where('gs3.setting_value', '<>', '1'); 1095afbc57aSGreg Roach }); 1105afbc57aSGreg Roach }); 1115afbc57aSGreg Roach } 1125afbc57aSGreg Roach 1135afbc57aSGreg Roach return $query 1145afbc57aSGreg Roach ->get() 1155afbc57aSGreg Roach ->mapWithKeys(static function (stdClass $row): array { 1161e653452SGreg Roach return [$row->tree_name => Tree::rowMapper()($row)]; 1175afbc57aSGreg Roach }); 1185afbc57aSGreg Roach }); 1195afbc57aSGreg Roach } 1205afbc57aSGreg Roach 1215afbc57aSGreg Roach /** 1221e653452SGreg Roach * Find a tree by its ID. 1235afbc57aSGreg Roach * 1241e653452SGreg Roach * @param int $id 1255afbc57aSGreg Roach * 1261e653452SGreg Roach * @return Tree 1275afbc57aSGreg Roach */ 1281e653452SGreg Roach public function find(int $id): Tree 1295afbc57aSGreg Roach { 1301e653452SGreg Roach $tree = $this->all()->first(static function (Tree $tree) use ($id): bool { 1311e653452SGreg Roach return $tree->id() === $id; 1325afbc57aSGreg Roach }); 1331e653452SGreg Roach 1341e653452SGreg Roach assert($tree instanceof Tree, new RuntimeException()); 1351e653452SGreg Roach 1361e653452SGreg Roach return $tree; 1371e653452SGreg Roach } 1381e653452SGreg Roach 1391e653452SGreg Roach /** 1401e653452SGreg Roach * All trees, name => title 1411e653452SGreg Roach * 1421e653452SGreg Roach * @return string[] 1431e653452SGreg Roach */ 1441e653452SGreg Roach public function titles(): array 1451e653452SGreg Roach { 1461e653452SGreg Roach return $this->all()->map(static function (Tree $tree): string { 1471e653452SGreg Roach return $tree->title(); 1481e653452SGreg Roach })->all(); 1495afbc57aSGreg Roach } 1505afbc57aSGreg Roach 1515afbc57aSGreg Roach /** 1525afbc57aSGreg Roach * @param string $name 1535afbc57aSGreg Roach * @param string $title 1545afbc57aSGreg Roach * 1555afbc57aSGreg Roach * @return Tree 1565afbc57aSGreg Roach */ 1575afbc57aSGreg Roach public function create(string $name, string $title): Tree 1585afbc57aSGreg Roach { 1595afbc57aSGreg Roach DB::table('gedcom')->insert([ 1605afbc57aSGreg Roach 'gedcom_name' => $name, 1615afbc57aSGreg Roach ]); 1625afbc57aSGreg Roach 1635afbc57aSGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 1645afbc57aSGreg Roach 1655afbc57aSGreg Roach $tree = new Tree($tree_id, $name, $title); 1665afbc57aSGreg Roach 1675afbc57aSGreg Roach $tree->setPreference('imported', '1'); 1685afbc57aSGreg Roach $tree->setPreference('title', $title); 1695afbc57aSGreg Roach 1705afbc57aSGreg Roach // Set preferences from default tree 1715afbc57aSGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 1725afbc57aSGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 1735afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1745afbc57aSGreg Roach $query 1755afbc57aSGreg Roach ->select([new Expression($tree_id), 'setting_name', 'setting_value']) 1765afbc57aSGreg Roach ->from('gedcom_setting') 1775afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 1785afbc57aSGreg Roach } 1795afbc57aSGreg Roach ); 1805afbc57aSGreg Roach 1815afbc57aSGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 1825afbc57aSGreg Roach ['gedcom_id', 'tag_type', 'resn'], 1835afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1845afbc57aSGreg Roach $query 1855afbc57aSGreg Roach ->select([new Expression($tree_id), 'tag_type', 'resn']) 1865afbc57aSGreg Roach ->from('default_resn') 1875afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 1885afbc57aSGreg Roach } 1895afbc57aSGreg Roach ); 1905afbc57aSGreg Roach 1915afbc57aSGreg Roach // Gedcom and privacy settings 1925afbc57aSGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 1935afbc57aSGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 19465cf5706SGreg Roach $tree->setPreference('LANGUAGE', I18N::languageTag()); // Default to the current admin’s language 19565cf5706SGreg Roach $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[I18N::languageTag()] ?? 'paternal'); 1965afbc57aSGreg Roach 1975afbc57aSGreg Roach // A tree needs at least one record. 1989b5c9597SGreg 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"; 1999b5c9597SGreg Roach FunctionsImport::importRecord($head, $tree, true); 2005afbc57aSGreg Roach 2015afbc57aSGreg Roach // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. 2025afbc57aSGreg Roach $name = I18N::translate('John /DOE/'); 2035afbc57aSGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 2049b5c9597SGreg Roach $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note; 2059b5c9597SGreg Roach FunctionsImport::importRecord($indi, $tree, true); 2065afbc57aSGreg Roach 2075afbc57aSGreg Roach return $tree; 2085afbc57aSGreg Roach } 2095afbc57aSGreg Roach 2105afbc57aSGreg Roach /** 2115afbc57aSGreg Roach * @param Tree $tree 2125afbc57aSGreg Roach */ 2135afbc57aSGreg Roach public function delete(Tree $tree): void 2145afbc57aSGreg Roach { 2155afbc57aSGreg Roach // If this is the default tree, then unset it 2165afbc57aSGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) { 2175afbc57aSGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 2185afbc57aSGreg Roach } 2195afbc57aSGreg Roach 2205afbc57aSGreg Roach $tree->deleteGenealogyData(false); 2215afbc57aSGreg Roach 2225afbc57aSGreg Roach DB::table('block_setting') 2235afbc57aSGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 2245afbc57aSGreg Roach ->where('gedcom_id', '=', $tree->id()) 2255afbc57aSGreg Roach ->delete(); 2265afbc57aSGreg Roach DB::table('block')->where('gedcom_id', '=', $tree->id())->delete(); 2275afbc57aSGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2285afbc57aSGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2295afbc57aSGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete(); 2305afbc57aSGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete(); 2315afbc57aSGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete(); 2325afbc57aSGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete(); 2335afbc57aSGreg Roach DB::table('log')->where('gedcom_id', '=', $tree->id())->delete(); 2345afbc57aSGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete(); 2355afbc57aSGreg Roach } 2365afbc57aSGreg Roach 2375afbc57aSGreg Roach /** 2385afbc57aSGreg Roach * Generate a unique name for a new tree. 2395afbc57aSGreg Roach * 2405afbc57aSGreg Roach * @return string 2415afbc57aSGreg Roach */ 2425afbc57aSGreg Roach public function uniqueTreeName(): string 2435afbc57aSGreg Roach { 2445afbc57aSGreg Roach $name = 'tree'; 2455afbc57aSGreg Roach $number = 1; 2465afbc57aSGreg Roach 2471e653452SGreg Roach while ($this->all()->get($name . $number) instanceof Tree) { 2485afbc57aSGreg Roach $number++; 2495afbc57aSGreg Roach } 2505afbc57aSGreg Roach 2515afbc57aSGreg Roach return $name . $number; 2525afbc57aSGreg Roach } 2535afbc57aSGreg Roach} 254