forked from librenms-plugins/Weathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTML_ImageMap.class.php
427 lines (367 loc) · 9.86 KB
/
HTML_ImageMap.class.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
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
<?php
// Copyright Howard Jones, 2005-2020 [email protected]
// http://wotsit.thingy.com/haj/cacti/
// Released under the MIT License
// A simple port of the guts of Apache's mod_imap
// - if you have an image control in a form, it's not really defined what happens to USEMAP
// attributes. They are allowed in HTML 4.0 and XHTML, but some testing shows that they're
// basically ignored. So you need to use server-side imagemaps if you want to have a form
// where you are choosing a verb from (for example) a <SELECT> and also specifying part of
// an image with an IMAGE control.
//
//
class HTML_ImageMap_Area
{
var $href;
var $name;
var $id;
var $alt;
var $z;
var $extrahtml;
function common_html()
{
$h = "";
if($this->name != "")
{
// $h .= " alt=\"".$this->name."\" ";
$h .= "id=\"".$this->name."\" ";
}
if($this->href != "")
{
$h .= "href=\"".$this->href."\" ";
}
else { $h .= "nohref "; }
if($this->extrahtml != "")
{
$h .= $this->extrahtml." ";
}
return $h;
}
}
class HTML_ImageMap_Area_Polygon extends HTML_ImageMap_Area
{
var $points = array();
var $minx,$maxx,$miny,$maxy; // bounding box
var $npoints;
function asHTML()
{
foreach ($this->points as $point)
{
$flatpoints[] = $point[0];
$flatpoints[] = $point[1];
}
$coordstring = join(",",$flatpoints);
return '<area '.$this->common_html().'shape="poly" coords="'.$coordstring.'" />';
}
function asJSON()
{
$json = "{ \"shape\":'poly', \"npoints\":".$this->npoints.", \"name\":'".$this->name."',";
$xlist = '';
$ylist = '';
foreach ($this->points as $point)
{
$xlist .= $point[0].",";
$ylist .= $point[1].",";
}
$xlist = rtrim($xlist,", ");
$ylist = rtrim($ylist,", ");
$json .= " \"x\": [ $xlist ], \"y\":[ $ylist ], \"minx\": ".$this->minx.", \"miny\": ".$this->miny.", \"maxx\":".$this->maxx.", \"maxy\":".$this->maxy."}";
return($json);
}
function hitTest($x,$y)
{
$c = 0;
// do the easy bounding-box test first.
if( ($x < $this->minx) || ($x>$this->maxx) || ($y<$this->miny) || ($y>$this->maxy))
{
return false;
}
// Algotithm from from
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html#The%20C%20Code
for ($i = 0, $j = $this->npoints-1; $i < $this->npoints; $j = $i++)
{
// print "Checking: $i, $j\n";
$x1 = $this->points[$i][0];
$y1 = $this->points[$i][1];
$x2 = $this->points[$j][0];
$y2 = $this->points[$j][1];
// print "($x,$y) vs ($x1,$y1)-($x2,$y2)\n";
if (((($y1<=$y) && ($y<$y2)) || (($y2<=$y) && ($y<$y1))) &&
($x < ($x2 - $x1) * ($y - $y1) / ($y2 - $y1) + $x1))
{
$c = !$c;
}
}
return ($c);
}
function __construct ( $name="", $href="",$coords)
{
$c = $coords[0];
$this->name = $name;
$this->href= $href;
$this->npoints = count($c)/2;
if( intval($this->npoints) != ($this->npoints))
{
die("Odd number of points!");
}
for ($i=0; $i<count($c); $i+=2)
{
$x = round($c[$i]);
$y = round($c[$i+1]);
$point = array($x,$y);
$xlist[] = $x; // these two are used to get the bounding box in a moment
$ylist[] = $y;
$this->points[] = $point;
}
$this->minx = min($xlist);
$this->maxx = max($xlist);
$this->miny = min($ylist);
$this->maxy = max($ylist);
// print $this->asHTML()."\n";
}
}
class HTML_ImageMap_Area_Rectangle extends HTML_ImageMap_Area
{
var $x1,$x2,$y1,$y2;
function __construct ( $name="", $href="",$coords)
{
$c = $coords[0];
$x1 = round($c[0]);
$y1 = round($c[1]);
$x2 = round($c[2]);
$y2 = round($c[3]);
// sort the points, so that the first is the top-left
if($x1>$x2)
{
$this->x1=$x2;
$this->x2=$x1;
}
else
{
$this->x1=$x1;
$this->x2=$x2;
}
if($y1>$y2)
{
$this->y1=$y2;
$this->y2=$y1;
}
else
{
$this->y1=$y1;
$this->y2=$y2;
}
$this->name = $name;
$this->href = $href;
}
function hitTest($x,$y)
{
return ( ($x > $this->x1) && ($x < $this->x2) && ($y > $this->y1) && ($y < $this->y2) );
}
function asHTML()
{
$coordstring = join(",",array($this->x1,$this->y1,$this->x2,$this->y2));
return '<area '.$this->common_html().'shape="rect" coords="'.$coordstring.'" />';
}
function asJSON()
{
$json = "{ \"shape\":'rect', ";
$json .= " \"x1\":".$this->x1.", \"y1\":".$this->y1.", \"x2\":".$this->x2.", \"y2\":".$this->y2.", \"name\":'".$this->name."'}";
return($json);
}
}
class HTML_ImageMap_Area_Circle extends HTML_ImageMap_Area
{
var $centx,$centy, $edgex, $edgey;
function asHTML()
{
$coordstring = join(",",array($this->centx,$this->centy,$this->edgex,$this->edgey) );
return '<area '.$this->common_html().'shape="circle" coords="'.$coordstring.'" />';
}
function hitTest($x,$y)
{
$radius1 = ($this->edgey - $this->centy) * ($this->edgey - $this->centy)
+ ($this->edgex - $this->centx) * ($this->edgex - $this->centx);
$radius2 = ($this->centy - $y) * ($this->centy - $y)
+ ($this->centx - $x) * ($this->centx - $x);
return ($radius2 <= $radius1);
}
function __construct($name="", $href="",$coords)
{
$c = $coords[0];
$this->name = $name;
$this->href = $href;
$this->centx = round($c[0]);
$this->centy = round($c[1]);
$this->edgex = round($c[2]);
$this->edgey = round($c[3]);
}
}
class HTML_ImageMap
{
var $shapes;
var $nshapes;
var $name;
function __construct($name="")
{
$this->Reset();
$this->name = $name;
}
function Reset()
{
$this->shapes = array();
$this->nshapes = 0;
$this->name = "";
}
// add an element to the map - takes an array with the info, in a similar way to HTML_QuickForm
function addArea($element)
{
if (is_object($element) && is_subclass_of($element, 'html_imagemap_area')) {
$elementObject = &$element;
} else {
$args = func_get_args();
$className = "HTML_ImageMap_Area_".$element;
$elementObject = new $className($args[1],$args[2],array_slice($args, 3));
}
$this->shapes[] =& $elementObject;
$this->nshapes++;
// print $this->nshapes." shapes\n";
}
// do a hit-test based on the current map
// - can be limited to only match elements whose names match the filter
// (e.g. pick a building, in a campus map)
function hitTest($x,$y,$namefilter="")
{
$preg = '/'.$namefilter.'/';
foreach ($this->shapes as $shape)
{
if($shape->hitTest($x,$y))
{
if( ($namefilter == "") || ( preg_match($preg,$shape->name) ) )
{
return $shape->name;
}
}
}
return false;
}
// update a property on all elements in the map that match a name
// (use it for retro-actively adding in link information to a pre-built geometry before generating HTML)
// returns the number of elements that were matched/changed
function setProp($which, $what, $where)
{
$count = 0;
for($i=0; $i<count($this->shapes); $i++)
{
// this USED to be a substring match, but that broke some things
// and wasn't actually used as one anywhere.
if( ($where == "") || ( $this->shapes[$i]->name==$where) )
{
switch($which)
{
case 'href':
$this->shapes[$i]->href= $what;
break;
case 'extrahtml':
$this->shapes[$i]->extrahtml= $what;
#print "IMAGEMAP: Found $where and adding $which\n";
break;
}
$count++;
}
}
return $count;
}
// update a property on all elements in the map that match a name as a substring
// (use it for retro-actively adding in link information to a pre-built geometry before generating HTML)
// returns the number of elements that were matched/changed
function setPropSub($which, $what, $where)
{
$count = 0;
for($i=0; $i<count($this->shapes); $i++)
{
if( ($where == "") || ( strstr($this->shapes[$i]->name,$where)!=FALSE ) )
{
switch($which)
{
case 'href':
$this->shapes[$i]->href= $what;
break;
case 'extrahtml':
$this->shapes[$i]->extrahtml= $what;
break;
}
$count++;
}
}
return $count;
}
// Return the imagemap as an HTML client-side imagemap for inclusion in a page
function asHTML()
{
$html = '<map';
if($this->name != "")
{
$html .= ' name="'.$this->name.'"';
}
$html .=">\n";
foreach ($this->shapes as $shape)
{
$html .= $shape->asHTML()."\n";
$html .= "\n";
}
$html .= "</map>\n";
return $html;
}
function subJSON($namefilter="",$reverseorder=false)
{
$json = '';
$preg = '/'.$namefilter.'/';
foreach ($this->shapes as $shape)
{
if( ($namefilter == "") || ( preg_match($preg,$shape->name) ))
{
if($reverseorder)
{
$json = $shape->asJSON().",\n".$json;
}
else
{
$json .= $shape->asJSON().",\n";
}
}
}
$json = rtrim($json,"\n, ");
$json .= "\n";
return $json;
}
// return HTML for a subset of the map, specified by the filter string
// (suppose you want some partof your UI to have precedence over another part
// - the imagemap is checked from top-to-bottom in the HTML)
// - skipnolinks -> in normal HTML output, we don't need areas for things with no href
function subHTML($namefilter="",$reverseorder=false, $skipnolinks=false)
{
$html = "";
$preg = '/'.$namefilter.'/';
foreach ($this->shapes as $shape)
{
# if( ($namefilter == "") || ( preg_match($preg,$shape->name) ))
if( ($namefilter == "") || ( strstr($shape->name, $namefilter) !== FALSE ))
{
if(!$skipnolinks || $shape->href != "" || $shape->extrahtml != "" )
{
if($reverseorder)
{
$html = $shape->asHTML()."\n".$html;
}
else
{
$html .= $shape->asHTML()."\n";
}
}
}
}
return $html;
}
}
// vim:ts=4:sw=4: