Beispiel #1
0
  public static void makeHiddenFormLinkForm(
      Appendable writer,
      String target,
      String targetType,
      String targetWindow,
      Map<String, String> parameterMap,
      ModelFormField modelFormField,
      HttpServletRequest request,
      HttpServletResponse response,
      Map<String, Object> context)
      throws IOException {
    writer.append("<form method=\"post\"");
    writer.append(" action=\"");
    // note that this passes null for the parameterList on purpose so they won't be put into the URL
    WidgetWorker.buildHyperlinkUrl(
        writer, target, targetType, null, null, false, false, true, request, response, context);
    writer.append("\"");

    if (UtilValidate.isNotEmpty(targetWindow)) {
      writer.append(" target=\"");
      writer.append(targetWindow);
      writer.append("\"");
    }

    writer.append(" onsubmit=\"javascript:submitFormDisableSubmits(this)\"");

    writer.append(" name=\"");
    writer.append(makeLinkHiddenFormName(context, modelFormField));
    writer.append("\">");

    for (Map.Entry<String, String> parameter : parameterMap.entrySet()) {
      if (parameter.getValue() != null) {
        writer.append("<input name=\"");
        writer.append(parameter.getKey());
        writer.append("\" value=\"");
        writer.append(StringUtil.htmlEncoder.encode(parameter.getValue()));
        writer.append("\" type=\"hidden\"/>");
      }
    }

    writer.append("</form>");
  }
  protected void renderScreenletPaginateMenu(
      Appendable writer, Map<String, Object> context, ModelScreenWidget.Form form)
      throws IOException {
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    HttpServletRequest request = (HttpServletRequest) context.get("request");
    ModelForm modelForm = form.getModelForm(context);
    modelForm.runFormActions(context);
    modelForm.preparePager(context);
    String targetService = modelForm.getPaginateTarget(context);
    if (targetService == null) {
      targetService = "${targetService}";
    }

    // get the parametrized pagination index and size fields
    int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
    String viewIndexParam = modelForm.getMultiPaginateIndexField(context);
    String viewSizeParam = modelForm.getMultiPaginateSizeField(context);

    int viewIndex = modelForm.getViewIndex(context);
    int viewSize = modelForm.getViewSize(context);
    int listSize = modelForm.getListSize(context);

    int highIndex = modelForm.getHighIndex(context);
    int actualPageSize = modelForm.getActualPageSize(context);

    // if this is all there seems to be (if listSize < 0, then size is unknown)
    if (actualPageSize >= listSize && listSize >= 0) return;

    // needed for the "Page" and "rows" labels
    Map<String, String> uiLabelMap = UtilGenerics.cast(context.get("uiLabelMap"));
    String ofLabel = "";
    if (uiLabelMap == null) {
      Debug.logWarning("Could not find uiLabelMap in context", module);
    } else {
      ofLabel = uiLabelMap.get("CommonOf");
      ofLabel = ofLabel.toLowerCase();
    }

    // for legacy support, the viewSizeParam is VIEW_SIZE and viewIndexParam is VIEW_INDEX when the
    // fields are "viewSize" and "viewIndex"
    if (viewIndexParam.equals("viewIndex" + "_" + paginatorNumber))
      viewIndexParam = "VIEW_INDEX" + "_" + paginatorNumber;
    if (viewSizeParam.equals("viewSize" + "_" + paginatorNumber))
      viewSizeParam = "VIEW_SIZE" + "_" + paginatorNumber;

    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");

    Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
    // strip out any multi form fields if the form is of type multi
    if (modelForm.getType().equals("multi")) {
      inputFields = UtilHttp.removeMultiFormParameters(inputFields);
    }
    String queryString = UtilHttp.urlEncodeArgs(inputFields);
    // strip legacy viewIndex/viewSize params from the query string
    queryString = UtilHttp.stripViewParamsFromQueryString(queryString, "" + paginatorNumber);
    // strip parametrized index/size params from the query string
    HashSet<String> paramNames = new HashSet<String>();
    paramNames.add(viewIndexParam);
    paramNames.add(viewSizeParam);
    queryString = UtilHttp.stripNamedParamsFromQueryString(queryString, paramNames);

    String anchor = "";
    String paginateAnchor = modelForm.getPaginateTargetAnchor();
    if (paginateAnchor != null) anchor = "#" + paginateAnchor;

    // preparing the link text, so that later in the code we can reuse this and just add the
    // viewIndex
    String prepLinkText = "";
    prepLinkText = targetService;
    if (prepLinkText.indexOf("?") < 0) {
      prepLinkText += "?";
    } else if (!prepLinkText.endsWith("?")) {
      prepLinkText += "&amp;";
    }
    if (!UtilValidate.isEmpty(queryString) && !queryString.equals("null")) {
      prepLinkText += queryString + "&amp;";
    }
    prepLinkText += viewSizeParam + "=" + viewSize + "&amp;" + viewIndexParam + "=";

    String linkText;

    // The current screenlet title bar navigation syling requires rendering
    // these links in reverse order
    // Last button
    String lastLinkUrl = "";
    if (highIndex < listSize) {
      int page = (listSize / viewSize);
      linkText = prepLinkText + page + anchor;
      lastLinkUrl = rh.makeLink(request, response, linkText);
    }
    String nextLinkUrl = "";
    if (highIndex < listSize) {
      linkText = prepLinkText + (viewIndex + 1) + anchor;
      // - make the link
      nextLinkUrl = rh.makeLink(request, response, linkText);
    }
    String previousLinkUrl = "";
    if (viewIndex > 0) {
      linkText = prepLinkText + (viewIndex - 1) + anchor;
      previousLinkUrl = rh.makeLink(request, response, linkText);
    }
    String firstLinkUrl = "";
    if (viewIndex > 0) {
      linkText = prepLinkText + 0 + anchor;
      firstLinkUrl = rh.makeLink(request, response, linkText);
    }

    Map<String, Object> parameters = FastMap.newInstance();
    parameters.put("lowIndex", modelForm.getLowIndex(context));
    parameters.put("actualPageSize", actualPageSize);
    parameters.put("ofLabel", ofLabel);
    parameters.put("listSize", listSize);
    parameters.put("paginateLastStyle", modelForm.getPaginateLastStyle());
    parameters.put("lastLinkUrl", lastLinkUrl);
    parameters.put("paginateLastLabel", modelForm.getPaginateLastLabel(context));
    parameters.put("paginateNextStyle", modelForm.getPaginateNextStyle());
    parameters.put("nextLinkUrl", nextLinkUrl);
    parameters.put("paginateNextLabel", modelForm.getPaginateNextLabel(context));
    parameters.put("paginatePreviousStyle", modelForm.getPaginatePreviousStyle());
    parameters.put("paginatePreviousLabel", modelForm.getPaginatePreviousLabel(context));
    parameters.put("previousLinkUrl", previousLinkUrl);
    parameters.put("paginateFirstStyle", modelForm.getPaginateFirstStyle());
    parameters.put("paginateFirstLabel", modelForm.getPaginateFirstLabel(context));
    parameters.put("firstLinkUrl", firstLinkUrl);
    executeMacro(writer, "renderScreenletPaginateMenu", parameters);
  }
  public void renderLink(
      Appendable writer, Map<String, Object> context, ModelScreenWidget.Link link)
      throws IOException {
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    HttpServletRequest request = (HttpServletRequest) context.get("request");

    String targetWindow = link.getTargetWindow(context);
    String target = link.getTarget(context);

    String uniqueItemName =
        link.getModelScreen().getName()
            + "_LF_"
            + UtilMisc.<String>addToBigDecimalInMap(
                context, "screenUniqueItemIndex", BigDecimal.ONE);

    String linkType =
        WidgetWorker.determineAutoLinkType(link.getLinkType(), target, link.getUrlMode(), request);
    String linkUrl = "";
    String actionUrl = "";
    StringBuilder parameters = new StringBuilder();
    String width = link.getWidth();
    if (UtilValidate.isEmpty(width)) {
      width = "300";
    }
    String height = link.getHeight();
    if (UtilValidate.isEmpty(height)) {
      height = "200";
    }
    if ("hidden-form".equals(linkType) || "ajax-window".equals(linkType)) {
      StringBuilder sb = new StringBuilder();
      WidgetWorker.buildHyperlinkUrl(
          sb,
          target,
          link.getUrlMode(),
          null,
          link.getPrefix(context),
          link.getFullPath(),
          link.getSecure(),
          link.getEncode(),
          request,
          response,
          context);
      actionUrl = sb.toString();
      parameters.append("[");
      for (Map.Entry<String, String> parameter : link.getParameterMap(context).entrySet()) {
        if (parameters.length() > 1) {
          parameters.append(",");
        }
        parameters.append("{'name':'");
        parameters.append(parameter.getKey());
        parameters.append("'");
        parameters.append(",'value':'");
        parameters.append(parameter.getValue());
        parameters.append("'}");
      }
      parameters.append("]");
    }
    String id = link.getId(context);
    String style = link.getStyle(context);
    String name = link.getName(context);
    String text = link.getText(context);
    if (UtilValidate.isNotEmpty(target)) {
      if (!"hidden-form".equals(linkType)) {
        StringBuilder sb = new StringBuilder();
        WidgetWorker.buildHyperlinkUrl(
            sb,
            target,
            link.getUrlMode(),
            link.getParameterMap(context),
            link.getPrefix(context),
            link.getFullPath(),
            link.getSecure(),
            link.getEncode(),
            request,
            response,
            context);
        linkUrl = sb.toString();
      }
    }
    String imgStr = "";
    ModelScreenWidget.Image img = link.getImage();
    if (img != null) {
      StringWriter sw = new StringWriter();
      renderImage(sw, context, img);
      imgStr = sw.toString();
    }
    StringWriter sr = new StringWriter();
    sr.append("<@renderLink ");
    sr.append("parameterList=");
    sr.append(parameters.length() == 0 ? "\"\"" : parameters.toString());
    sr.append(" targetWindow=\"");
    sr.append(targetWindow);
    sr.append("\" target=\"");
    sr.append(target);
    sr.append("\" uniqueItemName=\"");
    sr.append(uniqueItemName);
    sr.append("\" linkType=\"");
    sr.append(linkType);
    sr.append("\" actionUrl=\"");
    sr.append(actionUrl);
    sr.append("\" id=\"");
    sr.append(id);
    sr.append("\" style=\"");
    sr.append(style);
    sr.append("\" name=\"");
    sr.append(name);
    sr.append("\" width=\"");
    sr.append(width);
    sr.append("\" height=\"");
    sr.append(height);
    sr.append("\" linkUrl=\"");
    sr.append(linkUrl);
    sr.append("\" text=\"");
    sr.append(text);
    sr.append("\" imgStr=\"");
    sr.append(imgStr.replaceAll("\"", "\\\\\""));
    sr.append("\" />");
    executeMacro(writer, sr.toString());
  }
