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