📄 cmsform.java
字号:
// get the field value
String fieldValue = "";
if (!initial) {
fieldValue = jsp.getRequest().getParameter(CmsCaptchaField.C_PARAM_CAPTCHA_PHRASE);
if (fieldValue == null) {
fieldValue = "";
}
}
// get the image settings from the XML content
CmsCaptchaSettings captchaSettings = CmsCaptchaSettings.getInstance(jsp);
captchaSettings.init(cms, xmlContent, locale);
m_captchaField = new CmsCaptchaField(captchaSettings, fieldLabel, fieldValue);
}
}
}
/**
* Initializes the general online form settings.<p>
*
* @param content the XML configuration content
* @param cms the CmsObject to access the content values
* @param locale the currently active Locale
* @param messages the localized messages
* @throws Exception if initializing the form settings fails
*/
private void initFormGlobalConfiguration(CmsXmlContent content, CmsObject cms, Locale locale, CmsMessages messages)
throws Exception {
// get the form text
String stringValue = content.getStringValue(cms, NODE_FORMTEXT, locale);
setFormText(getConfigurationValue(stringValue, ""));
// get the form confirmation text
stringValue = content.getStringValue(cms, NODE_FORMCONFIRMATION, locale);
setFormConfirmationText(getConfigurationValue(stringValue, ""));
// get the optional target URI
stringValue = content.getStringValue(cms, NODE_TARGET_URI, locale);
setTargetUri(getConfigurationValue(stringValue, ""));
// get the mail from address
stringValue = content.getStringValue(cms, NODE_MAILFROM, locale);
setMailFrom(getConfigurationValue(stringValue, ""));
// get the mail to address(es)
stringValue = content.getStringValue(cms, NODE_MAILTO, locale);
setMailTo(getConfigurationValue(stringValue, ""));
// get the mail subject
stringValue = content.getStringValue(cms, NODE_MAILSUBJECT, locale);
setMailSubject(getConfigurationValue(stringValue, ""));
// get the optional mail subject prefix from localized messages
stringValue = messages.key("form.mailsubject.prefix");
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(stringValue)) {
// prefix present, set it
setMailSubjectPrefix(stringValue + " ");
} else {
// no prefix present
setMailSubjectPrefix("");
}
CmsXmlHtmlValue mailTextValue = (CmsXmlHtmlValue)content.getValue(NODE_MAILTEXT, locale);
if (mailTextValue != null) {
// get the mail text as plain text
stringValue = mailTextValue.getPlainText(cms);
setMailTextPlain(getConfigurationValue(stringValue, ""));
// get the mail text
stringValue = mailTextValue.getStringValue(cms);
setMailText(getConfigurationValue(stringValue, ""));
} else {
setMailTextPlain("");
setMailText("");
}
// optional configuration options
String pathPrefix = NODE_OPTIONALCONFIGURATION + "/";
// get the mail type
stringValue = content.getStringValue(cms, pathPrefix + NODE_MAILTYPE, locale);
setMailType(getConfigurationValue(stringValue, MAILTYPE_HTML));
// get the mail CC recipient(s)
stringValue = content.getStringValue(cms, pathPrefix + NODE_MAILCC, locale);
setMailCC(getConfigurationValue(stringValue, ""));
// get the mail BCC recipient(s)
stringValue = content.getStringValue(cms, pathPrefix + NODE_MAILBCC, locale);
setMailBCC(getConfigurationValue(stringValue, ""));
// get the form check page flag
stringValue = content.getStringValue(cms, pathPrefix + NODE_SHOWCHECK, locale);
setShowCheck(Boolean.valueOf(stringValue).booleanValue());
// get the check page text
stringValue = content.getStringValue(cms, pathPrefix + NODE_FORMCHECKTEXT, locale);
setFormCheckText(getConfigurationValue(stringValue, ""));
// get the form attributes
stringValue = content.getStringValue(cms, pathPrefix + NODE_FORMATTRIBUTES, locale);
if (CmsStringUtil.isNotEmpty(stringValue)) {
setFormAttributes(" " + stringValue);
}
// get the field attributes
stringValue = content.getStringValue(cms, pathPrefix + NODE_FORMFIELDATTRIBUTES, locale);
if (CmsStringUtil.isNotEmpty(stringValue)) {
setFormFieldAttributes(" " + stringValue);
} else {
// no field attributes specified, check default field attributes
String defaultAttributes = messages.key("form.field.default.attributes");
if (CmsStringUtil.isNotEmpty(defaultAttributes)) {
setFormFieldAttributes(" " + defaultAttributes);
}
}
// optional confirmation mail nodes
pathPrefix = NODE_OPTIONALCONFIRMATION + "/";
// get the confirmation mail enabled flag
stringValue = content.getStringValue(cms, pathPrefix + NODE_CONFIRMATIONMAILENABLED, locale);
setConfirmationMailEnabled(Boolean.valueOf(stringValue).booleanValue());
// get other confirmation mail nodes only if confirmation mail is enabled
if (isConfirmationMailEnabled()) {
// get the confirmation mail subject
stringValue = content.getStringValue(cms, pathPrefix + NODE_CONFIRMATIONMAILSUBJECT, locale);
setConfirmationMailSubject(getConfigurationValue(stringValue, ""));
mailTextValue = (CmsXmlHtmlValue)content.getValue(pathPrefix + NODE_CONFIRMATIONMAILTEXT, locale);
if (mailTextValue != null) {
// get the confirmation mail text
stringValue = mailTextValue.getPlainText(cms);
setConfirmationMailTextPlain(getConfigurationValue(stringValue, ""));
stringValue = mailTextValue.getStringValue(cms);
setConfirmationMailText(getConfigurationValue(stringValue, ""));
} else {
setConfirmationMailTextPlain("");
setConfirmationMailText("");
}
// get the confirmation mail field index number
stringValue = content.getStringValue(cms, pathPrefix + NODE_CONFIRMATIONMAILFIELD, locale);
int fieldIndex = 1;
try {
fieldIndex = Integer.parseInt(getConfigurationValue(stringValue, "1")) - 1;
} catch (Exception e) {
// ignore this exception, use first field
}
setConfirmationMailField(fieldIndex);
// get the confirmation mail optional flag
stringValue = content.getStringValue(cms, pathPrefix + NODE_CONFIRMATIONMAILOPTIONAL, locale);
setConfirmationMailOptional(Boolean.valueOf(stringValue).booleanValue());
// get the confirmation mail checkbox label text
stringValue = content.getStringValue(cms, pathPrefix + NODE_CONFIRMATIONMAILCHECKBOXLABEL, locale);
setConfirmationMailCheckboxLabel(getConfigurationValue(
stringValue,
messages.key("form.confirmation.checkbox")));
}
}
/**
* Initializes the field objects of the form.<p>
*
* @param content the XML configuration content
* @param jsp the initialized CmsJspActionElement to access the OpenCms API
* @param locale the currently active Locale
* @param messages the localized messages
* @param initial if true, field values are filled with values specified in the XML configuration, otherwise values are read from the request
* @throws CmsConfigurationException if parsing the configuration fails
*/
private void initInputFields(
CmsXmlContent content,
CmsJspActionElement jsp,
Locale locale,
CmsMessages messages,
boolean initial) throws CmsConfigurationException {
CmsObject cms = jsp.getCmsObject();
List fieldValues = content.getValues(NODE_INPUTFIELD, locale);
int fieldValueSize = fieldValues.size();
CmsFieldFactory fieldFactory = CmsFieldFactory.getSharedInstance();
for (int i = 0; i < fieldValueSize; i++) {
I_CmsXmlContentValue inputField = (I_CmsXmlContentValue)fieldValues.get(i);
String inputFieldPath = inputField.getPath() + "/";
A_CmsField field = null;
// get the field from the factory for the specified type
String stringValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDTYPE, locale);
field = fieldFactory.getField(stringValue);
// create the field name
field.setName(inputFieldPath.substring(0, inputFieldPath.length() - 1));
// get the field label
stringValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDLABEL, locale);
field.setLabel(getConfigurationValue(stringValue, ""));
// validation error message
stringValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDERRORMESSAGE, locale);
field.setErrorMessage(stringValue);
// get the field value
if (initial && CmsStringUtil.isEmpty(jsp.getRequest().getParameter(field.getName()))) {
// only fill in values from configuration file if called initially
String fieldValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDDEFAULTVALUE, locale);
if (CmsStringUtil.isNotEmpty(fieldValue)) {
CmsMacroResolver resolver = CmsMacroResolver.newInstance().setCmsObject(cms).setJspPageContext(
jsp.getJspContext());
field.setValue(resolver.resolveMacros(fieldValue));
}
} else {
// get field value from request for standard fields
if (!CmsCheckboxField.class.isAssignableFrom(field.getClass())) {
String[] parameterValues = jsp.getRequest().getParameterValues(field.getName());
StringBuffer value = new StringBuffer();
if (parameterValues != null) {
for (int j = 0; j < parameterValues.length; j++) {
if (j != 0) {
value.append(", ");
}
value.append(parameterValues[j]);
}
}
field.setValue(value.toString());
}
}
// fill object members in case this is no hidden field
if (!CmsHiddenField.class.isAssignableFrom(field.getClass())) {
// get the field validation regular expression
stringValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDVALIDATION, locale);
if (CmsEmailField.class.isAssignableFrom(field.getClass()) && CmsStringUtil.isEmpty(stringValue)) {
// set default email validation expression for confirmation email address input field
field.setValidationExpression(CmsEmailField.VALIDATION_REGEX);
} else {
field.setValidationExpression(getConfigurationValue(stringValue, ""));
}
// get the field mandatory flag
stringValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDMANDATORY, locale);
boolean isMandatory = Boolean.valueOf(stringValue).booleanValue();
field.setMandatory(isMandatory);
if (isMandatory) {
// set flag that determines if mandatory fields are present
setHasMandatoryFields(true);
}
if (field.needsItems()) {
// create items for checkboxes, radio buttons and selectboxes
String fieldValue = content.getStringValue(cms, inputFieldPath + NODE_FIELDDEFAULTVALUE, locale);
if (CmsStringUtil.isNotEmpty(fieldValue)) {
// get items from String
StringTokenizer T = new StringTokenizer(fieldValue, "|");
List items = new ArrayList(T.countTokens());
while (T.hasMoreTokens()) {
String part = T.nextToken();
// check preselection of current item
boolean isPreselected = part.indexOf('*') != -1;
String value = "";
String label = "";
String selected = "";
int delimPos = part.indexOf(':');
if (delimPos != -1) {
// a special label text is given
value = part.substring(0, delimPos);
label = part.substring(delimPos + 1);
} else {
// no special label text present, use complete String
value = part;
label = value;
}
if (isPreselected) {
// remove preselected flag marker from Strings
value = CmsStringUtil.substitute(value, "*", "");
label = CmsStringUtil.substitute(label, "*", "");
}
if (initial) {
// only fill in values from configuration file if called initially
if (isPreselected) {
selected = Boolean.toString(true);
}
} else {
// get selected flag from request for current item
selected = readSelectedFromRequest(
jsp.getRequest(),
field,
value);
}
// add new item object
items.add(new CmsFieldItem(value, label, Boolean.valueOf(selected).booleanValue()));
}
field.setItems(items);
} else {
// no items specified for checkbox, radio button or selectbox
throw new CmsConfigurationException(Messages.get().container(
Messages.ERR_INIT_INPUT_FIELD_MISSING_ITEM_2,
field.getName(),
field.getType()));
}
}
}
addField(field);
}
// validate the form configuration
validateFormConfiguration(messages);
if (isConfirmationMailEnabled() && isConfirmationMailOptional()) {
// add the checkbox to activate confirmation mail for customer
I_CmsField confirmationMailCheckbox = createConfirmationMailCheckbox(jsp, messages, initial);
addField(confirmationMailCheckbox);
}
}
/**
* Initializes the member variables.<p>
*/
private void initMembers() {
setConfigurationErrors(new ArrayList());
setFormAttributes("");
setFormCheckText("");
setFormConfirmationText("");
setFormFieldAttributes("");
setFormText("");
setMailBCC("");
setMailCC("");
setMailFrom("");
setMailSubject("");
setMailText("");
setMailTextPlain("");
setMailTo("");
setMailType(MAILTYPE_HTML);
setConfirmationMailSubject("");
setConfirmationMailText("");
setConfirmationMailTextPlain("");
}
/**
* Validates the loaded online form configuration and creates a list of error messages, if necessary.<p>
*
* @param messages the localized messages
*/
private void validateFormConfiguration(CmsMessages messages) {
if (isConfirmationMailEnabled()) {
// confirmation mail is enabled, make simple field check to avoid errors
I_CmsField confirmField = new CmsTextField();
try {
// try to get the confirmation email field
confirmField = (I_CmsField)getFields().get(getConfirmationMailField());
} catch (IndexOutOfBoundsException e) {
// specified confirmation email field does not exist
getConfigurationErrors().add(messages.key("form.configuration.error.emailfield.notfound"));
setConfirmationMailEnabled(false);
return;
}
if (!CmsEmailField.class.isAssignableFrom(confirmField.getClass())) {
// specified confirmation mail input field has wrong field type
getConfigurationErrors().add(messages.key("form.configuration.error.emailfield.type"));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -