BrightSide Workbench Full Report + Source Code
CommentItCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.www.commentit;
20 
21 import java.text.DateFormat;
22 import java.util.Collection;
23 import org.amic.util.date.DateFormats;
24 import org.turro.string.Strings;
25 import org.turro.auth.Authentication;
26 import org.turro.commentit.CommentItUtil;
27 import org.turro.contacts.CommentIt;
28 import org.turro.elephant.context.ElephantContext;
29 import org.turro.elephant.context.IConstructor;
30 import org.turro.elephant.impl.util.StringParser;
31 import org.turro.elephant.security.IUser;
32 import org.turro.html.HTMLHelper;
33 import org.turro.i18n.I_;
34 import org.turro.marker.ElephantMarker;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.plugin.contacts.SoftContact;
37 import org.turro.util.IdGenerator;
38 
43 public class CommentItCtrl {
44 
45  private HTMLHelper html;
46  private String entityPath;
47  private final IConstructor constructor;
48  private boolean readOnly;
49 
50  public CommentItCtrl(IConstructor constructor) {
51  this.constructor = constructor;
52  }
53 
54  public void setEntityPath(String entityPath) {
55  this.entityPath = entityPath;
56  }
57 
58  public void setReadOnly(boolean readOnly) {
59  this.readOnly = readOnly;
60  }
61 
62  public void renderTemplateComments(boolean container) {
64  SoftContact scontact = new SoftContact();
65  scontact.contact = contact;
66  scontact.author_ip = constructor.getRequest().getRemoteAddr();
67  Collection<CommentIt> comments = CommentItUtil.comments(entityPath, scontact);
68  String id = container ? "cic_" + IdGenerator.generate() : constructor.getParameter("domid");
69  ElephantMarker em = new ElephantMarker(constructor);
70  em.put("control", this);
71  em.put("contact", contact);
72  em.put("controlId", id);
73  em.put("container", container);
74  em.put("comments", comments);
75  em.put("entityPath", entityPath);
76  em.put("readOnly", readOnly);
77  if(contact != null && contact.isValid()) {
78  em.put("name", contact.getName());
79  em.put("email", contact.getConnector(IUser.CONNECTOR_EMAIL));
80  em.put("web", contact.getConnector("Web"));
81  }
82  em.process("widgets/webcomp", "commentIt");
83  }
84 
85  public void renderComments(boolean container) {
86  if(ElephantMarker.existsTemplate(constructor, false, "widgets/webcomp", "commentIt")) {
87  renderTemplateComments(container);
88  return;
89  }
91  SoftContact scontact = new SoftContact();
92  scontact.contact = contact;
93  scontact.author_ip = constructor.getRequest().getRemoteAddr();
94  Collection<CommentIt> comments = CommentItUtil.comments(entityPath, scontact);
95  html = new HTMLHelper(constructor);
96  String id = container ? "cic_" + IdGenerator.generate() : constructor.getParameter("domid");
97  if(container) {
98  html.startTag("div", "id='" + id + "'");
99  }
100  for(CommentIt ci : comments) {
101  String cid = "cid" + ci.getId();
102  html.startTag("div", "class='commentit-comment' id='" + cid + "'")
103  .startTag("div", "class='commentit-details'")
104  .startTag("span", "class='author'")
105  .write(ci.getName())
106  .endTag()
107  .startTag("span", "class='date'")
108  .write(DateFormats.format(ci.getDateCreation(), DateFormat.SHORT, DateFormat.SHORT, I_.api().used()))
109  .endTag()
110  .startTag("span", "class='web'")
111  .write(ci.getWeb())
112  .endTag()
113  .startTag("a", "onClick='" + getRemoveUrl(ci.getId(), cid) + "' style='display:" + (ci.isAccepted() ? "none" : "") + "'")
114  .doTag("img", "src='" + ElephantContext.getRootWebPath() + "/_internal/system/images/delete.png'")
115  .endTag()
116  .endTag()
117  .startTag("div", "class='body'")
118  .write(StringParser.toHTML(ci.getBody()))
119  .endTag()
120  .endTag();
121  }
122  if(!readOnly) {
123  renderNewComment(html, id, contact);
124  }
125  html.endAllTags();
126  }
127 
128  public String getSubmitUrl(String id) {
129  return "$.post(\"" + ElephantContext.getRootWebPath() + "/xpaction/contact\"," +
130  "$(\"#form_" + id + "\").serialize())" +
131  ".done(function(data) { $(\"#" + id + "\").html(data); });" +
132  "return false;";
133  }
134 
135  public String getRemoveUrl(String commentId, String id) {
136  return "$.post(\"" + ElephantContext.getRootWebPath() + "/xpaction/contact\"," +
137  "{ action: \"comment-it\", id: \"" + commentId + "\", remove: true, domid: \"" + id + "\" })" +
138  ".done(function(data) { $(\"#" + id + "\").html(data); });";
139  }
140 
141  public String toHTML(String value) {
142  return StringParser.toHTML(value);
143  }
144 
145  private void renderNewComment(HTMLHelper html, String id, IContact contact) {
146  String name = null, email = null, web = null;
147  if(contact != null && contact.isValid()) {
148  name = contact.getName();
149  email = contact.getConnector(IUser.CONNECTOR_EMAIL);
150  web = contact.getConnector("Web");
151  }
152  html.startTag("form", "class='commentit-new' id='form_" + id + "'")
153  .startTable("width='100%'")
154  .startTableCol("")
155  .write(I_.get("Name"))
156  .startTableCol("")
157  .doTag("input", "name='author' value='" +
158  (!Strings.isBlank(name) ? name : "") + "' style='width:100%'")
159  .endTableRow()
160  .startTableCol("")
161  .write(I_.get("Email"))
162  .startTableCol("")
163  .doTag("input", "name='author_email' value='" +
164  (!Strings.isBlank(email) ? email : "") + "' style='width:100%'")
165  .endTableRow()
166  .startTableCol("")
167  .write(I_.get("Site"))
168  .startTableCol("")
169  .doTag("input", "name='author_web' value='" +
170  (!Strings.isBlank(web) ? web : "") + "' style='width:100%'")
171  .endTableRow()
172  .startTableCol("colspan='2'")
173  .startTag("textarea", "name='body' rows='8' style='width:100%'").endTag()
174  .endTableRow()
175  .startTableCol("colspan='2' style='text-align:right'")
176  .doTag("input", "type='hidden' name='action' value='comment-it'")
177  .doTag("input", "type='hidden' name='path' value='" + entityPath + "'")
178  .doTag("input", "type='hidden' name='domid' value='" + id + "'")
179  .startTag("button", "onClick='" + getSubmitUrl(id) + "'")
180  .write(I_.get("Add comment"))
181  .endTag()
182  .endTable()
183  .endTag();
184  }
185 
186 }
static Collection< CommentIt > comments(Object entity, SoftContact contact)
static String toHTML(String value)
HTMLGenerator write(String value)
HTMLGenerator doTag(String tag)
HTMLGenerator startTag(String tag)
HTMLGenerator startTableCol(String attributes)
HTMLGenerator startTable(String attributes)
static I18nApiWrapper api()
Definition: I_.java:65
void process(String rootTmpl, String tmpl)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)
Object put(Object key, Object value)
String getRemoveUrl(String commentId, String id)
CommentItCtrl(IConstructor constructor)
void setEntityPath(String entityPath)
void renderTemplateComments(boolean container)
void renderComments(boolean container)
static final String CONNECTOR_EMAIL
Definition: IUser.java:27