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