Search in sources :

Example 1 with BroadleafTemplateModel

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModel in project BroadleafCommerce by BroadleafCommerce.

the class ResourceBundleProcessor method getReplacementModel.

@Override
public BroadleafTemplateModel getReplacementModel(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    String name = tagAttributes.get("name");
    String mappingPrefix = tagAttributes.get("mapping-prefix");
    boolean async = tagAttributes.containsKey("async");
    boolean defer = tagAttributes.containsKey("defer");
    boolean includeAsyncDeferUnbundled = tagAttributes.containsKey("includeAsyncDeferUnbundled") && Boolean.parseBoolean(tagAttributes.get("includeAsyncDeferUnbundled"));
    String dependencyEvent = tagAttributes.get("bundle-dependency-event");
    List<String> files = new ArrayList<>();
    for (String file : tagAttributes.get("files").split(",")) {
        files.add(file.trim());
    }
    List<String> additionalBundleFiles = bundlingService.getAdditionalBundleFiles(name);
    if (additionalBundleFiles != null) {
        files.addAll(additionalBundleFiles);
    }
    BroadleafTemplateModel model = context.createModel();
    if (getBundleEnabled()) {
        String bundleResourceName = bundlingService.resolveBundleResourceName(name, mappingPrefix, files);
        String bundleUrl = getBundleUrl(bundleResourceName, context);
        addElementToModel(bundleUrl, async, defer, dependencyEvent, context, model);
    } else {
        if (async) {
            async = includeAsyncDeferUnbundled;
        }
        if (defer) {
            defer = includeAsyncDeferUnbundled;
        }
        for (String fileName : files) {
            fileName = fileName.trim();
            String fullFileName = (String) context.parseExpression("@{'" + mappingPrefix + fileName + "'}");
            addElementToModel(fullFileName, async, defer, context, model);
        }
    }
    return model;
}
Also used : BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) ArrayList(java.util.ArrayList)

Example 2 with BroadleafTemplateModel

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModel 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;
}
Also used : BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel)

Example 3 with BroadleafTemplateModel

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModel 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");
}
Also used : HashMap(java.util.HashMap) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) PaymentException(org.broadleafcommerce.common.vendor.service.exception.PaymentException) BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) BroadleafTemplateModelModifierDTO(org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO) PaymentRequestDTO(org.broadleafcommerce.common.payment.dto.PaymentRequestDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with BroadleafTemplateModel

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModel 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;
}
Also used : BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) ServiceException(org.broadleafcommerce.common.exception.ServiceException) HashMap(java.util.HashMap) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) BroadleafTemplateModelModifierDTO(org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO)

Example 5 with BroadleafTemplateModel

use of org.broadleafcommerce.presentation.model.BroadleafTemplateModel 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;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) HttpServletRequest(javax.servlet.http.HttpServletRequest) BroadleafTemplateElement(org.broadleafcommerce.presentation.model.BroadleafTemplateElement) BroadleafTemplateModel(org.broadleafcommerce.presentation.model.BroadleafTemplateModel) HashMap(java.util.HashMap) Map(java.util.Map) BroadleafTemplateNonVoidElement(org.broadleafcommerce.presentation.model.BroadleafTemplateNonVoidElement)

Aggregations

BroadleafTemplateModel (org.broadleafcommerce.presentation.model.BroadleafTemplateModel)6 BroadleafTemplateElement (org.broadleafcommerce.presentation.model.BroadleafTemplateElement)5 HashMap (java.util.HashMap)3 Map (java.util.Map)2 BroadleafTemplateModelModifierDTO (org.broadleafcommerce.presentation.model.BroadleafTemplateModelModifierDTO)2 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 PaymentRequestDTO (org.broadleafcommerce.common.payment.dto.PaymentRequestDTO)1 PaymentException (org.broadleafcommerce.common.vendor.service.exception.PaymentException)1 Order (org.broadleafcommerce.core.order.domain.Order)1 BroadleafTemplateNonVoidElement (org.broadleafcommerce.presentation.model.BroadleafTemplateNonVoidElement)1