use of org.broadleafcommerce.presentation.model.BroadleafTemplateElement in project BroadleafCommerce by BroadleafCommerce.
the class ResourceBundleProcessor method addDependentBundleRestrictionToModel.
protected void addDependentBundleRestrictionToModel(String src, boolean async, boolean defer, String dependencyEvent, BroadleafTemplateContext context, BroadleafTemplateModel model) {
String methodName = src;
if (methodName.contains("/")) {
methodName = methodName.substring(methodName.lastIndexOf("/") + 1, methodName.length());
}
methodName = methodName.replaceAll("-", "_");
methodName = methodName.replaceAll("\\.", "_");
StringBuilder sb = new StringBuilder();
sb.append("<script>\n");
sb.append("if (typeof(" + dependencyEvent + "Event) !== 'undefined'){");
sb.append("$(function() {");
sb.append("handle" + methodName + "();");
sb.append("});");
sb.append("} else {");
sb.append("document.body.addEventListener('" + dependencyEvent + "',");
sb.append("function (elem) {");
sb.append("$(function() {");
sb.append("handle" + methodName + "();");
sb.append("});");
sb.append("}, false");
sb.append(");");
sb.append("}");
sb.append("function handle" + methodName + "() {");
sb.append("var script= document.createElement('script');");
sb.append("script.type= 'text/javascript';");
sb.append("script.src= '" + src + "';");
sb.append("document.body.append(script);");
sb.append("}");
sb.append("\n</script>");
BroadleafTemplateElement linkedData = context.createTextElement(sb.toString());
model.addElement(linkedData);
}
use of org.broadleafcommerce.presentation.model.BroadleafTemplateElement in project BroadleafCommerce by BroadleafCommerce.
the class LinkedDataProcessor method getReplacementModel.
@Override
public BroadleafTemplateModel getReplacementModel(final String s, final Map<String, String> map, final BroadleafTemplateContext context) {
String linkedDataText = "<script type=\"application/ld+json\">\n" + getData(context.getRequest()) + "\n</script>";
final BroadleafTemplateModel model = context.createModel();
final BroadleafTemplateElement linkedData = context.createTextElement(linkedDataText);
model.addElement(linkedData);
return model;
}
use of org.broadleafcommerce.presentation.model.BroadleafTemplateElement in project BroadleafCommerce by BroadleafCommerce.
the class TransparentRedirectCreditCardFormProcessor method getInjectedModelAndTagAttributes.
@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
PaymentRequestDTO requestDTO = (PaymentRequestDTO) context.parseExpression(rootTagAttributes.get("paymentRequestDTO"));
Map<String, Map<String, String>> formParameters = new HashMap<>();
Map<String, String> configurationSettings = new HashMap<>();
// Create the configuration settings map to pass into the payment module
Map<String, String> keysToKeep = new HashMap<>();
for (String key : rootTagAttributes.keySet()) {
if (key.startsWith("config-")) {
final int trimLength = "config-".length();
String configParam = key.substring(trimLength);
configurationSettings.put(configParam, rootTagAttributes.get(key));
} else {
keysToKeep.put(key, rootTagAttributes.get(key));
}
}
keysToKeep.remove("paymentRequestDTO");
try {
extensionManager.getProxy().createTransparentRedirectForm(formParameters, requestDTO, configurationSettings);
} catch (PaymentException e) {
throw new RuntimeException("Unable to Create the Transparent Redirect Form", e);
}
StringBuilder formActionKey = new StringBuilder("formActionKey");
StringBuilder formHiddenParamsKey = new StringBuilder("formHiddenParamsKey");
extensionManager.getProxy().setFormActionKey(formActionKey);
extensionManager.getProxy().setFormHiddenParamsKey(formHiddenParamsKey);
// Change the action attribute on the form to the Payment Gateways Endpoint
String actionUrl = "";
Map<String, String> actionValue = formParameters.get(formActionKey.toString());
if (actionValue != null && actionValue.size() > 0) {
String key = (String) actionValue.keySet().toArray()[0];
actionUrl = actionValue.get(key);
}
keysToKeep.put("action", actionUrl);
BroadleafTemplateModel model = context.createModel();
// Append any hidden fields necessary for the Transparent Redirect
Map<String, String> hiddenFields = formParameters.get(formHiddenParamsKey.toString());
if (MapUtils.isNotEmpty(hiddenFields)) {
for (String key : hiddenFields.keySet()) {
Map<String, String> attributes = new HashMap<>();
attributes.put("type", "hidden");
attributes.put("name", key);
attributes.put("value", hiddenFields.get(key));
BroadleafTemplateElement input = context.createStandaloneElement("input", attributes, true);
model.addElement(input);
}
}
return new BroadleafTemplateModelModifierDTO(model, keysToKeep, "form");
}
use of org.broadleafcommerce.presentation.model.BroadleafTemplateElement in project BroadleafCommerce by BroadleafCommerce.
the class FormProcessor method getInjectedModelAndTagAttributes.
@Override
public BroadleafTemplateModelModifierDTO getInjectedModelAndTagAttributes(String rootTagName, Map<String, String> rootTagAttributes, BroadleafTemplateContext context) {
Map<String, String> formAttributes = new HashMap<>();
formAttributes.putAll(rootTagAttributes);
BroadleafTemplateModel model = context.createModel();
BroadleafTemplateModelModifierDTO dto = new BroadleafTemplateModelModifierDTO();
// We do this instead of checking for a POST because post is default if nothing is specified
if (!"GET".equalsIgnoreCase(formAttributes.get("method"))) {
try {
String csrfToken = eps.getCSRFToken();
String stateVersionToken = null;
if (spps.isEnabled()) {
stateVersionToken = spps.getStateVersionToken();
}
// detect multipart form
if ("multipart/form-data".equalsIgnoreCase(formAttributes.get("enctype"))) {
String csrfQueryParameter = "?" + eps.getCsrfTokenParameter() + "=" + csrfToken;
if (stateVersionToken != null) {
csrfQueryParameter += "&" + spps.getStateVersionTokenParameter() + "=" + stateVersionToken;
}
// Add this into the attribute map to be used for the new <form> tag. The expression has already
// been executed, don't need to treat the value as an expression
String actionValue = formAttributes.get("action");
actionValue += csrfQueryParameter;
formAttributes.put("action", actionValue);
} else {
Map<String, String> csrfAttributes = new HashMap<>();
csrfAttributes.put("type", "hidden");
csrfAttributes.put("name", eps.getCsrfTokenParameter());
csrfAttributes.put("value", csrfToken);
BroadleafTemplateElement csrfTag = context.createStandaloneElement("input", csrfAttributes, true);
model.addElement(csrfTag);
if (stateVersionToken != null) {
Map<String, String> stateVersionAttributes = new HashMap<>();
stateVersionAttributes.put("type", "hidden");
stateVersionAttributes.put("name", spps.getStateVersionTokenParameter());
stateVersionAttributes.put("value", stateVersionToken);
BroadleafTemplateElement stateVersionTag = context.createStandaloneElement("input", stateVersionAttributes, true);
model.addElement(stateVersionTag);
}
dto.setModel(model);
}
} catch (ServiceException e) {
throw new RuntimeException("Could not get a CSRF token for this session", e);
}
}
dto.setFormParameters(formAttributes);
dto.setReplacementTagName("form");
return dto;
}
use of org.broadleafcommerce.presentation.model.BroadleafTemplateElement in project BroadleafCommerce by BroadleafCommerce.
the class GoogleUniversalAnalyticsProcessor method getReplacementModel.
@Override
public BroadleafTemplateModel getReplacementModel(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
StringBuffer sb = new StringBuffer();
Map<String, String> trackers = getTrackers();
if (MapUtils.isNotEmpty(trackers)) {
sb.append("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
sb.append("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
sb.append("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
sb.append("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
String orderNumberExpression = tagAttributes.get("ordernumber");
String orderNumber = null;
if (orderNumberExpression != null) {
orderNumber = (String) context.parseExpression(orderNumberExpression);
}
Order order = null;
if (orderNumber != null) {
order = orderService.findOrderByOrderNumber(orderNumber);
}
for (Entry<String, String> tracker : trackers.entrySet()) {
String trackerName = tracker.getKey();
String trackerPrefix = "";
String id = tracker.getValue();
sb.append("ga('create', '" + id + "', 'auto', {");
if (!"webProperty".equals(trackerName)) {
trackerPrefix = trackerName + ".";
sb.append("'name': '" + trackerName + "'");
if (testLocal) {
sb.append(",");
}
}
if (testLocal) {
sb.append("'cookieDomain': 'none'");
}
sb.append("});");
if ("webProperty".equals(trackerName)) {
HttpServletRequest request = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
if (request != null) {
Map<String, String> setValuesMap = (Map<String, String>) request.getAttribute("blGAValuesMap");
if (setValuesMap != null) {
for (Map.Entry<String, String> entry : setValuesMap.entrySet()) {
sb.append("ga('" + trackerPrefix + "set',").append(entry.getKey()).append(",").append(entry.getValue()).append(");");
}
}
}
}
sb.append("ga('" + trackerPrefix + "send', 'pageview');");
if (isIncludeLinkAttribution()) {
sb.append(getLinkAttributionJs(trackerPrefix));
}
if (isIncludeDisplayAdvertising()) {
sb.append(getDisplayAdvertisingJs(trackerPrefix));
}
if (order != null) {
sb.append(getTransactionJs(order, trackerPrefix));
}
}
// Add contentNode to the document
BroadleafTemplateModel model = context.createModel();
BroadleafTemplateNonVoidElement scriptTag = context.createNonVoidElement("script");
BroadleafTemplateElement script = context.createTextElement(sb.toString());
scriptTag.addChild(script);
model.addElement(scriptTag);
return model;
} else {
LOG.warn("No trackers were found, not outputting Google Analytics script. Set the googleAnalytics.webPropertyId" + " and/or the googleAnalytics.masterWebPropertyId system properties to output Google Analytics");
}
return null;
}
Aggregations