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