001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.list;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.util.CmsCollectionsGenericWrapper;
036import org.opencms.util.CmsRequestUtil;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.workplace.CmsWorkplaceSettings;
039import org.opencms.workplace.explorer.CmsExplorer;
040import org.opencms.workplace.tools.A_CmsHtmlIconButton;
041import org.opencms.workplace.tools.CmsExplorerDialog;
042import org.opencms.workplace.tools.CmsHtmlIconButtonStyleEnum;
043import org.opencms.workplace.tools.CmsTool;
044import org.opencms.workplace.tools.CmsToolDialog;
045import org.opencms.workplace.tools.CmsToolMacroResolver;
046import org.opencms.workplace.tools.CmsToolManager;
047
048import java.io.IOException;
049import java.util.Collection;
050import java.util.Collections;
051import java.util.HashMap;
052import java.util.Iterator;
053import java.util.Map;
054
055import org.apache.commons.logging.Log;
056
057import jakarta.servlet.ServletException;
058import jakarta.servlet.http.HttpServletRequest;
059import jakarta.servlet.http.HttpServletResponse;
060import jakarta.servlet.jsp.JspWriter;
061import jakarta.servlet.jsp.PageContext;
062
063/**
064 * Explorer dialog for the project files view.<p>
065 *
066 * @since 6.0.0
067 */
068public class CmsListExplorerFrameset extends CmsExplorerDialog {
069
070    /** Page parameter name. */
071    public static final String PARAM_PAGE = "explorer_page";
072
073    /** Title uri parameter name. */
074    public static final String PARAM_TITLE_URI = "title_uri";
075
076    /** The log object for this class. */
077    private static final Log LOG = CmsLog.getLog(CmsListExplorerFrameset.class);
078
079    /**
080     * Public constructor with JSP action element.<p>
081     *
082     * @param jsp an initialized JSP action element
083     */
084    public CmsListExplorerFrameset(CmsJspActionElement jsp) {
085
086        super(jsp);
087    }
088
089    /**
090     * Public constructor with JSP variables.<p>
091     *
092     * @param context the JSP page context
093     * @param req the JSP request
094     * @param res the JSP response
095     */
096    public CmsListExplorerFrameset(PageContext context, HttpServletRequest req, HttpServletResponse res) {
097
098        this(new CmsJspActionElement(context, req, res));
099    }
100
101    /**
102     * Generates the dialog starting html code.<p>
103     *
104     * @return html code
105     */
106    public String defaultActionHtml() {
107
108        String params = allParamsAsRequest();
109        String titleUri = CmsEncoder.escapeXml(getJsp().getRequest().getParameter(PARAM_TITLE_URI));
110        if (CmsStringUtil.isEmptyOrWhitespaceOnly(titleUri)) {
111            titleUri = CmsToolManager.ADMINVIEW_ROOT_LOCATION + "/list-title.jsp";
112        }
113        String titleSrc = getFrameSource("tool_title", getJsp().link(titleUri + "?" + params));
114        String contentSrc = getFrameSource(
115            "tool_content",
116            getJsp().link(CmsToolManager.ADMINVIEW_ROOT_LOCATION + "/list-explorer.jsp") + "?" + params);
117        StringBuffer html = new StringBuffer(1024);
118        html.append("<!DOCTYPE html>\n");
119        html.append("<html>\n");
120        html.append("\t<head>\n");
121        html.append("\t\t<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=\"").append(
122            getEncoding()).append("\">\n");
123        String uplink = "/opencms_a/opencms/system/workplace/views/admin/admin-main.jsp?path=/projects/files&action=initial&projectid=fa9f561f-be30-11e2-bac3-21ebf444feef&showexplorer=true";
124        html.append("<script >var uplink = \"" + CmsStringUtil.escapeJavaScript(uplink) + "\";</script>\n");
125        html.append("\t\t<title>\n");
126        html.append("\t\t\t").append(
127            key(
128                org.opencms.workplace.Messages.GUI_LABEL_WPTITLE_1,
129                new Object[] {getSettings().getUser().getFullName()})).append("\n");
130        html.append("\t\t</title>\n");
131        html.append("\t</head>\n");
132        html.append("\t<frameset rows='57,*' border='0' frameborder='0' framespacing='0'>\n");
133        html.append("\t\t<frame ").append(titleSrc).append(" frameborder='0' border='0' noresize scrolling='no'>\n");
134        html.append("\t\t<frame ").append(contentSrc).append(
135            " frameborder='0' border='0' noresize scrolling='auto' framespacing='0' marginheight='2' marginwidth='2' >\n");
136        html.append("\t</frameset>\n");
137        html.append("</html>\n");
138        return html.toString();
139    }
140
141    /**
142     * @see org.opencms.workplace.tools.CmsToolDialog#dialogTitle()
143     */
144    @Override
145    public String dialogTitle() {
146
147        StringBuffer html = new StringBuffer(512);
148        String toolPath = getCurrentToolPath();
149        String parentPath = getParentPath();
150        String rootKey = getToolManager().getCurrentRoot(this).getKey();
151        CmsTool parentTool = getToolManager().resolveAdminTool(rootKey, parentPath);
152        String upLevelLink = CmsToolManager.linkForToolPath(
153            getJsp(),
154            parentPath,
155            parentTool.getHandler().getParameters(this));
156        String listLevelLink = CmsToolManager.linkForToolPath(
157            getJsp(),
158            toolPath,
159            getToolManager().resolveAdminTool(rootKey, toolPath).getHandler().getParameters(this));
160        listLevelLink = CmsRequestUtil.appendParameter(
161            listLevelLink,
162            A_CmsListExplorerDialog.PARAM_SHOW_EXPLORER,
163            Boolean.FALSE.toString());
164        String parentName = getToolManager().resolveAdminTool(rootKey, parentPath).getHandler().getName();
165
166        html.append(getToolManager().generateNavBar(toolPath, this));
167
168        // check if page switch needed
169        int items = 0;
170        try {
171            items = getSettings().getCollector().getResults(getCms()).size();
172        } catch (CmsException e) {
173            // ignore
174            LOG.error(e.getLocalizedMessage(), e);
175        }
176
177        int size = (int)Math.ceil((double)items / getSettings().getUserSettings().getExplorerFileEntries());
178        // build title
179        html.append("<div class='screenTitle'>\n");
180        html.append("\t<table width='100%' cellspacing='0'>\n");
181        html.append("\t\t<tr>\n");
182        html.append("\t\t\t<td>\n");
183        html.append(getAdminTool().getHandler().getName());
184        html.append("\n\t\t\t</td>\n");
185        html.append("\t\t\t<td class='uplevel'>\n");
186        html.append("<form name='title-form' method='post' target='_parent' action='");
187        html.append(getJsp().link(A_CmsListExplorerDialog.PATH_EXPLORER_LIST)).append("'>\n");
188        html.append(getFormContent());
189        // if page switch needed
190        if (size > 1) {
191            html.append("<select name='").append(PARAM_PAGE);
192            html.append("' class='location' onchange='this.form.submit()'>\n");
193            html.append(
194                CmsHtmlList.htmlPageSelector(
195                    size,
196                    getSettings().getUserSettings().getExplorerFileEntries(),
197                    items,
198                    getSettings().getExplorerPage(),
199                    getLocale()));
200            html.append("</select>\n");
201        }
202        // list view button
203        CmsMessages messages = Messages.get().getBundle(getLocale());
204        html.append(
205            A_CmsHtmlIconButton.defaultButtonHtml(
206                CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
207                "id-list-switch",
208                messages.key(Messages.GUI_LIST_ACTION_LIST_SWITCH_NAME_0),
209                messages.key(Messages.GUI_LIST_ACTION_LIST_SWITCH_HELP_0),
210                true,
211                "list/list.png",
212                null,
213                "openPage('" + listLevelLink + "');"));
214        html.append("\n");
215        // uplevel button only if needed
216        if (!toolPath.equals(getParentPath())) {
217            html.append(
218                A_CmsHtmlIconButton.defaultButtonHtml(
219                    CmsHtmlIconButtonStyleEnum.SMALL_ICON_TEXT,
220                    "id-up-level",
221                    org.opencms.workplace.tools.Messages.get().getBundle(getLocale()).key(
222                        org.opencms.workplace.tools.Messages.GUI_ADMIN_VIEW_UPLEVEL_0),
223                    parentName,
224                    true,
225                    "admin/images/up.png",
226                    null,
227                    "openPage('" + upLevelLink + "');"));
228        }
229        html.append("</form>");
230        html.append("\n\t\t\t</td>\n");
231        html.append("\t\t</tr>\n");
232        html.append("\t</table>\n");
233        html.append("</div>\n");
234
235        String code = html.toString().replaceAll("openPage\\('([^']+)'\\)", "openPageIn('$1', parent)");
236        return CmsToolMacroResolver.resolveMacros(code, this);
237    }
238
239    /**
240     * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
241     *
242     * @throws ServletException if forwarding explorer view fails
243     * @throws IOException if forwarding explorer view fails
244     */
245    public void displayDialog() throws IOException, ServletException {
246
247        getSettings().setExplorerMode(CmsExplorer.VIEW_LIST);
248        Map<String, String[]> addParams = new HashMap<String, String[]>();
249        HttpServletRequest req = getJsp().getRequest();
250        String addParamsVal = req.getParameter(CmsExplorer.PARAMETER_CONTEXTMENUPARAMS);
251        if (addParamsVal != null) {
252            addParams.put(CmsExplorer.PARAMETER_CONTEXTMENUPARAMS, new String[] {addParamsVal});
253        }
254        getToolManager().jspForwardPage(this, FILE_EXPLORER_FILELIST, addParams);
255    }
256
257    /**
258     * Validates the needed parameters and display the frameset.<p>
259     *
260     * @throws IOException in case of errros displaying to the required page
261     */
262    public void displayFrameSet() throws IOException {
263
264        if (getJsp().getRequest().getParameter(CmsListExplorerFrameset.PARAM_PAGE) != null) {
265            int page = Integer.parseInt(getJsp().getRequest().getParameter(CmsListExplorerFrameset.PARAM_PAGE));
266            getSettings().setExplorerPage(page);
267
268            if (getSettings().getCollector() instanceof I_CmsListResourceCollector) {
269                I_CmsListResourceCollector collector = (I_CmsListResourceCollector)getSettings().getCollector();
270                collector.setPage(page);
271            }
272        }
273        JspWriter out = getJsp().getJspContext().getOut();
274        out.print(defaultActionHtml());
275    }
276
277    /**
278     * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden(java.util.Collection)
279     */
280    @Override
281    public String paramsAsHidden(Collection<String> excludes) {
282
283        StringBuffer result = new StringBuffer(512);
284        Map<String, String[]> params = CmsCollectionsGenericWrapper.map(getJsp().getRequest().getParameterMap());
285        params = new HashMap<String, String[]>(params);
286        params.remove(CmsListExplorerFrameset.PARAM_PAGE);
287        Iterator<Map.Entry<String, String[]>> it = params.entrySet().iterator();
288        while (it.hasNext()) {
289            Map.Entry<String, String[]> entry = it.next();
290            String param = entry.getKey();
291            if ((excludes == null) || (!excludes.contains(param))) {
292                String[] value = entry.getValue();
293                for (int i = 0; i < value.length; i++) {
294                    result.append("<input type=\"hidden\" name=\"");
295                    result.append(param);
296                    result.append("\" value=\"");
297                    result.append(CmsEncoder.encode(value[i], getCms().getRequestContext().getEncoding()));
298                    result.append("\">\n");
299                }
300            }
301        }
302        return result.toString();
303    }
304
305    /**
306     * Returns the form contents.<p>
307     *
308     * @return the form contents
309     */
310    protected String getFormContent() {
311
312        return paramsAsHidden(Collections.singleton(PARAM_PAGE));
313    }
314
315    /**
316     * @see org.opencms.workplace.CmsDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, jakarta.servlet.http.HttpServletRequest)
317     */
318    @Override
319    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
320
321        setParamStyle(CmsToolDialog.STYLE_NEW);
322        super.initWorkplaceRequestValues(settings, request);
323    }
324
325}