Skip to content

Commit

Permalink
Fixed issue with empty tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapjansma committed May 30, 2022
1 parent 4d8e07a commit 18f84fd
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
2 changes: 1 addition & 1 deletion integration-civicrm-leaflet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 104 additions & 2 deletions integration_civicrm_leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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;
};
}
5 changes: 3 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 18f84fd

Please sign in to comment.