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.tools;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.OpenCms;
033import org.opencms.security.CmsRoleViolationException;
034import org.opencms.util.CmsRequestUtil;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.workplace.CmsDialog;
037import org.opencms.workplace.CmsWorkplace;
038import org.opencms.workplace.CmsWorkplaceSettings;
039
040import java.util.HashMap;
041import java.util.Map;
042
043import javax.servlet.http.HttpServletRequest;
044
045/**
046 * Helper class that encapsulates all the code for the "new"
047 * style of the administration dialogs.<p>
048 *
049 * @since 6.0.0
050 */
051public class CmsToolDialog extends CmsWorkplace {
052
053    /** Request parameter name for the base tool path in the navigation, should be a parent tool of path. */
054    public static final String PARAM_BASE = "base";
055
056    /** Request parameter name for the force flag. */
057    public static final String PARAM_FORCE = "force";
058
059    /** Request parameter name for the tool path, should be an accessible tool under the given root. */
060    public static final String PARAM_PATH = "path";
061
062    /** Request parameter name for the root tool path. */
063    public static final String PARAM_ROOT = "root";
064
065    /** Request parameter name for the style type. */
066    public static final String PARAM_STYLE = "style";
067
068    /** Request parameter value for the 'new' dialog style. */
069    public static final String STYLE_NEW = "new";
070
071    /** The adminProject parameter name. */
072    public static final String PARAM_ADMIN_PROJECT = "adminProject";
073
074    /** Base parameter value. */
075    private String m_paramBase;
076
077    /** Force parameter value. */
078    private String m_paramForce;
079
080    /** Path parameter value. */
081    private String m_paramPath;
082
083    /** Root parameter value. */
084    private String m_paramRoot;
085
086    /** Style parameter value. */
087    private String m_paramStyle;
088
089    /**
090     * Default Constructor.<p>
091     *
092     * @param jsp the jsp action element
093     */
094    public CmsToolDialog(CmsJspActionElement jsp) {
095
096        super(jsp);
097    }
098
099    /**
100     * Builds the standard javascript for submitting the dialog.<p>
101     *
102     * Should only be used by the <code>{@link CmsDialog#dialogScriptSubmit()}</code> method.<p>
103     *
104     * @return the standard javascript for submitting the dialog
105     */
106    public String dialogScriptSubmit() {
107
108        StringBuffer html = new StringBuffer(512);
109        html.append("function submitAction(actionValue, theForm, formName) {\n");
110        html.append("\tif (theForm == null) {\n");
111        html.append("\t\ttheForm = document.forms[formName];\n");
112        html.append("\t}\n");
113        html.append("\ttry {\n");
114        html.append("\t\ttheForm.").append(CmsDialog.PARAM_FRAMENAME).append(".value = window.name;\n");
115        html.append("\t} catch (e) {}\n");
116        html.append("\tif (actionValue == '" + CmsDialog.DIALOG_OK + "') {\n");
117        html.append("\t\tloadingOn();\n");
118        html.append("\t\treturn true;\n");
119        html.append("\t}\n");
120        html.append("\ttheForm." + CmsDialog.PARAM_ACTION + ".value = actionValue;\n");
121        html.append("\tsubmitForm(theForm);\n");
122        html.append("\treturn true;\n");
123        html.append("}\n");
124        return html.toString();
125    }
126
127    /**
128     * Generates the standard new style dialog title row, and tool grouping.<p>
129     *
130     * It is called by the <code>{@link org.opencms.workplace.CmsDialog#dialog(int, String)}</code> method.<p>
131     *
132     * @return a dialog window start / end segment
133     */
134    public String dialogTitle() {
135
136        StringBuffer html = new StringBuffer(512);
137        String toolPath = getCurrentToolPath();
138        String parentPath = getParentPath();
139        String rootKey = getToolManager().getCurrentRoot(this).getKey();
140        String upLevelLink = computeUpLevelLink();
141
142        html.append(getToolManager().generateNavBar(toolPath, this));
143        // build title
144        html.append("<div class='screenTitle'>\n");
145        html.append("\t<table width='100%' cellspacing='0'>\n");
146        html.append("\t\t<tr>\n");
147        html.append("\t\t\t<td>\n");
148        html.append(CmsEncoder.decode(CmsToolMacroResolver.resolveMacros(getAdminTool().getHandler().getName(), this)));
149        html.append("\n\t\t\t</td>");
150        // uplevel button only if needed
151        if ((upLevelLink != null) && !getParentPath().equals(toolPath)) {
152            String parentName = getToolManager().resolveAdminTool(rootKey, parentPath).getHandler().getName();
153            html.append("\t\t\t<td class='uplevel'>\n\t\t\t\t");
154            html.append(
155                A_CmsHtmlIconButton.defaultButtonHtml(
156                    CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
157                    "id-up-level",
158                    Messages.get().getBundle(getLocale()).key(Messages.GUI_ADMIN_VIEW_UPLEVEL_0),
159                    parentName,
160                    true,
161                    "admin/images/up.png",
162                    null,
163                    "openPage('" + upLevelLink + "');"));
164            html.append("\n\t\t\t</td>\n");
165        }
166        html.append("\t\t</tr>\n");
167        html.append("\t</table>\n");
168        html.append("</div>\n");
169        return CmsToolMacroResolver.resolveMacros(html.toString(), this);
170    }
171
172    /**
173     * Returns the admin tool.<p>
174     *
175     * @return the admin tool
176     */
177    public CmsTool getAdminTool() {
178
179        return getToolManager().getCurrentTool(this);
180    }
181
182    /**
183     * Returns the current tool path.<p>
184     *
185     * @return the current tool path
186     */
187    public String getCurrentToolPath() {
188
189        return getToolManager().getCurrentToolPath(this);
190    }
191
192    /**
193     * Returns the value for the base parameter.<p>
194     *
195     * @return the value for the base parameter
196     */
197    public String getParamBase() {
198
199        return m_paramBase;
200    }
201
202    /**
203     * Returns the value for the force parameter.<p>
204     *
205     * @return the value for the force parameter
206     */
207    public String getParamForce() {
208
209        return m_paramForce;
210    }
211
212    /**
213     * Returns the path parameter value.<p>
214     *
215     * @return the path parameter value
216     */
217    public String getParamPath() {
218
219        return m_paramPath;
220    }
221
222    /**
223     * Returns the root parameter value.<p>
224     *
225     * @return the root parameter value
226     */
227    public String getParamRoot() {
228
229        return m_paramRoot;
230    }
231
232    /**
233     * Returns the style parameter value.<p>
234     *
235     * @return the style parameter value
236     */
237    public String getParamStyle() {
238
239        return m_paramStyle;
240    }
241
242    /**
243     * Returns the path to the parent tool.<p>
244     *
245     * @return tha path to the parent tool
246     */
247    public String getParentPath() {
248
249        return getToolManager().getParent(this, getCurrentToolPath());
250    }
251
252    /**
253     * Returns the tool manager.<p>
254     *
255     * @return the tool manager
256     */
257    public CmsToolManager getToolManager() {
258
259        return OpenCms.getWorkplaceManager().getToolManager();
260    }
261
262    /**
263     * Builds an block area for icons.<p>
264     *
265     * @param segment the HTML segment (START / END)
266     * @param headline the headline String for the block
267    
268     * @return block area start / end segment
269     *
270     * @see CmsDialog#dialogBlock(int, String, boolean)
271     */
272    public String iconsBlockArea(int segment, String headline) {
273
274        StringBuffer result = new StringBuffer(512);
275        if (segment == HTML_START) {
276            result.append("<!-- icons block area start -->\n");
277            result.append("<div class=\"dialogcontent\" unselectable=\"on\">");
278            result.append("<fieldset class=\"dialogblock\">\n");
279            result.append("<legend>");
280            result.append("<span class=\"textbold");
281            result.append("\" unselectable=\"on\">");
282            result.append(headline);
283            result.append("</span></legend>\n");
284            result.append("\t\t<table class='toolsArea' width='100%' cellspacing='0' cellpadding='0' border='0'>\n");
285            result.append("\t\t\t<tr><td>\n");
286        } else {
287            result.append("\t\t\t</td></tr>\n");
288            result.append("\t\t</table>\n");
289            result.append("</fieldset></div>\n");
290            result.append("<p>&nbsp;</p>\n");
291            result.append("<!-- icons block area end -->\n");
292        }
293        return result.toString();
294    }
295
296    /**
297     * Builds the end HTML for a block area with border in the dialog content area.<p>
298     *
299     * @return block area end segment
300     *
301     * @see CmsDialog#dialogBlockEnd()
302     */
303    public String iconsBlockAreaEnd() {
304
305        return iconsBlockArea(HTML_END, null);
306    }
307
308    /**
309     * Builds the start HTML for a block area with border and optional subheadline in the dialog content area.<p>
310     *
311     * @param headline the headline String for the block
312     *
313     * @return block area start segment
314     *
315     * @see CmsDialog#dialogBlockStart(String)
316     */
317    public String iconsBlockAreaStart(String headline) {
318
319        return iconsBlockArea(HTML_START, headline);
320    }
321
322    /**
323     * Initializes the admin tool main view.<p>
324     *
325     * @return the new modified parameters array
326     *
327     * @throws CmsRoleViolationException in case the dialog is opened by a user without the necessary privileges
328     */
329    public Map<String, String[]> initAdminTool() throws CmsRoleViolationException {
330
331        Map<String, String[]> params = new HashMap<String, String[]>(getParameterMap());
332        // initialize
333        getToolManager().initParams(this);
334
335        // adjust parameters if called as default
336        if (!useNewStyle()) {
337            params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
338            setParamStyle(CmsToolDialog.STYLE_NEW);
339        }
340
341        try {
342            // a dialog just to access the close link parameter
343            CmsDialog wp = (CmsDialog)this;
344            // set close link
345            if (CmsStringUtil.isEmptyOrWhitespaceOnly(wp.getParamCloseLink())) {
346                if (!getToolManager().getBaseToolPath(this).equals(getToolManager().getCurrentToolPath(this))) {
347                    Map<String, String[]> args = getToolManager().resolveAdminTool(
348                        getParamRoot(),
349                        getParentPath()).getHandler().getParameters(wp);
350                    wp.setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), getParentPath(), args));
351                    params.put(CmsDialog.PARAM_CLOSELINK, new String[] {wp.getParamCloseLink()});
352                }
353            }
354        } catch (Exception e) {
355            // ignore
356        }
357
358        if (!getToolManager().getCurrentTool(this).getHandler().isEnabled(this)) {
359            throw new CmsRoleViolationException(Messages.get().container(Messages.ERR_ADMIN_INSUFFICIENT_RIGHTS_0));
360        }
361
362        return params;
363    }
364
365    /**
366     * @see org.opencms.workplace.CmsWorkplace#pageBody(int, java.lang.String, java.lang.String)
367     */
368    @Override
369    public String pageBody(int segment, String className, String parameters) {
370
371        if (!useNewStyle()) {
372            return super.pageBody(segment, className, parameters);
373        } else {
374            Map<String, String> data = CmsStringUtil.extendAttribute(parameters, "onLoad", "bodyLoad();");
375            String onLoad = data.get("value");
376            String myPars = data.get("text");
377            data = CmsStringUtil.extendAttribute(myPars, "onUnload", "bodyUnload();");
378            String onUnload = data.get("value");
379            myPars = data.get("text");
380            if (segment == HTML_START) {
381                StringBuffer html = new StringBuffer(512);
382                html.append("</head>\n");
383                html.append("<body onLoad=");
384                html.append(onLoad);
385                html.append(" onUnload=");
386                html.append(onUnload);
387                html.append(CmsStringUtil.isNotEmpty(className) ? " class='" + className + "'" : "");
388                html.append(CmsStringUtil.isNotEmpty(myPars) ? " " + myPars : "");
389                html.append(">\n");
390                html.append(
391                    "\t<table border='0' cellspacing='0' cellpadding='0' id='loaderContainer' onClick='return false;'>\n");
392                html.append("\t\t<tr><td id='loaderContainerH'><div id='loader'>\n");
393                html.append("\t\t\t<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td>\n");
394                html.append("\t\t\t\t<p><img src='");
395                html.append(getSkinUri());
396                html.append("commons/wait.gif");
397                html.append("' height='32' width='32' alt=''/>\n");
398                html.append("\t\t\t\t<strong>");
399                html.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_ADMIN_VIEW_LOADING_0));
400                html.append("</strong></p>\n");
401                html.append("\t\t\t</td></tr></table>\n");
402                html.append("\t\t</div></td></tr>\n");
403                html.append("\t</table>\n");
404                html.append("\t<table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td id='screenH'>\n");
405                return html.toString();
406            } else {
407                StringBuffer html = new StringBuffer(128);
408                html.append("\t</td></tr></table>\n");
409                html.append("</body>");
410                return html.toString();
411            }
412        }
413    }
414
415    /**
416     * @see org.opencms.workplace.CmsWorkplace#pageHtmlStyle(int, java.lang.String, java.lang.String)
417     */
418    @Override
419    public String pageHtmlStyle(int segment, String title, String stylesheet) {
420
421        if (!useNewStyle() || (segment != HTML_START)) {
422            return super.pageHtmlStyle(segment, title, stylesheet);
423        }
424
425        StringBuffer html = new StringBuffer(512);
426        html.append("<!DOCTYPE html>\n");
427        html.append("<html>\n");
428        html.append("<head>\n");
429        html.append("<meta http-equiv='Content-Type' content='text/html; charset=");
430        html.append(getEncoding());
431        html.append("' >\n");
432        if (title != null) {
433            html.append("<title>");
434            html.append(title);
435            html.append("</title>\n");
436        } else {
437            // the title tag is required for valid HTML
438            html.append("<title></title>\n");
439        }
440        html.append("<link rel='stylesheet' type='text/css' href='");
441        html.append(getStyleUri(getJsp()));
442        html.append("new_admin.css'>\n");
443        html.append("<script  src='");
444        html.append(getSkinUri());
445        html.append("admin/javascript/general.js'></script>\n");
446        html.append("<script  src='");
447        html.append(getResourceUri());
448        html.append("editors/xmlcontent/help.js'></script>\n\n");
449        html.append("<script >\n");
450        html.append("\tfunction bodyLoad() {\n");
451
452        // add a special CSS class in case we are in the new vaadin based workplace
453
454        html.append("\tif (this.name != \"admin_content\" && this.name != \"explorer_files\") {\n");
455        html.append("\t\tvar cssClass=document.body.getAttribute(\"class\");\n");
456        html.append("\t\tcssClass+=\" legacy-app\";\n");
457        html.append("\t\tdocument.body.setAttribute(\"class\",cssClass);\n");
458        html.append("\t}\n");
459
460        html.append("\t\tsetContext(\"");
461        html.append(CmsStringUtil.escapeJavaScript(resolveMacros(getAdminTool().getHandler().getHelpText())));
462        html.append("\");\n");
463        html.append("\t\tsetActiveItemByName(\"");
464        html.append(getCurrentToolPath());
465        html.append("\");\n");
466        html.append("\t\tloadingOff();\n");
467        html.append("\t\ttry {\n");
468        html.append("\t\t\tdocument.getElementById('loaderContainerH').height = wHeight();\n");
469        html.append("\t\t} catch (e) {}\n");
470        html.append("\t}\n");
471        html.append("\tfunction bodyUnload() {\n");
472        html.append("\t\tloadingOn();\n");
473        html.append("\t}\n");
474        html.append("</script>\n");
475        return html.toString();
476    }
477
478    /**
479     * Sets the value of the base parameter.<p>
480     *
481     * @param paramBase the value of the base parameter to set
482     */
483    public void setParamBase(String paramBase) {
484
485        m_paramBase = paramBase;
486    }
487
488    /**
489     * Sets the value of the force parameter.<p>
490     *
491     * @param paramForce the value of the force parameter to set
492     */
493    public void setParamForce(String paramForce) {
494
495        m_paramForce = paramForce;
496    }
497
498    /**
499     * Sets the path parameter value.<p>
500     *
501     * @param paramPath the path parameter value to set
502     */
503    public void setParamPath(String paramPath) {
504
505        m_paramPath = paramPath;
506    }
507
508    /**
509     * Sets the root parameter value.<p>
510     *
511     * @param paramRoot the root parameter value to set
512     */
513    public void setParamRoot(String paramRoot) {
514
515        m_paramRoot = paramRoot;
516    }
517
518    /**
519     * Sets the style parameter value.<p>
520     *
521     * @param paramStyle the style parameter value to set
522     */
523    public void setParamStyle(String paramStyle) {
524
525        m_paramStyle = paramStyle;
526    }
527
528    /**
529     * Tests if we are working with the new administration dialog style.<p>
530     *
531     * The default is the new style, this parameter is not intended for external use.<p>
532     *
533     * @return <code>true</code> if using the new style
534     */
535    public boolean useNewStyle() {
536
537        return (getParamStyle() != null) && getParamStyle().equals(CmsToolDialog.STYLE_NEW);
538    }
539
540    /**
541     * Creates the link for the 'up' button.<p>
542     *
543     * @return the link for the 'up' button
544     */
545    protected String computeUpLevelLink() {
546
547        String parentPath = getParentPath();
548        String rootKey = getToolManager().getCurrentRoot(this).getKey();
549        CmsTool parentTool = getToolManager().resolveAdminTool(rootKey, parentPath);
550        String upLevelLink = null;
551        if (parentTool != null) {
552            upLevelLink = CmsToolManager.linkForToolPath(
553                getJsp(),
554                parentPath,
555                parentTool.getHandler().getParameters(this));
556            upLevelLink = CmsRequestUtil.appendParameter(upLevelLink, PARAM_FORCE, Boolean.TRUE.toString());
557        }
558        return upLevelLink;
559    }
560
561    /**
562     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
563     */
564    @Override
565    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
566
567        fillParamValues(request);
568    }
569}