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 */ 17*fcfa147eSGreg Roach 185afbc57aSGreg Roachdeclare(strict_types=1); 195afbc57aSGreg Roach 205afbc57aSGreg Roachnamespace Fisharebest\Webtrees\Services; 215afbc57aSGreg Roach 225afbc57aSGreg Roachuse Fisharebest\Webtrees\Auth; 235afbc57aSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsImport; 245afbc57aSGreg Roachuse Fisharebest\Webtrees\I18N; 255afbc57aSGreg Roachuse Fisharebest\Webtrees\Site; 265afbc57aSGreg Roachuse Fisharebest\Webtrees\Tree; 275afbc57aSGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 285afbc57aSGreg Roachuse Illuminate\Database\Query\Builder; 295afbc57aSGreg Roachuse Illuminate\Database\Query\Expression; 305afbc57aSGreg Roachuse Illuminate\Database\Query\JoinClause; 315afbc57aSGreg Roachuse Illuminate\Support\Collection; 325afbc57aSGreg Roachuse stdClass; 335afbc57aSGreg Roach 345afbc57aSGreg Roachuse function app; 355afbc57aSGreg Roach 365afbc57aSGreg Roach/** 375afbc57aSGreg Roach * Tree management and queries. 385afbc57aSGreg Roach */ 395afbc57aSGreg Roachclass TreeService 405afbc57aSGreg Roach{ 415afbc57aSGreg Roach // The most likely surname tradition for a given language. 425afbc57aSGreg Roach private const DEFAULT_SURNAME_TRADITIONS = [ 435afbc57aSGreg Roach 'es' => 'spanish', 445afbc57aSGreg Roach 'is' => 'icelandic', 455afbc57aSGreg Roach 'lt' => 'lithuanian', 465afbc57aSGreg Roach 'pl' => 'polish', 475afbc57aSGreg Roach 'pt' => 'portuguese', 485afbc57aSGreg Roach 'pt-BR' => 'portuguese', 495afbc57aSGreg Roach ]; 505afbc57aSGreg Roach 515afbc57aSGreg Roach /** 525afbc57aSGreg Roach * All the trees that the current user has permission to access. 535afbc57aSGreg Roach * 545afbc57aSGreg Roach * @return Collection 555afbc57aSGreg Roach */ 565afbc57aSGreg Roach public function all(): Collection 575afbc57aSGreg Roach { 585afbc57aSGreg Roach return app('cache.array')->rememberForever(__CLASS__ . __METHOD__, static function (): Collection { 595afbc57aSGreg Roach // All trees 605afbc57aSGreg Roach $query = DB::table('gedcom') 615afbc57aSGreg Roach ->leftJoin('gedcom_setting', static function (JoinClause $join): void { 625afbc57aSGreg Roach $join->on('gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 635afbc57aSGreg Roach ->where('gedcom_setting.setting_name', '=', 'title'); 645afbc57aSGreg Roach }) 655afbc57aSGreg Roach ->where('gedcom.gedcom_id', '>', 0) 665afbc57aSGreg Roach ->select([ 675afbc57aSGreg Roach 'gedcom.gedcom_id AS tree_id', 685afbc57aSGreg Roach 'gedcom.gedcom_name AS tree_name', 695afbc57aSGreg Roach 'gedcom_setting.setting_value AS tree_title', 705afbc57aSGreg Roach ]) 715afbc57aSGreg Roach ->orderBy('gedcom.sort_order') 725afbc57aSGreg Roach ->orderBy('gedcom_setting.setting_value'); 735afbc57aSGreg Roach 745afbc57aSGreg Roach // Non-admins may not see all trees 755afbc57aSGreg Roach if (!Auth::isAdmin()) { 765afbc57aSGreg Roach $query 775afbc57aSGreg Roach ->join('gedcom_setting AS gs2', static function (JoinClause $join): void { 785afbc57aSGreg Roach $join->on('gs2.gedcom_id', '=', 'gedcom.gedcom_id') 795afbc57aSGreg Roach ->where('gs2.setting_name', '=', 'imported'); 805afbc57aSGreg Roach }) 815afbc57aSGreg Roach ->join('gedcom_setting AS gs3', static function (JoinClause $join): void { 825afbc57aSGreg Roach $join->on('gs3.gedcom_id', '=', 'gedcom.gedcom_id') 835afbc57aSGreg Roach ->where('gs3.setting_name', '=', 'REQUIRE_AUTHENTICATION'); 845afbc57aSGreg Roach }) 855afbc57aSGreg Roach ->leftJoin('user_gedcom_setting', static function (JoinClause $join): void { 865afbc57aSGreg Roach $join->on('user_gedcom_setting.gedcom_id', '=', 'gedcom.gedcom_id') 875afbc57aSGreg Roach ->where('user_gedcom_setting.user_id', '=', Auth::id()) 885afbc57aSGreg Roach ->where('user_gedcom_setting.setting_name', '=', 'canedit'); 895afbc57aSGreg Roach }) 905afbc57aSGreg Roach ->where(static function (Builder $query): void { 915afbc57aSGreg Roach $query 925afbc57aSGreg Roach // Managers 935afbc57aSGreg Roach ->where('user_gedcom_setting.setting_value', '=', 'admin') 945afbc57aSGreg Roach // Members 955afbc57aSGreg Roach ->orWhere(static function (Builder $query): void { 965afbc57aSGreg Roach $query 975afbc57aSGreg Roach ->where('gs2.setting_value', '=', '1') 985afbc57aSGreg Roach ->where('gs3.setting_value', '=', '1') 995afbc57aSGreg Roach ->where('user_gedcom_setting.setting_value', '<>', 'none'); 1005afbc57aSGreg Roach }) 1015afbc57aSGreg Roach // Public trees 1025afbc57aSGreg Roach ->orWhere(static function (Builder $query): void { 1035afbc57aSGreg Roach $query 1045afbc57aSGreg Roach ->where('gs2.setting_value', '=', '1') 1055afbc57aSGreg Roach ->where('gs3.setting_value', '<>', '1'); 1065afbc57aSGreg Roach }); 1075afbc57aSGreg Roach }); 1085afbc57aSGreg Roach } 1095afbc57aSGreg Roach 1105afbc57aSGreg Roach return $query 1115afbc57aSGreg Roach ->get() 1125afbc57aSGreg Roach ->mapWithKeys(static function (stdClass $row): array { 1135afbc57aSGreg Roach return [$row->tree_id => Tree::rowMapper()($row)]; 1145afbc57aSGreg Roach }); 1155afbc57aSGreg Roach }); 1165afbc57aSGreg Roach } 1175afbc57aSGreg Roach 1185afbc57aSGreg Roach /** 1195afbc57aSGreg Roach * Find the tree with a specific name. 1205afbc57aSGreg Roach * 1215afbc57aSGreg Roach * @param string $name 1225afbc57aSGreg Roach * 1235afbc57aSGreg Roach * @return Tree|null 1245afbc57aSGreg Roach */ 1255afbc57aSGreg Roach public function findByName($name): ?Tree 1265afbc57aSGreg Roach { 1275afbc57aSGreg Roach return $this->all()->first(static function (Tree $tree) use ($name): bool { 1285afbc57aSGreg Roach return $tree->name() === $name; 1295afbc57aSGreg Roach }); 1305afbc57aSGreg Roach } 1315afbc57aSGreg Roach 1325afbc57aSGreg Roach /** 1335afbc57aSGreg Roach * @param string $name 1345afbc57aSGreg Roach * @param string $title 1355afbc57aSGreg Roach * 1365afbc57aSGreg Roach * @return Tree 1375afbc57aSGreg Roach */ 1385afbc57aSGreg Roach public function create(string $name, string $title): Tree 1395afbc57aSGreg Roach { 1405afbc57aSGreg Roach DB::table('gedcom')->insert([ 1415afbc57aSGreg Roach 'gedcom_name' => $name, 1425afbc57aSGreg Roach ]); 1435afbc57aSGreg Roach 1445afbc57aSGreg Roach $tree_id = (int) DB::connection()->getPdo()->lastInsertId(); 1455afbc57aSGreg Roach 1465afbc57aSGreg Roach $tree = new Tree($tree_id, $name, $title); 1475afbc57aSGreg Roach 1485afbc57aSGreg Roach $tree->setPreference('imported', '1'); 1495afbc57aSGreg Roach $tree->setPreference('title', $title); 1505afbc57aSGreg Roach 1515afbc57aSGreg Roach // Set preferences from default tree 1525afbc57aSGreg Roach (new Builder(DB::connection()))->from('gedcom_setting')->insertUsing( 1535afbc57aSGreg Roach ['gedcom_id', 'setting_name', 'setting_value'], 1545afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1555afbc57aSGreg Roach $query 1565afbc57aSGreg Roach ->select([new Expression($tree_id), 'setting_name', 'setting_value']) 1575afbc57aSGreg Roach ->from('gedcom_setting') 1585afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 1595afbc57aSGreg Roach } 1605afbc57aSGreg Roach ); 1615afbc57aSGreg Roach 1625afbc57aSGreg Roach (new Builder(DB::connection()))->from('default_resn')->insertUsing( 1635afbc57aSGreg Roach ['gedcom_id', 'tag_type', 'resn'], 1645afbc57aSGreg Roach static function (Builder $query) use ($tree_id): void { 1655afbc57aSGreg Roach $query 1665afbc57aSGreg Roach ->select([new Expression($tree_id), 'tag_type', 'resn']) 1675afbc57aSGreg Roach ->from('default_resn') 1685afbc57aSGreg Roach ->where('gedcom_id', '=', -1); 1695afbc57aSGreg Roach } 1705afbc57aSGreg Roach ); 1715afbc57aSGreg Roach 1725afbc57aSGreg Roach // Gedcom and privacy settings 1735afbc57aSGreg Roach $tree->setPreference('CONTACT_USER_ID', (string) Auth::id()); 1745afbc57aSGreg Roach $tree->setPreference('WEBMASTER_USER_ID', (string) Auth::id()); 1755afbc57aSGreg Roach $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language 1765afbc57aSGreg Roach $tree->setPreference('SURNAME_TRADITION', self::DEFAULT_SURNAME_TRADITIONS[WT_LOCALE] ?? 'paternal'); 1775afbc57aSGreg Roach 1785afbc57aSGreg Roach // A tree needs at least one record. 1799b5c9597SGreg 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"; 1809b5c9597SGreg Roach FunctionsImport::importRecord($head, $tree, true); 1815afbc57aSGreg Roach 1825afbc57aSGreg Roach // I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. 1835afbc57aSGreg Roach $name = I18N::translate('John /DOE/'); 1845afbc57aSGreg Roach $note = I18N::translate('Edit this individual and replace their details with your own.'); 1859b5c9597SGreg Roach $indi = "0 @X1@ INDI\n1 NAME " . $name . "\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE " . $note; 1869b5c9597SGreg Roach FunctionsImport::importRecord($indi, $tree, true); 1875afbc57aSGreg Roach 1885afbc57aSGreg Roach return $tree; 1895afbc57aSGreg Roach } 1905afbc57aSGreg Roach 1915afbc57aSGreg Roach /** 1925afbc57aSGreg Roach * @param Tree $tree 1935afbc57aSGreg Roach */ 1945afbc57aSGreg Roach public function delete(Tree $tree): void 1955afbc57aSGreg Roach { 1965afbc57aSGreg Roach // If this is the default tree, then unset it 1975afbc57aSGreg Roach if (Site::getPreference('DEFAULT_GEDCOM') === $tree->name()) { 1985afbc57aSGreg Roach Site::setPreference('DEFAULT_GEDCOM', ''); 1995afbc57aSGreg Roach } 2005afbc57aSGreg Roach 2015afbc57aSGreg Roach $tree->deleteGenealogyData(false); 2025afbc57aSGreg Roach 2035afbc57aSGreg Roach DB::table('block_setting') 2045afbc57aSGreg Roach ->join('block', 'block.block_id', '=', 'block_setting.block_id') 2055afbc57aSGreg Roach ->where('gedcom_id', '=', $tree->id()) 2065afbc57aSGreg Roach ->delete(); 2075afbc57aSGreg Roach DB::table('block')->where('gedcom_id', '=', $tree->id())->delete(); 2085afbc57aSGreg Roach DB::table('user_gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2095afbc57aSGreg Roach DB::table('gedcom_setting')->where('gedcom_id', '=', $tree->id())->delete(); 2105afbc57aSGreg Roach DB::table('module_privacy')->where('gedcom_id', '=', $tree->id())->delete(); 2115afbc57aSGreg Roach DB::table('hit_counter')->where('gedcom_id', '=', $tree->id())->delete(); 2125afbc57aSGreg Roach DB::table('default_resn')->where('gedcom_id', '=', $tree->id())->delete(); 2135afbc57aSGreg Roach DB::table('gedcom_chunk')->where('gedcom_id', '=', $tree->id())->delete(); 2145afbc57aSGreg Roach DB::table('log')->where('gedcom_id', '=', $tree->id())->delete(); 2155afbc57aSGreg Roach DB::table('gedcom')->where('gedcom_id', '=', $tree->id())->delete(); 2165afbc57aSGreg Roach } 2175afbc57aSGreg Roach 2185afbc57aSGreg Roach /** 2195afbc57aSGreg Roach * Generate a unique name for a new tree. 2205afbc57aSGreg Roach * 2215afbc57aSGreg Roach * @return string 2225afbc57aSGreg Roach */ 2235afbc57aSGreg Roach public function uniqueTreeName(): string 2245afbc57aSGreg Roach { 2255afbc57aSGreg Roach $name = 'tree'; 2265afbc57aSGreg Roach $number = 1; 2275afbc57aSGreg Roach 2285afbc57aSGreg Roach while ($this->findByName($name . $number) instanceof Tree) { 2295afbc57aSGreg Roach $number++; 2305afbc57aSGreg Roach } 2315afbc57aSGreg Roach 2325afbc57aSGreg Roach return $name . $number; 2335afbc57aSGreg Roach } 2345afbc57aSGreg Roach} 235