View Javadoc

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 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   * Displays Magnolia editBar which allows you to edit a paragraph. This tag is often used within
62   * contentNodeIterator, which in turn will set all relevant parameters automatically.
63   *
64   * @jsp.tag name="editBar" body-content="JSP"
65   *
66   * @author Sameer Charles
67   * @author Marcel Salathe
68   * @version $Revision: 48832 $ ($Author: had $)
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       * Show links only in admin instance.
90       */
91      private boolean adminOnly = true;
92  
93      /**
94       * Show paragraph name in the bar.
95       */
96      private boolean showParagraphName = false;
97  
98      /**
99       * The contentNode (i.e. paragraph) you wish to edit.
100      * @jsp.attribute required="false" rtexprvalue="true"
101      */
102     public void setContentNodeName(String name) {
103         this.nodeName = name;
104     }
105 
106     /**
107      * The contentNode collection.
108      * @jsp.attribute required="false" rtexprvalue="true"
109      */
110     public void setContentNodeCollectionName(String name) {
111         this.nodeCollectionName = name;
112     }
113 
114     /**
115      * Name of paragraph (as defined in config). Does not
116      * have to be set inside "contentNodeIterator".
117      * @jsp.attribute required="false" rtexprvalue="true"
118      */
119     public void setParagraph(String paragraph) {
120         this.paragraph = paragraph;
121     }
122 
123     /**
124      * Name of the dialog to open. If specified, overrides
125      * the paragraph attribute, or the dialog name determined
126      * by the current paragraph if any.
127      * @jsp.attribute required="false" rtexprvalue="true"
128      */
129     public void setDialog(String dialog) {
130         this.dialog = dialog;
131     }
132 
133     /**
134      * The text of the edit button, defaults to "Edit".
135      * @jsp.attribute required="false" rtexprvalue="true"
136      */
137     public void setEditLabel(String label) {
138         this.editLabel = label;
139     }
140 
141     /**
142      * The text of the delete button, defaults to "Delete". Use "" to get no delete button.
143      * @jsp.attribute required="false" rtexprvalue="true"
144      */
145     public void setDeleteLabel(String label) {
146         this.deleteLabel = label;
147     }
148 
149     /**
150      * The text of the move button, defaults to "Move". Use "" to get no move button.
151      * @jsp.attribute required="false" rtexprvalue="true"
152      */
153     public void setMoveLabel(String label) {
154         this.moveLabel = label;
155     }
156 
157     /**
158      * Show only in admin instance, default to true.
159      * @param adminOnly The adminOnly to set.
160      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
161      */
162     public void setAdminOnly(boolean adminOnly) {
163         this.adminOnly = adminOnly;
164     }
165 
166     public boolean isShowParagraphName() {
167         return showParagraphName;
168     }
169 
170     /**
171      * Show the paragraph name, default to false.
172      * @param showParagraphName Show the paragraph name.
173      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
174      */
175     public void setShowParagraphName(boolean showParagraphName) {
176         this.showParagraphName = showParagraphName;
177     }
178 
179     /**
180      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
181      */
182     @Override
183     public int doStartTag() {
184         return EVAL_BODY_INCLUDE;
185     }
186 
187     /**
188      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
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                 // TODO - yes this is a bit ugly - 1) we should in fact open the correct dialog immediately instead of faking the paragraph parameter - 2) the gui elements should not have defaults nor know anything about urls and onclick functions
253                 if (this.dialog == null) {
254                     bar.getButtonEdit().setDialogPath(ParagraphSelectDialog.EDITPARAGRAPH_DIALOG_URL);
255                     bar.getButtonEdit().setDefaultOnclick(); // re-set the onclick after having set the dialog path.
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 }