summaryrefslogtreecommitdiff
path: root/modules_v3/googlemap/admin_places.php
blob: 41fb9cd96c1ccf5b96f004169cb7a50341805083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
<?php
// Interface to edit place locations
//
// webtrees: Web based Family History software
// Copyright (C) 2014 webtrees development team.
//
// Derived from PhpGedView
// Copyright (C) 2002 to 2009 PGV Development Team. All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

if (!defined('WT_WEBTREES')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

require WT_ROOT.WT_MODULES_DIR.'googlemap/defaultconfig.php';
require WT_ROOT.'includes/functions/functions_edit.php';

$action       = WT_Filter::get('action');
$parent       = WT_Filter::get('parent');
$inactive     = WT_Filter::getBool('inactive');
$mode         = WT_Filter::get('mode');
$deleteRecord = WT_Filter::get('deleteRecord');

if (!isset($parent)) $parent=0;
if (!isset($inactive)) $inactive=false;

// Take a place id and find its place in the hierarchy
// Input: place ID
// Output: ordered array of id=>name values, starting with the Top Level
// e.g. array(0=>'Top Level', 16=>'England', 19=>'London', 217=>'Westminster');
// NB This function exists in both places.php and places_edit.php
function place_id_to_hierarchy($id) {
	$statement=
		WT_DB::prepare("SELECT pl_parent_id, pl_place FROM `##placelocation` WHERE pl_id=?");
	$arr=array();
	while ($id!=0) {
		$row=$statement->execute(array($id))->fetchOneRow();
		$arr=array($id=>$row->pl_place)+$arr;
		$id=$row->pl_parent_id;
	}
	return $arr;
}

// NB This function exists in both admin_places.php and places_edit.php
function getHighestIndex() {
	return (int)WT_DB::prepare("SELECT MAX(pl_id) FROM `##placelocation`")->fetchOne();
}

function getHighestLevel() {
	return (int)WT_DB::prepare("SELECT MAX(pl_level) FROM `##placelocation`")->fetchOne();
}

/**
 * Find all of the places in the hierarchy
 */
function get_place_list_loc($parent_id, $inactive=false) {
	if ($inactive) {
		$rows=
			WT_DB::prepare("SELECT pl_id, pl_place, pl_lati, pl_long, pl_zoom, pl_icon FROM `##placelocation` WHERE pl_parent_id=? ORDER BY pl_place COLLATE ".WT_I18N::$collation)
			->execute(array($parent_id))
			->fetchAll();
	} else {
		$rows=
			WT_DB::prepare(
				"SELECT DISTINCT pl_id, pl_place, pl_lati, pl_long, pl_zoom, pl_icon".
				" FROM `##placelocation`".
				" INNER JOIN `##places` ON `##placelocation`.pl_place=`##places`.p_place".
				" WHERE pl_parent_id=? ORDER BY pl_place COLLATE ".WT_I18N::$collation
			)
			->execute(array($parent_id))
			->fetchAll();
	}

	$placelist=array();
	foreach ($rows as $row) {
		$placelist[]=array('place_id'=>$row->pl_id, 'place'=>$row->pl_place, 'lati'=>$row->pl_lati, 'long'=>$row->pl_long, 'zoom'=>$row->pl_zoom, 'icon'=>$row->pl_icon);
	}
	return $placelist;
}

function outputLevel($parent_id) {
	$tmp = place_id_to_hierarchy($parent_id);
	$maxLevel = getHighestLevel();
	if ($maxLevel>8) $maxLevel = 8;
	$prefix = implode(';', $tmp);
	if ($prefix!='')
		$prefix.=';';
	$suffix=str_repeat(';', $maxLevel-count($tmp));
	$level=count($tmp);

	$rows=
		WT_DB::prepare("SELECT pl_id, pl_place, pl_long, pl_lati, pl_zoom, pl_icon FROM `##placelocation` WHERE pl_parent_id=? ORDER BY pl_place")
		->execute(array($parent_id))
		->fetchAll();

	foreach ($rows as $row) {
		echo $level,';',$prefix,$row->pl_place,$suffix,';',$row->pl_long,';',$row->pl_lati,';',$row->pl_zoom,';',$row->pl_icon,"\r\n";
		if ($level < $maxLevel) {
			outputLevel($row->pl_id);
		}
	}
}

/**
 * recursively find all of the csv files on the server
 *
 * @param string $path
 */
function findFiles($path) {
	global $placefiles;
	if (file_exists($path)) {
		$dir = dir($path);
		while (false !== ($entry = $dir->read())) {
			if ($entry!='.' && $entry!='..' && $entry!='.svn') {
				if (is_dir($path.'/'.$entry)) {
					findFiles($path.'/'.$entry);
				} elseif (strstr($entry, '.csv')!==false) {
					$placefiles[] = preg_replace('~'.WT_MODULES_DIR.'googlemap/extra~', '', $path).'/'.$entry;
				}
			}
		}
		$dir->close();
	}
}

$controller=new WT_Controller_Page();
$controller->requireAdminLogin();

if ($action=='ExportFile' && WT_USER_IS_ADMIN) {
	Zend_Session::writeClose();
	$tmp = place_id_to_hierarchy($parent);
	$maxLevel = getHighestLevel();
	if ($maxLevel>8) $maxLevel=8;
	$tmp[0] = 'places';
	$outputFileName=preg_replace('/[:;\/\\\(\)\{\}\[\] $]/', '_', implode('-', $tmp)).'.csv';
	header('Content-Type: application/octet-stream');
	header('Content-Disposition: attachment; filename="'.$outputFileName.'"');
	echo '"', WT_I18N::translate('Level'), '";"', WT_I18N::translate('Country'), '";';
	if ($maxLevel>0) echo '"', WT_I18N::translate('State'), '";';
	if ($maxLevel>1) echo '"', WT_I18N::translate('County'), '";';
	if ($maxLevel>2) echo '"', WT_I18N::translate('City'), '";';
	if ($maxLevel>3) echo '"', WT_I18N::translate('Place'), '";';
	if ($maxLevel>4) echo '"', WT_I18N::translate('Place'), '";';
	if ($maxLevel>5) echo '"', WT_I18N::translate('Place'), '";';
	if ($maxLevel>6) echo '"', WT_I18N::translate('Place'), '";';
	if ($maxLevel>7) echo '"', WT_I18N::translate('Place'), '";';
	echo '"', WT_I18N::translate('Longitude'), '";"', WT_I18N::translate('Latitude'), '";';
	echo '"', WT_I18N::translate('Zoom level'), '";"', WT_I18N::translate('Icon'), '";', WT_EOL;
	outputLevel($parent);
	exit;
}

$controller
	->setPageTitle(WT_I18N::translate('Google Maps™'))
	->pageHeader();

?>
<table id="gm_config">
	<tr>
		<th>
			<a href="module.php?mod=googlemap&amp;mod_action=admin_config">
				<?php echo WT_I18N::translate('Google Maps™ preferences'); ?>
			</a>
		</th>
		<th>
			<a class="current" href="module.php?mod=googlemap&amp;mod_action=admin_places">
				<?php echo WT_I18N::translate('Geographic data'); ?>
			</a>
		</th>
		<th>
			<a href="module.php?mod=googlemap&amp;mod_action=admin_placecheck">
				<?php echo WT_I18N::translate('Place Check'); ?>
			</a>
		</th>
	</tr>
</table>
<?php

if ($action=='ImportGedcom') {
	$placelist=array();
	$j=0;
	$statement=
		WT_DB::prepare("SELECT i_gedcom FROM `##individuals` WHERE i_file=? UNION ALL SELECT f_gedcom FROM `##families` WHERE f_file=?")
		->execute(array(WT_GED_ID, WT_GED_ID));
	while ($gedrec=$statement->fetchColumn()) {
		$i = 1;
		$placerec = get_sub_record(2, '2 PLAC', $gedrec, $i);
		while (!empty($placerec)) {
			if (preg_match("/2 PLAC (.+)/", $placerec, $match)) {
				$placelist[$j] = array();
				$placelist[$j]['place'] = trim($match[1]);
				if (preg_match("/4 LATI (.*)/", $placerec, $match)) {
					$placelist[$j]['lati'] = trim($match[1]);
					if (($placelist[$j]['lati'][0] != 'N') && ($placelist[$j]['lati'][0] != 'S')) {
						if ($placelist[$j]['lati'] < 0) {
							$placelist[$j]['lati'][0] = 'S';
						} else {
							$placelist[$j]['lati'] = 'N'.$placelist[$j]['lati'];
						}
					}
				}
				else $placelist[$j]['lati'] = NULL;
				if (preg_match("/4 LONG (.*)/", $placerec, $match)) {
					$placelist[$j]['long'] = trim($match[1]);
					if (($placelist[$j]['long'][0] != 'E') && ($placelist[$j]['long'][0] != 'W')) {
						if ($placelist[$j]['long'] < 0) {
							$placelist[$j]['long'][0] = 'W';
						} else {
							$placelist[$j]['long'] = 'E'.$placelist[$j]['long'];
						}
					}
				}
				else $placelist[$j]['long'] = NULL;
				$j = $j + 1;
			}
			$i = $i + 1;
			$placerec = get_sub_record(2, '2 PLAC', $gedrec, $i);
		}
	}
	asort($placelist);

	$prevPlace = '';
	$prevLati = '';
	$prevLong = '';
	$placelistUniq = array();
	$j = 0;
	foreach ($placelist as $k=>$place) {
		if ($place['place'] != $prevPlace) {
			$placelistUniq[$j] = array();
			$placelistUniq[$j]['place'] = $place['place'];
			$placelistUniq[$j]['lati'] = $place['lati'];
			$placelistUniq[$j]['long'] = $place['long'];
			$j = $j + 1;
		} else if (($place['place'] == $prevPlace) && (($place['lati'] != $prevLati) || ($place['long'] != $prevLong))) {
			if (($placelistUniq[$j-1]['lati'] == 0) || ($placelistUniq[$j-1]['long'] == 0)) {
				$placelistUniq[$j-1]['lati'] = $place['lati'];
				$placelistUniq[$j-1]['long'] = $place['long'];
			} else if (($place['lati'] != '0') || ($place['long'] != '0')) {
				echo 'Difference: previous value = ', $prevPlace, ', ', $prevLati, ', ', $prevLong, ' current = ', $place['place'], ', ', $place['lati'], ', ', $place['long'], '<br>';
			}
		}
		$prevPlace = $place['place'];
		$prevLati = $place['lati'];
		$prevLong = $place['long'];
	}

	$highestIndex = getHighestIndex();

	$default_zoom_level=array(4, 7, 10, 12);
	foreach ($placelistUniq as $k=>$place) {
        $parent=preg_split('/ *, */', $place['place']);
		$parent=array_reverse($parent);
		$parent_id=0;
		for ($i=0; $i<count($parent); $i++) {
			if (!isset($default_zoom_level[$i]))
				$default_zoom_level[$i]=$default_zoom_level[$i-1];
			$escparent=$parent[$i];
			if ($escparent == '') {
				$escparent = 'Unknown';
			}
			$row=
				WT_DB::prepare("SELECT pl_id, pl_long, pl_lati, pl_zoom FROM `##placelocation` WHERE pl_level=? AND pl_parent_id=? AND pl_place LIKE ?")
				->execute(array($i, $parent_id, $escparent))
				->fetchOneRow();
			if ($i < count($parent)-1) {
				// Create higher-level places, if necessary
				if (empty($row)) {
					$highestIndex++;
					WT_DB::prepare("INSERT INTO `##placelocation` (pl_id, pl_parent_id, pl_level, pl_place, pl_zoom) VALUES (?, ?, ?, ?, ?)")
						->execute(array($highestIndex, $parent_id, $i, $escparent, $default_zoom_level[$i]));
					echo WT_Filter::escapeHtml($escparent), '<br>';
					$parent_id=$highestIndex;
				} else {
					$parent_id=$row->pl_id;
				}
			} else {
				// Create lowest-level place, if necessary
				if (empty($row->pl_id)) {
					$highestIndex++;
					WT_DB::prepare("INSERT INTO `##placelocation` (pl_id, pl_parent_id, pl_level, pl_place, pl_long, pl_lati, pl_zoom) VALUES (?, ?, ?, ?, ?, ?, ?)")
						->execute(array($highestIndex, $parent_id, $i, $escparent, $place['long'], $place['lati'], $default_zoom_level[$i]));
					echo WT_Filter::escapeHtml($escparent), '<br>';
				} else {
					if (empty($row->pl_long) && empty($row->pl_lati) && $place['lati']!='0' && $place['long']!='0') {
						WT_DB::prepare("UPDATE `##placelocation` SET pl_lati=?, pl_long=? WHERE pl_id=?")
							->execute(array($place['lati'], $place['long'], $row->pl_id));
						echo WT_Filter::escapeHtml($escparent), '<br>';
					}
				}
			}
		}
	}
	$parent=0;
}

if ($action=='ImportFile') {
	$placefiles = array();
	findFiles(WT_MODULES_DIR.'googlemap/extra');
	sort($placefiles);
?>
<form method="post" enctype="multipart/form-data" id="importfile" name="importfile" action="module.php?mod=googlemap&amp;mod_action=admin_places&amp;action=ImportFile2">
	<table class="gm_plac_edit">
		<tr>
			<th><?php echo WT_I18N::translate('File containing places (CSV)'); ?></th>
			<td><input type="file" name="placesfile" size="50"></td>
		</tr>
		<?php if (count($placefiles)>0) { ?>
		<tr>
			<th><?php echo WT_I18N::translate('Server file containing places (CSV)'), help_link('PLIF_LOCALFILE','googlemap'); ?></th>
			<td>
				<select name="localfile">
					<option></option>
					<?php foreach ($placefiles as $p=>$placefile) { ?>
					<option value="<?php echo WT_Filter::escapeHtml($placefile); ?>"><?php
						if (substr($placefile, 0, 1)=="/") echo substr($placefile, 1);
						else echo $placefile; ?></option>
					<?php } ?>
				</select>
			</td>
		</tr>
		<?php } ?>
		<tr>
			<th><?php echo WT_I18N::translate('Delete all existing geographic data before importing the file.'); ?></th>
			<td><input type="checkbox" name="cleardatabase"></td>
		</tr>
		<tr>
			<th><?php echo WT_I18N::translate('Do not create new locations, just import coordinates for existing locations.'); ?></th>
			<td><input type="checkbox" name="updateonly"></td>
		</tr>
		<tr>
			<th><?php echo WT_I18N::translate('Overwrite existing coordinates.'); ?></th>
			<td><input type="checkbox" name="overwritedata"></td>
		</tr>
	</table>
	<input id="savebutton" type="submit" value="<?php echo WT_I18N::translate('Continue Adding'); ?>"><br>
</form>
<?php
	exit;
}

if ($action=='ImportFile2') {
	$country_names=array();
	foreach (WT_Stats::iso3166() as $key=>$value) {
		$country_names[$key]=WT_I18N::translate($key);
	}
	if (isset($_POST['cleardatabase'])) {
		WT_DB::exec("DELETE FROM `##placelocation` WHERE 1=1");
	}
	if (!empty($_FILES['placesfile']['tmp_name'])) {
		$lines = file($_FILES['placesfile']['tmp_name']);
	} elseif (!empty($_REQUEST['localfile'])) {
		$lines = file(WT_MODULES_DIR.'googlemap/extra'.$_REQUEST['localfile']);
	}
	// Strip BYTE-ORDER-MARK, if present
	if (!empty($lines[0]) && substr($lines[0], 0, 3)==WT_UTF8_BOM) $lines[0]=substr($lines[0], 3);
	asort($lines);
	$highestIndex = getHighestIndex();
	$placelist = array();
	$j = 0;
	$maxLevel = 0;
	foreach ($lines as $p => $placerec) {
		$fieldrec = explode(';', $placerec);
		if ($fieldrec[0] > $maxLevel) $maxLevel = $fieldrec[0];
	}
	$fields = count($fieldrec);
	$set_icon = true;
	if (!is_dir(WT_MODULES_DIR.'googlemap/places/flags/')) {
		$set_icon = false;
	}
	foreach ($lines as $p => $placerec) {
		$fieldrec = explode(';', $placerec);
		if (is_numeric($fieldrec[0]) && $fieldrec[0]<=$maxLevel) {
			$placelist[$j] = array();
			$placelist[$j]['place'] = '';
			for ($ii=$fields-4; $ii>1; $ii--) {
				if ($fieldrec[0] > $ii-2) $placelist[$j]['place'] .= $fieldrec[$ii].',';
			}
			foreach ($country_names as $countrycode => $countryname) {
				if ($countrycode == strtoupper($fieldrec[1])) {
					$fieldrec[1] = $countryname;
					break;
				}
			}
			$placelist[$j]['place'] .= $fieldrec[1];
			$placelist[$j]['long'] = $fieldrec[$fields-4];
			$placelist[$j]['lati'] = $fieldrec[$fields-3];
			$placelist[$j]['zoom'] = $fieldrec[$fields-2];
			if($set_icon) {
				$placelist[$j]['icon'] = trim($fieldrec[$fields-1]);
			} else {
				$placelist[$j]['icon'] = '';
			}
			$j = $j + 1;
		}
	}

	$prevPlace = '';
	$prevLati = '';
	$prevLong = '';
	$placelistUniq = array();
	$j = 0;
	foreach ($placelist as $k=>$place) {
		if ($place['place'] != $prevPlace) {
			$placelistUniq[$j] = array();
			$placelistUniq[$j]['place'] = $place['place'];
			$placelistUniq[$j]['lati'] = $place['lati'];
			$placelistUniq[$j]['long'] = $place['long'];
			$placelistUniq[$j]['zoom'] = $place['zoom'];
			$placelistUniq[$j]['icon'] = $place['icon'];
			$j = $j + 1;
		} else if (($place['place'] == $prevPlace) && (($place['lati'] != $prevLati) || ($place['long'] != $prevLong))) {
			if (($placelistUniq[$j-1]['lati'] == 0) || ($placelistUniq[$j-1]['long'] == 0)) {
				$placelistUniq[$j-1]['lati'] = $place['lati'];
				$placelistUniq[$j-1]['long'] = $place['long'];
				$placelistUniq[$j-1]['zoom'] = $place['zoom'];
				$placelistUniq[$j-1]['icon'] = $place['icon'];
			} else if (($place['lati'] != '0') || ($place['long'] != '0')) {
				echo 'Difference: previous value = ', $prevPlace, ', ', $prevLati, ', ', $prevLong, ' current = ', $place['place'], ', ', $place['lati'], ', ', $place['long'], '<br>';
			}
		}
		$prevPlace = $place['place'];
		$prevLati = $place['lati'];
		$prevLong = $place['long'];
	}

	$default_zoom_level = array();
	$default_zoom_level[0] = 4;
	$default_zoom_level[1] = 7;
	$default_zoom_level[2] = 10;
	$default_zoom_level[3] = 12;
	foreach ($placelistUniq as $k=>$place) {
		$parent = explode(',', $place['place']);
		$parent = array_reverse($parent);
		$parent_id=0;
		for ($i=0; $i<count($parent); $i++) {
			$escparent=$parent[$i];
			if ($escparent == '') {
				$escparent = 'Unknown';
			}
			$row=
				WT_DB::prepare("SELECT pl_id, pl_long, pl_lati, pl_zoom, pl_icon FROM `##placelocation` WHERE pl_level=? AND pl_parent_id=? AND pl_place LIKE ? ORDER BY pl_place")
				->execute(array($i, $parent_id, $escparent))
				->fetchOneRow();
			if (empty($row)) {       // this name does not yet exist: create entry
				if (!isset($_POST['updateonly'])) {
					$highestIndex = $highestIndex + 1;
					if (($i+1) == count($parent)) {
						$zoomlevel = $place['zoom'];
					} elseif (isset($default_zoom_level[$i])) {
						$zoomlevel = $default_zoom_level[$i];
					} else {
						$zoomlevel = $GOOGLEMAP_MAX_ZOOM;
					}
					if (($place['lati'] == '0') || ($place['long'] == '0') || (($i+1) < count($parent))) {
						WT_DB::prepare("INSERT INTO `##placelocation` (pl_id, pl_parent_id, pl_level, pl_place, pl_zoom, pl_icon) VALUES (?, ?, ?, ?, ?, ?)")
							->execute(array($highestIndex, $parent_id, $i, $escparent, $zoomlevel, $place['icon']));
					} else {
						//delete leading zero
						$pl_lati = str_replace(array('N', 'S', ','), array('', '-', '.') , $place['lati']);
						$pl_long = str_replace(array('E', 'W', ','), array('', '-', '.') , $place['long']);
						if ($pl_lati >= 0) {
							$place['lati'] = 'N'.abs($pl_lati);
						} elseif ($pl_lati < 0) {
							$place['lati'] = 'S'.abs($pl_lati);
						}
						if ($pl_long >= 0) {
							$place['long'] = 'E'.abs($pl_long);
						} elseif ($pl_long < 0) {
							$place['long'] = 'W'.abs($pl_long);
						}
						WT_DB::prepare("INSERT INTO `##placelocation` (pl_id, pl_parent_id, pl_level, pl_place, pl_long, pl_lati, pl_zoom, pl_icon) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
							->execute(array($highestIndex, $parent_id, $i, $escparent, $place['long'], $place['lati'], $zoomlevel, $place['icon']));
					}
					$parent_id = $highestIndex;
				}
			} else {
				$parent_id = $row->pl_id;
				if ((isset($_POST['overwritedata'])) && ($i+1 == count($parent))) {
					WT_DB::prepare("UPDATE `##placelocation` SET pl_lati=?, pl_long=?, pl_zoom=?, pl_icon=? WHERE pl_id=?")
						->execute(array($place['lati'], $place['long'], $place['zoom'], $place['icon'], $parent_id));
				} else {
					if ((($row->pl_long == '0') || ($row->pl_long == null)) && (($row->pl_lati == '0') || ($row->pl_lati == null))) {
						WT_DB::prepare("UPDATE `##placelocation` SET pl_lati=?, pl_long=? WHERE pl_id=?")
							->execute(array($place['lati'], $place['long'], $parent_id));
					}
					if (empty($row->pl_icon) && !empty($place['icon'])) {
						WT_DB::prepare("UPDATE `##placelocation` SET pl_icon=? WHERE pl_id=?")
							->execute(array($place['icon'], $parent_id));
					}
				}
			}
		}
	}
	$parent=0;
}

if ($action=='DeleteRecord') {
	$exists=
		WT_DB::prepare("SELECT 1 FROM `##placelocation` WHERE pl_parent_id=?")
		->execute(array($deleteRecord))
		->fetchOne();

	if (!$exists) {
		WT_DB::prepare("DELETE FROM `##placelocation` WHERE pl_id=?")
			->execute(array($deleteRecord));
	} else {
		echo '<table class="facts_table"><tr><td>', WT_I18N::translate('Location not removed: this location contains sub-locations'), '</td></tr></table>';
	}
}

?>
<script>
function updateList(inactive) {
	window.location.href='<?php if (strstrb($_SERVER['REQUEST_URI'], '&inactive')) { $uri=strstrb($_SERVER['REQUEST_URI'], '&inactive');} else { $uri=$_SERVER['REQUEST_URI']; } echo $uri, '&inactive='; ?>'+inactive;
}

function edit_place_location(placeid) {
	window.open('module.php?mod=googlemap&mod_action=places_edit&action=update&placeid='+placeid, '_blank', gmap_window_specs);
	return false;
}

function add_place_location(placeid) {
	window.open('module.php?mod=googlemap&mod_action=places_edit&action=add&placeid='+placeid, '_blank', gmap_window_specs);
	return false;
}

function delete_place(placeid) {
	var answer=confirm('<?php echo WT_I18N::translate('Remove this location?'); ?>');
	if (answer == true) {
		window.location = '<?php echo $_SERVER['REQUEST_URI']; ?>&action=DeleteRecord&deleteRecord=' + placeid;
	}
}
</script>
<?php
echo '<div id="gm_breadcrumb">';
$where_am_i=place_id_to_hierarchy($parent);
foreach (array_reverse($where_am_i, true) as $id=>$place) {
	if ($id==$parent) {
		if ($place != 'Unknown') {
			echo WT_Filter::escapeHtml($place);
		} else {
			echo WT_I18N::translate('unknown');
		}
	} else {
		echo '<a href="module.php?mod=googlemap&mod_action=admin_places&parent=', $id, '&inactive=', $inactive, '">';
		if ($place != 'Unknown') {
			echo WT_Filter::escapeHtml($place), '</a>';
		} else {
			echo WT_I18N::translate('unknown'), '</a>';
		}
	}
	echo ' - ';
}
echo '<a href="module.php?mod=googlemap&mod_action=admin_places&parent=0&inactive=', $inactive, '">', WT_I18N::translate('Top Level'), '</a></div>';
echo '<form name="active" method="post" action="module.php?mod=googlemap&mod_action=admin_places&parent=', $parent, '&inactive=', $inactive, '"><div id="gm_active">';
echo '<label for="inactive">', WT_I18N::translate('Show inactive places'), '</label>';
echo '<input type="checkbox" name="inactive" id="inactive"';
if ($inactive) echo ' checked="checked"';
echo ' onclick="updateList(this.checked)"';
echo '>',  help_link('PLE_ACTIVE','googlemap'), '</div></form>';

$placelist=get_place_list_loc($parent, $inactive);
echo '<div class="gm_plac_edit">';
echo '<table class="gm_plac_edit"><tr>';
echo '<th>', WT_Gedcom_Tag::getLabel('PLAC'), '</th>';
echo '<th>', WT_Gedcom_Tag::getLabel('LATI'), '</th>';
echo '<th>', WT_Gedcom_Tag::getLabel('LONG'), '</th>';
echo '<th>', WT_I18N::translate('Zoom level'), '</th>';
echo '<th>', WT_I18N::translate('Icon'), '</th>';
echo '<th>';
echo WT_I18N::translate('Edit'), '</th><th>', WT_I18N::translate('Delete'), '</th></tr>';
if (count($placelist) == 0)
	echo '<tr><td colspan="7" class="accepted">', WT_I18N::translate('No places found'), '</td></tr>';
foreach ($placelist as $place) {
	echo '<tr><td><a href="module.php?mod=googlemap&mod_action=admin_places&parent=', $place['place_id'], '&inactive=', $inactive, '">';
	if ($place['place'] != 'Unknown')
			echo WT_Filter::escapeHtml($place['place']), '</a></td>';
		else
			echo WT_I18N::translate('unknown'), '</a></td>';
	echo '<td>', $place['lati'], '</td>';
	echo '<td>', $place['long'], '</td>';
	echo '<td>', $place['zoom'], '</td>';
	echo '<td>';
	if (($place['icon'] == NULL) || ($place['icon'] == '')) {
		if (($place['lati'] == NULL) || ($place['long'] == NULL) || (($place['lati'] == '0') && ($place['long'] == '0'))) {
			echo '<img src="', WT_STATIC_URL, WT_MODULES_DIR, 'googlemap/images/mm_20_yellow.png">';
		}
		else {
			echo '<img src="', WT_STATIC_URL, WT_MODULES_DIR, 'googlemap/images/mm_20_red.png">';
		}
	} else {
		echo '<img src="', WT_STATIC_URL, WT_MODULES_DIR, 'googlemap/', $place['icon'], '" width="25" height="15">';
	}
	echo '</td>';
	echo '<td class="narrow"><a href="#" onclick="edit_place_location(', $place['place_id'], ');return false;" class="icon-edit" title="', WT_I18N::translate('Edit'), '"></a></td>';
	$noRows=
		WT_DB::prepare("SELECT COUNT(pl_id) FROM `##placelocation` WHERE pl_parent_id=?")
		->execute(array($place['place_id']))
		->fetchOne();
	if ($noRows==0) { ?>
		<td><a href="#" onclick="delete_place(<?php echo $place['place_id']?>);return false;" class="icon-delete" title="<?php echo WT_I18N::translate('Remove'); ?>"></a></td>
<?php       } else { ?>
		<td><i class="icon-delete-grey"></i></td>
<?php       } ?>
	</tr>
	<?php
}
?>
</table>
</div>

<table id="gm_manage">
	<tr>
		<td>
			<?php echo WT_I18N::translate('Add  a new geographic location'); ?>
		</td>
		<td>
			<form action="?" onsubmit="add_place_location(this.parent_id.options[this.parent_id.selectedIndex].value); return false;">
				<?php echo select_edit_control('parent_id', $where_am_i, WT_I18N::translate('Top Level'), $parent); ?>
				<input type="submit" value="<?php echo WT_I18N::translate('Add'); ?>">
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<?php echo WT_I18N::translate('Import all places from a family tree'); ?>
		</td>
		<td>
			<form action="module.php" method="get">
				<input type="hidden" name="mod" value="googlemap">
				<input type="hidden" name="mod_action" value="admin_places">
				<input type="hidden" name="action" value="ImportGedcom">
				<?php echo select_edit_control('ged', WT_Tree::getNameList(), null, WT_GEDCOM); ?>
				<input type="submit" value="<?php echo WT_I18N::translate('Import'); ?>">
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<?php echo WT_I18N::translate('Upload geographic data'); ?>
		</td>
		<td>
			<form action="module.php" method="get">
				<input type="hidden" name="mod" value="googlemap">
				<input type="hidden" name="mod_action" value="admin_places">
				<input type="hidden" name="action" value="ImportFile">
				<input type="submit" value="<?php echo WT_I18N::translate('Upload'); ?>">
			</form>
		</td>
	</tr>
	<tr>
		<td>
			<?php echo WT_I18N::translate('Download geographic data'); ?>
		</td>
		<td>
			<form action="module.php" method="get">
				<input type="hidden" name="mod" value="googlemap">
				<input type="hidden" name="mod_action" value="admin_places">
				<input type="hidden" name="action" value="ExportFile">
				<?php echo select_edit_control('parent', $where_am_i, WT_I18N::translate('All'), WT_GED_ID); ?>
				<input type="submit" value="<?php echo WT_I18N::translate('Download'); ?>">
			</form>
		</td>
	</tr>
</table>