diff --git a/integration-civicrm-leaflet.php b/integration-civicrm-leaflet.php index 05f9b15..0733dff 100644 --- a/integration-civicrm-leaflet.php +++ b/integration-civicrm-leaflet.php @@ -2,7 +2,7 @@ /* Plugin Name: Integration between Leaflet Map and CiviCRM Description: Integrates data from the CiviCRM api into a Leaflet Map. You can use this plugin with Connector to CiviCRM with CiviMcRestFace (https://wordpress.org/plugins/connector-civicrm-mcrestface/) -Version: 1.0.7 +Version: 1.0.8 Author: Jaap Jansma License: AGPL3 License URI: https://www.gnu.org/licenses/agpl-3.0.html diff --git a/integration_civicrm_leaflet.js b/integration_civicrm_leaflet.js index 1fba81a..297c0e5 100644 --- a/integration_civicrm_leaflet.js +++ b/integration_civicrm_leaflet.js @@ -94,7 +94,7 @@ function IntegrationCiviCRMLeaflet (tooltip_text, popup_text, popup_property, ap */ this.defaultFeature = function (feature, layer) { var props = feature.properties || {}; - var text = window.WPLeafletMapPlugin.template(config.popup_text, feature.properties); + var text = this.template(config.popup_text, feature.properties); if (config.popup_property) { text = props[config.popup_property]; } @@ -125,7 +125,7 @@ function IntegrationCiviCRMLeaflet (tooltip_text, popup_text, popup_property, ap */ this.defaultTooltip = function (feature, layer) { if (config.tooltip_text) { - var tooltip = window.WPLeafletMapPlugin.template(config.tooltip_text, feature.properties); + var tooltip = this.template(config.tooltip_text, feature.properties); layer.bindTooltip(tooltip); } }; @@ -169,4 +169,106 @@ function IntegrationCiviCRMLeaflet (tooltip_text, popup_text, popup_property, ap var strYear = (dateObject.getFullYear()).toString().padStart(4, '0'); return strYear+strMonth+strDate; }; + + var templateRe = /\{ *(.*?) *\}/g; + + /** + * It interpolates variables in curly brackets (regex above) + * + * ex: "Property Value: {property_key}" + * + * @param {string} str + * @param {object} data e.g. feature.properties + */ + this.template = function (str, data) { + if (data == null) { + return str; + } + + return str.replace(templateRe, function (match, key) { + var obj = this.liquid(key); + var value = this.parseKey(data, obj.key); + if (value === undefined && obj.default) { + return obj.default; + } else if (value === undefined && data.hasOwnProperty(key)) { + return ''; + } else if (value === undefined) { + return match; + } + return value; + }.bind(this)); + }; + + /** + * parses liquid tags from a string + * + * @param {string} str + */ + this.liquid = function (str) { + var tags = str.split(' | '); + var obj = {}; + + // removes initial variable from array + var key = tags.shift(); + + for (var i = 0, len = tags.length; i < len; i++) { + var tag = tags[i].split(': '); + var tagName = tag.shift(); + var tagValue = tag.join(': ') || true; + + obj[tagName] = tagValue; + } + + // always preserve the original string + obj.key = key; + + return obj; + }; + + /** + * It uses strToPath to access a possibly nested path value + * + * @param {object} obj + * @param {string} key + */ + this.parseKey = function (obj, key) { + var arr = this.strToPath(unescape(key)); + var value = obj; + + for (var i = 0, len = arr.length; i < len; i++) { + value = value[arr[i]]; + if (!value) { + return undefined; + } + } + + return value; + }; + + /** + * Converts nested object keys to array + * + * ex: `this.that['and'].theOther[4]` -> + * ['this', 'that', 'and', 'theOther', '4'] + * @param {string} key + */ + this.strToPath = function (key) { + if (key == null) { + return []; + } + /** used in strToPath */ + var strToPathRe = /[.‘’'“”"\[\]]+/g; + var input = key.split(strToPathRe); + var output = []; + + // failsafe for all empty strings; + // mostly catches brackets at the end of a string + for (var i = 0, len = input.length; i < len; i++) { + if (input[i] !== '') { + output.push(input[i]); + } + } + + return output; + }; } \ No newline at end of file diff --git a/readme.txt b/readme.txt index b15833c..4568873 100644 --- a/readme.txt +++ b/readme.txt @@ -3,9 +3,9 @@ Contributors: jaapjansma Donate link: https://github.com/CiviMRF/integration-civicrm-leaflet Tags: leaflet, CiviCRM, map, leaflet map, api, connector, rest Requires at least: 5.2 -Tested up to: 5.6 +Tested up to: 6.0 Requires PHP: 7.2 -Stable tag: 1.0.7 +Stable tag: 1.0.8 License: AGPL-3.0 Provides an integration between CiviCRM api and the [leaflet map](https://wordpress.org/plugins/leaflet-map/). Meaning you can create maps from CiviCRM Data. @@ -38,6 +38,7 @@ For more documentation see: [README.md](https://github.com/CiviMRF/integration-c == Changelog == +1.0.8: Fixed issue with empty tokens. 1.0.7: Made it easier to set custom makers. Also clustering works with all data sources. 1.0.6: Added possibility to have multiple data sources. 1.0.5: Fixed issue with php 7.1, and 7.2