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