Skip to content

Commit 16ebc63

Browse files
committed
closes #4
1 parent 1d613a0 commit 16ebc63

File tree

1 file changed

+189
-168
lines changed

1 file changed

+189
-168
lines changed

code/Module.class.php

Lines changed: 189 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class Module extends FormToolsModule
1818
protected $author = "Ben Keen";
1919
protected $authorEmail = "[email protected]";
2020
protected $authorLink = "http://formtools.org";
21-
protected $version = "2.1.0";
22-
protected $date = "2019-10-16";
21+
protected $version = "2.1.1";
22+
protected $date = "2019-10-19";
2323
protected $originLanguage = "en_us";
2424

25+
2526
protected $nav = array(
2627
"module_name" => array("index.php", false)
2728
);
@@ -30,7 +31,6 @@ public function install($module_id)
3031
{
3132
$db = Core::$db;
3233
$LANG = Core::$L;
33-
$root_url = Core::getRootUrl();
3434

3535
// check it's not already installed (i.e. check for the unique field type identifier)
3636
$field_type_info = FieldTypes::getFieldTypeByIdentifier("google_maps_field");
@@ -60,169 +60,6 @@ public function install($module_id)
6060
$count = $db->fetch(PDO::FETCH_COLUMN);
6161
$next_list_order = $count + 1;
6262

63-
$resources_js =<<< END
64-
window.googleMapsInit = function () {
65-
var maps = {};
66-
var defaultMapSettings = {
67-
zoom: 3,
68-
center: new google.maps.LatLng(42.258881, -100.195313),
69-
mapTypeId: google.maps.MapTypeId.ROADMAP,
70-
streetViewControl: false,
71-
mapTypeControl: false
72-
};
73-
74-
function addYourLocationButton(map) {
75-
var controlDiv = document.createElement('div');
76-
77-
var defaultIconOpacity = 0.6;
78-
79-
var bg = document.createElement('button');
80-
bg.style.backgroundColor = '#fff';
81-
bg.style.border = 'none';
82-
bg.style.outline = 'none';
83-
bg.style.width = '40px';
84-
bg.style.height = '40px';
85-
bg.style.borderRadius = '2px';
86-
bg.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)';
87-
bg.style.cursor = 'pointer';
88-
bg.style.marginRight = '10px';
89-
bg.style.padding = '0';
90-
bg.title = 'Your Location';
91-
controlDiv.appendChild(bg);
92-
93-
var icon = document.createElement('div');
94-
icon.style.margin = '7px';
95-
icon.style.width = '26px';
96-
icon.style.height = '26px';
97-
icon.style.backgroundImage = 'url($root_url/modules/field_type_google_maps/images/location.png)';
98-
icon.style.backgroundSize = '26px 26px';
99-
icon.style.backgroundPosition = '0 0';
100-
icon.style.backgroundRepeat = 'no-repeat';
101-
icon.style.opacity = defaultIconOpacity;
102-
bg.appendChild(icon);
103-
104-
google.maps.event.addListener(map, 'center_changed', function () {
105-
icon.style.opacity = defaultIconOpacity;
106-
});
107-
108-
bg.addEventListener('click', function (e) {
109-
e.preventDefault();
110-
var currOpacity = 0,
111-
animationInterval = setInterval(function () {
112-
currOpacity = currOpacity === defaultIconOpacity ? 0.3 : defaultIconOpacity;
113-
icon.style.opacity = currOpacity;
114-
}, 500);
115-
116-
if (navigator.geolocation) {
117-
navigator.geolocation.getCurrentPosition(function(position) {
118-
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
119-
map.setCenter(latlng);
120-
map.setZoom(8);
121-
clearInterval(animationInterval);
122-
icon.style.opacity = defaultIconOpacity;
123-
}, function () {
124-
clearInterval(animationInterval);
125-
icon.style.opacity = defaultIconOpacity;
126-
}, { timeout: 60000 });
127-
} else {
128-
clearInterval(animationInterval);
129-
icon.style.opacity = defaultIconOpacity;
130-
}
131-
});
132-
133-
controlDiv.index = 1;
134-
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
135-
}
136-
137-
var initMap = function () {
138-
var gmf_id = $(this).attr("id");
139-
var address_field = $(this).find(".cf_gmf_address");
140-
var field_name = address_field.attr("name");
141-
var data = $(this).find(".cf_gmf_data").val();
142-
var opts = $.extend(true, {}, defaultMapSettings);
143-
144-
if (data) {
145-
var parts = data.split("|");
146-
if (parts.length == 3 && parts[1].length != 0) {
147-
var lat_lng = parts[1].split(", ");
148-
opts.zoom = parseInt(parts[2], 10);
149-
opts.center = new google.maps.LatLng(parseFloat(lat_lng[0]), parseFloat(lat_lng[1]));
150-
}
151-
}
152-
153-
maps[gmf_id] = {
154-
map: new google.maps.Map($(this).find(".cf_gmf")[0], opts)
155-
};
156-
157-
if (address_field.val() != "") {
158-
maps[gmf_id].marker = new google.maps.Marker({ position: opts.center, map: maps[gmf_id].map });
159-
} else {
160-
maps[gmf_id].marker = new google.maps.Marker();
161-
}
162-
163-
google.maps.event.addListener(maps[gmf_id].map, 'zoom_changed', function (e) {
164-
$("#" + gmf_id).find("[name=" + field_name + "_zoom]").val(maps[gmf_id].map.getZoom());
165-
});
166-
167-
if (location.protocol === 'https:') {
168-
addYourLocationButton(maps[gmf_id].map);
169-
}
170-
};
171-
172-
// googleMapsInit may fire before or after DOM ready
173-
$(function () {
174-
$(".cf_gmf_section").each(initMap);
175-
176-
$(".cf_gmf_address").bind("keydown", function (e) {
177-
if (e.keyCode === 13) {
178-
$(e.target).closest(".cf_gmf_section").find(".cf_gmf_update").trigger("click");
179-
return false;
180-
}
181-
});
182-
183-
$(".cf_gmf_update").bind("click", update_map);
184-
185-
function update_map (e) {
186-
var gmf_div = $(e.target).closest(".cf_gmf_section");
187-
var field_name = gmf_div.find(".cf_gmf_address").attr("name");
188-
var map_info = maps[gmf_div.attr("id")];
189-
var map = map_info.map;
190-
var address = gmf_div.find(".cf_gmf_address").val();
191-
var geocoder = new google.maps.Geocoder();
192-
193-
geocoder.geocode({'address': address}, function (results, status) {
194-
if (status == google.maps.GeocoderStatus.OK) {
195-
var loc = results[0].geometry.location;
196-
map.setCenter(loc);
197-
var coords = loc.lat() + ", " + loc.lng();
198-
$(gmf_div).find("[name=" + field_name + "_coords]").val(coords);
199-
$(gmf_div).find("[name=" + field_name + "_zoom]").val(map.getZoom());
200-
$(".cf_gmf_coords_str").html(coords);
201-
map_info.marker.setPosition(loc);
202-
map_info.marker.setMap(map);
203-
}
204-
});
205-
}
206-
});
207-
};
208-
209-
210-
$(function () {
211-
// if google maps isn't included (found by an ID on the script tag),
212-
if ($("#google-maps-field-lib").length) {
213-
return;
214-
}
215-
216-
$(".cf_gmf_section").each(function () {
217-
var address_field = $(this).find(".cf_gmf_address");
218-
var field_name = address_field.attr("name");
219-
$("#cf_gmf_" + field_name + "_map").after("<div class=\"hint\">Please set your Google Maps API key in the Google Maps Field module settings.</div>");
220-
$("#cf_gmf_" + field_name + "_map").remove();
221-
$(this).find(".cf_gmf_update").hide();
222-
});
223-
});
224-
END;
225-
22663
$db->query("
22764
INSERT INTO {PREFIX}field_types (is_editable, non_editable_info, managed_by_module_id,
22865
field_type_name, field_type_identifier, group_id, is_file_field, is_date_field, raw_field_type_map,
@@ -251,7 +88,7 @@ function update_map (e) {
25188
"edit_field_smarty_markup" => "{assign var=address value=\"\"}\r\n{assign var=coordinates value=\"\"}\r\n{assign var=zoom_level value=\"\"}\r\n{if \$VALUE}\r\n {assign var=parts value=\"|\"|explode:\$VALUE}\r\n {assign var=address value=\$parts[0]}\r\n {assign var=coords value=\$parts[1]}\r\n {assign var=zoom_level value=\$parts[2]}\r\n{/if}\r\n\r\n<div class=\"cf_gmf_section\" id=\"cf_gmf_{\$NAME}\">\r\n <input type=\"hidden\" class=\"cf_gmf_data\" value=\"{\$VALUE|escape}\" />\r\n <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\r\n <tr>\r\n <td><input type=\"text\" name=\"{\$NAME}\" class=\"cf_gmf_address\" \r\n placeholder=\"Enter Address\" value=\"{\$address|escape}\" /></td>\r\n <td width=\"60\"><input type=\"button\" class=\"cf_gmf_update\" value=\"{\$LANG.word_update}\" /></td>\r\n </tr>\r\n </table>\r\n\r\n <div id=\"cf_gmf_{\$NAME}_map\" class=\"cf_gmf {\$map_size}\"></div>\r\n <input type=\"hidden\" name=\"{\$NAME}_coords\" value=\"{\$coords|default:\"\"}\" />\r\n <input type=\"hidden\" name=\"{\$NAME}_zoom\" value=\"{\$zoom_level}\" />\r\n \r\n {if \$show_coordinates == \"yes\"}\r\n <div class=\"medium_grey cf_gmf_coords_str\">{\$coords|default:\"&#8212;\"}</div>\r\n {/if}\r\n {if \$comments}\r\n <div class=\"cf_field_comments\">{\$comments}</div>\r\n {/if}\r\n</div>\r\n\r\n",
25289
"php_processing" => "\$field_name = \$vars[\"field_info\"][\"field_name\"];\r\n\r\n\$value = \"\";\r\nif (!empty(\$vars[\"data\"][\$field_name])) {\r\n \$address = \$vars[\"data\"][\$field_name];\r\n \$coords = \$vars[\"data\"][\"{\$field_name}_coords\"];\r\n \$zoom = \$vars[\"data\"][\"{\$field_name}_zoom\"];\r\n \$value = \"\$address|\$coords|\$zoom\";\r\n}\r\n",
25390
"resources_css" => ".cf_gmf_address {\r\n width: 98%;\r\n}\r\n.cf_gmf_tiny {\r\n height: 160px; \r\n}\r\n.cf_gmf_small {\r\n height: 250px; \r\n}\r\n.cf_gmf_medium {\r\n height: 350px; \r\n}\r\n.cf_gmf_large {\r\n height: 590px; \r\n}\r\n",
254-
"resources_js" => $resources_js
91+
"resources_js" => self::getResourcesJs()
25592
));
25693
$db->execute();
25794
$field_type_id = $db->getInsertId();
@@ -361,8 +198,22 @@ public function uninstall($module_id)
361198
return array(true, "");
362199
}
363200