Beispiel #4
0
  public static void makeHyperlinkByType(
      Appendable writer,
      String linkType,
      String linkStyle,
      String targetType,
      String target,
      Map<String, String> parameterMap,
      String description,
      String targetWindow,
      String confirmation,
      ModelFormField modelFormField,
      HttpServletRequest request,
      HttpServletResponse response,
      Map<String, Object> context)
      throws IOException {
    String realLinkType = WidgetWorker.determineAutoLinkType(linkType, target, targetType, request);
    if ("hidden-form".equals(realLinkType)) {
      if (modelFormField != null && "multi".equals(modelFormField.getModelForm().getType())) {
        WidgetWorker.makeHiddenFormLinkAnchor(
            writer,
            linkStyle,
            description,
            confirmation,
            modelFormField,
            request,
            response,
            context);

        // this is a bit trickier, since we can't do a nested form we'll have to put the link to
        // submit the form in place, but put the actual form def elsewhere, ie after the big form is
        // closed
        Map<String, Object> wholeFormContext =
            UtilGenerics.checkMap(context.get("wholeFormContext"));
        Appendable postMultiFormWriter =
            wholeFormContext != null
                ? (Appendable) wholeFormContext.get("postMultiFormWriter")
                : null;
        if (postMultiFormWriter == null) {
          postMultiFormWriter = new StringWriter();
          wholeFormContext.put("postMultiFormWriter", postMultiFormWriter);
        }
        WidgetWorker.makeHiddenFormLinkForm(
            postMultiFormWriter,
            target,
            targetType,
            targetWindow,
            parameterMap,
            modelFormField,
            request,
            response,
            context);
      } else {
        WidgetWorker.makeHiddenFormLinkForm(
            writer,
            target,
            targetType,
            targetWindow,
            parameterMap,
            modelFormField,
            request,
            response,
            context);
        WidgetWorker.makeHiddenFormLinkAnchor(
            writer,
            linkStyle,
            description,
            confirmation,
            modelFormField,
            request,
            response,
            context);
      }
    } else {
      WidgetWorker.makeHyperlinkString(
          writer,
          linkStyle,
          targetType,
          target,
          parameterMap,
          description,
          confirmation,
          modelFormField,
          request,
          response,
          context,
          targetWindow);
    }
  }
 @Override
 public void renderLink(Appendable writer, Map<String, Object> context, MenuLink link)
     throws IOException {
   Map<String, Object> parameters = new HashMap<String, Object>();
   String target = link.getTarget(context);
   ModelMenuItem menuItem = link.getLinkMenuItem();
   if (isDisableIfEmpty(menuItem, context)) {
     target = null;
   }
   parameters.put("id", link.getId(context));
   parameters.put("style", link.getStyle(context));
   parameters.put("name", link.getName(context));
   parameters.put("text", link.getText(context));
   parameters.put("targetWindow", link.getTargetWindow(context));
   String uniqueItemName =
       menuItem.getModelMenu().getName()
           + "_"
           + menuItem.getName()
           + "_LF_"
           + UtilMisc.<String>addToBigDecimalInMap(context, "menuUniqueItemIndex", BigDecimal.ONE);
   if (menuItem.getModelMenu().getExtraIndex(context) != null) {
     uniqueItemName += "_" + menuItem.getModelMenu().getExtraIndex(context);
   }
   parameters.put("uniqueItemName", uniqueItemName);
   String linkType = "";
   if (UtilValidate.isNotEmpty(target)) {
     linkType =
         WidgetWorker.determineAutoLinkType(
             link.getLinkType(), target, link.getUrlMode(), request);
   }
   parameters.put("linkType", linkType);
   String linkUrl = "";
   String actionUrl = "";
   StringBuilder targetParameters = new StringBuilder();
   if ("hidden-form".equals(linkType) || "ajax-window".equals(linkType)) {
     StringBuilder sb = new StringBuilder();
     WidgetWorker.buildHyperlinkUrl(
         sb,
         target,
         link.getUrlMode(),
         null,
         link.getPrefix(context),
         link.getFullPath(),
         link.getSecure(),
         link.getEncode(),
         request,
         response,
         context);
     actionUrl = sb.toString();
     targetParameters.append("[");
     for (Map.Entry<String, String> parameter : link.getParameterMap(context).entrySet()) {
       if (targetParameters.length() > 1) {
         targetParameters.append(",");
       }
       targetParameters.append("{'name':'");
       targetParameters.append(parameter.getKey());
       targetParameters.append("'");
       targetParameters.append(",'value':'");
       targetParameters.append(parameter.getValue());
       targetParameters.append("'}");
     }
     targetParameters.append("]");
   }
   if (targetParameters.length() == 0) {
     targetParameters.append("\"\"");
   }
   if (UtilValidate.isNotEmpty(target)) {
     if (!"hidden-form".equals(linkType)) {
       StringBuilder sb = new StringBuilder();
       WidgetWorker.buildHyperlinkUrl(
           sb,
           target,
           link.getUrlMode(),
           link.getParameterMap(context),
           link.getPrefix(context),
           link.getFullPath(),
           link.getSecure(),
           link.getEncode(),
           request,
           response,
           context);
       linkUrl = sb.toString();
     }
   }
   parameters.put("linkUrl", linkUrl);
   parameters.put("actionUrl", actionUrl);
   parameters.put("parameterList", targetParameters);
   String imgStr = "";
   Image img = link.getImage();
   if (img != null) {
     StringWriter sw = new StringWriter();
     renderImage(sw, context, img);
     imgStr = sw.toString();
   }
   parameters.put("imgStr", imgStr);
   try {
     executeMacro(writer, "renderLink", parameters);
   } catch (TemplateException e) {
     throw new IOException(e);
   }
 }