xref: /webtrees/app/Tree.php (revision a25f0a04682c4c39c1947220c90af4118c713952)
1*a25f0a04SGreg Roach<?php
2*a25f0a04SGreg Roachnamespace Webtrees;
3*a25f0a04SGreg Roach
4*a25f0a04SGreg Roach/**
5*a25f0a04SGreg Roach * webtrees: online genealogy
6*a25f0a04SGreg Roach * Copyright (C) 2015 webtrees development team
7*a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
8*a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
9*a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
10*a25f0a04SGreg Roach * (at your option) any later version.
11*a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
12*a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a25f0a04SGreg Roach * GNU General Public License for more details.
15*a25f0a04SGreg Roach * You should have received a copy of the GNU General Public License
16*a25f0a04SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
17*a25f0a04SGreg Roach */
18*a25f0a04SGreg Roach
19*a25f0a04SGreg Roachuse PDOException;
20*a25f0a04SGreg Roach
21*a25f0a04SGreg Roach/**
22*a25f0a04SGreg Roach * Class Tree - Provide an interface to the wt_gedcom table
23*a25f0a04SGreg Roach */
24*a25f0a04SGreg Roachclass Tree {
25*a25f0a04SGreg Roach	/** @var integer The tree's ID number */
26*a25f0a04SGreg Roach	private $id;
27*a25f0a04SGreg Roach	/** @var string The tree's name */
28*a25f0a04SGreg Roach	private $name;
29*a25f0a04SGreg Roach	/** @var string The tree's title */
30*a25f0a04SGreg Roach	private $title;
31*a25f0a04SGreg Roach
32*a25f0a04SGreg Roach	/** @var Tree[] All trees that we have permission to see. */
33*a25f0a04SGreg Roach	private static $trees;
34*a25f0a04SGreg Roach
35*a25f0a04SGreg Roach	/** @var string[] Cached copy of the wt_gedcom_setting table. */
36*a25f0a04SGreg Roach	private $preferences;
37*a25f0a04SGreg Roach
38*a25f0a04SGreg Roach	/** @var string[][] Cached copy of the wt_user_gedcom_setting table. */
39*a25f0a04SGreg Roach	private $user_preferences = array();
40*a25f0a04SGreg Roach
41*a25f0a04SGreg Roach	/**
42*a25f0a04SGreg Roach	 * Create a tree object.  This is a private constructor - it can only
43*a25f0a04SGreg Roach	 * be called from Tree::getAll() to ensure proper initialisation.
44*a25f0a04SGreg Roach	 *
45*a25f0a04SGreg Roach	 * @param integer $id
46*a25f0a04SGreg Roach	 * @param string  $tree_name
47*a25f0a04SGreg Roach	 * @param string  $tree_title
48*a25f0a04SGreg Roach	 */
49*a25f0a04SGreg Roach	private function __construct($id, $tree_name, $tree_title) {
50*a25f0a04SGreg Roach		$this->id    = $id;
51*a25f0a04SGreg Roach		$this->name  = $tree_name;
52*a25f0a04SGreg Roach		$this->title = $tree_title;
53*a25f0a04SGreg Roach	}
54*a25f0a04SGreg Roach
55*a25f0a04SGreg Roach	/**
56*a25f0a04SGreg Roach	 * The ID of this tree
57*a25f0a04SGreg Roach	 *
58*a25f0a04SGreg Roach	 * @return integer
59*a25f0a04SGreg Roach	 */
60*a25f0a04SGreg Roach	public function id() {
61*a25f0a04SGreg Roach		return $this->id;
62*a25f0a04SGreg Roach	}
63*a25f0a04SGreg Roach
64*a25f0a04SGreg Roach	/**
65*a25f0a04SGreg Roach	 * The name of this tree
66*a25f0a04SGreg Roach	 *
67*a25f0a04SGreg Roach	 * @return string
68*a25f0a04SGreg Roach	 */
69*a25f0a04SGreg Roach	public function name() {
70*a25f0a04SGreg Roach		return $this->name;
71*a25f0a04SGreg Roach	}
72*a25f0a04SGreg Roach
73*a25f0a04SGreg Roach	/**
74*a25f0a04SGreg Roach	 * The name of this tree
75*a25f0a04SGreg Roach	 *
76*a25f0a04SGreg Roach	 * @return string
77*a25f0a04SGreg Roach	 */
78*a25f0a04SGreg Roach	public function nameHtml() {
79*a25f0a04SGreg Roach		return Filter::escapeHtml($this->name);
80*a25f0a04SGreg Roach	}
81*a25f0a04SGreg Roach
82*a25f0a04SGreg Roach	/**
83*a25f0a04SGreg Roach	 * The name of this tree
84*a25f0a04SGreg Roach	 *
85*a25f0a04SGreg Roach	 * @return string
86*a25f0a04SGreg Roach	 */
87*a25f0a04SGreg Roach	public function nameUrl() {
88*a25f0a04SGreg Roach		return Filter::escapeUrl($this->name);
89*a25f0a04SGreg Roach	}
90*a25f0a04SGreg Roach
91*a25f0a04SGreg Roach	/**
92*a25f0a04SGreg Roach	 * The title of this tree
93*a25f0a04SGreg Roach	 *
94*a25f0a04SGreg Roach	 * @return string
95*a25f0a04SGreg Roach	 */
96*a25f0a04SGreg Roach	public function title() {
97*a25f0a04SGreg Roach		return $this->title;
98*a25f0a04SGreg Roach	}
99*a25f0a04SGreg Roach
100*a25f0a04SGreg Roach	/**
101*a25f0a04SGreg Roach	 * The title of this tree, with HTML markup
102*a25f0a04SGreg Roach	 *
103*a25f0a04SGreg Roach	 * @return string
104*a25f0a04SGreg Roach	 */
105*a25f0a04SGreg Roach	public function titleHtml() {
106*a25f0a04SGreg Roach		return Filter::escapeHtml($this->title);
107*a25f0a04SGreg Roach	}
108*a25f0a04SGreg Roach
109*a25f0a04SGreg Roach	/**
110*a25f0a04SGreg Roach	 * Get the tree’s configuration settings.
111*a25f0a04SGreg Roach	 *
112*a25f0a04SGreg Roach	 * @param string      $setting_name
113*a25f0a04SGreg Roach	 * @param string|null $default
114*a25f0a04SGreg Roach	 *
115*a25f0a04SGreg Roach	 * @return string|null
116*a25f0a04SGreg Roach	 */
117*a25f0a04SGreg Roach	public function getPreference($setting_name, $default = null) {
118*a25f0a04SGreg Roach		if ($this->preferences === null) {
119*a25f0a04SGreg Roach			$this->preferences = Database::prepare(
120*a25f0a04SGreg Roach				"SELECT SQL_CACHE setting_name, setting_value FROM `##gedcom_setting` WHERE gedcom_id = ?"
121*a25f0a04SGreg Roach			)->execute(array($this->id))->fetchAssoc();
122*a25f0a04SGreg Roach		}
123*a25f0a04SGreg Roach
124*a25f0a04SGreg Roach		if (array_key_exists($setting_name, $this->preferences)) {
125*a25f0a04SGreg Roach			return $this->preferences[$setting_name];
126*a25f0a04SGreg Roach		} else {
127*a25f0a04SGreg Roach			return $default;
128*a25f0a04SGreg Roach		}
129*a25f0a04SGreg Roach	}
130*a25f0a04SGreg Roach
131*a25f0a04SGreg Roach	/**
132*a25f0a04SGreg Roach	 * Set the tree’s configuration settings.
133*a25f0a04SGreg Roach	 *
134*a25f0a04SGreg Roach	 * @param string $setting_name
135*a25f0a04SGreg Roach	 * @param string $setting_value
136*a25f0a04SGreg Roach	 *
137*a25f0a04SGreg Roach	 * @return $this
138*a25f0a04SGreg Roach	 */
139*a25f0a04SGreg Roach	public function setPreference($setting_name, $setting_value) {
140*a25f0a04SGreg Roach		if ($setting_value !== $this->getPreference($setting_name)) {
141*a25f0a04SGreg Roach			// Update the database
142*a25f0a04SGreg Roach			if ($setting_value === null) {
143*a25f0a04SGreg Roach				Database::prepare(
144*a25f0a04SGreg Roach					"DELETE FROM `##gedcom_setting` WHERE gedcom_id = :tree_id AND setting_name = :setting_name"
145*a25f0a04SGreg Roach				)->execute(array(
146*a25f0a04SGreg Roach					'tree_id'      => $this->id,
147*a25f0a04SGreg Roach					'setting_name' => $setting_name,
148*a25f0a04SGreg Roach				));
149*a25f0a04SGreg Roach			} else {
150*a25f0a04SGreg Roach				Database::prepare(
151*a25f0a04SGreg Roach					"REPLACE INTO `##gedcom_setting` (gedcom_id, setting_name, setting_value)" .
152*a25f0a04SGreg Roach					" VALUES (:tree_id, :setting_name, LEFT(:setting_value, 255))"
153*a25f0a04SGreg Roach				)->execute(array(
154*a25f0a04SGreg Roach					'tree_id'       => $this->id,
155*a25f0a04SGreg Roach					'setting_name'  => $setting_name,
156*a25f0a04SGreg Roach					'setting_value' => $setting_value,
157*a25f0a04SGreg Roach				));
158*a25f0a04SGreg Roach			}
159*a25f0a04SGreg Roach			// Update our cache
160*a25f0a04SGreg Roach			$this->preferences[$setting_name] = $setting_value;
161*a25f0a04SGreg Roach			// Audit log of changes
162*a25f0a04SGreg Roach			Log::addConfigurationLog('Tree setting "' . $setting_name . '" set to "' . $setting_value . '"', $this);
163*a25f0a04SGreg Roach		}
164*a25f0a04SGreg Roach
165*a25f0a04SGreg Roach		return $this;
166*a25f0a04SGreg Roach	}
167*a25f0a04SGreg Roach
168*a25f0a04SGreg Roach	/**
169*a25f0a04SGreg Roach	 * Get the tree’s user-configuration settings.
170*a25f0a04SGreg Roach	 *
171*a25f0a04SGreg Roach	 * @param User        $user
172*a25f0a04SGreg Roach	 * @param string      $setting_name
173*a25f0a04SGreg Roach	 * @param string|null $default
174*a25f0a04SGreg Roach	 *
175*a25f0a04SGreg Roach	 * @return string
176*a25f0a04SGreg Roach	 */
177*a25f0a04SGreg Roach	public function getUserPreference(User $user, $setting_name, $default = null) {
178*a25f0a04SGreg Roach		// There are lots of settings, and we need to fetch lots of them on every page
179*a25f0a04SGreg Roach		// so it is quicker to fetch them all in one go.
180*a25f0a04SGreg Roach		if (!array_key_exists($user->getUserId(), $this->user_preferences)) {
181*a25f0a04SGreg Roach			$this->user_preferences[$user->getUserId()] = Database::prepare(
182*a25f0a04SGreg Roach				"SELECT SQL_CACHE setting_name, setting_value FROM `##user_gedcom_setting` WHERE user_id = ? AND gedcom_id = ?"
183*a25f0a04SGreg Roach			)->execute(array($user->getUserId(), $this->id))->fetchAssoc();
184*a25f0a04SGreg Roach		}
185*a25f0a04SGreg Roach
186*a25f0a04SGreg Roach		if (array_key_exists($setting_name, $this->user_preferences[$user->getUserId()])) {
187*a25f0a04SGreg Roach			return $this->user_preferences[$user->getUserId()][$setting_name];
188*a25f0a04SGreg Roach		} else {
189*a25f0a04SGreg Roach			return $default;
190*a25f0a04SGreg Roach		}
191*a25f0a04SGreg Roach	}
192*a25f0a04SGreg Roach
193*a25f0a04SGreg Roach	/**
194*a25f0a04SGreg Roach	 * Set the tree’s user-configuration settings.
195*a25f0a04SGreg Roach	 *
196*a25f0a04SGreg Roach	 * @param User    $user
197*a25f0a04SGreg Roach	 * @param string  $setting_name
198*a25f0a04SGreg Roach	 * @param string  $setting_value
199*a25f0a04SGreg Roach	 *
200*a25f0a04SGreg Roach	 * @return $this
201*a25f0a04SGreg Roach	 */
202*a25f0a04SGreg Roach	public function setUserPreference(User $user, $setting_name, $setting_value) {
203*a25f0a04SGreg Roach		if ($this->getUserPreference($user, $setting_name) !== $setting_value) {
204*a25f0a04SGreg Roach			// Update the database
205*a25f0a04SGreg Roach			if ($setting_value === null) {
206*a25f0a04SGreg Roach				Database::prepare(
207*a25f0a04SGreg Roach					"DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = :tree_id AND user_id = :user_id AND setting_name = :setting_name"
208*a25f0a04SGreg Roach				)->execute(array(
209*a25f0a04SGreg Roach					'tree_id'      => $this->id,
210*a25f0a04SGreg Roach					'user_id'      => $user->getUserId(),
211*a25f0a04SGreg Roach					'setting_name' => $setting_name,
212*a25f0a04SGreg Roach				));
213*a25f0a04SGreg Roach			} else {
214*a25f0a04SGreg Roach				Database::prepare(
215*a25f0a04SGreg Roach					"REPLACE INTO `##user_gedcom_setting` (user_id, gedcom_id, setting_name, setting_value) VALUES (:user_id, :tree_id, :setting_name, LEFT(:setting_value, 255))"
216*a25f0a04SGreg Roach				)->execute(array(
217*a25f0a04SGreg Roach					'user_id'       => $user->getUserId(),
218*a25f0a04SGreg Roach					'tree_id'       => $this->id,
219*a25f0a04SGreg Roach					'setting_name'  => $setting_name,
220*a25f0a04SGreg Roach					'setting_value' => $setting_value
221*a25f0a04SGreg Roach				));
222*a25f0a04SGreg Roach			}
223*a25f0a04SGreg Roach			// Update our cache
224*a25f0a04SGreg Roach			$this->user_preferences[$user->getUserId()][$setting_name] = $setting_value;
225*a25f0a04SGreg Roach			// Audit log of changes
226*a25f0a04SGreg Roach			Log::addConfigurationLog('Tree setting "' . $setting_name . '" set to "' . $setting_value . '" for user "' . $user->getUserName() . '"', $this);
227*a25f0a04SGreg Roach		}
228*a25f0a04SGreg Roach
229*a25f0a04SGreg Roach		return $this;
230*a25f0a04SGreg Roach	}
231*a25f0a04SGreg Roach
232*a25f0a04SGreg Roach	/**
233*a25f0a04SGreg Roach	 * Can a user accept changes for this tree?
234*a25f0a04SGreg Roach	 *
235*a25f0a04SGreg Roach	 * @param User $user
236*a25f0a04SGreg Roach	 *
237*a25f0a04SGreg Roach	 * @return boolean
238*a25f0a04SGreg Roach	 */
239*a25f0a04SGreg Roach	public function canAcceptChanges(User $user) {
240*a25f0a04SGreg Roach		return Auth::isModerator($this, $user);
241*a25f0a04SGreg Roach	}
242*a25f0a04SGreg Roach
243*a25f0a04SGreg Roach	/**
244*a25f0a04SGreg Roach	 * Fetch all the trees that we have permission to access.
245*a25f0a04SGreg Roach	 *
246*a25f0a04SGreg Roach	 * @return Tree[]
247*a25f0a04SGreg Roach	 */
248*a25f0a04SGreg Roach	public static function getAll() {
249*a25f0a04SGreg Roach		if (self::$trees === null) {
250*a25f0a04SGreg Roach			self::$trees = array();
251*a25f0a04SGreg Roach			$rows = Database::prepare(
252*a25f0a04SGreg Roach				"SELECT SQL_CACHE g.gedcom_id AS tree_id, g.gedcom_name AS tree_name, gs1.setting_value AS tree_title" .
253*a25f0a04SGreg Roach				" FROM `##gedcom` g" .
254*a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs1 ON (g.gedcom_id=gs1.gedcom_id AND gs1.setting_name='title')" .
255*a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs2 ON (g.gedcom_id=gs2.gedcom_id AND gs2.setting_name='imported')" .
256*a25f0a04SGreg Roach				" LEFT JOIN `##gedcom_setting`      gs3 ON (g.gedcom_id=gs3.gedcom_id AND gs3.setting_name='REQUIRE_AUTHENTICATION')" .
257*a25f0a04SGreg Roach				" LEFT JOIN `##user_gedcom_setting` ugs ON (g.gedcom_id=ugs.gedcom_id AND ugs.setting_name='canedit' AND ugs.user_id=?)" .
258*a25f0a04SGreg Roach				" WHERE " .
259*a25f0a04SGreg Roach				"  g.gedcom_id>0 AND (" . // exclude the "template" tree
260*a25f0a04SGreg Roach				"    EXISTS (SELECT 1 FROM `##user_setting` WHERE user_id=? AND setting_name='canadmin' AND setting_value=1)" . // Admin sees all
261*a25f0a04SGreg Roach				"   ) OR (" .
262*a25f0a04SGreg Roach				"    gs2.setting_value = 1 AND (" . // Allow imported trees, with either:
263*a25f0a04SGreg Roach				"     gs3.setting_value <> 1 OR" . // visitor access
264*a25f0a04SGreg Roach				"     IFNULL(ugs.setting_value, 'none')<>'none'" . // explicit access
265*a25f0a04SGreg Roach				"   )" .
266*a25f0a04SGreg Roach				"  )" .
267*a25f0a04SGreg Roach				" ORDER BY g.sort_order, 3"
268*a25f0a04SGreg Roach			)->execute(array(Auth::id(), Auth::id()))->fetchAll();
269*a25f0a04SGreg Roach			foreach ($rows as $row) {
270*a25f0a04SGreg Roach				self::$trees[$row->tree_id] = new self($row->tree_id, $row->tree_name, $row->tree_title);
271*a25f0a04SGreg Roach			}
272*a25f0a04SGreg Roach		}
273*a25f0a04SGreg Roach
274*a25f0a04SGreg Roach		return self::$trees;
275*a25f0a04SGreg Roach	}
276*a25f0a04SGreg Roach
277*a25f0a04SGreg Roach	/**
278*a25f0a04SGreg Roach	 * Get the tree with a specific ID.
279*a25f0a04SGreg Roach	 *
280*a25f0a04SGreg Roach	 * @param integer $tree_id
281*a25f0a04SGreg Roach	 *
282*a25f0a04SGreg Roach	 * @return Tree
283*a25f0a04SGreg Roach	 */
284*a25f0a04SGreg Roach	public static function get($tree_id) {
285*a25f0a04SGreg Roach		$trees = self::getAll();
286*a25f0a04SGreg Roach
287*a25f0a04SGreg Roach		return $trees[$tree_id];
288*a25f0a04SGreg Roach	}
289*a25f0a04SGreg Roach
290*a25f0a04SGreg Roach	/**
291*a25f0a04SGreg Roach	 * Create arguments to select_edit_control()
292*a25f0a04SGreg Roach	 * Note - these will be escaped later
293*a25f0a04SGreg Roach	 *
294*a25f0a04SGreg Roach	 * @return string[]
295*a25f0a04SGreg Roach	 */
296*a25f0a04SGreg Roach	public static function getIdList() {
297*a25f0a04SGreg Roach		$list = array();
298*a25f0a04SGreg Roach		foreach (self::getAll() as $tree) {
299*a25f0a04SGreg Roach			$list[$tree->id] = $tree->title;
300*a25f0a04SGreg Roach		}
301*a25f0a04SGreg Roach
302*a25f0a04SGreg Roach		return $list;
303*a25f0a04SGreg Roach	}
304*a25f0a04SGreg Roach
305*a25f0a04SGreg Roach	/**
306*a25f0a04SGreg Roach	 * Create arguments to select_edit_control()
307*a25f0a04SGreg Roach	 * Note - these will be escaped later
308*a25f0a04SGreg Roach	 *
309*a25f0a04SGreg Roach	 * @return string[]
310*a25f0a04SGreg Roach	 */
311*a25f0a04SGreg Roach	public static function getNameList() {
312*a25f0a04SGreg Roach		$list = array();
313*a25f0a04SGreg Roach		foreach (self::getAll() as $tree) {
314*a25f0a04SGreg Roach			$list[$tree->name] = $tree->title;
315*a25f0a04SGreg Roach		}
316*a25f0a04SGreg Roach
317*a25f0a04SGreg Roach		return $list;
318*a25f0a04SGreg Roach	}
319*a25f0a04SGreg Roach
320*a25f0a04SGreg Roach	/**
321*a25f0a04SGreg Roach	 * Find the ID number for a tree name
322*a25f0a04SGreg Roach	 *
323*a25f0a04SGreg Roach	 * @param integer $tree_name
324*a25f0a04SGreg Roach	 *
325*a25f0a04SGreg Roach	 * @return integer|null
326*a25f0a04SGreg Roach	 */
327*a25f0a04SGreg Roach	public static function getIdFromName($tree_name) {
328*a25f0a04SGreg Roach		foreach (self::getAll() as $tree_id => $tree) {
329*a25f0a04SGreg Roach			if ($tree->name == $tree_name) {
330*a25f0a04SGreg Roach				return $tree_id;
331*a25f0a04SGreg Roach			}
332*a25f0a04SGreg Roach		}
333*a25f0a04SGreg Roach
334*a25f0a04SGreg Roach		return null;
335*a25f0a04SGreg Roach	}
336*a25f0a04SGreg Roach
337*a25f0a04SGreg Roach	/**
338*a25f0a04SGreg Roach	 * Find the tree name from a numeric ID.
339*a25f0a04SGreg Roach	 * @param integer $tree_id
340*a25f0a04SGreg Roach	 *
341*a25f0a04SGreg Roach	 * @return string
342*a25f0a04SGreg Roach	 */
343*a25f0a04SGreg Roach	public static function getNameFromId($tree_id) {
344*a25f0a04SGreg Roach		return self::get($tree_id)->name;
345*a25f0a04SGreg Roach	}
346*a25f0a04SGreg Roach
347*a25f0a04SGreg Roach	/**
348*a25f0a04SGreg Roach	 * Create a new tree
349*a25f0a04SGreg Roach	 *
350*a25f0a04SGreg Roach	 * @param string $tree_name
351*a25f0a04SGreg Roach	 * @param string $tree_title
352*a25f0a04SGreg Roach	 *
353*a25f0a04SGreg Roach	 * @return Tree
354*a25f0a04SGreg Roach	 */
355*a25f0a04SGreg Roach	public static function create($tree_name, $tree_title) {
356*a25f0a04SGreg Roach		try {
357*a25f0a04SGreg Roach			// Create a new tree
358*a25f0a04SGreg Roach			Database::prepare(
359*a25f0a04SGreg Roach				"INSERT INTO `##gedcom` (gedcom_name) VALUES (?)"
360*a25f0a04SGreg Roach			)->execute(array($tree_name));
361*a25f0a04SGreg Roach			$tree_id = Database::prepare("SELECT LAST_INSERT_ID()")->fetchOne();
362*a25f0a04SGreg Roach		} catch (PDOException $ex) {
363*a25f0a04SGreg Roach			// A tree with that name already exists?
364*a25f0a04SGreg Roach			return self::get(self::getIdFromName($tree_name));
365*a25f0a04SGreg Roach		}
366*a25f0a04SGreg Roach
367*a25f0a04SGreg Roach		// Update the list of trees - to include this new one
368*a25f0a04SGreg Roach		self::$trees = null;
369*a25f0a04SGreg Roach		$tree        = self::get($tree_id);
370*a25f0a04SGreg Roach
371*a25f0a04SGreg Roach		$tree->setPreference('imported', '0');
372*a25f0a04SGreg Roach		$tree->setPreference('title', $tree_title);
373*a25f0a04SGreg Roach
374*a25f0a04SGreg Roach		// Module privacy
375*a25f0a04SGreg Roach		Module::setDefaultAccess($tree_id);
376*a25f0a04SGreg Roach
377*a25f0a04SGreg Roach		// Gedcom and privacy settings
378*a25f0a04SGreg Roach		$tree->setPreference('ADVANCED_NAME_FACTS', 'NICK,_AKA');
379*a25f0a04SGreg Roach		$tree->setPreference('ADVANCED_PLAC_FACTS', '');
380*a25f0a04SGreg Roach		$tree->setPreference('ALLOW_THEME_DROPDOWN', '1');
381*a25f0a04SGreg Roach		$tree->setPreference('CALENDAR_FORMAT', 'gregorian');
382*a25f0a04SGreg Roach		$tree->setPreference('CHART_BOX_TAGS', '');
383*a25f0a04SGreg Roach		$tree->setPreference('COMMON_NAMES_ADD', '');
384*a25f0a04SGreg Roach		$tree->setPreference('COMMON_NAMES_REMOVE', '');
385*a25f0a04SGreg Roach		$tree->setPreference('COMMON_NAMES_THRESHOLD', '40');
386*a25f0a04SGreg Roach		$tree->setPreference('CONTACT_USER_ID', Auth::id());
387*a25f0a04SGreg Roach		$tree->setPreference('DEFAULT_PEDIGREE_GENERATIONS', '4');
388*a25f0a04SGreg Roach		$tree->setPreference('EXPAND_RELATIVES_EVENTS', '0');
389*a25f0a04SGreg Roach		$tree->setPreference('EXPAND_SOURCES', '0');
390*a25f0a04SGreg Roach		$tree->setPreference('FAM_FACTS_ADD', 'CENS,MARR,RESI,SLGS,MARR_CIVIL,MARR_RELIGIOUS,MARR_PARTNERS,RESN');
391*a25f0a04SGreg Roach		$tree->setPreference('FAM_FACTS_QUICK', 'MARR,DIV,_NMR');
392*a25f0a04SGreg Roach		$tree->setPreference('FAM_FACTS_UNIQUE', 'NCHI,MARL,DIV,ANUL,DIVF,ENGA,MARB,MARC,MARS');
393*a25f0a04SGreg Roach		$tree->setPreference('FAM_ID_PREFIX', 'F');
394*a25f0a04SGreg Roach		$tree->setPreference('FORMAT_TEXT', 'markdown');
395*a25f0a04SGreg Roach		$tree->setPreference('FULL_SOURCES', '0');
396*a25f0a04SGreg Roach		$tree->setPreference('GEDCOM_ID_PREFIX', 'I');
397*a25f0a04SGreg Roach		$tree->setPreference('GEDCOM_MEDIA_PATH', '');
398*a25f0a04SGreg Roach		$tree->setPreference('GENERATE_UIDS', '0');
399*a25f0a04SGreg Roach		$tree->setPreference('HIDE_GEDCOM_ERRORS', '1');
400*a25f0a04SGreg Roach		$tree->setPreference('HIDE_LIVE_PEOPLE', '1');
401*a25f0a04SGreg Roach		$tree->setPreference('INDI_FACTS_ADD', 'AFN,BIRT,DEAT,BURI,CREM,ADOP,BAPM,BARM,BASM,BLES,CHRA,CONF,FCOM,ORDN,NATU,EMIG,IMMI,CENS,PROB,WILL,GRAD,RETI,DSCR,EDUC,IDNO,NATI,NCHI,NMR,OCCU,PROP,RELI,RESI,SSN,TITL,BAPL,CONL,ENDL,SLGC,_MILI,ASSO,RESN');
402*a25f0a04SGreg Roach		$tree->setPreference('INDI_FACTS_QUICK', 'BIRT,BURI,BAPM,CENS,DEAT,OCCU,RESI');
403*a25f0a04SGreg Roach		$tree->setPreference('INDI_FACTS_UNIQUE', '');
404*a25f0a04SGreg Roach		$tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', '');
405*a25f0a04SGreg Roach		$tree->setPreference('KEEP_ALIVE_YEARS_DEATH', '');
406*a25f0a04SGreg Roach		$tree->setPreference('LANGUAGE', WT_LOCALE); // Default to the current admin’s language
407*a25f0a04SGreg Roach		$tree->setPreference('MAX_ALIVE_AGE', 120);
408*a25f0a04SGreg Roach		$tree->setPreference('MAX_DESCENDANCY_GENERATIONS', '15');
409*a25f0a04SGreg Roach		$tree->setPreference('MAX_PEDIGREE_GENERATIONS', '10');
410*a25f0a04SGreg Roach		$tree->setPreference('MEDIA_DIRECTORY', 'media/');
411*a25f0a04SGreg Roach		$tree->setPreference('MEDIA_ID_PREFIX', 'M');
412*a25f0a04SGreg Roach		$tree->setPreference('MEDIA_UPLOAD', WT_PRIV_USER);
413*a25f0a04SGreg Roach		$tree->setPreference('META_DESCRIPTION', '');
414*a25f0a04SGreg Roach		$tree->setPreference('META_TITLE', WT_WEBTREES);
415*a25f0a04SGreg Roach		$tree->setPreference('NOTE_FACTS_ADD', 'SOUR,RESN');
416*a25f0a04SGreg Roach		$tree->setPreference('NOTE_FACTS_QUICK', '');
417*a25f0a04SGreg Roach		$tree->setPreference('NOTE_FACTS_UNIQUE', '');
418*a25f0a04SGreg Roach		$tree->setPreference('NOTE_ID_PREFIX', 'N');
419*a25f0a04SGreg Roach		$tree->setPreference('NO_UPDATE_CHAN', '0');
420*a25f0a04SGreg Roach		$tree->setPreference('PEDIGREE_FULL_DETAILS', '1');
421*a25f0a04SGreg Roach		$tree->setPreference('PEDIGREE_LAYOUT', '1');
422*a25f0a04SGreg Roach		$tree->setPreference('PEDIGREE_ROOT_ID', '');
423*a25f0a04SGreg Roach		$tree->setPreference('PEDIGREE_SHOW_GENDER', '0');
424*a25f0a04SGreg Roach		$tree->setPreference('PREFER_LEVEL2_SOURCES', '1');
425*a25f0a04SGreg Roach		$tree->setPreference('QUICK_REQUIRED_FACTS', 'BIRT,DEAT');
426*a25f0a04SGreg Roach		$tree->setPreference('QUICK_REQUIRED_FAMFACTS', 'MARR');
427*a25f0a04SGreg Roach		$tree->setPreference('REPO_FACTS_ADD', 'PHON,EMAIL,FAX,WWW,RESN');
428*a25f0a04SGreg Roach		$tree->setPreference('REPO_FACTS_QUICK', '');
429*a25f0a04SGreg Roach		$tree->setPreference('REPO_FACTS_UNIQUE', 'NAME,ADDR');
430*a25f0a04SGreg Roach		$tree->setPreference('REPO_ID_PREFIX', 'R');
431*a25f0a04SGreg Roach		$tree->setPreference('REQUIRE_AUTHENTICATION', '0');
432*a25f0a04SGreg Roach		$tree->setPreference('SAVE_WATERMARK_IMAGE', '0');
433*a25f0a04SGreg Roach		$tree->setPreference('SAVE_WATERMARK_THUMB', '0');
434*a25f0a04SGreg Roach		$tree->setPreference('SHOW_AGE_DIFF', '0');
435*a25f0a04SGreg Roach		$tree->setPreference('SHOW_COUNTER', '1');
436*a25f0a04SGreg Roach		$tree->setPreference('SHOW_DEAD_PEOPLE', WT_PRIV_PUBLIC);
437*a25f0a04SGreg Roach		$tree->setPreference('SHOW_EST_LIST_DATES', '0');
438*a25f0a04SGreg Roach		$tree->setPreference('SHOW_FACT_ICONS', '1');
439*a25f0a04SGreg Roach		$tree->setPreference('SHOW_GEDCOM_RECORD', '0');
440*a25f0a04SGreg Roach		$tree->setPreference('SHOW_HIGHLIGHT_IMAGES', '1');
441*a25f0a04SGreg Roach		$tree->setPreference('SHOW_LDS_AT_GLANCE', '0');
442*a25f0a04SGreg Roach		$tree->setPreference('SHOW_LEVEL2_NOTES', '1');
443*a25f0a04SGreg Roach		$tree->setPreference('SHOW_LIVING_NAMES', WT_PRIV_USER);
444*a25f0a04SGreg Roach		$tree->setPreference('SHOW_MEDIA_DOWNLOAD', '0');
445*a25f0a04SGreg Roach		$tree->setPreference('SHOW_NO_WATERMARK', WT_PRIV_USER);
446*a25f0a04SGreg Roach		$tree->setPreference('SHOW_PARENTS_AGE', '1');
447*a25f0a04SGreg Roach		$tree->setPreference('SHOW_PEDIGREE_PLACES', '9');
448*a25f0a04SGreg Roach		$tree->setPreference('SHOW_PEDIGREE_PLACES_SUFFIX', '0');
449*a25f0a04SGreg Roach		$tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', '1');
450*a25f0a04SGreg Roach		$tree->setPreference('SHOW_RELATIVES_EVENTS', '_BIRT_CHIL,_BIRT_SIBL,_MARR_CHIL,_MARR_PARE,_DEAT_CHIL,_DEAT_PARE,_DEAT_GPAR,_DEAT_SIBL,_DEAT_SPOU');
451*a25f0a04SGreg Roach		$tree->setPreference('SHOW_STATS', '0');
452*a25f0a04SGreg Roach		$tree->setPreference('SOURCE_ID_PREFIX', 'S');
453*a25f0a04SGreg Roach		$tree->setPreference('SOUR_FACTS_ADD', 'NOTE,REPO,SHARED_NOTE,RESN');
454*a25f0a04SGreg Roach		$tree->setPreference('SOUR_FACTS_QUICK', 'TEXT,NOTE,REPO');
455*a25f0a04SGreg Roach		$tree->setPreference('SOUR_FACTS_UNIQUE', 'AUTH,ABBR,TITL,PUBL,TEXT');
456*a25f0a04SGreg Roach		$tree->setPreference('SUBLIST_TRIGGER_I', '200');
457*a25f0a04SGreg Roach		$tree->setPreference('SURNAME_LIST_STYLE', 'style2');
458*a25f0a04SGreg Roach		switch (WT_LOCALE) {
459*a25f0a04SGreg Roach		case 'es':
460*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'spanish');
461*a25f0a04SGreg Roach			break;
462*a25f0a04SGreg Roach		case 'is':
463*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'icelandic');
464*a25f0a04SGreg Roach			break;
465*a25f0a04SGreg Roach		case 'lt':
466*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'lithuanian');
467*a25f0a04SGreg Roach			break;
468*a25f0a04SGreg Roach		case 'pl':
469*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'polish');
470*a25f0a04SGreg Roach			break;
471*a25f0a04SGreg Roach		case 'pt':
472*a25f0a04SGreg Roach		case 'pt-BR':
473*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'portuguese');
474*a25f0a04SGreg Roach			break;
475*a25f0a04SGreg Roach		default:
476*a25f0a04SGreg Roach			$tree->setPreference('SURNAME_TRADITION', 'paternal');
477*a25f0a04SGreg Roach			break;
478*a25f0a04SGreg Roach		}
479*a25f0a04SGreg Roach		$tree->setPreference('THEME_DIR', 'webtrees');
480*a25f0a04SGreg Roach		$tree->setPreference('THUMBNAIL_WIDTH', '100');
481*a25f0a04SGreg Roach		$tree->setPreference('USE_RIN', '0');
482*a25f0a04SGreg Roach		$tree->setPreference('USE_SILHOUETTE', '1');
483*a25f0a04SGreg Roach		$tree->setPreference('WATERMARK_THUMB', '0');
484*a25f0a04SGreg Roach		$tree->setPreference('WEBMASTER_USER_ID', Auth::id());
485*a25f0a04SGreg Roach		$tree->setPreference('WEBTREES_EMAIL', '');
486*a25f0a04SGreg Roach		$tree->setPreference('WORD_WRAPPED_NOTES', '0');
487*a25f0a04SGreg Roach
488*a25f0a04SGreg Roach		// Default restriction settings
489*a25f0a04SGreg Roach		$statement = Database::prepare(
490*a25f0a04SGreg Roach			"INSERT INTO `##default_resn` (gedcom_id, xref, tag_type, resn) VALUES (?, NULL, ?, ?)"
491*a25f0a04SGreg Roach		);
492*a25f0a04SGreg Roach		$statement->execute(array($tree_id, 'SSN', 'confidential'));
493*a25f0a04SGreg Roach		$statement->execute(array($tree_id, 'SOUR', 'privacy'));
494*a25f0a04SGreg Roach		$statement->execute(array($tree_id, 'REPO', 'privacy'));
495*a25f0a04SGreg Roach		$statement->execute(array($tree_id, 'SUBM', 'confidential'));
496*a25f0a04SGreg Roach		$statement->execute(array($tree_id, 'SUBN', 'confidential'));
497*a25f0a04SGreg Roach
498*a25f0a04SGreg Roach		// Genealogy data
499*a25f0a04SGreg Roach		// It is simpler to create a temporary/unimported GEDCOM than to populate all the tables...
500*a25f0a04SGreg Roach		$john_doe = /* I18N: This should be a common/default/placeholder name of an individual.  Put slashes around the surname. */
501*a25f0a04SGreg Roach			I18N::translate('John /DOE/');
502*a25f0a04SGreg Roach		$note     = I18N::translate('Edit this individual and replace their details with your own');
503*a25f0a04SGreg Roach		Database::prepare("INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)")->execute(array(
504*a25f0a04SGreg Roach			$tree_id,
505*a25f0a04SGreg Roach			"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"
506*a25f0a04SGreg Roach		));
507*a25f0a04SGreg Roach
508*a25f0a04SGreg Roach		// Set the initial blocks
509*a25f0a04SGreg Roach		Database::prepare(
510*a25f0a04SGreg Roach			"INSERT INTO `##block` (gedcom_id, location, block_order, module_name)" .
511*a25f0a04SGreg Roach			" SELECT ?, location, block_order, module_name" .
512*a25f0a04SGreg Roach			" FROM `##block`" .
513*a25f0a04SGreg Roach			" WHERE gedcom_id = -1"
514*a25f0a04SGreg Roach		)->execute(array($tree_id));
515*a25f0a04SGreg Roach
516*a25f0a04SGreg Roach		// Update our cache
517*a25f0a04SGreg Roach		self::$trees[$tree->id] = $tree;
518*a25f0a04SGreg Roach
519*a25f0a04SGreg Roach		return $tree;
520*a25f0a04SGreg Roach	}
521*a25f0a04SGreg Roach
522*a25f0a04SGreg Roach	/**
523*a25f0a04SGreg Roach	 * Delete all the genealogy data from a tree - in preparation for importing
524*a25f0a04SGreg Roach	 * new data.  Optionally retain the media data, for when the user has been
525*a25f0a04SGreg Roach	 * editing their data offline using an application which deletes (or does not
526*a25f0a04SGreg Roach	 * support) media data.
527*a25f0a04SGreg Roach	 *
528*a25f0a04SGreg Roach	 * @param bool $keep_media
529*a25f0a04SGreg Roach	 */
530*a25f0a04SGreg Roach	public function deleteGenealogyData($keep_media) {
531*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id = ?")->execute(array($this->id));
532*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##individuals`  WHERE i_file    = ?")->execute(array($this->id));
533*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##families`     WHERE f_file    = ?")->execute(array($this->id));
534*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##sources`      WHERE s_file    = ?")->execute(array($this->id));
535*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##other`        WHERE o_file    = ?")->execute(array($this->id));
536*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##places`       WHERE p_file    = ?")->execute(array($this->id));
537*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##placelinks`   WHERE pl_file   = ?")->execute(array($this->id));
538*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##name`         WHERE n_file    = ?")->execute(array($this->id));
539*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##dates`        WHERE d_file    = ?")->execute(array($this->id));
540*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##change`       WHERE gedcom_id = ?")->execute(array($this->id));
541*a25f0a04SGreg Roach
542*a25f0a04SGreg Roach		if ($keep_media) {
543*a25f0a04SGreg Roach			Database::prepare("DELETE FROM `##link` WHERE l_file =? AND l_type<>'OBJE'")->execute(array($this->id));
544*a25f0a04SGreg Roach		} else {
545*a25f0a04SGreg Roach			Database::prepare("DELETE FROM `##link`  WHERE l_file =?")->execute(array($this->id));
546*a25f0a04SGreg Roach			Database::prepare("DELETE FROM `##media` WHERE m_file =?")->execute(array($this->id));
547*a25f0a04SGreg Roach		}
548*a25f0a04SGreg Roach	}
549*a25f0a04SGreg Roach
550*a25f0a04SGreg Roach	/**
551*a25f0a04SGreg Roach	 * Delete everything relating to a tree
552*a25f0a04SGreg Roach	 */
553*a25f0a04SGreg Roach	public function delete() {
554*a25f0a04SGreg Roach		// If this is the default tree, then unset it
555*a25f0a04SGreg Roach		if (Site::getPreference('DEFAULT_GEDCOM') === self::getNameFromId($this->id)) {
556*a25f0a04SGreg Roach			Site::setPreference('DEFAULT_GEDCOM', '');
557*a25f0a04SGreg Roach		}
558*a25f0a04SGreg Roach
559*a25f0a04SGreg Roach		$this->deleteGenealogyData(false);
560*a25f0a04SGreg Roach
561*a25f0a04SGreg Roach		Database::prepare("DELETE `##block_setting` FROM `##block_setting` JOIN `##block` USING (block_id) WHERE gedcom_id=?")->execute(array($this->id));
562*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##block`               WHERE gedcom_id = ?")->execute(array($this->id));
563*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##user_gedcom_setting` WHERE gedcom_id = ?")->execute(array($this->id));
564*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##gedcom_setting`      WHERE gedcom_id = ?")->execute(array($this->id));
565*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##module_privacy`      WHERE gedcom_id = ?")->execute(array($this->id));
566*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##next_id`             WHERE gedcom_id = ?")->execute(array($this->id));
567*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##hit_counter`         WHERE gedcom_id = ?")->execute(array($this->id));
568*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##default_resn`        WHERE gedcom_id = ?")->execute(array($this->id));
569*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##gedcom_chunk`        WHERE gedcom_id = ?")->execute(array($this->id));
570*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##log`                 WHERE gedcom_id = ?")->execute(array($this->id));
571*a25f0a04SGreg Roach		Database::prepare("DELETE FROM `##gedcom`              WHERE gedcom_id = ?")->execute(array($this->id));
572*a25f0a04SGreg Roach
573*a25f0a04SGreg Roach		// After updating the database, we need to fetch a new (sorted) copy
574*a25f0a04SGreg Roach		self::$trees = null;
575*a25f0a04SGreg Roach	}
576*a25f0a04SGreg Roach
577*a25f0a04SGreg Roach	/**
578*a25f0a04SGreg Roach	 * Export the tree to a GEDCOM file
579*a25f0a04SGreg Roach	 *
580*a25f0a04SGreg Roach	 * @param $gedcom_file
581*a25f0a04SGreg Roach	 *
582*a25f0a04SGreg Roach	 * @return bool
583*a25f0a04SGreg Roach	 */
584*a25f0a04SGreg Roach	public function exportGedcom($gedcom_file) {
585*a25f0a04SGreg Roach		require_once WT_ROOT . 'includes/functions/functions_export.php';
586*a25f0a04SGreg Roach
587*a25f0a04SGreg Roach		// To avoid partial trees on timeout/diskspace/etc, write to a temporary file first
588*a25f0a04SGreg Roach		$tmp_file = $gedcom_file . '.tmp';
589*a25f0a04SGreg Roach
590*a25f0a04SGreg Roach		$file_pointer = @fopen($tmp_file, 'w');
591*a25f0a04SGreg Roach		if ($file_pointer === false) {
592*a25f0a04SGreg Roach			return false;
593*a25f0a04SGreg Roach		}
594*a25f0a04SGreg Roach
595*a25f0a04SGreg Roach		$buffer = reformat_record_export(gedcom_header($this->name));
596*a25f0a04SGreg Roach
597*a25f0a04SGreg Roach		$stmt = Database::prepare(
598*a25f0a04SGreg Roach			"SELECT i_gedcom AS gedcom FROM `##individuals` WHERE i_file = ?" .
599*a25f0a04SGreg Roach			" UNION ALL " .
600*a25f0a04SGreg Roach			"SELECT f_gedcom AS gedcom FROM `##families`    WHERE f_file = ?" .
601*a25f0a04SGreg Roach			" UNION ALL " .
602*a25f0a04SGreg Roach			"SELECT s_gedcom AS gedcom FROM `##sources`     WHERE s_file = ?" .
603*a25f0a04SGreg Roach			" UNION ALL " .
604*a25f0a04SGreg Roach			"SELECT o_gedcom AS gedcom FROM `##other`       WHERE o_file = ? AND o_type NOT IN ('HEAD', 'TRLR')" .
605*a25f0a04SGreg Roach			" UNION ALL " .
606*a25f0a04SGreg Roach			"SELECT m_gedcom AS gedcom FROM `##media`       WHERE m_file = ?"
607*a25f0a04SGreg Roach		)->execute(array($this->id, $this->id, $this->id, $this->id, $this->id));
608*a25f0a04SGreg Roach
609*a25f0a04SGreg Roach		while ($row = $stmt->fetch()) {
610*a25f0a04SGreg Roach			$buffer .= reformat_record_export($row->gedcom);
611*a25f0a04SGreg Roach			if (strlen($buffer) > 65535) {
612*a25f0a04SGreg Roach				fwrite($file_pointer, $buffer);
613*a25f0a04SGreg Roach				$buffer = '';
614*a25f0a04SGreg Roach			}
615*a25f0a04SGreg Roach		}
616*a25f0a04SGreg Roach
617*a25f0a04SGreg Roach		fwrite($file_pointer, $buffer . '0 TRLR' . WT_EOL);
618*a25f0a04SGreg Roach		fclose($file_pointer);
619*a25f0a04SGreg Roach
620*a25f0a04SGreg Roach		return @rename($tmp_file, $gedcom_file);
621*a25f0a04SGreg Roach	}
622*a25f0a04SGreg Roach
623*a25f0a04SGreg Roach	/**
624*a25f0a04SGreg Roach	 * Import data from a gedcom file into this tree.
625*a25f0a04SGreg Roach	 *
626*a25f0a04SGreg Roach	 * @param string  $path       The full path to the (possibly temporary) file.
627*a25f0a04SGreg Roach	 * @param string  $filename   The preferred filename, for export/download.
628*a25f0a04SGreg Roach	 * @param boolean $keep_media Whether to retain any existing media records
629*a25f0a04SGreg Roach	 *
630*a25f0a04SGreg Roach	 * @throws \Exception
631*a25f0a04SGreg Roach	 */
632*a25f0a04SGreg Roach	public function importGedcomFile($path, $filename, $keep_media) {
633*a25f0a04SGreg Roach		// Read the file in blocks of roughly 64K.  Ensure that each block
634*a25f0a04SGreg Roach		// contains complete gedcom records.  This will ensure we don’t split
635*a25f0a04SGreg Roach		// multi-byte characters, as well as simplifying the code to import
636*a25f0a04SGreg Roach		// each block.
637*a25f0a04SGreg Roach
638*a25f0a04SGreg Roach		$file_data = '';
639*a25f0a04SGreg Roach		$fp = fopen($path, 'rb');
640*a25f0a04SGreg Roach
641*a25f0a04SGreg Roach		// Don’t allow the user to cancel the request.  We do not want to be left with an incomplete transaction.
642*a25f0a04SGreg Roach		ignore_user_abort(true);
643*a25f0a04SGreg Roach
644*a25f0a04SGreg Roach		Database::beginTransaction();
645*a25f0a04SGreg Roach		$this->deleteGenealogyData($keep_media);
646*a25f0a04SGreg Roach		$this->setPreference('gedcom_filename', $filename);
647*a25f0a04SGreg Roach		$this->setPreference('imported', '0');
648*a25f0a04SGreg Roach
649*a25f0a04SGreg Roach		while (!feof($fp)) {
650*a25f0a04SGreg Roach			$file_data .= fread($fp, 65536);
651*a25f0a04SGreg Roach			// There is no strrpos() function that searches for substrings :-(
652*a25f0a04SGreg Roach			for ($pos = strlen($file_data) - 1; $pos > 0; --$pos) {
653*a25f0a04SGreg Roach				if ($file_data[$pos] === '0' && ($file_data[$pos - 1] === "\n" || $file_data[$pos - 1] === "\r")) {
654*a25f0a04SGreg Roach					// We’ve found the last record boundary in this chunk of data
655*a25f0a04SGreg Roach					break;
656*a25f0a04SGreg Roach				}
657*a25f0a04SGreg Roach			}
658*a25f0a04SGreg Roach			if ($pos) {
659*a25f0a04SGreg Roach				Database::prepare(
660*a25f0a04SGreg Roach					"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
661*a25f0a04SGreg Roach				)->execute(array($this->id, substr($file_data, 0, $pos)));
662*a25f0a04SGreg Roach				$file_data = substr($file_data, $pos);
663*a25f0a04SGreg Roach			}
664*a25f0a04SGreg Roach		}
665*a25f0a04SGreg Roach		Database::prepare(
666*a25f0a04SGreg Roach			"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
667*a25f0a04SGreg Roach		)->execute(array($this->id, $file_data));
668*a25f0a04SGreg Roach
669*a25f0a04SGreg Roach		Database::commit();
670*a25f0a04SGreg Roach		fclose($fp);
671*a25f0a04SGreg Roach	}
672*a25f0a04SGreg Roach}
673