001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsFormHandler.java,v $
003: * Date : $Date: 2008-02-27 12:05:22 $
004: * Version: $Revision: 1.29 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.frontend.templateone.form;
033:
034: import org.opencms.i18n.CmsEncoder;
035: import org.opencms.i18n.CmsMessages;
036: import org.opencms.jsp.CmsJspActionElement;
037: import org.opencms.mail.CmsHtmlMail;
038: import org.opencms.mail.CmsSimpleMail;
039: import org.opencms.main.CmsLog;
040: import org.opencms.main.OpenCms;
041: import org.opencms.module.CmsModule;
042: import org.opencms.util.CmsRequestUtil;
043: import org.opencms.util.CmsStringUtil;
044: import org.opencms.util.CmsByteArrayDataSource;
045:
046: import java.io.File;
047: import java.util.ArrayList;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Map;
053: import java.util.StringTokenizer;
054:
055: import javax.mail.internet.AddressException;
056: import javax.mail.internet.InternetAddress;
057: import javax.servlet.http.HttpServletRequest;
058: import javax.servlet.http.HttpServletResponse;
059: import javax.servlet.jsp.PageContext;
060:
061: import org.apache.commons.fileupload.FileItem;
062: import org.apache.commons.logging.Log;
063:
064: /**
065: * The form handler controls the html or mail output of a configured email form.<p>
066: *
067: * Provides methods to determine the action that takes place and methods to create different
068: * output formats of a submitted form.<p>
069: *
070: * @author Andreas Zahner
071: * @author Thomas Weckert
072: * @author Jan Baudisch
073: *
074: * @version $Revision: 1.29 $
075: *
076: * @since 6.0.0
077: */
078: public class CmsFormHandler extends CmsJspActionElement {
079:
080: /** Request parameter value for the form action parameter: correct the input. */
081: public static final String ACTION_CONFIRMED = "confirmed";
082:
083: /** Request parameter value for the form action parameter: correct the input. */
084: public static final String ACTION_CORRECT_INPUT = "correct";
085:
086: /** Request parameter value for the form action parameter: form submitted. */
087: public static final String ACTION_SUBMIT = "submit";
088:
089: /** Form error: mandatory field not filled out. */
090: public static final String ERROR_MANDATORY = "mandatory";
091:
092: /** Form error: validation error of input. */
093: public static final String ERROR_VALIDATION = "validation";
094:
095: /** Request parameter name for the hidden form action parameter to determine the action. */
096: public static final String PARAM_FORMACTION = "formaction";
097:
098: /** Name of the file item session attribute. */
099: public static final String ATTRIBUTE_FILEITEMS = "fileitems";
100:
101: /** The log object for this class. */
102: private static final Log LOG = CmsLog.getLog(CmsFormHandler.class);
103:
104: private static final String MODULE = "org.opencms.frontend.templateone.form";
105:
106: /** Contains eventual validation errors. */
107: private Map m_errors;
108:
109: /** The form configuration object. */
110: private CmsForm m_formConfiguration;
111:
112: /** Temporarily stores the fields as hidden fields in the String. */
113: private String m_hiddenFields;
114:
115: /** Flag indicating if the form is displayed for the first time. */
116: private boolean m_initial;
117:
118: /** The localized messages for the form handler. */
119: private CmsMessages m_messages;
120:
121: /** Boolean indicating if the form is validated correctly. */
122: private Boolean m_isValidatedCorrect;
123:
124: /** The map of request parameters. */
125: private Map m_parameterMap;
126:
127: /** The multipart file items. */
128: private List m_mulipartFileItems;
129:
130: /**
131: * Constructor, creates the necessary form configuration objects.<p>
132: *
133: * @param context the JSP page context object
134: * @param req the JSP request
135: * @param res the JSP response
136: * @throws Exception if creating the form configuration objects fails
137: */
138: public CmsFormHandler(PageContext context, HttpServletRequest req,
139: HttpServletResponse res) throws Exception {
140:
141: super (context, req, res);
142: init(req, null);
143: }
144:
145: /**
146: * Constructor, creates the necessary form configuration objects using a given configuration file URI.<p>
147: *
148: * @param context the JSP page context object
149: * @param req the JSP request
150: * @param res the JSP response
151: * @param formConfigUri URI of the form configuration file, if not provided, current URI is used for configuration
152: * @throws Exception if creating the form configuration objects fails
153: */
154: public CmsFormHandler(PageContext context, HttpServletRequest req,
155: HttpServletResponse res, String formConfigUri)
156: throws Exception {
157:
158: super (context, req, res);
159: init(req, formConfigUri);
160: }
161:
162: /**
163: * Replaces line breaks with html <br>.<p>
164: *
165: * @param value the value to substitute
166: * @return the substituted value
167: */
168: public String convertToHtmlValue(String value) {
169:
170: return convertValue(value, "html");
171: }
172:
173: /**
174: * Replaces html <br> with line breaks.<p>
175: *
176: * @param value the value to substitute
177: * @return the substituted value
178: */
179: public String convertToPlainValue(String value) {
180:
181: return convertValue(value, "");
182: }
183:
184: /**
185: * Converts a given String value to the desired output format.<p>
186: *
187: * The following output formats are possible:
188: * <ul>
189: * <li>"html" meaning that <br> tags are added</li>
190: * <li>"plain" or any other String value meaning that <br> tags are removed</li>
191: * </ul>
192: *
193: * @param value the String value to convert
194: * @param outputType the type of the resulting output
195: * @return the converted String in the desired output format
196: */
197: public String convertValue(String value, String outputType) {
198:
199: if ("html".equalsIgnoreCase(outputType)) {
200: // output should be html, add line break tags and characters
201: value = CmsStringUtil.escapeHtml(value);
202: } else {
203: // output should be plain, remove html line break tags and characters
204: value = CmsStringUtil.substitute(value, "<br>", "\n");
205: }
206: return value;
207: }
208:
209: /**
210: * Returns the configured form field values as hidden input fields.<p>
211: *
212: * @return the configured form field values as hidden input fields
213: */
214: public String createHiddenFields() {
215:
216: if (CmsStringUtil.isEmpty(m_hiddenFields)) {
217: List fields = getFormConfiguration().getFields();
218: StringBuffer result = new StringBuffer(fields.size() * 8);
219: // iterate the form fields
220: for (int i = 0, n = fields.size(); i < n; i++) {
221:
222: I_CmsField currentField = (I_CmsField) fields.get(i);
223:
224: if (currentField == null) {
225: continue;
226: } else if (CmsCheckboxField.class
227: .isAssignableFrom(currentField.getClass())) {
228: // special case: checkbox, can have more than one value
229: Iterator k = currentField.getItems().iterator();
230: while (k.hasNext()) {
231: CmsFieldItem item = (CmsFieldItem) k.next();
232: if (item.isSelected()) {
233: result
234: .append("<input type=\"hidden\" name=\"");
235: result.append(currentField.getName());
236: result.append("\" value=\"");
237: result.append(CmsEncoder.escapeXml(item
238: .getValue()));
239: result.append("\">\n");
240: }
241: }
242: } else if (CmsStringUtil.isNotEmpty(currentField
243: .getValue())) {
244: // all other fields are converted to a simple hidden field
245: result.append("<input type=\"hidden\" name=\"");
246: result.append(currentField.getName());
247: result.append("\" value=\"");
248: result.append(CmsEncoder.escapeXml(currentField
249: .getValue()));
250: result.append("\">\n");
251: }
252:
253: }
254: // store the generated input fields for further usage to avoid unnecessary rebuilding
255: m_hiddenFields = result.toString();
256: }
257: // return generated result list
258: return m_hiddenFields;
259: }
260:
261: /**
262: * Returns the errors found when validating the form.<p>
263: *
264: * @return the errors found when validating the form
265: */
266: public Map getErrors() {
267:
268: return m_errors;
269: }
270:
271: /**
272: * Returns the form configuration.<p>
273: *
274: * @return the form configuration
275: */
276: public CmsForm getFormConfiguration() {
277:
278: return m_formConfiguration;
279: }
280:
281: /**
282: * Returns the localized messages.<p>
283: *
284: * @return the localized messages
285: */
286: public CmsMessages getMessages() {
287:
288: return m_messages;
289: }
290:
291: /**
292: * Returns if the submitted values contain validation errors.<p>
293: *
294: * @return true if the submitted values contain validation errors, otherwise false
295: */
296: public boolean hasValidationErrors() {
297:
298: return (!isInitial() && (getErrors().size() > 0));
299: }
300:
301: /**
302: * Initializes the form handler and creates the necessary configuration objects.<p>
303: *
304: * @param req the JSP request
305: * @param formConfigUri URI of the form configuration file, if not provided, current URI is used for configuration
306: * @throws Exception if creating the form configuration objects fails
307: */
308: public void init(HttpServletRequest req, String formConfigUri)
309: throws Exception {
310:
311: m_mulipartFileItems = CmsRequestUtil
312: .readMultipartFileItems(req);
313:
314: if (m_mulipartFileItems != null) {
315: m_parameterMap = CmsRequestUtil
316: .readParameterMapFromMultiPart(getRequestContext()
317: .getEncoding(), m_mulipartFileItems);
318: } else {
319: m_parameterMap = new HashMap();
320: }
321:
322: if (m_mulipartFileItems != null) {
323: Map fileUploads = (Map) req.getSession().getAttribute(
324: ATTRIBUTE_FILEITEMS);
325: if (fileUploads == null) {
326: fileUploads = new HashMap();
327: }
328: // check, if there are any attachments
329: Iterator i = m_mulipartFileItems.iterator();
330: while (i.hasNext()) {
331: FileItem fileItem = (FileItem) i.next();
332: if (CmsStringUtil.isNotEmpty(fileItem.getName())) {
333: // append file upload to the map of file items
334: fileUploads.put(fileItem.getFieldName(), fileItem);
335: m_parameterMap.put(fileItem.getFieldName(),
336: new String[] { fileItem.getName() });
337: }
338: }
339: req.getSession().setAttribute(ATTRIBUTE_FILEITEMS,
340: fileUploads);
341: } else {
342: req.getSession().removeAttribute(ATTRIBUTE_FILEITEMS);
343: }
344: String formAction = getParameter(PARAM_FORMACTION);
345: setErrors(new HashMap());
346: m_isValidatedCorrect = null;
347: setInitial(CmsStringUtil.isEmpty(formAction));
348: // get the localized messages
349: CmsModule module = OpenCms.getModuleManager().getModule(MODULE);
350: String para = module.getParameter("message",
351: "/org/opencms/frontend/templateone/form/workplace");
352:
353: setMessages(new CmsMessages(para, getRequestContext()
354: .getLocale()));
355: // get the form configuration
356: setFormConfiguration(new CmsForm(this , getMessages(),
357: isInitial(), formConfigUri, formAction));
358: }
359:
360: /**
361: * Returns if the form is displayed for the first time.<p>
362: *
363: * @return true if the form is displayed for the first time, otherwise false
364: */
365: public boolean isInitial() {
366:
367: return m_initial;
368: }
369:
370: /**
371: * Returns the map of request parameters.<p>
372: *
373: * @return the map of request parameters
374: */
375: public Map getParameterMap() {
376:
377: return m_parameterMap;
378: }
379:
380: /**
381: * Sends the confirmation mail with the form data to the specified email address.<p>
382: *
383: * @throws Exception if sending the confirmation mail fails
384: */
385: public void sendConfirmationMail() throws Exception {
386:
387: // get the field which contains the confirmation email address
388: I_CmsField mailField = (I_CmsField) getFormConfiguration()
389: .getFields().get(
390: getFormConfiguration()
391: .getConfirmationMailField());
392: String mailTo = mailField.getValue();
393:
394: // create the new confirmation mail message depending on the configured email type
395: if (getFormConfiguration().getMailType().equals(
396: CmsForm.MAILTYPE_HTML)) {
397: // create a HTML email
398: CmsHtmlMail theMail = new CmsHtmlMail();
399: theMail.setCharset(getCmsObject().getRequestContext()
400: .getEncoding());
401: if (CmsStringUtil.isNotEmpty(getFormConfiguration()
402: .getMailFrom())) {
403: theMail.setFrom(getFormConfiguration().getMailFrom());
404: }
405: theMail.setTo(createInternetAddresses(mailTo));
406: theMail.setSubject(getFormConfiguration()
407: .getMailSubjectPrefix()
408: + getFormConfiguration()
409: .getConfirmationMailSubject());
410: theMail.setHtmlMsg(createMailTextFromFields(true, true));
411: theMail.setTextMsg(createMailTextFromFields(false, true));
412: // send the mail
413: theMail.send();
414: } else {
415: // create a plain text email
416: CmsSimpleMail theMail = new CmsSimpleMail();
417: theMail.setCharset(getCmsObject().getRequestContext()
418: .getEncoding());
419: if (CmsStringUtil.isNotEmpty(getFormConfiguration()
420: .getMailFrom())) {
421: theMail.setFrom(getFormConfiguration().getMailFrom());
422: }
423: theMail.setTo(createInternetAddresses(mailTo));
424: theMail.setSubject(getFormConfiguration()
425: .getMailSubjectPrefix()
426: + getFormConfiguration()
427: .getConfirmationMailSubject());
428: theMail.setMsg(createMailTextFromFields(false, true));
429: // send the mail
430: theMail.send();
431: }
432: }
433:
434: /**
435: * Sends the mail with the form data to the specified recipients.<p>
436: *
437: * If configured, sends also a confirmation mail to the form submitter.<p>
438: *
439: * @return true if the mail has been successfully sent, otherwise false
440: */
441: public boolean sendMail() {
442:
443: try {
444: // send optional confirmation mail
445: if (getFormConfiguration().isConfirmationMailEnabled()) {
446: if (!getFormConfiguration()
447: .isConfirmationMailOptional()
448: || Boolean
449: .valueOf(
450: getParameter(CmsForm.PARAM_SENDCONFIRMATION))
451: .booleanValue()) {
452: sendConfirmationMail();
453: }
454: }
455: // create the new mail message depending on the configured email type
456: if (getFormConfiguration().getMailType().equals(
457: CmsForm.MAILTYPE_HTML)) {
458: // create a HTML email
459: CmsHtmlMail theMail = new CmsHtmlMail();
460: theMail.setCharset(getCmsObject().getRequestContext()
461: .getEncoding());
462: if (CmsStringUtil.isNotEmpty(getFormConfiguration()
463: .getMailFrom())) {
464: theMail.setFrom(getFormConfiguration()
465: .getMailFrom());
466: }
467: theMail
468: .setTo(createInternetAddresses(getFormConfiguration()
469: .getMailTo()));
470: theMail
471: .setCc(createInternetAddresses(getFormConfiguration()
472: .getMailCC()));
473: theMail
474: .setBcc(createInternetAddresses(getFormConfiguration()
475: .getMailBCC()));
476: theMail.setSubject(getFormConfiguration()
477: .getMailSubjectPrefix()
478: + getFormConfiguration().getMailSubject());
479: theMail
480: .setHtmlMsg(createMailTextFromFields(true,
481: false));
482: theMail.setTextMsg(createMailTextFromFields(false,
483: false));
484:
485: // attach file uploads
486: Map fileUploads = (Map) getRequest().getSession()
487: .getAttribute(ATTRIBUTE_FILEITEMS);
488: Iterator i = fileUploads.values().iterator();
489: while (i.hasNext()) {
490: FileItem attachment = (FileItem) i.next();
491: if (attachment != null) {
492: String filename = attachment
493: .getName()
494: .substring(
495: attachment.getName()
496: .lastIndexOf(
497: File.separator) + 1);
498: theMail
499: .attach(
500: new CmsByteArrayDataSource(
501: filename,
502: attachment.get(),
503: OpenCms
504: .getResourceManager()
505: .getMimeType(
506: filename,
507: null,
508: "application/octet-stream")),
509: filename, filename);
510: }
511: }
512:
513: // send the mail
514: theMail.send();
515: } else {
516: // create a plain text email
517: CmsSimpleMail theMail = new CmsSimpleMail();
518: theMail.setCharset(getCmsObject().getRequestContext()
519: .getEncoding());
520: if (CmsStringUtil.isNotEmpty(getFormConfiguration()
521: .getMailFrom())) {
522: theMail.setFrom(getFormConfiguration()
523: .getMailFrom());
524: }
525: theMail
526: .setTo(createInternetAddresses(getFormConfiguration()
527: .getMailTo()));
528: theMail
529: .setCc(createInternetAddresses(getFormConfiguration()
530: .getMailCC()));
531: theMail
532: .setBcc(createInternetAddresses(getFormConfiguration()
533: .getMailBCC()));
534: theMail.setSubject(getFormConfiguration()
535: .getMailSubjectPrefix()
536: + getFormConfiguration().getMailSubject());
537: theMail.setMsg(createMailTextFromFields(false, false));
538: // send the mail
539: theMail.send();
540: }
541: } catch (Exception e) {
542: // an error occured during mail creation
543: if (LOG.isErrorEnabled()) {
544: LOG.error(e.getLocalizedMessage(), e);
545: }
546: m_errors.put("sendmail", e.getMessage());
547: return false;
548: }
549: return true;
550: }
551:
552: /**
553: * Returns the request parameter with the specified name.<p>
554: *
555: * @param parameter the parameter to return
556: *
557: * @return the parameter value
558: */
559: private String getParameter(String parameter) {
560:
561: try {
562: return ((String[]) m_parameterMap.get(parameter))[0];
563: } catch (NullPointerException e) {
564: return "";
565: }
566: }
567:
568: /**
569: * Returns if the optional check page should be displayed.<p>
570: *
571: * @return true if the optional check page should be displayed, otherwise false
572: */
573: public boolean showCheck() {
574:
575: boolean result = false;
576:
577: if (getFormConfiguration().getShowCheck()
578: && ACTION_SUBMIT.equals(getParameter(PARAM_FORMACTION))) {
579: result = true;
580: } else if (getFormConfiguration().captchaFieldIsOnCheckPage()
581: && ACTION_CONFIRMED
582: .equals(getParameter(PARAM_FORMACTION))
583: && !validate()) {
584: result = true;
585: }
586:
587: return result;
588: }
589:
590: /**
591: * Returns if the input form should be displayed.<p>
592: *
593: * @return true if the input form should be displayed, otherwise false
594: */
595: public boolean showForm() {
596:
597: boolean result = false;
598:
599: if (isInitial()) {
600: // initial call
601: result = true;
602: } else if (ACTION_CORRECT_INPUT
603: .equalsIgnoreCase(getParameter(PARAM_FORMACTION))) {
604: // user decided to modify his inputs
605: result = true;
606: } else if (ACTION_SUBMIT
607: .equalsIgnoreCase(getParameter(PARAM_FORMACTION))
608: && !validate()) {
609: // input field validation failed
610: result = true;
611:
612: if (getFormConfiguration().hasCaptchaField()
613: && getFormConfiguration()
614: .captchaFieldIsOnCheckPage()) {
615: // if there is a captcha field and a check page configured, we do have to remove the already
616: // initialized captcha field from the form again. the captcha field gets initialized together with
617: // the form, in this moment it is not clear yet whether we have validation errors or and need to
618: // to go back to the input form...
619: getFormConfiguration().removeCaptchaField();
620: }
621: } else if (ACTION_CONFIRMED
622: .equalsIgnoreCase(getParameter(PARAM_FORMACTION))
623: && getFormConfiguration().captchaFieldIsOnCheckPage()
624: && !validate()) {
625: // captcha field validation on check page failed- redisplay the check page, not the input page!
626: result = false;
627: }
628:
629: return result;
630: }
631:
632: /**
633: * Validation method that checks the given input fields.<p>
634: *
635: * All errors are stored in the member m_errors Map, with the input field name as key
636: * and the error message String as value.<p>
637: *
638: * @return true if all neccessary fields can be validated, otherwise false
639: */
640: public boolean validate() {
641:
642: if (m_isValidatedCorrect != null) {
643: return m_isValidatedCorrect.booleanValue();
644: }
645:
646: boolean allOk = true;
647: // iterate the form fields
648: List fields = getFormConfiguration().getFields();
649:
650: // validate each form field
651: for (int i = 0, n = fields.size(); i < n; i++) {
652:
653: I_CmsField currentField = (I_CmsField) fields.get(i);
654:
655: if (currentField == null) {
656: continue;
657: }
658:
659: if (CmsCaptchaField.class.isAssignableFrom(currentField
660: .getClass())) {
661: // the captcha field doesn't get validated here...
662: continue;
663: }
664:
665: String validationError = currentField.validate(this );
666: if (CmsStringUtil.isNotEmpty(validationError)) {
667: getErrors()
668: .put(currentField.getName(), validationError);
669: allOk = false;
670: }
671: }
672:
673: CmsCaptchaField captchaField = m_formConfiguration
674: .getCaptchaField();
675: if (captchaField != null) {
676:
677: boolean captchaFieldIsOnInputPage = getFormConfiguration()
678: .captchaFieldIsOnInputPage()
679: && getFormConfiguration().isInputFormSubmitted();
680: boolean captchaFieldIsOnCheckPage = getFormConfiguration()
681: .captchaFieldIsOnCheckPage()
682: && getFormConfiguration().isCheckPageSubmitted();
683:
684: if (captchaFieldIsOnInputPage || captchaFieldIsOnCheckPage) {
685: if (!captchaField.validateCaptchaPhrase(this ,
686: captchaField.getValue())) {
687: getErrors().put(captchaField.getName(),
688: ERROR_VALIDATION);
689: allOk = false;
690: }
691: }
692: }
693:
694: m_isValidatedCorrect = Boolean.valueOf(allOk);
695: return allOk;
696: }
697:
698: /**
699: * Creates a list of internet addresses (email) from a semicolon separated String.<p>
700: *
701: * @param mailAddresses a semicolon separated String with email addresses
702: * @return list of internet addresses (email)
703: * @throws AddressException if an email address is not correct
704: */
705: protected List createInternetAddresses(String mailAddresses)
706: throws AddressException {
707:
708: if (CmsStringUtil.isNotEmpty(mailAddresses)) {
709: // at least one email address is present, generate list
710: StringTokenizer T = new StringTokenizer(mailAddresses, ";");
711: List addresses = new ArrayList(T.countTokens());
712: while (T.hasMoreTokens()) {
713: InternetAddress address = new InternetAddress(T
714: .nextToken());
715: addresses.add(address);
716: }
717: return addresses;
718: } else {
719: // no address given, return empty list
720: return Collections.EMPTY_LIST;
721: }
722: }
723:
724: /**
725: * Creates the output String of the submitted fields for email creation.<p>
726: *
727: * @param isHtmlMail if true, the output is formatted as HTML, otherwise as plain text
728: * @param isConfirmationMail if true, the text for the confirmation mail is created, otherwise the text for mail receiver
729: * @return the output String of the submitted fields for email creation
730: */
731: protected String createMailTextFromFields(boolean isHtmlMail,
732: boolean isConfirmationMail) {
733:
734: List fieldValues = getFormConfiguration().getFields();
735: StringBuffer result = new StringBuffer(fieldValues.size() * 8);
736: if (isHtmlMail) {
737: // create html head with style definitions and body
738: result.append("<html><head>\n");
739: result.append("<style type=\"text/css\"><!--\n");
740: String style = getMessages().key("form.email.style.body");
741: if (CmsStringUtil.isNotEmpty(style)) {
742: result.append("body,h1,p,td { ");
743: result.append(style);
744: result.append(" }\n");
745: }
746: style = getMessages().key("form.email.style.h1");
747: if (CmsStringUtil.isNotEmpty(style)) {
748: result.append("h1 { ");
749: result.append(style);
750: result.append(" }\n");
751: }
752: style = getMessages().key("form.email.style.p");
753: if (CmsStringUtil.isNotEmpty(style)) {
754: result.append("p { ");
755: result.append(style);
756: result.append(" }\n");
757: }
758: style = getMessages().key("form.email.style.fields");
759: if (CmsStringUtil.isNotEmpty(style)) {
760: result.append("table.fields { ");
761: result.append(style);
762: result.append(" }\n");
763: }
764: style = getMessages().key("form.email.style.fieldlabel");
765: if (CmsStringUtil.isNotEmpty(style)) {
766: result.append("td.fieldlabel { ");
767: result.append(style);
768: result.append(" }\n");
769: }
770: style = getMessages().key("form.email.style.fieldvalue");
771: if (CmsStringUtil.isNotEmpty(style)) {
772: result.append("td.fieldvalue { ");
773: result.append(style);
774: result.append(" }\n");
775: }
776: style = getMessages().key("form.email.style.misc");
777: if (CmsStringUtil.isNotEmpty(style)) {
778: result.append(getMessages()
779: .key("form.email.style.misc"));
780: }
781: result.append("//--></style>\n");
782: result.append("</head><body>\n");
783: if (isConfirmationMail) {
784: // append the confirmation mail text
785: result.append(getFormConfiguration()
786: .getConfirmationMailText());
787: } else {
788: // append the email text
789: result.append(getFormConfiguration().getMailText());
790: }
791: result.append("<table border=\"0\" class=\"fields\">\n");
792: } else {
793: // generate simple text mail
794: if (isConfirmationMail) {
795: // append the confirmation mail text
796: result.append(getFormConfiguration()
797: .getConfirmationMailTextPlain());
798: } else {
799: // append the email text
800: result
801: .append(getFormConfiguration()
802: .getMailTextPlain());
803: }
804: result.append("\n\n");
805: }
806: // generate output for submitted form fields
807: Iterator i = fieldValues.iterator();
808: while (i.hasNext()) {
809: I_CmsField current = (I_CmsField) i.next();
810: // dont show the letter of agreement (CmsPrivacyField)
811: if (isHtmlMail && !(current instanceof CmsPrivacyField)) {
812: // format output as HTML
813: result.append("<tr><td class=\"fieldlabel\">");
814: result.append(current.getLabel());
815: result.append("</td><td class=\"fieldvalue\">");
816: result.append(convertToHtmlValue(current.toString()));
817: result.append("</td></tr>\n");
818: } else if (!(current instanceof CmsPrivacyField)) {
819: // format output as plain text
820: result.append(current.getLabel());
821: result.append("\t");
822: result.append(current.toString());
823: result.append("\n");
824: }
825: }
826: if (isHtmlMail) {
827: // create html table closing tag
828: result.append("</table>\n");
829: if (!isConfirmationMail
830: && getFormConfiguration().hasConfigurationErrors()) {
831: // write form configuration errors to html mail
832: result.append("<h1>");
833: result.append(getMessages().key(
834: "form.configuration.error.headline"));
835: result.append("</h1>\n<p>");
836: for (int k = 0; k < getFormConfiguration()
837: .getConfigurationErrors().size(); k++) {
838: result.append(getFormConfiguration()
839: .getConfigurationErrors().get(k));
840: result.append("<br>");
841: }
842: result.append("</p>\n");
843: }
844: // create body and html closing tags
845: result.append("</body></html>");
846: } else if (!isConfirmationMail
847: && getFormConfiguration().hasConfigurationErrors()) {
848: // write form configuration errors to text mail
849: result.append("\n");
850: result.append(getMessages().key(
851: "form.configuration.error.headline"));
852: result.append("\n");
853: for (int k = 0; k < getFormConfiguration()
854: .getConfigurationErrors().size(); k++) {
855: result.append(getFormConfiguration()
856: .getConfigurationErrors().get(k));
857: result.append("\n");
858: }
859: }
860:
861: return result.toString();
862: }
863:
864: /**
865: * Sets the errors found when validating the form.<p>
866: *
867: * @param errors the errors found when validating the form
868: */
869: protected void setErrors(Map errors) {
870:
871: m_errors = errors;
872: }
873:
874: /**
875: * Sets the form configuration.<p>
876: *
877: * @param configuration the form configuration
878: */
879: protected void setFormConfiguration(CmsForm configuration) {
880:
881: m_formConfiguration = configuration;
882: }
883:
884: /**
885: * Sets if the form is displayed for the first time.<p>
886: * @param initial true if the form is displayed for the first time, otherwise false
887: */
888: protected void setInitial(boolean initial) {
889:
890: m_initial = initial;
891: }
892:
893: /**
894: * Sets the localized messages.<p>
895: *
896: * @param messages the localized messages
897: */
898: protected void setMessages(CmsMessages messages) {
899:
900: m_messages = messages;
901: }
902:
903: }
|