-
Notifications
You must be signed in to change notification settings - Fork 4
/
module.php
286 lines (219 loc) · 8.26 KB
/
module.php
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
<?php
namespace vendor\WebtreesModules\OpenStreetMapModule;
use Composer\Autoload\ClassLoader;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleTabInterface;
class OpenStreetMapModule extends AbstractModule implements ModuleTabInterface {
var $directory;
public function __construct()
{
parent::__construct('OpenStreetMapModule');
$this->directory = WT_MODULES_DIR . $this->getName();
$this->action = Filter::get('mod_action');
// register the namespaces
$loader = new ClassLoader();
$loader->addPsr4('vendor\\WebtreesModules\\OpenStreetMapModule\\', $this->directory);
$loader->register();
}
// Extend AbstractModule. Unique internal name for this module. Must match the directory name
public function getName() {
return "openstreetmap";
}
// Extend AbstractModule. This title should be normalized when this module will be added officially
public function getTitle() {
return /* I18N: Name of a module */ I18N::translate('OpenStreetMap');
}
// Extend AbstractModule
public function getDescription() {
return /* I18N: Description of the “OSM” module */ I18N::translate('Show the location of places and events using OpenStreetMap (OSM)');
}
// Extend AbstractModule
public function defaultAccessLevel() {
# Auth::PRIV_PRIVATE actually means public.
# Auth::PRIV_NONE - no acces to anybody.
return Auth::PRIV_PRIVATE;
}
// Implement ModuleTabInterface
public function defaultTabOrder() {
return 81;
}
// Implement ModuleTabInterface
public function getTabContent() {
global $controller;
$this->individual_map();
}
// Implement ModuleTabInterface
public function hasTabContent() {
global $SEARCH_SPIDER;
return !$SEARCH_SPIDER;
}
// Implement ModuleTabInterface
public function isGrayedOut() {
return false;
}
// Implement ModuleTabInterface
public function canLoadAjax() {
return true;
}
// Implement ModuleTabInterface
public function getPreLoadContent() {
}
// Extend AbstractModule
// Here, we define the actions available for the module
public function modAction($mod_action) {
switch($mod_action){
case 'pedigree_map':
$this->pedigree_map();
}
}
private function pedigree_map() {
global $controller;
$controller = new WT_Controller_Pedigree();
$this->includes($controller);
$this->drawMap();
}
private function individual_map() {
global $controller;
$this->includes($controller);
## This still needs some work. We'll probably want to copy this directly
## from googlemaps
list($events, $geodata) = $this->getEvents();
// If no places, display message and quit
if (!$geodata) {
echo "No map data for this person." . "\n";
return;
}
$this->drawMap($events);
}
private function getEvents() {
global $controller;
$events = array(); # Array of indivuals/events
$geodata = false; # Boolean indicating if we have any geo-tagged data
$thisPerson = $controller->record;
### Get all people that we want events for ###
$people = array();
array_push($people, $thisPerson); # Self
foreach($thisPerson->getChildFamilies() as $family) {
# Parents
foreach($family->getSpouses() as $parent) {
array_push($people, $parent);
}
# Siblings
foreach($family->getChildren() as $child) {
if ( $child !== $thisPerson) {
array_push($people, $child);
}
}
}
foreach($thisPerson->getSpouseFamilies() as $family) {
# Spouse
foreach($family->getSpouses() as $spouse) {
if ( $spouse !== $thisPerson) {
array_push($people, $spouse);
}
}
# Children
foreach($family->getChildren() as $child) {
array_push($people, $child);
}
}
# Map each person to their facts
foreach($people as $person) {
$xref = $person->getXref();
$events[$xref] = array();
foreach($person->getFacts() as $fact) {
$placefact = new \FactPlace($fact);
array_push($events[$xref], $placefact);
if ($placefact->knownLatLon()) $geodata = true;
}
// sort facts by date
usort($events[$xref], array('FactPlace','CompareDate'));
}
return array($events,$geodata);
}
private function includes($controller) {
// Leaflet JS
echo '<script src="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/js/leaflet/leaflet.js"></script>';
// Leaflet CSS
echo '<link type="text/css" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/css/leaflet.css" rel="stylesheet">';
echo '<link type="text/css" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/css/osm-module.css" rel="stylesheet">';
// Leaflet markercluster
echo '<link type="text/css" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/css/MarkerCluster.Default.css" rel="stylesheet">';
echo '<link type="text/css" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/css/MarkerCluster.css" rel="stylesheet">';
echo '<script src="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/js/leaflet/leaflet.markercluster.js"></script>';
// Leaflet Fontawesome markers
echo '<link rel="stylesheet" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/font-awesome-4.3.0/css/font-awesome.min.css">';
echo '<link rel="stylesheet" href="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/css/Leaflet.vector-markers.css">';
echo '<script src="', WT_STATIC_URL, WT_MODULES_DIR, 'openstreetmap/js/leaflet/Leaflet.vector-markers.min.js"></script>';
require_once $this->directory.'/classes/FactPlace.php';
}
private function drawMap($eventsMap) {
$attributionOsmString = 'Map data © <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors';
$attributionMapBoxString = 'Map data © <a href=\"http://openstreetmap.org\">OpenStreetMap</a> contributors, <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-BY-SA</a>, Imagery © <a href=\"http://mapbox.com\">Mapbox</a>';
echo '<div id=map>';
echo '</div>';
echo "<script>
var osm = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '$attributionOsmString',
maxZoom: 18
});
var mapbox = L.tileLayer('//{s}.tiles.mapbox.com/v3/oddityoverseer13.ino7n4nl/{z}/{x}/{y}.png', {
attribution: '$attributionMapBoxString',
maxZoom: 18
});
var map = L.map('map').fitWorld().setZoom(2);
osm.addTo(map);
var baseLayers = {
'Mapbox': mapbox,
'OpenStreetMap': osm
};
L.control.layers(baseLayers).addTo(map);
";
// Set up markercluster
echo "var markers = new L.MarkerClusterGroup();" . "\n";
$colors = array('#0C448C', '#D60500', '#009E30', '#D6A000', '#008C65', '#AB0061', '#5B088F', '#D66500');
$event_options_map = array(
'BIRT' => array('icon' => 'birthday-cake'),
'RESI' => array('icon' => 'home'),
'CENS' => array('icon' => 'users'),
'GRAD' => array('icon' => 'graduation-cap'),
'OCCU' => array('icon' => 'briefcase')
);
$color_i = 0;
// Populate the leaflet map with markers
foreach($eventsMap as $xref => $personEvents) {
// Set up polyline
echo "var polyline = L.polyline([], {color: '" . $colors[$color_i] . "'});" . "\n";
usort($personEvents, array('FactPlace','CompareDate'));
foreach($personEvents as $event) {
if ($event->knownLatLon()) {
$tag = $event->fact->getTag();
$options = array_key_exists($tag,$event_options_map) ? $event_options_map[$tag] : array('icon' => 'circle');
$options['markerColor'] = $colors[$color_i];
echo "var icon = L.VectorMarkers.icon(".json_encode($options).");";
echo "var marker = L.marker(".$event->getLatLonJSArray().", {icon: icon});" . "\n";
echo "marker.bindPopup('".$event->shortSummary()."');" . "\n";
// Add to markercluster
echo "markers.addLayer(marker);" . "\n";
if ($event->fact->getDate()->isOk()) {
// Append it to the polyline
echo "polyline.addLatLng(".$event->getLatLonJSArray().");" . "\n";
}
}
// Add polyline to map
echo "polyline.addTo(map);" . "\n";
}
$color_i = ($color_i + 1) % count($colors);
}
// Add markercluster to map
echo "var l = map.addLayer(markers);" . "\n";
// Zoom to bounds of polyline
echo "map.fitBounds(markers.getBounds());" . "\n";
echo "map.invalidateSize();" . "\n";
echo '</script>';
}
}
return new OpenStreetMapModule();