1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # |
---|
4 | # This file is part of myGmaps, a plugin for Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2010 Philippe aka amalgame |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | $core->addBehavior('publicHeadContent',array('myGmapsPublic','publicHeadContent')); |
---|
14 | $core->addBehavior('publicEntryAfterContent',array('myGmapsPublic','publicMapContent')); |
---|
15 | $core->addBehavior('publicPageAfterContent',array('myGmapsPublic','publicMapContent')); |
---|
16 | |
---|
17 | class myGmapsPublic |
---|
18 | { |
---|
19 | public static function thisPostMap ($post_id) |
---|
20 | { |
---|
21 | global $core; |
---|
22 | $meta =& $core->meta; |
---|
23 | $my_params['post_id'] = $post_id; |
---|
24 | $my_params['no_content'] = true; |
---|
25 | $my_params['post_type'] = array('post','page'); |
---|
26 | |
---|
27 | $rs = $core->blog->getPosts($my_params); |
---|
28 | return $meta->getMetaStr($rs->post_meta,'map'); |
---|
29 | } |
---|
30 | public static function publicHeadContent($core,$_ctx) |
---|
31 | { |
---|
32 | echo |
---|
33 | '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>'."\n". |
---|
34 | '<style type="text/css">'."\n". |
---|
35 | 'html,body { height: 100% }'."\n". |
---|
36 | '.map_canvas { width:100% ;height: 400px; }'."\n". |
---|
37 | '.map_canvas * { color:black }'."\n". |
---|
38 | '.map_canvas a { color:blue;text-decoration:underline }'."\n". |
---|
39 | '</style>'."\n"; |
---|
40 | } |
---|
41 | public static function publicMapContent($core,$_ctx) |
---|
42 | { |
---|
43 | # Settings |
---|
44 | |
---|
45 | $s =& $core->blog->settings->myGmaps; |
---|
46 | $url = $core->blog->getQmarkURL().'pf='.basename(dirname(__FILE__)); |
---|
47 | |
---|
48 | if (self::thisPostMap($_ctx->posts->post_id) != '') { |
---|
49 | |
---|
50 | echo |
---|
51 | '<script type="text/javascript">'."\n". |
---|
52 | "//<![CDATA[\n". |
---|
53 | '$(function () {'."\n"; |
---|
54 | |
---|
55 | |
---|
56 | $meta =& $GLOBALS['core']->meta; |
---|
57 | $post_map_options = explode(",",$meta->getMetaStr($_ctx->posts->post_meta,'map_options')); |
---|
58 | if ($post_map_options[3] == 'roadmap') { |
---|
59 | $mapTypeId = 'google.maps.MapTypeId.ROADMAP'; |
---|
60 | } elseif ($post_map_options[3] == 'satellite') { |
---|
61 | $mapTypeId = 'google.maps.MapTypeId.SATELLITE'; |
---|
62 | } elseif ($post_map_options[3] == 'hybrid') { |
---|
63 | $mapTypeId = 'google.maps.MapTypeId.HYBRID'; |
---|
64 | } elseif ($post_map_options[3] == 'terrain') { |
---|
65 | $mapTypeId = 'google.maps.MapTypeId.TERRAIN'; |
---|
66 | } |
---|
67 | echo |
---|
68 | 'var myOptions = {'."\n". |
---|
69 | 'zoom: parseFloat('.$post_map_options[2].'),'."\n". |
---|
70 | 'center: new google.maps.LatLng('.$post_map_options[0].','.$post_map_options[1].'),'."\n". |
---|
71 | 'scrollwheel: false,'."\n". |
---|
72 | 'mapTypeId: '.$mapTypeId."\n". |
---|
73 | '};'."\n". |
---|
74 | 'var map_'.$_ctx->posts->post_id.' = new google.maps.Map(document.getElementById("map_canvas_'.$_ctx->posts->post_id.'"), myOptions);'."\n". |
---|
75 | 'var infowindow_'.$_ctx->posts->post_id.' = new google.maps.InfoWindow({});'."\n". |
---|
76 | |
---|
77 | 'google.maps.event.addListener(map_'.$_ctx->posts->post_id.', "click", function (event) {'."\n". |
---|
78 | 'infowindow_'.$_ctx->posts->post_id.'.close();'."\n". |
---|
79 | '});'."\n"; |
---|
80 | |
---|
81 | |
---|
82 | $maps_array = explode(",",self::thisPostMap($_ctx->posts->post_id)); |
---|
83 | |
---|
84 | $params['post_type'] = 'map'; |
---|
85 | $params['post_status'] = '1'; |
---|
86 | $maps = $core->blog->getPosts($params); |
---|
87 | |
---|
88 | while ($maps->fetch()) { |
---|
89 | if (in_array($maps->post_id,$maps_array)) { |
---|
90 | $list = explode("\n",html::clean($maps->post_excerpt_xhtml)); |
---|
91 | $content = str_replace("\\", "\\\\", $maps->post_content_xhtml); |
---|
92 | $content = str_replace(array("\r\n", "\n", "\r"),"\\n",$content); |
---|
93 | $content = str_replace(array("'"),"\'",$content); |
---|
94 | if (sizeof($list) == 1) { |
---|
95 | $marker = explode("|",$list[0]); |
---|
96 | if (sizeof($marker) == 1) { |
---|
97 | $layer = $marker[0]; |
---|
98 | echo |
---|
99 | 'layer = new google.maps.KmlLayer("'.$layer.'", {'."\n". |
---|
100 | 'preserveViewport: true'."\n". |
---|
101 | '});'."\n". |
---|
102 | 'layer.setMap(map_'.$_ctx->posts->post_id.');'."\n"; |
---|
103 | } else { |
---|
104 | |
---|
105 | echo |
---|
106 | 'var title_'.$maps->post_id.' = "'.html::escapeHTML($maps->post_title).'";'."\n". |
---|
107 | 'var content_'.$maps->post_id.' = \''.$content.'\';'."\n". |
---|
108 | 'if (content_'.$maps->post_id.' == "<p>Pas de description</p>") {'."\n". |
---|
109 | 'content_'.$maps->post_id.' = "";'."\n". |
---|
110 | '}'."\n". |
---|
111 | |
---|
112 | 'marker = new google.maps.Marker({'."\n". |
---|
113 | 'icon : "'.$marker[2].'",'."\n". |
---|
114 | 'position: new google.maps.LatLng('.$marker[0].','.$marker[1].'),'."\n". |
---|
115 | 'title: title_'.$maps->post_id.','."\n". |
---|
116 | 'map: map_'.$_ctx->posts->post_id."\n". |
---|
117 | '});'."\n". |
---|
118 | |
---|
119 | 'google.maps.event.addListener(marker, "click", function() {'."\n". |
---|
120 | 'openmarkerinfowindow(this,title_'.$maps->post_id.',content_'.$maps->post_id.');'."\n". |
---|
121 | '});'."\n"; |
---|
122 | |
---|
123 | } |
---|
124 | } elseif (sizeof($list) > 1) { |
---|
125 | |
---|
126 | echo |
---|
127 | 'var list = "'.implode(",",$list).'";'."\n". |
---|
128 | 'var lines = list.split(",");'."\n". |
---|
129 | 'var polylineCoordinates = [];'."\n". |
---|
130 | 'for (var i = 0; i < lines.length; i++) {'."\n". |
---|
131 | 'if (lines[i].length > 1) {'."\n". |
---|
132 | 'var parts = lines[i].split("|");'."\n". |
---|
133 | 'var pos = new google.maps.LatLng(parseFloat(parts[0]), parseFloat(parts[1]));'."\n". |
---|
134 | 'polylineCoordinates.push(pos);'."\n". |
---|
135 | 'var color = parts[3];'."\n". |
---|
136 | '}'."\n". |
---|
137 | '}'."\n". |
---|
138 | 'var polyline = new google.maps.Polyline({'."\n". |
---|
139 | 'path: polylineCoordinates,'."\n". |
---|
140 | 'strokeColor: color,'."\n". |
---|
141 | 'strokeOpacity: 0.8,'."\n". |
---|
142 | 'strokeWeight: 3'."\n". |
---|
143 | '});'."\n". |
---|
144 | 'polyline.setMap(map_'.$_ctx->posts->post_id.');'."\n". |
---|
145 | |
---|
146 | 'var title_'.$maps->post_id.' = "'.html::escapeHTML($maps->post_title).'";'."\n". |
---|
147 | 'var content_'.$maps->post_id.' = \''.$content.'\';'."\n". |
---|
148 | 'if (content_'.$maps->post_id.' == "<p>Pas de description</p>") {'."\n". |
---|
149 | 'content_'.$maps->post_id.' = "";'."\n". |
---|
150 | '}'."\n". |
---|
151 | |
---|
152 | 'google.maps.event.addListener(polyline, "click", function(event) {'."\n". |
---|
153 | 'var pos = event.latLng;'."\n". |
---|
154 | 'openpolyinfowindow(title_'.$maps->post_id.',content_'.$maps->post_id.',pos);'."\n". |
---|
155 | '});'."\n"; |
---|
156 | |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | echo |
---|
161 | 'function openmarkerinfowindow(marker,title,content) {'."\n". |
---|
162 | 'infowindow_'.$_ctx->posts->post_id.'.setContent('."\n". |
---|
163 | '"<h3>"+title+"</h3>"+'."\n". |
---|
164 | '"<div class=\"post-infowindow\" id=\"post-infowindow_'.$_ctx->posts->post_id.'\">"+content+"</div>"'."\n". |
---|
165 | ');'."\n". |
---|
166 | 'infowindow_'.$_ctx->posts->post_id.'.open(map_'.$_ctx->posts->post_id.', marker);'."\n". |
---|
167 | '$("#post-infowindow_'.$_ctx->posts->post_id.'").parent("div", "div#map_canvas_'.$_ctx->posts->post_id.'").css("overflow","hidden");'."\n". |
---|
168 | '}'."\n"; |
---|
169 | |
---|
170 | echo |
---|
171 | 'function openpolyinfowindow(title,content,pos) {'."\n". |
---|
172 | 'infowindow_'.$_ctx->posts->post_id.'.setPosition(pos);'."\n". |
---|
173 | 'infowindow_'.$_ctx->posts->post_id.'.setContent('."\n". |
---|
174 | '"<h3>"+title+"</h3>"+'."\n". |
---|
175 | '"<div class=\"post-infowindow\" id=\"post-infowindow_'.$_ctx->posts->post_id.'\">"+content+"</div>"'."\n". |
---|
176 | ');'."\n". |
---|
177 | 'infowindow_'.$_ctx->posts->post_id.'.open(map_'.$_ctx->posts->post_id.');'."\n". |
---|
178 | '$("#post-infowindow_'.$_ctx->posts->post_id.'").parent("div", "div#map_canvas_'.$_ctx->posts->post_id.'").css("overflow","hidden");'."\n". |
---|
179 | '}'."\n"; |
---|
180 | |
---|
181 | echo |
---|
182 | '});'."\n". |
---|
183 | "\n//]]>\n". |
---|
184 | "</script>\n". |
---|
185 | '<noscript>'."\n". |
---|
186 | '<p>'.__('Sorry, javascript must be activated in your browser to see this map.').'</p>'."\n". |
---|
187 | '</noscript>'."\n". |
---|
188 | '<div class="map_canvas" id="map_canvas_'.$_ctx->posts->post_id.'"></div>'."\n"; |
---|
189 | |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | ?> |
---|