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