All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencms.apollo.template.webform.CmsSelectFieldWidget Maven / Gradle / Ivy

/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.apollo.template.webform;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsMessages;
import org.opencms.util.CmsMacroResolver;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.CmsInputWidget;
import org.opencms.widgets.CmsSelectWidget;
import org.opencms.widgets.CmsSelectWidgetOption;
import org.opencms.widgets.I_CmsWidget;
import org.opencms.widgets.I_CmsWidgetDialog;
import org.opencms.widgets.I_CmsWidgetParameter;
import org.opencms.xml.content.CmsXmlContent;
import org.opencms.xml.types.A_CmsXmlContentValue;
import org.opencms.xml.types.I_CmsXmlContentValue;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Special select widget that generates options from the configured form input fields.

* * The configuration contains key=value pairs, separated by a pipe |: *

    *
  • convertfieldindex: if true, a found old field value which is a number is tried to be converted to the field label.
  • *
  • fieldtype: the type of input fields that should be shown as select options.
  • *
  • referencebylabel: if true, the field label is stored to point to the field, otherwise the xPath.
  • *
*/ public class CmsSelectFieldWidget extends CmsSelectWidget { /** Configuration option key: convert the found field index. */ protected static final String CONFIGURATION_CONVERTFIELDINDEX = "convertfieldindex"; /** Configuration option key: field type. */ protected static final String CONFIGURATION_FIELDTYPE = "fieldtype"; /** Configuration option key: reference input field by label. */ protected static final String CONFIGURATION_REFERENCEBYLABEL = "referencebylabel"; /** Indicates to convert the found field index. */ private boolean m_convertFieldIndex; /** The field type to show as select options. */ private String m_fieldType; /** Indicates to reference the input field by label. */ private boolean m_referenceByLabel; /** * Creates a new select widget.

*/ public CmsSelectFieldWidget() { // empty constructor is required for class registration super(); } /** * Creates a select widget with the specified select options.

* * @param configuration the configuration (possible options) for the select box */ public CmsSelectFieldWidget(String configuration) { super(configuration); } /** * @see org.opencms.widgets.A_CmsSelectWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale) */ @Override public String getConfiguration( CmsObject cms, A_CmsXmlContentValue schemaType, CmsMessages messages, CmsResource resource, Locale contentLocale) { // no configuration needed return ""; } /** * @see org.opencms.widgets.CmsSelectWidget#getWidgetName() */ @Override public String getWidgetName() { // use the basic string widget for new content editor return CmsInputWidget.class.getName(); } /** * @see org.opencms.widgets.I_CmsWidget#newInstance() */ @Override public I_CmsWidget newInstance() { return new CmsSelectFieldWidget(getConfiguration()); } /** * @see org.opencms.widgets.A_CmsSelectWidget#getSelectedValue(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetParameter) */ @Override protected String getSelectedValue(CmsObject cms, I_CmsWidgetParameter param) { if (isConvertFieldIndex()) { try { Integer.parseInt(param.getStringValue(cms)); param.setStringValue(cms, null); } catch (Exception e) { // failed, just proceed } } return super.getSelectedValue(cms, param); } /** * Returns the select options for the widget, generated from the configured input fields of the XML content.

* * @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter) */ @Override protected List parseSelectOptions( CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) { // parse widget configuration parseConfiguration(cms, widgetDialog); // only create options if not already done if (getSelectOptions() == null) { List result = new ArrayList(); int fieldIndex = -1; // field index conversion is switched on, try to parse index if (isConvertFieldIndex()) { try { fieldIndex = Integer.parseInt(param.getStringValue(cms)); } catch (Exception e) { // failed to parse value, proceed } } // cast param to I_CmsXmlContentValue I_CmsXmlContentValue contentValue = (I_CmsXmlContentValue)param; Locale locale = contentValue.getLocale(); CmsXmlContent content = (CmsXmlContent)contentValue.getDocument(); List fieldValues = CmsFormContentUtil.getContentValues( content, CmsForm.NODE_INPUTFIELD, locale); int fieldValueSize = fieldValues.size(); for (int i = 0; i < fieldValueSize; i++) { I_CmsXmlContentValue inputField = fieldValues.get(i); // get the xPath as value String value = inputField.getPath(); // check the field type if configured if (CmsStringUtil.isNotEmpty(getFieldType())) { String type = content.getStringValue(cms, value + "/" + CmsForm.NODE_FIELDTYPE, locale); if (!getFieldType().equals(type)) { // not the defined field type, skip field continue; } } // get the field label String label = content.getStringValue(cms, value + "/" + CmsForm.NODE_FIELDLABEL, locale); label = CmsForm.getConfigurationValue(label, ""); if (isReferenceByLabel()) { // reference by label, check label if (CmsStringUtil.isEmptyOrWhitespaceOnly(label)) { // skip fields without label text continue; } // set the value to the label value = label; } if (CmsStringUtil.isEmpty(label)) { label = value; } // remove DB label if present int pos = label.indexOf('|'); if (pos > -1) { if (isReferenceByLabel() && ((pos + 1) < label.length())) { // only use DB label for reference value = label.substring(pos + 1); } label = label.substring(0, pos); } boolean isDefault = false; // check if this field should be marked as default because of index conversion if ((fieldIndex > 0) && (fieldIndex == (i + 1))) { isDefault = true; } result.add(new CmsSelectWidgetOption(value, isDefault, label)); } setSelectOptions(result); } return getSelectOptions(); } /** * Returns the field type to show as select options.

* * @return the field type to show as select options */ private String getFieldType() { return m_fieldType; } /** * Returns if the found field index should be converted to a field label when opening the form in an editor.

* * @return true if the found field index should be converted to a field label, otherwise false */ private boolean isConvertFieldIndex() { return m_convertFieldIndex; } /** * Returns if an input field is referenced by its label.

* * @return true if an input field is referenced by its label, otherwise false */ private boolean isReferenceByLabel() { return m_referenceByLabel; } /** * Parses the widget configuration string.

* * @param cms the current users OpenCms context * @param widgetDialog the dialog of this widget */ private void parseConfiguration(CmsObject cms, I_CmsWidgetDialog widgetDialog) { String configString = CmsMacroResolver.resolveMacros(getConfiguration(), cms, widgetDialog.getMessages()); Map config = CmsStringUtil.splitAsMap(configString, "|", "="); m_referenceByLabel = Boolean.valueOf(config.get(CONFIGURATION_REFERENCEBYLABEL)).booleanValue(); m_convertFieldIndex = Boolean.valueOf(config.get(CONFIGURATION_CONVERTFIELDINDEX)).booleanValue(); m_fieldType = config.get(CONFIGURATION_FIELDTYPE); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy