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.cms.taglibs;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.core.AggregationState;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.gui.control.Button;
40 import info.magnolia.cms.gui.inline.BarEdit;
41 import info.magnolia.cms.i18n.Messages;
42 import info.magnolia.cms.i18n.MessagesManager;
43 import info.magnolia.cms.security.Permission;
44 import info.magnolia.context.MgnlContext;
45 import info.magnolia.jcr.util.NodeUtil;
46 import info.magnolia.module.admininterface.dialogs.ParagraphSelectDialog;
47 import info.magnolia.objectfactory.Components;
48 import info.magnolia.registry.RegistrationException;
49 import info.magnolia.rendering.template.TemplateDefinition;
50 import info.magnolia.rendering.template.registry.TemplateDefinitionRegistry;
51
52 import java.io.IOException;
53
54 import javax.servlet.jsp.tagext.TagSupport;
55
56 import org.apache.commons.lang.StringUtils;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60
61
62
63
64
65
66
67
68
69
70 public class EditBar extends TagSupport {
71
72 private String nodeName;
73
74 private String nodeCollectionName;
75
76 private String paragraph;
77
78 private String dialog;
79
80 private String editLabel;
81
82 private String deleteLabel;
83
84 private String moveLabel;
85
86 private static final Logger log = LoggerFactory.getLogger(EditBar.class);
87
88
89
90
91 private boolean adminOnly = true;
92
93
94
95
96 private boolean showParagraphName = false;
97
98
99
100
101
102 public void setContentNodeName(String name) {
103 this.nodeName = name;
104 }
105
106
107
108
109
110 public void setContentNodeCollectionName(String name) {
111 this.nodeCollectionName = name;
112 }
113
114
115
116
117
118
119 public void setParagraph(String paragraph) {
120 this.paragraph = paragraph;
121 }
122
123
124
125
126
127
128
129 public void setDialog(String dialog) {
130 this.dialog = dialog;
131 }
132
133
134
135
136
137 public void setEditLabel(String label) {
138 this.editLabel = label;
139 }
140
141
142
143
144
145 public void setDeleteLabel(String label) {
146 this.deleteLabel = label;
147 }
148
149
150
151
152
153 public void setMoveLabel(String label) {
154 this.moveLabel = label;
155 }
156
157
158
159
160
161
162 public void setAdminOnly(boolean adminOnly) {
163 this.adminOnly = adminOnly;
164 }
165
166 public boolean isShowParagraphName() {
167 return showParagraphName;
168 }
169
170
171
172
173
174
175 public void setShowParagraphName(boolean showParagraphName) {
176 this.showParagraphName = showParagraphName;
177 }
178
179
180
181
182 @Override
183 public int doStartTag() {
184 return EVAL_BODY_INCLUDE;
185 }
186
187
188
189
190 @Override
191 public int doEndTag() {
192
193 final AggregationState aggregationState = MgnlContext.getAggregationState();
194 if ((!adminOnly || ServerConfiguration.getInstance().isAdmin()) && NodeUtil.isGranted(aggregationState.getMainContent().getJCRNode(), Permission.SET)) {
195 try {
196 BarEdit bar = new BarEdit();
197
198 Content localContentNode = Resource.getLocalContentNode();
199
200 if(StringUtils.isNotEmpty(this.nodeName)){
201 try {
202 if (localContentNode.hasContent(this.nodeName)) {
203 localContentNode = localContentNode.getContent(this.nodeName);
204 } else {
205 localContentNode = null;
206 }
207 } catch (Exception e) {
208 log.warn(e.getMessage(), e);
209 }
210 }
211
212 final String paragraphToUse;
213 if (this.dialog != null) {
214 paragraphToUse = this.dialog;
215 } else if (this.paragraph == null && localContentNode != null) {
216 paragraphToUse = localContentNode.getMetaData().getTemplate();
217 } else {
218 paragraphToUse = this.paragraph;
219 }
220 bar.setParagraph(paragraphToUse);
221
222 if (this.nodeCollectionName == null) {
223 this.nodeCollectionName = StringUtils.defaultString(Resource.getLocalContentNodeCollectionName());
224 }
225 bar.setNodeCollectionName(this.nodeCollectionName);
226
227 if (this.nodeName == null) {
228 if (localContentNode != null) {
229 this.nodeName = localContentNode.getName();
230 }
231 }
232 bar.setNodeName(this.nodeName);
233
234 try {
235 String path;
236 if (localContentNode != null) {
237 path = localContentNode.getParent().getHandle();
238 if (StringUtils.isNotEmpty(this.nodeCollectionName) && path.endsWith("/" + this.nodeCollectionName)) {
239 path = StringUtils.removeEnd(path, "/" + this.nodeCollectionName);
240 }
241 }
242 else {
243 path = Resource.getCurrentActivePage().getHandle();
244 }
245 bar.setPath(path);
246 }
247 catch (Exception re) {
248 bar.setPath(StringUtils.EMPTY);
249 }
250
251 bar.setDefaultButtons();
252
253 if (this.dialog == null) {
254 bar.getButtonEdit().setDialogPath(ParagraphSelectDialog.EDITPARAGRAPH_DIALOG_URL);
255 bar.getButtonEdit().setDefaultOnclick();
256 }
257
258 if (this.editLabel != null) {
259 if (StringUtils.isEmpty(this.editLabel)) {
260 bar.setButtonEdit(null);
261 }
262 else {
263 bar.getButtonEdit().setLabel(this.editLabel);
264 }
265 }
266
267 if (this.moveLabel != null) {
268 if (StringUtils.isEmpty(this.moveLabel)) {
269 bar.setButtonMove(null);
270 }
271 else {
272 bar.getButtonMove().setLabel(this.moveLabel);
273 }
274 }
275
276 if (this.deleteLabel != null) {
277 if (StringUtils.isEmpty(this.deleteLabel)) {
278 bar.setButtonDelete((Button)null);
279 }
280 else {
281 bar.getButtonDelete().setLabel(this.deleteLabel);
282 }
283 }
284 bar.placeDefaultButtons();
285
286 if (isShowParagraphName() && this.dialog == null) {
287 final TemplateDefinition paragraphInfo = Components.getComponent(TemplateDefinitionRegistry.class).get(paragraphToUse);
288 final Messages msgs = MessagesManager.getMessages(paragraphInfo.getI18nBasename());
289 final String label = msgs.getWithDefault(paragraphInfo.getTitle(), paragraphInfo.getTitle());
290 bar.setLabel(label);
291 }
292
293 bar.drawHtml(pageContext.getOut());
294 }
295 catch (IOException e) {
296 throw new RuntimeException(e);
297 }
298 catch (RegistrationException e1) {
299 throw new RuntimeException(e1);
300 }
301 }
302 reset();
303
304 return EVAL_PAGE;
305 }
306
307 protected void reset() {
308 this.nodeName = null;
309 this.nodeCollectionName = null;
310 this.paragraph = null;
311 this.dialog = null;
312 this.editLabel = null;
313 this.deleteLabel = null;
314 this.moveLabel = null;
315 this.adminOnly = true;
316 this.showParagraphName = false;
317 }
318
319 }