| Total Complexity | 76 |
| Total Lines | 747 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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.
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 |
||
| 26 | class Tree { |
||
| 27 | /** @var int The tree's ID number */ |
||
| 28 | private $tree_id; |
||
| 29 | |||
| 30 | /** @var string The tree's name */ |
||
| 31 | private $name; |
||
| 32 | |||
| 33 | /** @var string The tree's title */ |
||
| 34 | private $title; |
||
| 35 | |||
| 36 | /** @var int[] Default access rules for facts in this tree */ |
||
| 37 | private $fact_privacy; |
||
| 38 | |||
| 39 | /** @var int[] Default access rules for individuals in this tree */ |
||
| 40 | private $individual_privacy; |
||
| 41 | |||
| 42 | /** @var integer[][] Default access rules for individual facts in this tree */ |
||
| 43 | private $individual_fact_privacy; |
||
| 44 | |||
| 45 | /** @var Tree[] All trees that we have permission to see. */ |
||
| 46 | private static $trees; |
||
| 47 | |||
| 48 | /** @var string[] Cached copy of the wt_gedcom_setting table. */ |
||
| 49 | private $preferences = []; |
||
| 50 | |||
| 51 | /** @var string[][] Cached copy of the wt_user_gedcom_setting table. */ |
||
| 52 | private $user_preferences = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Create a tree object. This is a private constructor - it can only |
||
| 56 | * be called from Tree::getAll() to ensure proper initialisation. |
||
| 57 | * |
||
| 58 | * @param int $tree_id |
||
| 59 | * @param string $tree_name |
||
| 60 | * @param string $tree_title |
||
| 61 | */ |
||
| 62 | private function __construct($tree_id, $tree_name, $tree_title) { |
||
| 63 | $this->tree_id = $tree_id; |
||
| 64 | $this->name = $tree_name; |
||
| 65 | $this->title = $tree_title; |
||
| 66 | $this->fact_privacy = []; |
||
| 67 | $this->individual_privacy = []; |
||
| 68 | $this->individual_fact_privacy = []; |
||
| 69 | |||
| 70 | // Load the privacy settings for this tree |
||
| 71 | $rows = Database::prepare( |
||
| 72 | "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" . |
||
| 73 | " FROM `##default_resn` WHERE gedcom_id = :tree_id" |
||
| 74 | )->execute([ |
||
| 75 | 'priv_public' => Auth::PRIV_PRIVATE, |
||
| 76 | 'priv_user' => Auth::PRIV_USER, |
||
| 77 | 'priv_none' => Auth::PRIV_NONE, |
||
| 78 | 'priv_hide' => Auth::PRIV_HIDE, |
||
| 79 | 'tree_id' => $this->tree_id, |
||
| 80 | ])->fetchAll(); |
||
| 81 | |||
| 82 | foreach ($rows as $row) { |
||
| 83 | if ($row->xref !== null) { |
||
| 84 | if ($row->tag_type !== null) { |
||
| 85 | $this->individual_fact_privacy[$row->xref][$row->tag_type] = (int) $row->resn; |
||
| 86 | } else { |
||
| 87 | $this->individual_privacy[$row->xref] = (int) $row->resn; |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $this->fact_privacy[$row->tag_type] = (int) $row->resn; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The ID of this tree |
||
| 97 | * |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | public function getTreeId() { |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The name of this tree |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getName() { |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The name of this tree |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getNameUrl() { |
||
| 119 | return rawurlencode($this->name); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The title of this tree |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getTitle() { |
||
| 128 | return $this->title; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * The fact-level privacy for this tree. |
||
| 133 | * |
||
| 134 | * @return int[] |
||
| 135 | */ |
||
| 136 | public function getFactPrivacy() { |
||
| 137 | return $this->fact_privacy; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The individual-level privacy for this tree. |
||
| 142 | * |
||
| 143 | * @return int[] |
||
| 144 | */ |
||
| 145 | public function getIndividualPrivacy() { |
||
| 146 | return $this->individual_privacy; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The individual-fact-level privacy for this tree. |
||
| 151 | * |
||
| 152 | * @return int[][] |
||
| 153 | */ |
||
| 154 | public function getIndividualFactPrivacy() { |
||
| 155 | return $this->individual_fact_privacy; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get the tree’s configuration settings. |
||
| 160 | * |
||
| 161 | * @param string $setting_name |
||
| 162 | * @param string $default |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getPreference($setting_name, $default = '') { |
||
| 167 | if (empty($this->preferences)) { |
||
| 168 | $this->preferences = Database::prepare( |
||
| 169 | "SELECT SQL_CACHE setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?" |
||
| 170 | )->execute([$this->tree_id])->fetchAssoc(); |
||
| 171 | } |
||
| 172 | |||
| 173 | if (array_key_exists($setting_name, $this->preferences)) { |
||
| 174 | return $this->preferences[$setting_name]; |
||
| 175 | } else { |
||
| 176 | return $default; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set the tree’s configuration settings. |
||
| 182 | * |
||
| 183 | * @param string $setting_name |
||
| 184 | * @param string $setting_value |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | */ |
||
| 188 | public function setPreference($setting_name, $setting_value) { |
||
| 189 | if ($setting_value !== $this->getPreference($setting_name)) { |
||
| 190 | Database::prepare( |
||
| 191 | "REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" . |
||
| 192 | " VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))" |
||
| 193 | )->execute([ |
||
| 194 | 'tree_id' => $this->tree_id, |
||
| 195 | 'setting_name' => $setting_name, |
||
| 196 | 'setting_value' => $setting_value, |
||
| 197 | ]); |
||
| 198 | |||
| 199 | $this->preferences[$setting_name] = $setting_value; |
||
| 200 | |||
| 201 | Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '"', $this); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $this; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get the tree’s user-configuration settings. |
||
| 209 | * |
||
| 210 | * @param User $user |
||
| 211 | * @param string $setting_name |
||
| 212 | * @param string|null $default |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getUserPreference(User $user, $setting_name, $default = null) { |
||
| 217 | // There are lots of settings, and we need to fetch lots of them on every page |
||
| 218 | // so it is quicker to fetch them all in one go. |
||
| 219 | if (!array_key_exists($user->getUserId(), $this->user_preferences)) { |
||
| 220 | $this->user_preferences[$user->getUserId()] = Database::prepare( |
||
| 221 | "SELECT SQL_CACHE setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?" |
||
| 222 | )->execute([$user->getUserId(), $this->tree_id])->fetchAssoc(); |
||
| 223 | } |
||
| 224 | |||
| 225 | if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) { |
||
| 226 | return $this->user_preferences[$user->getUserId()][$setting_name]; |
||
| 227 | } else { |
||
| 228 | return $default; |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set the tree’s user-configuration settings. |
||
| 234 | * |
||
| 235 | * @param User $user |
||
| 236 | * @param string $setting_name |
||
| 237 | * @param string $setting_value |
||
| 238 | * |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | public function setUserPreference(User $user, $setting_name, $setting_value) { |
||
| 242 | if ($this->getUserPreference($user, $setting_name) !== $setting_value) { |
||
| 243 | // Update the database |
||
| 244 | if ($setting_value === null) { |
||
|
|
|||
| 245 | Database::prepare( |
||
| 246 | "DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name" |
||
| 247 | )->execute([ |
||
| 248 | 'tree_id' => $this->tree_id, |
||
| 249 | 'user_id' => $user->getUserId(), |
||
| 250 | 'setting_name' => $setting_name, |
||
| 251 | ]); |
||
| 252 | } else { |
||
| 253 | Database::prepare( |
||
| 254 | "REPLACE INTO `##user_gedcom_setting` (user_id, gedcom_id, setting_name, setting_value) VALUES (:user_id, :tree_id, :setting_name, LEFT(:setting_value, 255))" |
||
| 255 | )->execute([ |
||
| 256 | 'user_id' => $user->getUserId(), |
||
| 257 | 'tree_id' => $this->tree_id, |
||
| 258 | 'setting_name' => $setting_name, |
||
| 259 | 'setting_value' => $setting_value, |
||
| 260 | ]); |
||
| 261 | } |
||
| 262 | // Update our cache |
||
| 263 | $this->user_preferences[$user->getUserId()][$setting_name] = $setting_value; |
||
| 264 | // Audit log of changes |
||
| 265 | Log::addConfigurationLog('Tree preference "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this); |
||
| 266 | } |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Can a user accept changes for this tree? |
||
| 273 | * |
||
| 274 | * @param User $user |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | public function canAcceptChanges(User $user) { |
||
| 279 | return Auth::isModerator($this, $user); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Fetch all the trees that we have permission to access. |
||
| 284 | * |
||
| 285 | * @return Tree[] |
||
| 286 | */ |
||
| 287 | public static function getAll() { |
||
| 288 | if (self::$trees === null) { |
||
| 289 | self::$trees = []; |
||
| 290 | $rows = Database::prepare( |
||
| 291 | "SELECT SQL_CACHE g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" . |
||
| 292 | " FROM `##gedcom` g" . |
||
| 293 | " LEFT JOIN `##gedcom_setting` gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" . |
||
| 294 | " LEFT JOIN `##gedcom_setting` gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" . |
||
| 295 | " LEFT JOIN `##gedcom_setting` gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" . |
||
| 296 | " LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" . |
||
| 297 | " WHERE " . |
||
| 298 | " g.gedcom_id>0 AND (" . // exclude the "template" tree |
||
| 299 | " EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all |
||
| 300 | " ) OR (" . |
||
| 301 | " (gs2.setting_value = 1 OR ugs.setting_value = 'admin') AND (" . // Allow imported trees, with either: |
||
| 302 | " gs3.setting_value <> 1 OR" . // visitor access |
||
| 303 | " IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access |
||
| 304 | " )" . |
||
| 305 | " )" . |
||
| 306 | " ORDER BY g.sort_order, 3" |
||
| 307 | )->execute([Auth::id(), Auth::id()])->fetchAll(); |
||
| 308 | foreach ($rows as $row) { |
||
| 309 | self::$trees[$row->tree_name] = new self((int) $row->tree_id, $row->tree_name, $row->tree_title); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | return self::$trees; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Find the tree with a specific ID. |
||
| 318 | * |
||
| 319 | * @param int $tree_id |
||
| 320 | * |
||
| 321 | * @throws \DomainException |
||
| 322 | * |
||
| 323 | * @return Tree |
||
| 324 | */ |
||
| 325 | public static function findById($tree_id) { |
||
| 326 | foreach (self::getAll() as $tree) { |
||
| 327 | if ($tree->tree_id == $tree_id) { |
||
| 328 | return $tree; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | throw new \DomainException; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Find the tree with a specific name. |
||
| 336 | * |
||
| 337 | * @param string $tree_name |
||
| 338 | * |
||
| 339 | * @return Tree|null |
||
| 340 | */ |
||
| 341 | public static function findByName($tree_name) { |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Create arguments to select_edit_control() |
||
| 353 | * Note - these will be escaped later |
||
| 354 | * |
||
| 355 | * @return string[] |
||
| 356 | */ |
||
| 357 | public static function getIdList() { |
||
| 358 | $list = []; |
||
| 359 | foreach (self::getAll() as $tree) { |
||
| 360 | $list[$tree->tree_id] = $tree->title; |
||
| 361 | } |
||
| 362 | |||
| 363 | return $list; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Create arguments to select_edit_control() |
||
| 368 | * Note - these will be escaped later |
||
| 369 | * |
||
| 370 | * @return string[] |
||
| 371 | */ |
||
| 372 | public static function getNameList() { |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Create a new tree |
||
| 383 | * |
||
| 384 | * @param string $tree_name |
||
| 385 | * @param string $tree_title |
||
| 386 | * |
||
| 387 | * @return Tree |
||
| 388 | */ |
||
| 389 | public static function create($tree_name, $tree_title) { |
||
| 390 | try { |
||
| 391 | // Create a new tree |
||
| 392 | Database::prepare( |
||
| 393 | "INSERT INTO `##gedcom` (gedcom_name) VALUES (?)" |
||
| 394 | )->execute([$tree_name]); |
||
| 395 | $tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne(); |
||
| 396 | } catch (PDOException $ex) { |
||
| 397 | DebugBar::addThrowable($ex); |
||
| 398 | |||
| 399 | // A tree with that name already exists? |
||
| 400 | return self::findByName($tree_name); |
||
| 401 | } |
||
| 402 | |||
| 403 | // Update the list of trees - to include this new one |
||
| 404 | self::$trees = null; |
||
| 405 | $tree = self::findById($tree_id); |
||
| 406 | |||
| 407 | $tree->setPreference('imported', '0'); |
||
| 408 | $tree->setPreference('title', $tree_title); |
||
| 409 | |||
| 410 | // Module privacy |
||
| 411 | Module::setDefaultAccess($tree_id); |
||
| 412 | |||
| 413 | // Set preferences from default tree |
||
| 414 | Database::prepare( |
||
| 415 | "INSERT INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" . |
||
| 416 | " SELECT :tree_id, setting_name, setting_value" . |
||
| 417 | " FROM `##gedcom_setting` WHERE gedcom_id = -1" |
||
| 418 | )->execute([ |
||
| 419 | 'tree_id' => $tree_id, |
||
| 420 | ]); |
||
| 421 | |||
| 422 | Database::prepare( |
||
| 423 | "INSERT INTO `##default_resn` (gedcom_id, tag_type, resn)" . |
||
| 424 | " SELECT :tree_id, tag_type, resn" . |
||
| 425 | " FROM `##default_resn` WHERE gedcom_id = -1" |
||
| 426 | )->execute([ |
||
| 427 | 'tree_id' => $tree_id, |
||
| 428 | ]); |
||
| 429 | |||
| 430 | Database::prepare( |
||
| 431 | "INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" . |
||
| 432 | " SELECT :tree_id, location, block_order, module_name" . |
||
| 433 | " FROM `##block` WHERE gedcom_id = -1" |
||
| 434 | )->execute([ |
||
| 435 | 'tree_id' => $tree_id, |
||
| 436 | ]); |
||
| 437 | |||
| 438 | // Gedcom and privacy settings |
||
| 439 | $tree->setPreference('CONTACT_USER_ID', Auth::id()); |
||
| 440 | $tree->setPreference('WEBMASTER_USER_ID', Auth::id()); |
||
| 441 | $tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language |
||
| 442 | switch (WT_LOCALE) { |
||
| 443 | case 'es': |
||
| 444 | $tree->setPreference('SURNAME_TRADITION', 'spanish'); |
||
| 445 | break; |
||
| 446 | case 'is': |
||
| 447 | $tree->setPreference('SURNAME_TRADITION', 'icelandic'); |
||
| 448 | break; |
||
| 449 | case 'lt': |
||
| 450 | $tree->setPreference('SURNAME_TRADITION', 'lithuanian'); |
||
| 451 | break; |
||
| 452 | case 'pl': |
||
| 453 | $tree->setPreference('SURNAME_TRADITION', 'polish'); |
||
| 454 | break; |
||
| 455 | case 'pt': |
||
| 456 | case 'pt-BR': |
||
| 457 | $tree->setPreference('SURNAME_TRADITION', 'portuguese'); |
||
| 458 | break; |
||
| 459 | default: |
||
| 460 | $tree->setPreference('SURNAME_TRADITION', 'paternal'); |
||
| 461 | break; |
||
| 462 | } |
||
| 463 | |||
| 464 | // Genealogy data |
||
| 465 | // It is simpler to create a temporary/unimported GEDCOM than to populate all the tables... |
||
| 466 | $john_doe = /* I18N: This should be a common/default/placeholder name of an individual. Put slashes around the surname. */ I18N::translate('John /DOE/'); |
||
| 467 | $note = I18N::translate('Edit this individual and replace their details with your own.'); |
||
| 468 | Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute([ |
||
| 469 | $tree_id, |
||
| 470 | "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", |
||
| 471 | ]); |
||
| 472 | |||
| 473 | // Update our cache |
||
| 474 | self::$trees[$tree->tree_id] = $tree; |
||
| 475 | |||
| 476 | return $tree; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Are there any pending edits for this tree, than need reviewing by a moderator. |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public function hasPendingEdit() { |
||
| 485 | return (bool) Database::prepare( |
||
| 486 | "SELECT 1 FROM `##change` WHERE status = 'pending' AND gedcom_id = :tree_id" |
||
| 487 | )->execute([ |
||
| 488 | 'tree_id' => $this->tree_id, |
||
| 489 | ])->fetchOne(); |
||
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Delete all the genealogy data from a tree - in preparation for importing |
||
| 494 | * new data. Optionally retain the media data, for when the user has been |
||
| 495 | * editing their data offline using an application which deletes (or does not |
||
| 496 | * support) media data. |
||
| 497 | * |
||
| 498 | * @param bool $keep_media |
||
| 499 | */ |
||
| 500 | public function deleteGenealogyData($keep_media) { |
||
| 501 | Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 502 | Database::prepare("DELETE FROM `##individuals` WHERE i_file = ?")->execute([$this->tree_id]); |
||
| 503 | Database::prepare("DELETE FROM `##families` WHERE f_file = ?")->execute([$this->tree_id]); |
||
| 504 | Database::prepare("DELETE FROM `##sources` WHERE s_file = ?")->execute([$this->tree_id]); |
||
| 505 | Database::prepare("DELETE FROM `##other` WHERE o_file = ?")->execute([$this->tree_id]); |
||
| 506 | Database::prepare("DELETE FROM `##places` WHERE p_file = ?")->execute([$this->tree_id]); |
||
| 507 | Database::prepare("DELETE FROM `##placelinks` WHERE pl_file = ?")->execute([$this->tree_id]); |
||
| 508 | Database::prepare("DELETE FROM `##name` WHERE n_file = ?")->execute([$this->tree_id]); |
||
| 509 | Database::prepare("DELETE FROM `##dates` WHERE d_file = ?")->execute([$this->tree_id]); |
||
| 510 | Database::prepare("DELETE FROM `##change` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 511 | |||
| 512 | if ($keep_media) { |
||
| 513 | Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute([$this->tree_id]); |
||
| 514 | } else { |
||
| 515 | Database::prepare("DELETE FROM `##link` WHERE l_file =?")->execute([$this->tree_id]); |
||
| 516 | Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute([$this->tree_id]); |
||
| 517 | Database::prepare("DELETE FROM `##media_file` WHERE m_file =?")->execute([$this->tree_id]); |
||
| 518 | } |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Delete everything relating to a tree |
||
| 523 | */ |
||
| 524 | public function delete() { |
||
| 525 | // If this is the default tree, then unset it |
||
| 526 | if (Site::getPreference('DEFAULT_GEDCOM') === $this->name) { |
||
| 527 | Site::setPreference('DEFAULT_GEDCOM', ''); |
||
| 528 | } |
||
| 529 | |||
| 530 | $this->deleteGenealogyData(false); |
||
| 531 | |||
| 532 | Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute([$this->tree_id]); |
||
| 533 | Database::prepare("DELETE FROM `##block` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 534 | Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 535 | Database::prepare("DELETE FROM `##gedcom_setting` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 536 | Database::prepare("DELETE FROM `##module_privacy` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 537 | Database::prepare("DELETE FROM `##hit_counter` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 538 | Database::prepare("DELETE FROM `##default_resn` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 539 | Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 540 | Database::prepare("DELETE FROM `##log` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 541 | Database::prepare("DELETE FROM `##gedcom` WHERE gedcom_id = ?")->execute([$this->tree_id]); |
||
| 542 | |||
| 543 | // After updating the database, we need to fetch a new (sorted) copy |
||
| 544 | self::$trees = null; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Export the tree to a GEDCOM file |
||
| 549 | * |
||
| 550 | * @param resource $stream |
||
| 551 | */ |
||
| 552 | public function exportGedcom($stream) { |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Import data from a gedcom file into this tree. |
||
| 586 | * |
||
| 587 | * @param string $path The full path to the (possibly temporary) file. |
||
| 588 | * @param string $filename The preferred filename, for export/download. |
||
| 589 | * |
||
| 590 | * @throws \Exception |
||
| 591 | */ |
||
| 592 | public function importGedcomFile($path, $filename) { |
||
| 593 | // Read the file in blocks of roughly 64K. Ensure that each block |
||
| 594 | // contains complete gedcom records. This will ensure we don’t split |
||
| 595 | // multi-byte characters, as well as simplifying the code to import |
||
| 596 | // each block. |
||
| 597 | |||
| 598 | $file_data = ''; |
||
| 599 | $fp = fopen($path, 'rb'); |
||
| 600 | |||
| 601 | // Don’t allow the user to cancel the request. We do not want to be left with an incomplete transaction. |
||
| 602 | ignore_user_abort(true); |
||
| 603 | |||
| 604 | $this->deleteGenealogyData($this->getPreference('keep_media')); |
||
| 605 | $this->setPreference('gedcom_filename', $filename); |
||
| 606 | $this->setPreference('imported', '0'); |
||
| 607 | |||
| 608 | while (!feof($fp)) { |
||
| 609 | $file_data .= fread($fp, 65536); |
||
| 610 | // There is no strrpos() function that searches for substrings :-( |
||
| 611 | for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) { |
||
| 612 | if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) { |
||
| 613 | // We’ve found the last record boundary in this chunk of data |
||
| 614 | break; |
||
| 615 | } |
||
| 616 | } |
||
| 617 | if ($pos) { |
||
| 618 | Database::prepare( |
||
| 619 | "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" |
||
| 620 | )->execute([$this->tree_id, substr($file_data, 0, $pos)]); |
||
| 621 | $file_data = substr($file_data, $pos); |
||
| 622 | } |
||
| 623 | } |
||
| 624 | Database::prepare( |
||
| 625 | "INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)" |
||
| 626 | )->execute([$this->tree_id, $file_data]); |
||
| 627 | |||
| 628 | fclose($fp); |
||
| 629 | } |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Generate a new XREF, unique across all family trees |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | public function getNewXref() { |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Create a new record from GEDCOM data. |
||
| 688 | * |
||
| 689 | * @param string $gedcom |
||
| 690 | * |
||
| 691 | * @throws \Exception |
||
| 692 | * |
||
| 693 | * @return GedcomRecord|Individual|Family|Note|Source|Repository|Media |
||
| 694 | */ |
||
| 695 | public function createRecord($gedcom) { |
||
| 738 | } |
||
| 739 | |||
| 740 | /** |
||
| 741 | * What is the most significant individual in this tree. |
||
| 742 | * |
||
| 743 | * @param User $user |
||
| 744 | * |
||
| 745 | * @return Individual |
||
| 746 | */ |
||
| 747 | public function significantIndividual(User $user): Individual { |
||
| 775 |