示例#1
0
 /**
  * Generates a html form element linked to a controller action
  *
  * @param args tag attributes
  * @param body tag inner body
  * @param out the output writer
  * @param template enclosing template
  * @param fromLine template line number where the tag is defined
  */
 public static void _form(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   ActionDefinition actionDef = (ActionDefinition) args.get("arg");
   if (actionDef == null) {
     actionDef = (ActionDefinition) args.get("action");
   }
   String enctype = (String) args.get("enctype");
   if (enctype == null) {
     enctype = "application/x-www-form-urlencoded";
   }
   if (actionDef.star) {
     actionDef.method = "POST"; // prefer POST for form ....
   }
   if (args.containsKey("method")) {
     actionDef.method = args.get("method").toString();
   }
   String name = null;
   if (args.containsKey("name")) {
     name = args.get("name").toString();
   }
   if (!("GET".equals(actionDef.method) || "POST".equals(actionDef.method))) {
     String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
     actionDef.url += separator + "x-http-method-override=" + actionDef.method.toUpperCase();
     actionDef.method = "POST";
   }
   String encoding = Http.Response.current().encoding;
   out.print(
       "<form action=\""
           + actionDef.url
           + "\" method=\""
           + actionDef.method.toLowerCase()
           + "\" accept-charset=\""
           + encoding
           + "\" enctype=\""
           + enctype
           + "\" "
           + serialize(args, "name", "action", "method", "accept-charset", "enctype")
           + (name != null ? "name=\"" + name + "\"" : "")
           + ">");
   if (!("GET".equals(actionDef.method))) {
     _authenticityToken(args, body, out, template, fromLine);
   }
   out.println(JavaExtensions.toString(body));
   out.print("</form>");
 }
示例#2
0
 /**
  * Generates a html link to a controller action
  *
  * @param args tag attributes
  * @param body tag inner body
  * @param out the output writer
  * @param template enclosing template
  * @param fromLine template line number where the tag is defined
  */
 public static void _a(
     Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
   ActionDefinition actionDef = (ActionDefinition) args.get("arg");
   if (actionDef == null) {
     actionDef = (ActionDefinition) args.get("action");
   }
   if (!("GET".equals(actionDef.method))) {
     if (!("POST".equals(actionDef.method))) {
       String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
       actionDef.url += separator + "x-http-method-override=" + actionDef.method;
       actionDef.method = "POST";
     }
     String id = Codec.UUID();
     out.print(
         "<form method=\"POST\" id=\""
             + id
             + "\" "
             + (args.containsKey("target") ? "target=\"" + args.get("target") + "\"" : "")
             + " style=\"display:none\" action=\""
             + actionDef.url
             + "\">");
     _authenticityToken(args, body, out, template, fromLine);
     out.print("</form>");
     out.print(
         "<a href=\"javascript:document.getElementById('"
             + id
             + "').submit();\" "
             + serialize(args, "href")
             + ">");
     out.print(JavaExtensions.toString(body));
     out.print("</a>");
   } else {
     out.print("<a href=\"" + actionDef.url + "\" " + serialize(args, "href") + ">");
     out.print(JavaExtensions.toString(body));
     out.print("</a>");
   }
 }
示例#3
0
  /**
   * Follow notification.
   *
   * @param id the id of the notification.
   */
  public static void followNotification(int id) {
    User user = Session.user();
    Notification notification = user.getNotification(id);
    if (notification != null) {
      notification.unsetNew();
      Entry about = notification.getAbout();

      if (about instanceof Answer) {
        ActionDefinition action = reverse();
        Answer answer = (Answer) about;
        Application.question(answer.getQuestion().id());
        redirect(action.addRef("answer-" + answer.id()).toString());
      } else if (about instanceof Question) {
        Application.question(((Question) about).id());
      } else if (about instanceof Comment) {
        ActionDefinition action = reverse();
        Comment comment = (Comment) about;
        Application.question(comment.getQuestion().id());
        redirect(action.addRef("comment-" + comment.id()).toString());
      }
    } else if (!redirectToCallingPage()) {
      Application.notifications(0);
    }
  }