View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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.module.admininterface;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.security.Permission;
40  import info.magnolia.cms.util.NodeDataUtil;
41  import info.magnolia.context.MgnlContext;
42  
43  import java.text.MessageFormat;
44  import java.util.Iterator;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * This class is used to create the menud
53   * @author Philipp Bracher
54   * @version $Revision: 39191 $ ($Author: ochytil $)
55   */
56  public class Navigation {
57  
58  	private static final String CP_PREFIX = "contextPath + ";
59  
60      Logger log = LoggerFactory.getLogger(Navigation.class);
61  
62      /**
63       * The node containing the menu and submenupoints
64       */
65      Content node;
66  
67      /**
68       * The name of the Javascript variable
69       */
70      String jsName;
71  
72      /**
73       * @param path the path to the menu
74       */
75      public Navigation(String path, String jsName) {
76          try {
77              // get it with system permission
78              this.node = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG).getContent(path);
79              this.jsName = jsName;
80          }
81          catch (Exception e) {
82              log.error("can't initialize the menu", e);
83          }
84      }
85  
86      /**
87       * Generate the code to initialize the js navigation
88       * @return the javascript
89       */
90      public String getJavascript() {
91          StringBuffer str = new StringBuffer();
92  
93          // name, id, text, link, icon
94          String nodePattern = "{0}.addNode (\"{1}\", \"{2}\", \"{3}\", {4}\"{5}\");\n";
95          // name, parentId, id, text, link, icon
96          String subPattern = "{0}.getNode(\"{1}\").addNode (\"{2}\", \"{3}\", \"{4}\", {5}\"{6}\");\n";
97  
98          // loop over the menupoints
99          for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
100             Content mp = (Content) iter.next();
101             // check permission
102             if (isMenuPointRendered(mp)) {
103                 String icon = NodeDataUtil.getString(mp, "icon");
104                 String contextPath = "";
105                 if (!"".equals(icon)) {
106                     contextPath = CP_PREFIX;
107                 }
108                 str.append(MessageFormat.format(nodePattern, jsName,
109                 mp.getUUID(),
110                 getLabel(mp),
111                 NodeDataUtil.getString(mp, "onclick"),
112                 contextPath,
113                 icon));
114 
115 
116                 // sub menupoints (2 level only)
117                 for (Iterator iterator = mp.getChildren(ItemType.CONTENTNODE).iterator(); iterator.hasNext();) {
118                     Content sub = (Content) iterator.next();
119                     if (isMenuPointRendered(sub)) {
120                         String subIcon = NodeDataUtil.getString(sub, "icon");
121                         String subContextPath = "";
122                         if (!"".equals(subIcon)) {
123                             subContextPath = CP_PREFIX;
124                         }
125                         str.append(MessageFormat.format(subPattern, jsName,
126                         mp.getUUID(),
127                         sub.getUUID(),
128                         getLabel(sub),
129                         NodeDataUtil.getString(sub, "onclick"),
130                         subContextPath,
131                         subIcon));
132 
133                     }
134                 }
135             }
136         }
137 
138         return str.toString();
139     }
140 
141     /**
142      * @param mp
143      * @return
144      */
145     protected Object getLabel(Content mp) {
146         return NodeDataUtil.getI18NString(mp, "label");
147     }
148 
149     /**
150      * @param mp
151      * @return
152      */
153     protected boolean isMenuPointRendered(Content mp) {
154         return MgnlContext.getAccessManager(ContentRepository.CONFIG).isGranted(mp.getHandle(), Permission.READ);
155     }
156 
157     /**
158      * Get the first onclick in this menu. Used as the default src in the content iframe
159      * @return the href
160      */
161     public String getFirstId() {
162         return getFirstId(node);
163     }
164 
165     private String getFirstId(Content node) {
166         for (Iterator iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
167             Content sub = (Content) iter.next();
168             if (isMenuPointRendered(sub)) {
169                 if (StringUtils.isNotEmpty(NodeDataUtil.getString(sub, "onclick"))) {
170                     return sub.getUUID();
171                 }
172                 String uuid = getFirstId(sub);
173                 if (StringUtils.isNotEmpty(uuid)) {
174                     return uuid;
175                 }
176             }
177         }
178         return "";
179     }
180 
181 }