001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.workplace.administration;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.workplace.CmsWorkplace;
035import org.opencms.workplace.CmsWorkplaceSettings;
036import org.opencms.workplace.tools.CmsIdentifiableObjectContainer;
037import org.opencms.workplace.tools.CmsTool;
038import org.opencms.workplace.tools.CmsToolDialog;
039import org.opencms.workplace.tools.CmsToolMacroResolver;
040import org.opencms.workplace.tools.CmsToolManager;
041
042import java.util.Collection;
043import java.util.Iterator;
044import java.util.Map;
045import java.util.Map.Entry;
046
047import javax.servlet.http.HttpServletRequest;
048
049/**
050 * Implementation of the administration view leftside's menu.<p>
051 *
052 * @since 6.0.0
053 */
054public class CmsAdminMenu extends CmsToolDialog {
055
056    /** Default link target constant. */
057    public static final String DEFAULT_TARGET = "admin_content";
058
059    /** Group container. */
060    private CmsIdentifiableObjectContainer<CmsAdminMenuGroup> m_groupContainer = new CmsIdentifiableObjectContainer<CmsAdminMenuGroup>(
061        true,
062        true);
063
064    /**
065     * Default Constructor.<p>
066     *
067     * @param jsp the jsp context
068     */
069    public CmsAdminMenu(CmsJspActionElement jsp) {
070
071        super(jsp);
072        try {
073            initAdminTool();
074        } catch (Exception e) {
075            // ignore, only a role violation, not important for left side menu
076        }
077        installMenu();
078    }
079
080    /**
081     * Adds a group.<p>
082     *
083     * @param group the group
084     *
085     * @see CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object)
086     */
087    public void addGroup(CmsAdminMenuGroup group) {
088
089        m_groupContainer.addIdentifiableObject(group.getName(), group);
090    }
091
092    /**
093     * Adds a menu item at the given position.<p>
094     *
095     * @param group the group
096     * @param position the position
097     *
098     * @see CmsIdentifiableObjectContainer#addIdentifiableObject(String, Object, float)
099     */
100    public void addGroup(CmsAdminMenuGroup group, float position) {
101
102        m_groupContainer.addIdentifiableObject(group.getName(), group, position);
103    }
104
105    /**
106     * Adds a new item to the specified menu.<p>
107     *
108     * If the menu does not exist, it will be created.<p>
109     *
110     * @param groupName the name of the group
111     * @param name the name of the item
112     * @param icon the icon to display
113     * @param link the link to open when selected
114     * @param helpText the help text to display
115     * @param enabled if enabled or not
116     * @param position the relative position to install the item
117     * @param target the target frame to open the link into
118     *
119     * @return the new item
120     */
121    public CmsAdminMenuItem addItem(
122        String groupName,
123        String name,
124        String icon,
125        String link,
126        String helpText,
127        boolean enabled,
128        float position,
129        String target) {
130
131        groupName = CmsToolMacroResolver.resolveMacros(groupName, this);
132        CmsAdminMenuGroup group = getGroup(groupName);
133        if (group == null) {
134            String gid = "group" + m_groupContainer.elementList().size();
135            group = new CmsAdminMenuGroup(gid, groupName);
136            addGroup(group, position);
137        }
138        String id = "item" + group.getId() + group.getMenuItems().size();
139        CmsAdminMenuItem item = new CmsAdminMenuItem(id, name, icon, link, helpText, enabled, target);
140        group.addMenuItem(item, position);
141        return item;
142    }
143
144    /**
145     * Returns all initialized parameters of the current request
146     * that are not in the given exclusion list as hidden field tags that can be inserted in a form.<p>
147     *
148     * @param excludes the parameters to exclude
149     *
150     * @return all initialized parameters of the current request
151     */
152    public String allRequestParamsAsUrl(Collection<String> excludes) {
153
154        StringBuffer result = new StringBuffer(512);
155        Map<?, ?> params = getJsp().getRequest().getParameterMap();
156        Iterator<?> i = params.entrySet().iterator();
157        while (i.hasNext()) {
158            Entry<?, ?> entry = (Entry<?, ?>)i.next();
159            String param = (String)entry.getKey();
160            if ((excludes == null) || (!excludes.contains(param))) {
161                if (result.length() > 0) {
162                    result.append("&");
163                }
164                result.append(param);
165                result.append("=");
166                String value;
167                if (entry.getValue() instanceof String[]) {
168                    value = ((String[])entry.getValue())[0];
169                } else {
170                    value = (String)entry.getValue();
171                }
172                String encoded = CmsEncoder.encode(value, getCms().getRequestContext().getEncoding());
173                result.append(encoded);
174            }
175        }
176        return result.toString();
177    }
178
179    /**
180     * Returns the requested group.<p>
181     *
182     * @param name the name of the group
183     *
184     * @return the group
185     *
186     * @see CmsIdentifiableObjectContainer#getObject(String)
187     */
188    public CmsAdminMenuGroup getGroup(String name) {
189
190        return m_groupContainer.getObject(name);
191    }
192
193    /**
194     * Returns the admin manager.<p>
195     *
196     * @return the admin manager
197     */
198    @Override
199    public CmsToolManager getToolManager() {
200
201        return OpenCms.getWorkplaceManager().getToolManager();
202    }
203
204    /**
205     * Generates the necesary html code for the groups.<p>
206     *
207     * @param wp the page for which the code is generated
208     *
209     * @return html code
210     */
211    public String groupHtml(CmsWorkplace wp) {
212
213        StringBuffer html = new StringBuffer(2048);
214        Iterator<CmsAdminMenuGroup> itHtml = m_groupContainer.elementList().iterator();
215        while (itHtml.hasNext()) {
216            CmsAdminMenuGroup group = itHtml.next();
217            html.append(group.groupHtml(wp));
218        }
219        return html.toString();
220    }
221
222    /**
223     * Creates the default menu as the root tool structure.<p>
224     */
225    public void installMenu() {
226
227        // initialize the menu groups
228        m_groupContainer.clear();
229
230        // creates the context help menu
231        CmsAdminMenuGroup helpMenu = new CmsAdminMenuGroup(
232            "help",
233            Messages.get().getBundle(getLocale()).key(Messages.GUI_ADMIN_MENU_HELP_GROUP_0));
234        helpMenu.addMenuItem(new CmsAdminContextHelpMenuItem());
235        addGroup(helpMenu);
236
237        Iterator<CmsTool> itElems = getToolManager().getToolsForPath(
238            this,
239            getToolManager().getBaseToolPath(this),
240            false).iterator();
241        while (itElems.hasNext()) {
242            CmsTool tool = itElems.next();
243            // check visibility
244            String link = tool.getHandler().getLink();
245            if (link.indexOf("?") > 0) {
246                link = link.substring(0, link.indexOf("?"));
247            }
248            if (!getCms().existsResource(link) || !tool.getHandler().isVisible(this)) {
249                continue;
250            }
251
252            // cut out the base
253            String path = tool.getHandler().getPath().substring(getToolManager().getBaseToolPath(this).length());
254            // special case of the base tool
255            if (CmsStringUtil.isEmpty(path)) {
256                continue;
257            }
258            // skip initial '/'
259            int pos = tool.getHandler().getPath().indexOf(CmsToolManager.TOOLPATH_SEPARATOR);
260            // only install if at first level
261            if (path.indexOf(CmsToolManager.TOOLPATH_SEPARATOR, pos + 1) < 0) {
262
263                addItem(
264                    tool.getHandler().getGroup(),
265                    tool.getHandler().getShortName(),
266                    tool.getHandler().getSmallIconPath(),
267                    CmsToolManager.linkForToolPath(getJsp(), tool.getHandler().getPath()),
268                    tool.getHandler().isEnabled(this)
269                    ? tool.getHandler().getHelpText()
270                    : tool.getHandler().getDisabledHelpText(),
271                    tool.getHandler().isEnabled(this),
272                    tool.getHandler().getPosition(),
273                    CmsAdminMenu.DEFAULT_TARGET);
274            }
275        }
276    }
277
278    /**
279     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
280     */
281    @Override
282    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
283
284        fillParamValues(request);
285    }
286}