Skip to content

Commit

Permalink
closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
benkeen committed Oct 19, 2019
1 parent 1d613a0 commit 16ebc63
Showing 1 changed file with 189 additions and 168 deletions.
357 changes: 189 additions & 168 deletions code/Module.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class Module extends FormToolsModule
protected $author = "Ben Keen";
protected $authorEmail = "ben.keen@gmail.com";
protected $authorLink = "http://formtools.org";
protected $version = "2.1.0";
protected $date = "2019-10-16";
protected $version = "2.1.1";
protected $date = "2019-10-19";
protected $originLanguage = "en_us";


protected $nav = array(
"module_name" => array("index.php", false)
);
Expand All @@ -30,7 +31,6 @@ public function install($module_id)
{
$db = Core::$db;
$LANG = Core::$L;
$root_url = Core::getRootUrl();

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

$resources_js =<<< END
window.googleMapsInit = function () {
var maps = {};
var defaultMapSettings = {
zoom: 3,
center: new google.maps.LatLng(42.258881, -100.195313),
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
mapTypeControl: false
};
function addYourLocationButton(map) {
var controlDiv = document.createElement('div');
var defaultIconOpacity = 0.6;
var bg = document.createElement('button');
bg.style.backgroundColor = '#fff';
bg.style.border = 'none';
bg.style.outline = 'none';
bg.style.width = '40px';
bg.style.height = '40px';
bg.style.borderRadius = '2px';
bg.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)';
bg.style.cursor = 'pointer';
bg.style.marginRight = '10px';
bg.style.padding = '0';
bg.title = 'Your Location';
controlDiv.appendChild(bg);
var icon = document.createElement('div');
icon.style.margin = '7px';
icon.style.width = '26px';
icon.style.height = '26px';
icon.style.backgroundImage = 'url($root_url/modules/field_type_google_maps/images/location.png)';
icon.style.backgroundSize = '26px 26px';
icon.style.backgroundPosition = '0 0';
icon.style.backgroundRepeat = 'no-repeat';
icon.style.opacity = defaultIconOpacity;
bg.appendChild(icon);
google.maps.event.addListener(map, 'center_changed', function () {
icon.style.opacity = defaultIconOpacity;
});
bg.addEventListener('click', function (e) {
e.preventDefault();
var currOpacity = 0,
animationInterval = setInterval(function () {
currOpacity = currOpacity === defaultIconOpacity ? 0.3 : defaultIconOpacity;
icon.style.opacity = currOpacity;
}, 500);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(latlng);
map.setZoom(8);
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}, function () {
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}, { timeout: 60000 });
} else {
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}
});
controlDiv.index = 1;
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
}
var initMap = function () {
var gmf_id = $(this).attr("id");
var address_field = $(this).find(".cf_gmf_address");
var field_name = address_field.attr("name");
var data = $(this).find(".cf_gmf_data").val();
var opts = $.extend(true, {}, defaultMapSettings);
if (data) {
var parts = data.split("|");
if (parts.length == 3 && parts[1].length != 0) {
var lat_lng = parts[1].split(", ");
opts.zoom = parseInt(parts[2], 10);
opts.center = new google.maps.LatLng(parseFloat(lat_lng[0]), parseFloat(lat_lng[1]));
}
}
maps[gmf_id] = {
map: new google.maps.Map($(this).find(".cf_gmf")[0], opts)
};
if (address_field.val() != "") {
maps[gmf_id].marker = new google.maps.Marker({ position: opts.center, map: maps[gmf_id].map });
} else {
maps[gmf_id].marker = new google.maps.Marker();
}
google.maps.event.addListener(maps[gmf_id].map, 'zoom_changed', function (e) {
$("#" + gmf_id).find("[name=" + field_name + "_zoom]").val(maps[gmf_id].map.getZoom());
});
if (location.protocol === 'https:') {
addYourLocationButton(maps[gmf_id].map);
}
};
// googleMapsInit may fire before or after DOM ready
$(function () {
$(".cf_gmf_section").each(initMap);
$(".cf_gmf_address").bind("keydown", function (e) {
if (e.keyCode === 13) {
$(e.target).closest(".cf_gmf_section").find(".cf_gmf_update").trigger("click");
return false;
}
});
$(".cf_gmf_update").bind("click", update_map);
function update_map (e) {
var gmf_div = $(e.target).closest(".cf_gmf_section");
var field_name = gmf_div.find(".cf_gmf_address").attr("name");
var map_info = maps[gmf_div.attr("id")];
var map = map_info.map;
var address = gmf_div.find(".cf_gmf_address").val();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var loc = results[0].geometry.location;
map.setCenter(loc);
var coords = loc.lat() + ", " + loc.lng();
$(gmf_div).find("[name=" + field_name + "_coords]").val(coords);
$(gmf_div).find("[name=" + field_name + "_zoom]").val(map.getZoom());
$(".cf_gmf_coords_str").html(coords);
map_info.marker.setPosition(loc);
map_info.marker.setMap(map);
}
});
}
});
};
$(function () {
// if google maps isn't included (found by an ID on the script tag),
if ($("#google-maps-field-lib").length) {
return;
}
$(".cf_gmf_section").each(function () {
var address_field = $(this).find(".cf_gmf_address");
var field_name = address_field.attr("name");
$("#cf_gmf_" + field_name + "_map").after("<div class=\"hint\">Please set your Google Maps API key in the Google Maps Field module settings.</div>");
$("#cf_gmf_" + field_name + "_map").remove();
$(this).find(".cf_gmf_update").hide();
});
});
END;

