1 /**
2 * This file Copyright (c) 2003-2011 Magnolia International
3 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4 *
5 *
6 * This file is dual-licensed under both the Magnolia
7 * Network Agreement and the GNU General Public License.
8 * You may elect to use one or the other of these licenses.
9 *
10 * This file is distributed in the hope that it will be
11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14 * Redistribution, except as permitted by whichever of the GPL
15 * or MNA you select, is prohibited.
16 *
17 * 1. For the GPL license (GPL), you can redistribute and/or
18 * modify this file under the terms of the GNU General
19 * Public License, Version 3, as published by the Free Software
20 * Foundation. You should have received a copy of the GNU
21 * General Public License, Version 3 along with this program;
22 * if not, write to the Free Software Foundation, Inc., 51
23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * 2. For the Magnolia Network Agreement (MNA), this file
26 * and the accompanying materials are made available under the
27 * terms of the MNA which accompanies this distribution, and
28 * is available at http://www.magnolia-cms.com/mna.html
29 *
30 * Any modifications to this file must keep this entire header
31 * intact.
32 *
33 */
34 package info.magnolia.cms.taglibs;
35
36 import java.io.Writer;
37
38 import info.magnolia.module.templating.ParagraphManager;
39 import info.magnolia.cms.beans.config.ServerConfiguration;
40 import info.magnolia.cms.core.Content;
41 import info.magnolia.cms.gui.inline.ButtonEdit;
42 import info.magnolia.cms.security.Permission;
43 import info.magnolia.context.MgnlContext;
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import javax.servlet.jsp.JspWriter;
49 import javax.servlet.jsp.tagext.TagSupport;
50
51
52 /**
53 * Displays an edit button.
54 *
55 * @jsp.tag name="editButton" body-content="empty"
56 *
57 * @author Marcel Salathe
58 * @author Fabrizio Giustina
59 * @version $Revision $ ($Author $)
60 */
61 public class EditButton extends TagSupport {
62 private static final Logger log = LoggerFactory.getLogger(EditButton.class);
63
64 private String nodeName;
65
66 private String nodeCollectionName;
67
68 private String dialog;
69
70 private String label;
71
72 private String displayHandler;
73
74 private boolean small = true;
75
76 private boolean adminOnly = true;
77
78 public int doStartTag() {
79 this.displayHandler = StringUtils.EMPTY;
80 return EVAL_BODY_INCLUDE;
81 }
82
83 public int doEndTag() {
84 if ((!adminOnly || ServerConfiguration.getInstance().isAdmin()) && MgnlContext.getAggregationState().getMainContent().isGranted(Permission.SET)) {
85
86 try {
87 if (this.getNodeCollectionName() != null && this.getNodeName() == null) {
88 // cannot draw edit button with nodeCllection and without node
89 return EVAL_PAGE;
90 }
91 JspWriter out = pageContext.getOut();
92 ButtonEdit button = new ButtonEdit();
93 button.setPath(this.getPath());
94 button.setDialog(this.getDialog());
95 button.setNodeCollectionName(this.getNodeCollectionName());
96 button.setNodeName(this.getNodeName());
97 button.setDefaultOnclick();
98 if (this.getLabel() != null) {
99 button.setLabel(this.getLabel());
100 }
101 if (this.small) {
102 button.setSmall(true);
103 }
104 button.drawHtml((Writer) out);
105 }
106 catch (Exception e) {
107 log.error(e.getMessage(), e);
108 }
109 }
110 return EVAL_PAGE;
111 }
112
113 /**
114 * If attribute is not used, a content node with the name of the page will be added. If contentNodeName attribute is set but with no value
115 * between the quotes, the properties are stored directly under the Web page. If a contentNodeName is set then a content node with the
116 * according name will be added.
117 *
118 * @param name Container name which will be used to access/write content.
119 * @jsp.attribute required="false" rtexprvalue="true"
120 */
121 public void setContentNodeName(String name) {
122 this.nodeName = name;
123 }
124
125 private String getNodeName() {
126 if (this.nodeName == null) {
127 Content current = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
128 if (current == null) {
129 return null;
130 }
131 return current.getName();
132 }
133 return this.nodeName;
134 }
135
136 /**
137 * @param name , container name which will be used to access/write content.
138 * @jsp.attribute required="false" rtexprvalue="true"
139 */
140 public void setContentNodeCollectionName(String name) {
141 this.nodeCollectionName = name;
142 }
143
144 /**
145 * @return content node collection name
146 */
147 private String getNodeCollectionName() {
148 if (this.nodeCollectionName == null) {
149 return Resource.getLocalContentNodeCollectionName();
150 }
151 return this.nodeCollectionName;
152 }
153
154 /**
155 * The paragraph type.
156 * @deprecated since 4.1, use the setDialog() instead.
157 * @param type , content type
158 * @jsp.attribute required="false" rtexprvalue="true"
159 */
160 public void setParagraph(String type) {
161 setDialog(type);
162 }
163
164 /**
165 * @return String paragraph (type of par)
166 * @deprecated since 4.1, use the getDialog() instead.
167 */
168 private String getParagraph() {
169 return getDialog();
170 }
171
172 /**
173 * The dialog type.
174 * @param type , content type
175 * @jsp.attribute required="false" rtexprvalue="true"
176 */
177 public void setDialog (String dialog) {
178 this.dialog = dialog;
179 }
180
181 public String getDialog () {
182 return this.dialog;
183 }
184
185 /**
186 * Set display handler (JSP / Servlet), needs to know the relative path from WEB-INF.
187 * TODO: deprecate this ???
188 * @param path relative to WEB-INF.
189 * @jsp.attribute required="false" rtexprvalue="true"
190 */
191 public void setTemplate(String path) {
192 this.displayHandler = path;
193 }
194
195 /**
196 * Show only in admin instance, defaults to true.
197 * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
198 */
199 public void setAdminOnly(boolean adminOnly) {
200 this.adminOnly = adminOnly;
201 }
202
203 /**
204 * @return template path
205 */
206 public String getTemplate() {
207 if (this.displayHandler == null) {
208 Content localContainer = MgnlContext.getWebContext().getAggregationState().getCurrentContent();
209 String templateName = localContainer.getNodeData("dialog").getString(); //$NON-NLS-1$
210 return ParagraphManager.getInstance().getParagraphDefinition(templateName).getTemplatePath();
211 }
212 return this.displayHandler;
213 }
214
215 /**
216 * get the content path (Page or Node).
217 * @return String path
218 */
219 private String getPath() {
220 try {
221 return MgnlContext.getWebContext().getAggregationState().getCurrentContent().getHandle();
222 }
223 catch (Exception re) {
224 return StringUtils.EMPTY;
225 }
226 }
227
228 /**
229 * @deprecated use the label attribute instead.
230 * @jsp.attribute required="false" rtexprvalue="true"
231 */
232 public void setEditLabel(String label) {
233 this.setLabel(label);
234 }
235
236 /**
237 * The button's label. (defaults to "Edit")
238 * @jsp.attribute required="false" rtexprvalue="true"
239 */
240 public void setLabel(String label) {
241 this.label = label;
242 }
243
244 /**
245 * @return String , label for the edit bar
246 */
247 private String getLabel() {
248 return this.label;
249 }
250
251 /**
252 * Sets the size of the button, true for small button, false for a large one (defaults to true).
253 * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
254 */
255 public void setSmall(boolean small) {
256 this.small = small;
257 }
258
259 public void release() {
260 this.nodeName = null;
261 this.nodeCollectionName = null;
262 this.dialog = null;
263 this.label = null;
264 this.displayHandler = null;
265 this.small = true;
266 this.adminOnly=true;
267 super.release();
268 }
269 }