201+
public function upgrade($module_id, $old_module_version)
202+
{
203+
$db = Core::$db;
204+
205+
$db->query("
206+
UPDATE {PREFIX}field_types
207+
SET resources_js = :resources_js
208+
WHERE field_type_identifier = 'google_maps_field'
209+
");
210+
$db->bind("resources_js", self::getResourcesJs());
211+
$db->execute();
212+
213+
$this->resetHooks();
214+
}
364215

365-
public function includeGoogleMaps($template, $page_data)
216+
public function includeGoogleMaps($template, $page_data)
366217
{
367218
// we only need this field on the edit pages
368219
$curr_page = $page_data["page"];
@@ -433,4 +284,174 @@ public function updateSettings($google_maps_key) {
433284
));
434285
return array(true, $this->L["notify_settings_updated"]);
435286
}
287+
288+
289+
private static function getResourcesJs()
290+
{
291+
$root_url = Core::getRootUrl();
292+
293+
$resources_js =<<< END
294+
window.googleMapsInit = function () {
295+
var maps = {};
296+
var defaultMapSettings = {
297+
zoom: 3,
298+
center: new google.maps.LatLng(42.258881, -100.195313),
299+
mapTypeId: google.maps.MapTypeId.ROADMAP,
300+
streetViewControl: false,
301+
mapTypeControl: false
302+
};
303+
304+
function addYourLocationButton(map) {
305+
var controlDiv = document.createElement('div');
306+
307+
var defaultIconOpacity = 0.6;
308+
309+
var bg = document.createElement('button');
310+
bg.style.backgroundColor = '#fff';
311+
bg.style.border = 'none';
312+
bg.style.outline = 'none';
313+
bg.style.width = '40px';
314+
bg.style.height = '40px';
315+
bg.style.borderRadius = '2px';
316+
bg.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)';
317+
bg.style.cursor = 'pointer';
318+
bg.style.marginRight = '10px';
319+
bg.style.padding = '0';
320+
bg.title = 'Your Location';
321+
controlDiv.appendChild(bg);
322+
323+
var icon = document.createElement('div');
324+
icon.style.margin = '7px';
325+
icon.style.width = '26px';
326+
icon.style.height = '26px';
327+
icon.style.backgroundImage = 'url($root_url/modules/field_type_google_maps/images/location.png)';
328+
icon.style.backgroundSize = '26px 26px';
329+
icon.style.backgroundPosition = '0 0';
330+
icon.style.backgroundRepeat = 'no-repeat';
331+
icon.style.opacity = defaultIconOpacity;
332+
bg.appendChild(icon);
333+
334+
google.maps.event.addListener(map, 'center_changed', function () {
335+
icon.style.opacity = defaultIconOpacity;
336+
});
337+
338+
bg.addEventListener('click', function (e) {
339+
e.preventDefault();
340+
var currOpacity = 0,
341+
animationInterval = setInterval(function () {
342+
currOpacity = currOpacity === defaultIconOpacity ? 0.3 : defaultIconOpacity;
343+
icon.style.opacity = currOpacity;
344+
}, 500);
345+
346+
if (navigator.geolocation) {
347+
navigator.geolocation.getCurrentPosition(function(position) {
348+
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
349+
map.setCenter(latlng);
350+
map.setZoom(8);
351+
clearInterval(animationInterval);
352+
icon.style.opacity = defaultIconOpacity;
353+
}, function () {
354+
clearInterval(animationInterval);
355+
icon.style.opacity = defaultIconOpacity;
356+
}, { timeout: 60000 });
357+
} else {
358+
clearInterval(animationInterval);
359+
icon.style.opacity = defaultIconOpacity;
360+
}
361+
});
362+
363+
controlDiv.index = 1;
364+
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
365+
}
366+
367+
var initMap = function () {
368+
var gmf_id = $(this).attr("id");
369+
var address_field = $(this).find(".cf_gmf_address");
370+
var field_name = address_field.attr("name");
371+
var data = $(this).find(".cf_gmf_data").val();
372+
var opts = $.extend(true, {}, defaultMapSettings);
373+
374+
if (data) {
375+
var parts = data.split("|");
376+
if (parts.length == 3 && parts[1].length != 0) {
377+
var lat_lng = parts[1].split(", ");
378+
opts.zoom = parseInt(parts[2], 10);
379+
opts.center = new google.maps.LatLng(parseFloat(lat_lng[0]), parseFloat(lat_lng[1]));
380+
}
381+
}
382+
383+
maps[gmf_id] = {
384+
map: new google.maps.Map($(this).find(".cf_gmf")[0], opts)
385+
};
386+
387+
if (address_field.val() != "") {
388+
maps[gmf_id].marker = new google.maps.Marker({ position: opts.center, map: maps[gmf_id].map });
389+
} else {
390+
maps[gmf_id].marker = new google.maps.Marker();
391+
}
392+
393+
google.maps.event.addListener(maps[gmf_id].map, 'zoom_changed', function (e) {
394+
$("#" + gmf_id).find("[name=" + field_name + "_zoom]").val(maps[gmf_id].map.getZoom());
395+
});
396+
397+
if (location.protocol === 'https:') {
398+
addYourLocationButton(maps[gmf_id].map);
399+
}
400+
};
401+
402+
// googleMapsInit may fire before or after DOM ready
403+
$(function () {
404+
$(".cf_gmf_section").each(initMap);
405+
406+
$(".cf_gmf_address").bind("keydown", function (e) {
407+
if (e.keyCode === 13) {
408+
$(e.target).closest(".cf_gmf_section").find(".cf_gmf_update").trigger("click");
409+
return false;
410+
}
411+
});
412+
413+
$(".cf_gmf_update").bind("click", update_map);
414+
415+
function update_map (e) {
416+
var gmf_div = $(e.target).closest(".cf_gmf_section");
417+
var field_name = gmf_div.find(".cf_gmf_address").attr("name");
418+
var map_info = maps[gmf_div.attr("id")];
419+
var map = map_info.map;
420+
var address = gmf_div.find(".cf_gmf_address").val();
421+
var geocoder = new google.maps.Geocoder();
422+
423+
geocoder.geocode({'address': address}, function (results, status) {
424+
if (status == google.maps.GeocoderStatus.OK) {
425+
var loc = results[0].geometry.location;
426+
map.setCenter(loc);
427+
var coords = loc.lat() + ", " + loc.lng();
428+
$(gmf_div).find("[name=" + field_name + "_coords]").val(coords);
429+
$(gmf_div).find("[name=" + field_name + "_zoom]").val(map.getZoom());
430+
$(".cf_gmf_coords_str").html(coords);
431+
map_info.marker.setPosition(loc);
432+
map_info.marker.setMap(map);
433+
}
434+
});
435+
}
436+
});
437+
};
438+
439+
440+
$(function () {
441+
// if google maps isn't included (found by an ID on the script tag),
442+
if ($("#google-maps-field-lib").length) {
443+
return;
444+
}
445+
446+
$(".cf_gmf_section").each(function () {
447+
var address_field = $(this).find(".cf_gmf_address");
448+
var field_name = address_field.attr("name");
449+
$("#cf_gmf_" + field_name + "_map").after("<div class=\"hint\">Please set your Google Maps API key in the Google Maps Field module settings.</div>");
450+
$("#cf_gmf_" + field_name + "_map").remove();
451+
$(this).find(".cf_gmf_update").hide();
452+
});
453+
});
454+
END;
455+
return $resources_js;
456+
}
436457
}

0 commit comments

Comments
 (0)