1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* webtrees-lib: MyArtJaub library for webtrees |
4
|
|
|
* |
5
|
|
|
* @package MyArtJaub\Webtrees |
6
|
|
|
* @subpackage GeoDispersion |
7
|
|
|
* @author Jonathan Jaubart <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2009-2016, Jonathan Jaubart |
9
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3 |
10
|
|
|
*/ |
11
|
|
|
namespace MyArtJaub\Webtrees\Module\GeoDispersion\Model; |
12
|
|
|
|
13
|
|
|
use MyArtJaub\Webtrees\Constants; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Describe an Outline Map |
17
|
|
|
*/ |
18
|
|
|
class OutlineMap { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Name of the file containing the description of the map. |
22
|
|
|
* @var string $filename |
23
|
|
|
*/ |
24
|
|
|
protected $filename; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Indicates whether the description has been loaded from the file. |
28
|
|
|
* @var bool $is_loaded |
29
|
|
|
*/ |
30
|
|
|
protected $is_loaded; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Description/title of the map. |
34
|
|
|
* @var string $description |
35
|
|
|
*/ |
36
|
|
|
protected $description; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Name(s) of the parent level(s) of the map. |
40
|
|
|
* @var string $is_loaded |
41
|
|
|
*/ |
42
|
|
|
protected $top_level_name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Map canvas |
46
|
|
|
* @var OutlineMapCanvas $canvas |
47
|
|
|
*/ |
48
|
|
|
protected $canvas; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Map subdivisions |
52
|
|
|
* @var array $subdivisions |
53
|
|
|
*/ |
54
|
|
|
protected $subdivisions; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Places mappings |
58
|
|
|
* @var array $subdivisions |
59
|
|
|
*/ |
60
|
|
|
protected $mappings; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor for GeoAnalysisMap. |
64
|
|
|
* |
65
|
|
|
* @param string $filename Outline map file name |
66
|
|
|
* @param bool $load Should the map be loaded immediately |
67
|
|
|
*/ |
68
|
|
|
public function __construct($filename, $load = false) { |
69
|
|
|
$this->filename = $filename; |
70
|
|
|
$this->is_loaded = false; |
71
|
|
|
$this->subdivisions = array(); |
72
|
|
|
$this->mappings = array(); |
73
|
|
|
if($load) $this->load(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Load the map settings contained within its XML representation |
78
|
|
|
* |
79
|
|
|
* XML structure : |
80
|
|
|
* - displayName : Display name of the map |
81
|
|
|
* - topLevel : Values of the top level subdivisions (separated by commas, if multiple) |
82
|
|
|
* - canvas : all settings related to the map canvas. |
83
|
|
|
* - width : canvas width, in px |
84
|
|
|
* - height : canvas height, in px |
85
|
|
|
* - maxcolor : color to identify places with ancestors, RGB hexadecimal |
86
|
|
|
* - hovercolor : same as previous, color when mouse is hovering the place, RGB hexadecimal |
87
|
|
|
* - bgcolor : map background color, RGB hexadecimal |
88
|
|
|
* - bgstroke : map stroke color, RGB hexadecimal |
89
|
|
|
* - defaultcolor : default color of places, RGB hexadecimal |
90
|
|
|
* - defaultstroke : default stroke color, RGB hexadecimal |
91
|
|
|
* - subdvisions : for each subdivision : |
92
|
|
|
* - id : Subdivision id, must be compatible with PHP variable constraints, and unique |
93
|
|
|
* - name: Display name for the place |
94
|
|
|
* - parent: if any, describe to which parent level the place if belonging to |
95
|
|
|
* - <em>Element value<em> : SVG description of the subdvision shape |
96
|
|
|
* - mapping : for each subdivision : |
97
|
|
|
* - name : Name of the place to map |
98
|
|
|
* - mapto: Name of the place to map to |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
protected function load() { |
102
|
|
|
if(file_exists(WT_ROOT.WT_MODULES_DIR.Constants::MODULE_MAJ_GEODISP_NAME.'/maps/'.$this->filename)){ |
103
|
|
|
$xml = simplexml_load_file(WT_ROOT.WT_MODULES_DIR.Constants::MODULE_MAJ_GEODISP_NAME.'/maps/'.$this->filename); |
104
|
|
|
if($xml){ |
105
|
|
|
$this->description = trim($xml->displayName); |
106
|
|
|
$this->top_level_name = trim($xml->topLevel); |
107
|
|
|
$this->canvas = new OutlineMapCanvas( |
108
|
|
|
trim($xml->canvas->width), |
109
|
|
|
trim($xml->canvas->height), |
110
|
|
|
trim($xml->canvas->maxcolor), |
111
|
|
|
trim($xml->canvas->hovercolor), |
112
|
|
|
trim($xml->canvas->bgcolor), |
113
|
|
|
trim($xml->canvas->bgstroke), |
114
|
|
|
trim($xml->canvas->defaultcolor), |
115
|
|
|
trim($xml->canvas->defaultstroke) |
116
|
|
|
); |
117
|
|
|
foreach($xml->subdivisions->children() as $subdivision){ |
118
|
|
|
$attributes = $subdivision->attributes(); |
119
|
|
|
$key = trim($attributes['name']); |
120
|
|
|
if(isset($attributes['parent'])) $key .= '@'.trim($attributes['parent']); |
121
|
|
|
$this->subdivisions[$key] = array( |
122
|
|
|
'id' => trim($attributes['id']), |
123
|
|
|
'displayname' => trim($attributes['name']), |
124
|
|
|
'coord' => trim($subdivision[0]) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
if(isset($xml->mappings)) { |
128
|
|
|
foreach($xml->mappings->children() as $mappings){ |
129
|
|
|
$attributes = $mappings->attributes(); |
130
|
|
|
$this->mappings[trim($attributes['name'])] = trim($attributes['mapto']); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
$this->is_loaded = true; |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
throw new \Exception('The Outline Map could not be loaded from XML.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the status of the map loading from the XML file. |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function isLoaded() { |
146
|
|
|
try{ |
147
|
|
|
if(!$this->is_loaded) $this->load(); |
148
|
|
|
} |
149
|
|
|
catch (\Exception $ex) { } |
|
|
|
|
150
|
|
|
return $this->is_loaded; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the map file name. |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getFileName() { |
158
|
|
|
return $this->filename; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the map file name. |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getDescription() { |
166
|
|
|
if(!$this->is_loaded) $this->load(); |
167
|
|
|
return $this->description; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the name of the map parent level. |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getTopLevelName() { |
175
|
|
|
if(!$this->is_loaded) $this->load(); |
176
|
|
|
return $this->top_level_name; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the Outline Map canvas. |
181
|
|
|
* @return \MyArtJaub\Webtrees\Module\GeoDispersion\Model\OutlineMapCanvas |
182
|
|
|
*/ |
183
|
|
|
public function getCanvas() { |
184
|
|
|
if(!$this->is_loaded) $this->load(); |
185
|
|
|
return $this->canvas; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the subdivisions of the map. |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function getSubdivisions() { |
193
|
|
|
if(!$this->is_loaded) $this->load(); |
194
|
|
|
return $this->subdivisions; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Get the places mappings of the map. |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getPlacesMappings() { |
202
|
|
|
if(!$this->is_loaded) $this->load(); |
203
|
|
|
return $this->mappings; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|