Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Tree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tree, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 25 | class Tree { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 26 | /** @var int The tree's ID number */  | 
            ||
| 27 | private $tree_id;  | 
            ||
| 28 | |||
| 29 | /** @var string The tree's name */  | 
            ||
| 30 | private $name;  | 
            ||
| 31 | |||
| 32 | /** @var string The tree's title */  | 
            ||
| 33 | private $title;  | 
            ||
| 34 | |||
| 35 | /** @var int[] Default access rules for facts in this tree */  | 
            ||
| 36 | private $fact_privacy;  | 
            ||
| 37 | |||
| 38 | /** @var int[] Default access rules for individuals in this tree */  | 
            ||
| 39 | private $individual_privacy;  | 
            ||
| 40 | |||
| 41 | /** @var integer[][] Default access rules for individual facts in this tree */  | 
            ||
| 42 | private $individual_fact_privacy;  | 
            ||
| 43 | |||
| 44 | /** @var Tree[] All trees that we have permission to see. */  | 
            ||
| 45 | private static $trees;  | 
            ||
| 46 | |||
| 47 | /** @var string[] Cached copy of the wt_gedcom_setting table. */  | 
            ||
| 48 | private $preferences = [];  | 
            ||
| 49 | |||
| 50 | /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */  | 
            ||
| 51 | private $user_preferences = [];  | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * Create a tree object. This is a private constructor - it can only  | 
            ||
| 55 | * be called from Tree::getAll() to ensure proper initialisation.  | 
            ||
| 56 | *  | 
            ||
| 57 | * @param int $tree_id  | 
            ||
| 58 | * @param string $tree_name  | 
            ||
| 59 | * @param string $tree_title  | 
            ||
| 60 | */  | 
            ||
| 61 | 	private function __construct($tree_id, $tree_name, $tree_title) { | 
            ||
| 62 | $this->tree_id = $tree_id;  | 
            ||
| 63 | $this->name = $tree_name;  | 
            ||
| 64 | $this->title = $tree_title;  | 
            ||
| 65 | $this->fact_privacy = [];  | 
            ||
| 66 | $this->individual_privacy = [];  | 
            ||
| 67 | $this->individual_fact_privacy = [];  | 
            ||
| 68 | |||
| 69 | // Load the privacy settings for this tree  | 
            ||
| 70 | $rows = Database::prepare(  | 
            ||
| 71 | "SELECT SQL_CACHE xref, tag_type, CASE resn WHEN 'none' THEN :priv_public WHEN 'privacy' THEN :priv_user WHEN 'confidential' THEN :priv_none WHEN 'hidden' THEN :priv_hide END AS resn" .  | 
            ||
| 72 | " FROM `##default_resn` WHERE gedcom_id = :tree_id"  | 
            ||
| 73 | )->execute([  | 
            ||
| 74 | 'priv_public' => Auth::PRIV_PRIVATE,  | 
            ||
| 75 | 'priv_user' => Auth::PRIV_USER,  | 
            ||
| 76 | 'priv_none' => Auth::PRIV_NONE,  | 
            ||
| 77 | 'priv_hide' => Auth::PRIV_HIDE,  | 
            ||
| 78 | 'tree_id' => $this->tree_id,  | 
            ||
| 79 | ])->fetchAll();  | 
            ||
| 80 | |||
| 81 | 		foreach ($rows as $row) { | 
            ||
| 82 | 			if ($row->xref !== null) { | 
            ||
| 83 | 				if ($row->tag_type !== null) { | 
            ||
| 84 | $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn;  | 
            ||
| 85 | 				} else { | 
            ||
| 86 | $this->individual_privacy[$row->xref] = (int) $row->resn;  | 
            ||
| 87 | }  | 
            ||
| 88 | 			} else { | 
            ||
| 89 | $this->fact_privacy[$row->tag_type] = (int) $row->resn;  | 
            ||
| 90 | }  | 
            ||
| 91 | }  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * The ID of this tree  | 
            ||
| 96 | *  | 
            ||
| 97 | * @return int  | 
            ||
| 98 | */  | 
            ||
| 99 | 	public function getTreeId() { | 
            ||
| 100 | return $this->tree_id;  | 
            ||
| 101 | }  | 
            ||
| 102 | |||
| 103 | /**  | 
            ||
| 104 | * The name of this tree  | 
            ||
| 105 | *  | 
            ||
| 106 | * @return string  | 
            ||
| 107 | */  | 
            ||
| 108 | 	public function getName() { | 
            ||
| 109 | return $this->name;  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * The name of this tree  | 
            ||
| 114 | *  | 
            ||
| 115 | * @return string  | 
            ||
| 116 | */  | 
            ||
| 117 | 	public function getNameHtml() { | 
            ||
| 118 | return Html::escape($this->name);  | 
            ||
| 119 | }  | 
            ||
| 120 | |||
| 121 | /**  | 
            ||
| 122 | * The name of this tree  | 
            ||
| 123 | *  | 
            ||
| 124 | * @return string  | 
            ||
| 125 | */  | 
            ||
| 126 | 	public function getNameUrl() { | 
            ||
| 127 | return rawurlencode($this->name);  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * The title of this tree  | 
            ||
| 132 | *  | 
            ||
| 133 | * @return string  | 
            ||
| 134 | */  | 
            ||
| 135 | 	public function getTitle() { | 
            ||
| 136 | return $this->title;  | 
            ||
| 137 | }  | 
            ||
| 138 | |||
| 139 | /**  | 
            ||
| 140 | * The title of this tree, with HTML markup  | 
            ||
| 141 | *  | 
            ||
| 142 | * @return string  | 
            ||
| 143 | */  | 
            ||
| 144 | 	public function getTitleHtml() { | 
            ||
| 145 | return '<span dir="auto">' . Html::escape($this->title) . '</span>';  | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * The fact-level privacy for this tree.  | 
            ||
| 150 | *  | 
            ||
| 151 | * @return int[]  | 
            ||
| 152 | */  | 
            ||
| 153 | 	public function getFactPrivacy() { | 
            ||
| 154 | return $this->fact_privacy;  | 
            ||
| 155 | }  | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * The individual-level privacy for this tree.  | 
            ||
| 159 | *  | 
            ||
| 160 | * @return int[]  | 
            ||
| 161 | */  | 
            ||
| 162 | 	public function getIndividualPrivacy() { | 
            ||
| 163 | return $this->individual_privacy;  | 
            ||
| 164 | }  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * The individual-fact-level privacy for this tree.  | 
            ||
| 168 | *  | 
            ||
| 169 | * @return integer[][]  | 
            ||
| 170 | */  | 
            ||
| 171 | 	public function getIndividualFactPrivacy() { | 
            ||
| 172 | return $this->individual_fact_privacy;  | 
            ||
| 173 | }  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Get the tree’s configuration settings.  | 
            ||
| 177 | *  | 
            ||
| 178 | * @param string $setting_name  | 
            ||
| 179 | * @param string $default  | 
            ||
| 180 | *  | 
            ||
| 181 | * @return string  | 
            ||
| 182 | */  | 
            ||
| 183 | 	public function getPreference($setting_name, $default = '') { | 
            ||
| 184 | 		if (empty($this->preferences)) { | 
            ||
| 185 | $this->preferences = Database::prepare(  | 
            ||
| 186 | "SELECT SQL_CACHE setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?"  | 
            ||
| 187 | )->execute([$this->tree_id])->fetchAssoc();  | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | 		if (array_key_exists($setting_name, $this->preferences)) { | 
            ||
| 191 | return $this->preferences[$setting_name];  | 
            ||
| 192 | 		} else { | 
            ||
| 193 | return $default;  | 
            ||
| 194 | }  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Set the tree’s configuration settings.  | 
            ||
| 199 | *  | 
            ||
| 200 | * @param string $setting_name  | 
            ||
| 201 | * @param string $setting_value  | 
            ||
| 202 | *  | 
            ||
| 203 | * @return $this  | 
            ||
| 204 | */  | 
            ||
| 205 | 	public function setPreference($setting_name, $setting_value) { | 
            ||
| 206 | 		if ($setting_value !== $this->getPreference($setting_name)) { | 
            ||
| 207 | Database::prepare(  | 
            ||
| 208 | "REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .  | 
            ||
| 209 | " VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))"  | 
            ||
| 210 | )->execute([  | 
            ||
| 211 | 'tree_id' => $this->tree_id,  | 
            ||
| 212 | 'setting_name' => $setting_name,  | 
            ||
| 213 | 'setting_value' => $setting_value,  | 
            ||
| 214 | ]);  | 
            ||
| 215 | |||
| 216 | $this->preferences[$setting_name] = $setting_value;  | 
            ||
| 217 | |||
| 218 | 			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); | 
            ||
| 219 | }  | 
            ||
| 220 | |||
| 221 | return $this;  | 
            ||
| 222 | }  | 
            ||
| 223 | |||
| 224 | /**  | 
            ||
| 225 | * Get the tree’s user-configuration settings.  | 
            ||
| 226 | *  | 
            ||
| 227 | * @param User $user  | 
            ||
| 228 | * @param string $setting_name  | 
            ||
| 229 | * @param string|null $default  | 
            ||
| 230 | *  | 
            ||
| 231 | * @return string  | 
            ||
| 232 | */  | 
            ||
| 233 | 	public function getUserPreference(User $user, $setting_name, $default = null) { | 
            ||
| 234 | // There are lots of settings, and we need to fetch lots of them on every page  | 
            ||
| 235 | // so it is quicker to fetch them all in one go.  | 
            ||
| 236 | 		if (!array_key_exists($user->getUserId(), $this->user_preferences)) { | 
            ||
| 237 | $this->user_preferences[$user->getUserId()] = Database::prepare(  | 
            ||
| 238 | "SELECT SQL_CACHE setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?"  | 
            ||
| 239 | )->execute([$user->getUserId(), $this->tree_id])->fetchAssoc();  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | 		if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) { | 
            ||
| 243 | return $this->user_preferences[$user->getUserId()][$setting_name];  | 
            ||
| 244 | 		} else { | 
            ||
| 245 | return $default;  | 
            ||
| 246 | }  | 
            ||
| 247 | }  | 
            ||
| 248 | |||
| 249 | /**  | 
            ||
| 250 | * Set the tree’s user-configuration settings.  | 
            ||
| 251 | *  | 
            ||
| 252 | * @param User $user  | 
            ||
| 253 | * @param string $setting_name  | 
            ||
| 254 | * @param string $setting_value  | 
            ||
| 255 | *  | 
            ||
| 256 | * @return $this  | 
            ||
| 257 | */  | 
            ||
| 258 | 	public function setUserPreference(User $user, $setting_name, $setting_value) { | 
            ||
| 259 | 		if ($this->getUserPreference($user, $setting_name) !== $setting_value) { | 
            ||
| 260 | // Update the database  | 
            ||
| 261 | 			if ($setting_value === null) { | 
            ||
| 262 | Database::prepare(  | 
            ||
| 263 | "DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name"  | 
            ||
| 264 | )->execute([  | 
            ||
| 265 | 'tree_id' => $this->tree_id,  | 
            ||
| 266 | 'user_id' => $user->getUserId(),  | 
            ||
| 267 | 'setting_name' => $setting_name,  | 
            ||
| 268 | ]);  | 
            ||
| 269 | 			} else { | 
            ||
| 270 | Database::prepare(  | 
            ||
| 271 | "REPLACE INTO `##user_gedcom_setting` (user_id, gedcom_id, setting_name, setting_value) VALUES (:user_id, :tree_id, :setting_name, LEFT(:setting_value, 255))"  | 
            ||
| 272 | )->execute([  | 
            ||
| 273 | 'user_id' => $user->getUserId(),  | 
            ||
| 274 | 'tree_id' => $this->tree_id,  | 
            ||
| 275 | 'setting_name' => $setting_name,  | 
            ||
| 276 | 'setting_value' => $setting_value,  | 
            ||
| 277 | ]);  | 
            ||
| 278 | }  | 
            ||
| 279 | // Update our cache  | 
            ||
| 280 | $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value;  | 
            ||
| 281 | // Audit log of changes  | 
            ||
| 282 | 			Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); | 
            ||
| 283 | }  | 
            ||
| 284 | |||
| 285 | return $this;  | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * Can a user accept changes for this tree?  | 
            ||
| 290 | *  | 
            ||
| 291 | * @param User $user  | 
            ||
| 292 | *  | 
            ||
| 293 | * @return bool  | 
            ||
| 294 | */  | 
            ||
| 295 | 	public function canAcceptChanges(User $user) { | 
            ||
| 296 | return Auth::isModerator($this, $user);  | 
            ||
| 297 | }  | 
            ||
| 298 | |||
| 299 | /**  | 
            ||
| 300 | * Fetch all the trees that we have permission to access.  | 
            ||
| 301 | *  | 
            ||
| 302 | * @return Tree[]  | 
            ||
| 303 | */  | 
            ||
| 304 | 	public static function getAll() { | 
            ||
| 305 | 		if (self::$trees === null) { | 
            ||
| 306 | self::$trees = [];  | 
            ||
| 307 | $rows = Database::prepare(  | 
            ||
| 308 | "SELECT SQL_CACHE g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" .  | 
            ||
| 309 | " FROM `##gedcom` g" .  | 
            ||
| 310 | " LEFT JOIN `##gedcom_setting` gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" .  | 
            ||
| 311 | " LEFT JOIN `##gedcom_setting` gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" .  | 
            ||
| 312 | " LEFT JOIN `##gedcom_setting` gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" .  | 
            ||
| 313 | " LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" .  | 
            ||
| 314 | " WHERE " .  | 
            ||
| 315 | 				"  g.gedcom_id>0 AND (" . // exclude the "template" tree | 
            ||
| 316 | " EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all  | 
            ||
| 317 | 				"   ) OR (" . | 
            ||
| 318 | 				"    (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either: | 
            ||
| 319 | " gs3.setting_value <> 1 OR" . // visitor access  | 
            ||
| 320 | " IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access  | 
            ||
| 321 | " )" .  | 
            ||
| 322 | " )" .  | 
            ||
| 323 | " ORDER BY g.sort_order, 3"  | 
            ||
| 324 | )->execute([Auth::id(), Auth::id()])->fetchAll();  | 
            ||
| 325 | 			foreach ($rows as $row) { | 
            ||
| 326 | self::$trees[] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title);  | 
            ||
| 327 | }  | 
            ||
| 328 | }  | 
            ||
| 329 | |||
| 330 | return self::$trees;  | 
            ||
| 331 | }  | 
            ||
| 332 | |||
| 333 | /**  | 
            ||
| 334 | * Find the tree with a specific ID.  | 
            ||
| 335 | *  | 
            ||
| 336 | * @param int $tree_id  | 
            ||
| 337 | *  | 
            ||
| 338 | * @throws \DomainException  | 
            ||
| 339 | *  | 
            ||
| 340 | * @return Tree  | 
            ||
| 341 | */  | 
            ||
| 342 | 	public static function findById($tree_id) { | 
            ||
| 343 | 		foreach (self::getAll() as $tree) { | 
            ||
| 344 | 			if ($tree->tree_id == $tree_id) { | 
            ||
| 345 | return $tree;  | 
            ||
| 346 | }  | 
            ||
| 347 | }  | 
            ||
| 348 | throw new \DomainException;  | 
            ||
| 349 | }  | 
            ||
| 350 | |||
| 351 | /**  | 
            ||
| 352 | * Find the tree with a specific name.  | 
            ||
| 353 | *  | 
            ||
| 354 | * @param string $tree_name  | 
            ||
| 355 | *  | 
            ||
| 356 | * @return Tree|null  | 
            ||
| 357 | */  | 
            ||
| 358 | 	public static function findByName($tree_name) { | 
            ||
| 359 | 		foreach (self::getAll() as $tree) { | 
            ||
| 360 | 			if ($tree->name === $tree_name) { | 
            ||
| 361 | return $tree;  | 
            ||
| 362 | }  | 
            ||
| 363 | }  | 
            ||
| 364 | |||
| 365 | return null;  | 
            ||
| 366 | }  | 
            ||
| 367 | |||
| 368 | /**  | 
            ||
| 369 | * Create arguments to select_edit_control()  | 
            ||
| 370 | * Note - these will be escaped later  | 
            ||
| 371 | *  | 
            ||
| 372 | * @return string[]  | 
            ||
| 373 | */  | 
            ||
| 374 | View Code Duplication | 	public static function getIdList() { | 
            |
| 375 | $list = [];  | 
            ||
| 376 | 		foreach (self::getAll() as $tree) { | 
            ||
| 377 | $list[$tree->tree_id] = $tree->title;  | 
            ||
| 378 | }  | 
            ||
| 379 | |||
| 380 | return $list;  | 
            ||
| 381 | }  | 
            ||
| 382 | |||
| 383 | /**  | 
            ||
| 384 | * Create arguments to select_edit_control()  | 
            ||
| 385 | * Note - these will be escaped later  | 
            ||
| 386 | *  | 
            ||
| 387 | * @return string[]  | 
            ||
| 388 | */  | 
            ||
| 389 | View Code Duplication | 	public static function getNameList() { | 
            |
| 390 | $list = [];  | 
            ||
| 391 | 		foreach (self::getAll() as $tree) { | 
            ||
| 392 | $list[$tree->name] = $tree->title;  | 
            ||
| 393 | }  | 
            ||
| 394 | |||
| 395 | return $list;  | 
            ||
| 396 | }  | 
            ||
| 397 | |||
| 398 | /**  | 
            ||
| 399 | * Create a new tree  | 
            ||
| 400 | *  | 
            ||
| 401 | * @param string $tree_name  | 
            ||
| 402 | * @param string $tree_title  | 
            ||
| 403 | *  | 
            ||
| 404 | * @return Tree  | 
            ||
| 405 | */  | 
            ||
| 406 | 	public static function create($tree_name, $tree_title) { | 
            ||
| 407 | 		try { | 
            ||
| 408 | // Create a new tree  | 
            ||
| 409 | Database::prepare(  | 
            ||
| 410 | "INSERT INTO `##gedcom` (gedcom_name) VALUES (?)"  | 
            ||
| 411 | )->execute([$tree_name]);  | 
            ||
| 412 | 			$tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); | 
            ||
| 413 | 		} catch (PDOException $ex) { | 
            ||
| 414 | DebugBar::addThrowable($ex);  | 
            ||
| 415 | |||
| 416 | // A tree with that name already exists?  | 
            ||
| 417 | return self::findByName($tree_name);  | 
            ||
| 418 | }  | 
            ||
| 419 | |||
| 420 | // Update the list of trees - to include this new one  | 
            ||
| 421 | self::$trees = null;  | 
            ||
| 422 | $tree = self::findById($tree_id);  | 
            ||
| 423 | |||
| 424 | 		$tree->setPreference('imported', '0'); | 
            ||
| 425 | 		$tree->setPreference('title', $tree_title); | 
            ||
| 426 | |||
| 427 | // Module privacy  | 
            ||
| 428 | Module::setDefaultAccess($tree_id);  | 
            ||
| 429 | |||
| 430 | // Set preferences from default tree  | 
            ||
| 431 | Database::prepare(  | 
            ||
| 432 | "INSERT INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .  | 
            ||
| 433 | " SELECT :tree_id, setting_name, setting_value" .  | 
            ||
| 434 | " FROM `##gedcom_setting` WHERE gedcom_id = -1"  | 
            ||
| 435 | )->execute([  | 
            ||
| 436 | 'tree_id' => $tree_id,  | 
            ||
| 437 | ]);  | 
            ||
| 438 | |||
| 439 | Database::prepare(  | 
            ||
| 440 | "INSERT INTO `##default_resn` (gedcom_id, tag_type, resn)" .  | 
            ||
| 441 | " SELECT :tree_id, tag_type, resn" .  | 
            ||
| 442 | " FROM `##default_resn` WHERE gedcom_id = -1"  | 
            ||
| 443 | )->execute([  | 
            ||
| 444 | 'tree_id' => $tree_id,  | 
            ||
| 445 | ]);  | 
            ||
| 446 | |||
| 447 | Database::prepare(  | 
            ||
| 448 | "INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" .  | 
            ||
| 449 | " SELECT :tree_id, location, block_order, module_name" .  | 
            ||
| 450 | " FROM `##block` WHERE gedcom_id = -1"  | 
            ||
| 451 | )->execute([  | 
            ||
| 452 | 'tree_id' => $tree_id,  | 
            ||
| 453 | ]);  | 
            ||
| 454 | |||
| 455 | // Gedcom and privacy settings  | 
            ||
| 456 | 		$tree->setPreference('CONTACT_USER_ID', Auth::id()); | 
            ||
| 457 | 		$tree->setPreference('WEBMASTER_USER_ID', Auth::id()); | 
            ||
| 458 | 		$tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language | 
            ||
| 459 | 		switch (WT_LOCALE) { | 
            ||
| 460 | case 'es':  | 
            ||
| 461 | 				$tree->setPreference('SURNAME_TRADITION', 'spanish'); | 
            ||
| 462 | break;  | 
            ||
| 463 | case 'is':  | 
            ||
| 464 | 				$tree->setPreference('SURNAME_TRADITION', 'icelandic'); | 
            ||
| 465 | break;  | 
            ||
| 466 | case 'lt':  | 
            ||
| 467 | 				$tree->setPreference('SURNAME_TRADITION', 'lithuanian'); | 
            ||
| 468 | break;  | 
            ||
| 469 | case 'pl':  | 
            ||
| 470 | 				$tree->setPreference('SURNAME_TRADITION', 'polish'); | 
            ||
| 471 | break;  | 
            ||
| 472 | case 'pt':  | 
            ||
| 473 | case 'pt-BR':  | 
            ||
| 474 | 				$tree->setPreference('SURNAME_TRADITION', 'portuguese'); | 
            ||
| 475 | break;  | 
            ||
| 476 | default:  | 
            ||
| 477 | 				$tree->setPreference('SURNAME_TRADITION', 'paternal'); | 
            ||
| 478 | break;  | 
            ||
| 479 | }  | 
            ||
| 480 | |||
| 481 | // Genealogy data  | 
            ||
| 482 | // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...  | 
            ||
| 483 | 		$john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ I18N::translate('John /DOE/'); | 
            ||
| 484 | 		$note     = I18N::translate('Edit this individual and replace their details with your own.'); | 
            ||
| 485 | 		Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute([ | 
            ||
| 486 | $tree_id,  | 
            ||
| 487 | 			"0 HEAD\n1 CHAR UTF-8\n0 @I1@ INDI\n1 NAME {$john_doe}\n1 SEX M\n1 BIRT\n2 DATE 01 JAN 1850\n2 NOTE {$note}\n0 TRLR\n", | 
            ||
| 488 | ]);  | 
            ||
| 489 | |||
| 490 | // Update our cache  | 
            ||
| 491 | self::$trees[$tree->tree_id] = $tree;  | 
            ||
| 492 | |||
| 493 | return $tree;  | 
            ||
| 494 | }  | 
            ||
| 495 | |||
| 496 | /**  | 
            ||
| 497 | * Are there any pending edits for this tree, than need reviewing by a moderator.  | 
            ||
| 498 | *  | 
            ||
| 499 | * @return bool  | 
            ||
| 500 | */  | 
            ||
| 501 | 	public function hasPendingEdit() { | 
            ||
| 508 | |||
| 509 | /**  | 
            ||
| 510 | * Delete all the genealogy data from a tree - in preparation for importing  | 
            ||
| 511 | * new data. Optionally retain the media data, for when the user has been  | 
            ||
| 512 | * editing their data offline using an application which deletes (or does not  | 
            ||
| 513 | * support) media data.  | 
            ||
| 514 | *  | 
            ||
| 515 | * @param bool $keep_media  | 
            ||
| 516 | */  | 
            ||
| 517 | 	public function deleteGenealogyData($keep_media) { | 
            ||
| 536 | |||
| 537 | /**  | 
            ||
| 538 | * Delete everything relating to a tree  | 
            ||
| 539 | */  | 
            ||
| 540 | 	public function delete() { | 
            ||
| 541 | // If this is the default tree, then unset it  | 
            ||
| 542 | 		if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { | 
            ||
| 543 | 			Site::setPreference('DEFAULT_GEDCOM', ''); | 
            ||
| 544 | }  | 
            ||
| 545 | |||
| 546 | $this->deleteGenealogyData(false);  | 
            ||
| 547 | |||
| 548 | 		Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]); | 
            ||
| 549 | 		Database::prepare("DELETE FROM `##block`               WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 550 | 		Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 551 | 		Database::prepare("DELETE FROM `##gedcom_setting`      WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 552 | 		Database::prepare("DELETE FROM `##module_privacy`      WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 553 | 		Database::prepare("DELETE FROM `##hit_counter`         WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 554 | 		Database::prepare("DELETE FROM `##default_resn`        WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 555 | 		Database::prepare("DELETE FROM `##gedcom_chunk`        WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 556 | 		Database::prepare("DELETE FROM `##log`                 WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 557 | 		Database::prepare("DELETE FROM `##gedcom`              WHERE gedcom_id = ?")->execute([$this->tree_id]); | 
            ||
| 558 | |||
| 559 | // After updating the database, we need to fetch a new (sorted) copy  | 
            ||
| 560 | self::$trees = null;  | 
            ||
| 561 | }  | 
            ||
| 562 | |||
| 563 | /**  | 
            ||
| 564 | * Export the tree to a GEDCOM file  | 
            ||
| 565 | *  | 
            ||
| 566 | * @param resource $stream  | 
            ||
| 567 | */  | 
            ||
| 568 | 	public function exportGedcom($stream) { | 
            ||
| 599 | |||
| 600 | /**  | 
            ||
| 601 | * Import data from a gedcom file into this tree.  | 
            ||
| 602 | *  | 
            ||
| 603 | * @param string $path The full path to the (possibly temporary) file.  | 
            ||
| 604 | * @param string $filename The preferred filename, for export/download.  | 
            ||
| 605 | *  | 
            ||
| 606 | * @throws \Exception  | 
            ||
| 607 | */  | 
            ||
| 608 | 	public function importGedcomFile($path, $filename) { | 
            ||
| 648 | |||
| 649 | /**  | 
            ||
| 650 | * Generate a new XREF, unique across all family trees  | 
            ||
| 651 | *  | 
            ||
| 652 | * @return string  | 
            ||
| 653 | */  | 
            ||
| 654 | 	public function getNewXref() { | 
            ||
| 655 | $prefix = 'X';  | 
            ||
| 656 | |||
| 657 | $increment = 1.0;  | 
            ||
| 658 | 		do { | 
            ||
| 659 | // Use LAST_INSERT_ID(expr) to provide a transaction-safe sequence. See  | 
            ||
| 660 | // http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id  | 
            ||
| 661 | $statement = Database::prepare(  | 
            ||
| 662 | "UPDATE `##site_setting` SET setting_value = LAST_INSERT_ID(setting_value + :increment) WHERE setting_name = 'next_xref'"  | 
            ||
| 663 | );  | 
            ||
| 664 | $statement->execute([  | 
            ||
| 665 | 'increment' => (int) $increment,  | 
            ||
| 666 | ]);  | 
            ||
| 667 | |||
| 668 | 			if ($statement->rowCount() === 0) { | 
            ||
| 669 | // First time we've used this record type.  | 
            ||
| 670 | 				Site::setPreference('next_xref', '1'); | 
            ||
| 703 | |||
| 704 | /**  | 
            ||
| 705 | * Create a new record from GEDCOM data.  | 
            ||
| 706 | *  | 
            ||
| 707 | * @param string $gedcom  | 
            ||
| 708 | *  | 
            ||
| 709 | * @throws \Exception  | 
            ||
| 710 | *  | 
            ||
| 711 | * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media  | 
            ||
| 712 | */  | 
            ||
| 713 | 	public function createRecord($gedcom) { | 
            ||
| 757 | }  | 
            ||
| 758 |