$db->query("
INSERT INTO {PREFIX}field_types (is_editable, non_editable_info, managed_by_module_id,
field_type_name, field_type_identifier, group_id, is_file_field, is_date_field, raw_field_type_map,
Expand Down Expand Up @@ -251,7 +88,7 @@ function update_map (e) {
"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",
"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",
"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",
"resources_js" => $resources_js
"resources_js" => self::getResourcesJs()
));
$db->execute();
$field_type_id = $db->getInsertId();
Expand Down Expand Up @@ -361,8 +198,22 @@ public function uninstall($module_id)
return array(true, "");
}

public function upgrade($module_id, $old_module_version)
{
$db = Core::$db;

$db->query("
UPDATE {PREFIX}field_types
SET resources_js = :resources_js
WHERE field_type_identifier = 'google_maps_field'
");
$db->bind("resources_js", self::getResourcesJs());
$db->execute();

$this->resetHooks();
}

public function includeGoogleMaps($template, $page_data)
public function includeGoogleMaps($template, $page_data)
{
// we only need this field on the edit pages
$curr_page = $page_data["page"];
Expand Down Expand Up @@ -433,4 +284,174 @@ public function updateSettings($google_maps_key) {
));
return array(true, $this->L["notify_settings_updated"]);
}


private static function getResourcesJs()
{
$root_url = Core::getRootUrl();

$resources_js =<<< END
window.googleMapsInit = function () {
var maps = {};
var defaultMapSettings = {
zoom: 3,
center: new google.maps.LatLng(42.258881, -100.195313),
mapTypeId: google.maps.MapTypeId.ROADMAP,
streetViewControl: false,
mapTypeControl: false
};
function addYourLocationButton(map) {
var controlDiv = document.createElement('div');
var defaultIconOpacity = 0.6;
var bg = document.createElement('button');
bg.style.backgroundColor = '#fff';
bg.style.border = 'none';
bg.style.outline = 'none';
bg.style.width = '40px';
bg.style.height = '40px';
bg.style.borderRadius = '2px';
bg.style.boxShadow = '0 1px 4px rgba(0,0,0,0.3)';
bg.style.cursor = 'pointer';
bg.style.marginRight = '10px';
bg.style.padding = '0';
bg.title = 'Your Location';
controlDiv.appendChild(bg);
var icon = document.createElement('div');
icon.style.margin = '7px';
icon.style.width = '26px';
icon.style.height = '26px';
icon.style.backgroundImage = 'url($root_url/modules/field_type_google_maps/images/location.png)';
icon.style.backgroundSize = '26px 26px';
icon.style.backgroundPosition = '0 0';
icon.style.backgroundRepeat = 'no-repeat';
icon.style.opacity = defaultIconOpacity;
bg.appendChild(icon);
google.maps.event.addListener(map, 'center_changed', function () {
icon.style.opacity = defaultIconOpacity;
});
bg.addEventListener('click', function (e) {
e.preventDefault();
var currOpacity = 0,
animationInterval = setInterval(function () {
currOpacity = currOpacity === defaultIconOpacity ? 0.3 : defaultIconOpacity;
icon.style.opacity = currOpacity;
}, 500);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter(latlng);
map.setZoom(8);
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}, function () {
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}, { timeout: 60000 });
} else {
clearInterval(animationInterval);
icon.style.opacity = defaultIconOpacity;
}
});
controlDiv.index = 1;
map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
}
var initMap = function () {
var gmf_id = $(this).attr("id");
var address_field = $(this).find(".cf_gmf_address");
var field_name = address_field.attr("name");
var data = $(this).find(".cf_gmf_data").val();
var opts = $.extend(true, {}, defaultMapSettings);
if (data) {
var parts = data.split("|");
if (parts.length == 3 && parts[1].length != 0) {
var lat_lng = parts[1].split(", ");
opts.zoom = parseInt(parts[2], 10);
opts.center = new google.maps.LatLng(parseFloat(lat_lng[0]), parseFloat(lat_lng[1]));
}
}
maps[gmf_id] = {
map: new google.maps.Map($(this).find(".cf_gmf")[0], opts)
};
if (address_field.val() != "") {
maps[gmf_id].marker = new google.maps.Marker({ position: opts.center, map: maps[gmf_id].map });
} else {
maps[gmf_id].marker = new google.maps.Marker();
}
google.maps.event.addListener(maps[gmf_id].map, 'zoom_changed', function (e) {
$("#" + gmf_id).find("[name=" + field_name + "_zoom]").val(maps[gmf_id].map.getZoom());
});
if (location.protocol === 'https:') {
addYourLocationButton(maps[gmf_id].map);
}
};
// googleMapsInit may fire before or after DOM ready
$(function () {
$(".cf_gmf_section").each(initMap);
$(".cf_gmf_address").bind("keydown", function (e) {
if (e.keyCode === 13) {
$(e.target).closest(".cf_gmf_section").find(".cf_gmf_update").trigger("click");
return false;
}
});
$(".cf_gmf_update").bind("click", update_map);
function update_map (e) {
var gmf_div = $(e.target).closest(".cf_gmf_section");
var field_name = gmf_div.find(".cf_gmf_address").attr("name");
var map_info = maps[gmf_div.attr("id")];
var map = map_info.map;
var address = gmf_div.find(".cf_gmf_address").val();
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var loc = results[0].geometry.location;
map.setCenter(loc);
var coords = loc.lat() + ", " + loc.lng();
$(gmf_div).find("[name=" + field_name + "_coords]").val(coords);
$(gmf_div).find("[name=" + field_name + "_zoom]").val(map.getZoom());
$(".cf_gmf_coords_str").html(coords);
map_info.marker.setPosition(loc);
map_info.marker.setMap(map);
}
});
}
});
};
$(function () {
// if google maps isn't included (found by an ID on the script tag),
if ($("#google-maps-field-lib").length) {
return;
}
$(".cf_gmf_section").each(function () {
var address_field = $(this).find(".cf_gmf_address");
var field_name = address_field.attr("name");
$("#cf_gmf_" + field_name + "_map").after("<div class=\"hint\">Please set your Google Maps API key in the Google Maps Field module settings.</div>");
$("#cf_gmf_" + field_name + "_map").remove();
$(this).find(".cf_gmf_update").hide();
});
});
END;
return $resources_js;
}
}

0 comments on commit 16ebc63

Please sign in to comment.