1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.templating.jsp.taglib;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.core.Content;
38 import info.magnolia.cms.gui.inline.ButtonEdit;
39 import info.magnolia.cms.security.Permission;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.jcr.util.NodeUtil;
42 import info.magnolia.objectfactory.Components;
43 import info.magnolia.registry.RegistrationException;
44 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
45
46 import java.io.Writer;
47
48 import javax.servlet.jsp.JspWriter;
49 import javax.servlet.jsp.tagext.TagSupport;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 import org.tldgen.annotations.BodyContent;
56 import org.tldgen.annotations.Tag;
57
58
59
60
61
62
63
64
65
66
67
68 @Tag(name="editButton", bodyContent=BodyContent.EMPTY)
69
70 public class EditButton extends TagSupport {
71 private static final Logger log = LoggerFactory.getLogger(EditButton.class);
72
73 private String nodeName;
74
75 private String nodeCollectionName;
76
77 private String dialog;
78
79 private String label;
80
81 private String displayHandler;
82
83 private boolean small = true;
84
85 private boolean adminOnly = true;
86
87 @Override
88 public int doStartTag() {
89 this.displayHandler = StringUtils.EMPTY;
90 return EVAL_BODY_INCLUDE;
91 }
92
93 @Override
94 public int doEndTag() {
95 if ((!adminOnly || ServerConfiguration.getInstance().isAdmin()) && NodeUtil.isGranted(MgnlContext.getAggregationState().getMainContent().getJCRNode(), Permission.SET)) {
96
97 try {
98 if (this.getNodeCollectionName() != null && this.getNodeName() == null) {
99
100 return EVAL_PAGE;
101 }
102 JspWriter out = pageContext.getOut();
103 ButtonEdit button = new ButtonEdit();
104 button.setPath(this.getPath());
105 button.setDialog(this.getDialog());
106 button.setNodeCollectionName(this.getNodeCollectionName());
107 button.setNodeName(this.getNodeName());
108 button.setDefaultOnclick();
109 if (this.getLabel() != null) {
110 button.setLabel(this.getLabel());
111 }
112 if (this.small) {
113 button.setSmall(true);
114 }
115 button.drawHtml((Writer) out);
116 }
117 catch (Exception e) {
118 log.error(e.getMessage(), e);
119 }
120 }
121 return EVAL_PAGE;
122 }
123
124
125
126
127
128
129
130
131
132 public void setContentNodeName(String name) {
133 this.nodeName = name;
134 }
135
136 private String getNodeName() {
137 if (this.nodeName == null) {
138 Content current = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
139 if (current == null) {
140 return null;
141 }
142 return current.getName();
143 }
144 return this.nodeName;
145 }
146
147
148
149
150
151 public void setContentNodeCollectionName(String name) {
152 this.nodeCollectionName = name;
153 }
154
155
156
157
158 private String getNodeCollectionName() {
159 if (this.nodeCollectionName == null) {
160 try{
161 return (String) MgnlContext.getAttribute("localContentNodeCollectionName");
162 }catch (Exception e){
163 return StringUtils.EMPTY;
164 }
165 }
166 return this.nodeCollectionName;
167 }
168
169
170
171
172
173
174
175 @Deprecated
176 public void setParagraph(String type) {
177 setDialog(type);
178 }
179
180
181
182
183
184 @Deprecated
185 private String getParagraph() {
186 return getDialog();
187 }
188
189
190
191
192
193
194 public void setDialog (String dialog) {
195 this.dialog = dialog;
196 }
197
198 public String getDialog () {
199 return this.dialog;
200 }
201
202
203
204
205
206
207
208 public void setTemplate(String path) {
209 this.displayHandler = path;
210 }
211
212
213
214
215
216 public void setAdminOnly(boolean adminOnly) {
217 this.adminOnly = adminOnly;
218 }
219
220
221
222
223 public String getTemplate() {
224 if (this.displayHandler == null) {
225 Content localContainer = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
226 String templateName = localContainer.getNodeData("dialog").getString();
227
228 try {
229 return Components.getComponent(TemplateDefinitionRegistry.class).getTemplateDefinition(templateName).getTemplateScript();
230 } catch (RegistrationException e) {
231
232 throw new RuntimeException(e);
233 }
234 }
235 return this.displayHandler;
236 }
237
238
239
240
241
242 private String getPath() {
243 try {
244 return MgnlContext.getWebContext().getAggregationState().getCurrentContent().getHandle();
245 }
246 catch (Exception re) {
247 return StringUtils.EMPTY;
248 }
249 }
250
251
252
253
254
255 @Deprecated
256 public void setEditLabel(String label) {
257 this.setLabel(label);
258 }
259
260
261
262
263
264 public void setLabel(String label) {
265 this.label = label;
266 }
267
268
269
270
271 private String getLabel() {
272 return this.label;
273 }
274
275
276
277
278
279 public void setSmall(boolean small) {
280 this.small = small;
281 }
282
283 @Override
284 public void release() {
285 this.nodeName = null;
286 this.nodeCollectionName = null;
287 this.dialog = null;
288 this.label = null;
289 this.displayHandler = null;
290 this.small = true;
291 this.adminOnly=true;
292 super.release();
293 }
294 }