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.widgets;
029
030import org.opencms.file.CmsObject;
031import org.opencms.i18n.CmsMessages;
032import org.opencms.json.JSONArray;
033import org.opencms.json.JSONException;
034import org.opencms.json.JSONObject;
035import org.opencms.util.CmsMacroResolver;
036import org.opencms.util.CmsStringUtil;
037
038import java.util.ArrayList;
039import java.util.List;
040
041/**
042 * Configuration options for the VFS image widget.<p>
043 *
044 * The configuration options are read from the configuration String of the widget.<p>
045 *
046 * The configuration String has to be formatted as JSON object, with the following possible keys:<p>
047 * <ul>
048 * <li><code>class</code>: optional class implementing {@link I_CmsImageWidgetDynamicConfiguration} to dynamically
049 *            configure startup parameters and format values.</li>
050 * <li><code>formatnames</code>: list of format names to select, with pairs of selectable value and selectable text,
051 *            e.g. value1:optiontext1|value2:optiontext2</li>
052 * <li><code>formatvalues</code>: corresponding format values to the format names list,
053 *            can be dynamically generated by the dynamic configuration class.
054 *            The list of values should contain width and height information, with a '?' as sign for dynamic size
055 *            and with an 'x' as separator for width and height.
056 *            Example: ['200x?', '800x600']</li>
057 * <li><code>scaleparams</code>: default scale parameters (no width, height or crop information should be provided!)</li>
058 * <li><code>startup</code>: the startup folder, can be dynamically generated by the provided class,
059 *            in that case, use 'dynamic' as value.</li>
060 * <li><code>type</code>: the startup folder type, can be 'gallery' or 'category'. Can be dynamically generated
061 *            by the provided class, in that case, use 'dynamic' as value.</li>
062 * <li><code>usedescription</code>: indicates if the description input field for the image should be shown or not.</li>
063 * <li><code>useformat</code>: indicates if the format select box for the image should be shown or not.</li>
064 * </ul>
065 *
066 * An example configuration can look like this:<p>
067 * <code>{scaleparams: 'q:70,r:2,c:CCCC00', type: 'gallery', startup: '/demo_en/images/',
068 * usedescription: true, useformat: true, formatnames: 'imageleft:Image left|imageright:Image right|imagetop:Image top',
069 * formatvalues: ['150x?', '250x300', '?x250']}</code><p>
070 *
071 * @since 7.5.0
072 */
073public class CmsVfsImageWidgetConfiguration extends CmsGalleryWidgetConfiguration {
074
075    /** Configuration key name for the formatnames configuration. */
076    public static final String CONFIG_KEY_FORMATNAMES = "formatnames";
077
078    /** Configuration key name for the formatvalues configuration. */
079    public static final String CONFIG_KEY_FORMATVALUES = "formatvalues";
080
081    /** Configuration key name for the scaleparams configuration. */
082    public static final String CONFIG_KEY_SCALEPARAMS = "scaleparams";
083
084    /** Configuration key name for the usedescription configuration. */
085    public static final String CONFIG_KEY_USEDESCRIPTION = "usedescription";
086
087    /** Configuration key name for the useformat configuration. */
088    public static final String CONFIG_KEY_USEFORMAT = "useformat";
089
090    /** The type "category" for the initial image list to load. */
091    public static final String TYPE_CATEGORY = "category";
092
093    /** The type "gallery" for the initial image list to load. */
094    public static final String TYPE_GALLERY = "gallery";
095
096    /** The list of image format values matching the options for the format select box. */
097    private List<String> m_formatValues;
098
099    /** The scale parameters to apply to a scaled image (e.g. quality, type). */
100    private String m_scaleParams;
101
102    /** The list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects. */
103    private List<CmsSelectWidgetOption> m_selectFormat;
104
105    /** The select options for the format select box as String. */
106    private String m_selectFormatString;
107
108    /** The flag if the description field should be shown. */
109    private boolean m_showDescription;
110
111    /** The flag if the format select box should be shown. */
112    private boolean m_showFormat;
113
114    /**
115     * Generates an initialized configuration for the image widget using the given configuration string.<p>
116     *
117     * @param cms an initialized instance of a CmsObject
118     * @param widgetDialog the dialog where the widget is used on
119     * @param param the widget parameter to generate the widget for
120     * @param configuration the widget configuration string
121     */
122    public CmsVfsImageWidgetConfiguration(
123        CmsObject cms,
124        CmsMessages widgetDialog,
125        I_CmsWidgetParameter param,
126        String configuration) {
127
128        super();
129        init(cms, widgetDialog, param, configuration);
130    }
131
132    /**
133     * Returns the values as a parameter string.<p>
134     *
135     * @return the values as a parameter string
136     */
137    @Override
138    public String getConfigString() {
139
140        String result = "";
141        if (m_startup != null) {
142            result += "&startup=" + m_startup;
143        }
144        if (m_type != null) {
145            result += "&type=" + m_type;
146        }
147
148        return result;
149    }
150
151    /**
152     * Returns the list of image format values matching the options for the format select box.<p>
153     *
154     * @return the list of image format values matching the options for the format select box
155     */
156    public List<String> getFormatValues() {
157
158        return m_formatValues;
159    }
160
161    /**
162     * Returns the scale parameters to apply to a scaled image (e.g. quality, type).<p>
163     *
164     * @return scale the parameters to apply to a scaled image
165     */
166    public String getScaleParams() {
167
168        return m_scaleParams;
169    }
170
171    /**
172     * Returns the list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects.<p>
173     *
174     * @return the list of select options for the format select box
175     */
176    public List<CmsSelectWidgetOption> getSelectFormat() {
177
178        return m_selectFormat;
179    }
180
181    /**
182     * Returns the select options for the format select box as String.<p>
183     *
184     * The String has the following structure <code>format name 1:localized name 1|format name 2:localized name 2|...</code>.<p>
185     *
186     * @return the select options for the format select box
187     */
188    public String getSelectFormatString() {
189
190        return m_selectFormatString;
191    }
192
193    /**
194     * Returns if the description field should be shown.<p>
195     *
196     * @return true if the description field should be shown, otherwise false
197     */
198    public boolean isShowDescription() {
199
200        return m_showDescription;
201    }
202
203    /**
204     * Returns if the format select box should be shown.<p>
205     *
206     * @return true if the format select box should be shown, otherwise false
207     */
208    public boolean isShowFormat() {
209
210        return m_showFormat;
211    }
212
213    /**
214     * Initializes the widget configuration using the given configuration string.<p>
215     *
216     * @param cms an initialized instance of a CmsObject
217     * @param widgetDialog the dialog where the widget is used on
218     * @param param the widget parameter to generate the widget for
219     * @param configuration the widget configuration string
220     */
221    @Override
222    protected void init(CmsObject cms, CmsMessages widgetDialog, I_CmsWidgetParameter param, String configuration) {
223
224        if (configuration == null) {
225            // no configuration String found, return
226            return;
227        }
228        configuration = CmsMacroResolver.resolveMacros(configuration, cms, widgetDialog);
229        JSONObject jsonObj = new JSONObject();
230        try {
231            jsonObj = new JSONObject(configuration);
232        } catch (JSONException e) {
233            // initialization failed
234            return;
235        }
236        // determine the class name that fills in values dynamically
237        setClassName(jsonObj.optString(CONFIG_KEY_CLASS, null));
238        I_CmsImageWidgetDynamicConfiguration dynConf = null;
239        if (getClassName() != null) {
240            try {
241                dynConf = (I_CmsImageWidgetDynamicConfiguration)Class.forName(getClassName()).newInstance();
242            } catch (Exception e) {
243                // class not found
244            }
245        }
246        // determine if the description field should be shown
247        setShowDescription(jsonObj.optBoolean(CONFIG_KEY_USEDESCRIPTION));
248        // determine if the format select box should be shown
249        setShowFormat(jsonObj.optBoolean(CONFIG_KEY_USEFORMAT));
250        if (isShowFormat()) {
251            // only parse options if the format select box should be shown
252            String optionsStr = (String)jsonObj.opt(CONFIG_KEY_FORMATNAMES);
253            setSelectFormatString(optionsStr);
254            setSelectFormat(CmsSelectWidgetOption.parseOptions(optionsStr));
255            // get the corresponding format values as well
256            JSONArray formatValues = jsonObj.optJSONArray(CONFIG_KEY_FORMATVALUES);
257            if (formatValues != null) {
258                List<String> formatValueList = new ArrayList<String>(formatValues.length());
259                for (int i = 0; i < formatValues.length(); i++) {
260                    formatValueList.add(formatValues.optString(i));
261                }
262                setFormatValues(formatValueList);
263            }
264            if (dynConf != null) {
265                setFormatValues(
266                    dynConf.getFormatValues(cms, widgetDialog, param, getSelectFormat(), getFormatValues()));
267            }
268        }
269        // determine the initial image list settings
270        setType(jsonObj.optString(CONFIG_KEY_TYPE));
271        if ((CONFIG_VALUE_DYNAMIC.equals(getType()) || CmsStringUtil.isEmpty(getType())) && (dynConf != null)) {
272            setType(dynConf.getType(cms, widgetDialog, param));
273        }
274        setStartup(jsonObj.optString(CONFIG_KEY_STARTUP));
275        if ((CONFIG_VALUE_DYNAMIC.equals(getStartup()) || CmsStringUtil.isEmpty(getStartup())) && (dynConf != null)) {
276            setStartup(dynConf.getStartup(cms, widgetDialog, param));
277        }
278        // determine the scale parameters
279        setScaleParams(jsonObj.optString(CONFIG_KEY_SCALEPARAMS));
280    }
281
282    /**
283     * Sets the list of image format values matching the options for the format select box.<p>
284     *
285     * @param formatValues the list of image format values matching the options for the format select box
286     */
287    private void setFormatValues(List<String> formatValues) {
288
289        m_formatValues = formatValues;
290    }
291
292    /**
293     * Sets the scale parameters to apply to a scaled image (e.g. quality, type).<p>
294     *
295     * @param scaleParams the scale parameters to apply to a scaled image
296     */
297    private void setScaleParams(String scaleParams) {
298
299        m_scaleParams = scaleParams;
300    }
301
302    /**
303     * Sets the list of select options for the format select box, must contain {@link CmsSelectWidgetOption} objects.<p>
304     *
305     * @param selectFormat the list of select options for the format select box
306     */
307    private void setSelectFormat(List<CmsSelectWidgetOption> selectFormat) {
308
309        m_selectFormat = selectFormat;
310    }
311
312    /**
313     * Sets the select options for the format select box as String.<p>
314     *
315     * @param formatString the select options for the format select box as String
316     */
317    private void setSelectFormatString(String formatString) {
318
319        m_selectFormatString = formatString;
320    }
321
322    /**
323     * Sets if the description field should be shown.<p>
324     *
325     * @param showDescription true if the description field should be shown, otherwise false
326     */
327    private void setShowDescription(boolean showDescription) {
328
329        m_showDescription = showDescription;
330    }
331
332    /**
333     * Sets if the format select box should be shown.<p>
334     *
335     * @param showFormat true if the format select box should be shown, otherwise false
336     */
337    private void setShowFormat(boolean showFormat) {
338
339        m_showFormat = showFormat;
340    }
341
342}