Package org.olat.modules.co

Source Code of org.olat.modules.co.ContactForm

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.modules.co;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.CheckBoxElement;
import org.olat.core.gui.formelements.StaticTextElement;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.formelements.TextElement;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
import org.olat.core.id.UserConstants;
import org.olat.core.util.mail.ContactList;

/**
* highly configurable contact form. Depending on each field value the
* corresponding fields become editable or not.
* <p>
* By creation time the defaults are: a FROM: field containing the logged in
* users e-mail, an empty TO: field not editable, empty SUBJECT: and BODY:
* fields both editable, and last but not least a submit cancel buttons pair.
* <P>
* <ul>
* Send-Limitations:
* <li>maxLength for 'body' TextAreaElement -> 10000 characters, about 4 pages
* </li>>
* <li>maxLength for 'to' TextAreaElement -> 30000 characters, enough space for
* a lot of mail group names, each mail group containing umlimited ammount of
* e-mail addresses</li>
* </ul>
*
* Initial Date: Jul 19, 2004
* @author patrick
*/

class ContactForm extends Form {
  //
  private final static String NLS_CONTACT_TO = "contact.to";
  private TextElement tto = null;
  private TextAreaElement ttoBig = null;
  private final static String NLS_CONTACT_FROM = "contact.from";
  private TextElement tfrom;
  private final static String NLS_CONTACT_SUBJECT = "contact.subject";
  private TextElement tsubject;
  private final static String NLS_CONTACT_BODY = "contact.body";
  private TextAreaElement tbody;
  private boolean recipientsAreEditable = false;
  private final static int emailCols = 60;
  private Hashtable contactLists = new Hashtable();
  private boolean readOnly=false;
  private boolean hasMsgCancel=false;
  private boolean hasMsgSave = false;
  private final static String NLS_CONTACT_SEND_CP_FROM = "contact.cp.from";
  private CheckBoxElement tcpfrom;

  public ContactForm(String name, Translator translator, Identity emailFrom, boolean readOnly, boolean isCanceable, boolean hasRecipientsEditable) {
    super(name, translator);
    this.readOnly = readOnly;
    this.recipientsAreEditable = hasRecipientsEditable;
    setRecipientsEditableTo(recipientsAreEditable);
    setEmailFrom(emailFrom);
    addFormElement("tto", new StaticTextElement(NLS_CONTACT_TO, " "));
    addFormElement("tcpfrom", new CheckBoxElement(NLS_CONTACT_SEND_CP_FROM, true));
    setSubject(null);
    setBody(null);
   
    addSubmitKey("msg.save");
    if (isCanceable) {
      setCancelKey("msg.cancel");
    }
   
  }

  /**
   * @param emailFromIdentity
   */
  private void setEmailFrom(final Identity emailFromIdentity) {
    //fetch sender if one, otherwise field is editable
    String defaultEmailFrom = ((emailFromIdentity != null) ? (emailFromIdentity.getUser().getProperty(UserConstants.EMAIL, getLocale())) : (null));
    if (defaultEmailFrom != null || readOnly) {
      tfrom = new StaticTextElement(NLS_CONTACT_FROM, defaultEmailFrom);
    } else {
      tfrom = new TextElement(NLS_CONTACT_FROM, 255);
      tfrom.setMandatory(true);
    }
    addFormElement("tfrom", tfrom);
  }

  /**
   * @param defaultSubject
   */
  public void setSubject(final String defaultSubject) {
    if(readOnly){
      tsubject = new StaticTextElement(NLS_CONTACT_SUBJECT, defaultSubject);
    }else{
      tsubject = new TextElement(NLS_CONTACT_SUBJECT, defaultSubject, 255);
      tsubject.setMandatory(true);
    }
    addFormElement("tsubject", tsubject);
  }

  /**
   * add a ContactList as EmailTo:
   *
   * @param emailList
   */
  public void addEmailTo(ContactList emailList) {
    if (contactLists.containsKey(emailList.getName())) {
      //there is already a ContactList with this name...
      ContactList existing = (ContactList) contactLists.get(emailList.getName());
      //, merge their values.
      existing.add(emailList);
      //the form itself must not be updated, because it already displays
      // the name.
    } else {
      //a new ContactList, put it into contactLists
      contactLists.put(emailList.getName(), emailList);
      //and add its name in the form
      addContactFormEmailTo("<" + emailList.getName() + ">");
    }
  }

  /**
   * @param defaultEmailTo
   */
  private void addContactFormEmailTo(String defaultEmailTo) {
    //        tto = null;
    //        ttoBig = null;
    boolean formFieldAlreadyAdded = (tto != null) || (ttoBig != null);
    if (formFieldAlreadyAdded) {
      String tmp = "";
      if (tto != null) tmp = tto.getValue();
      if (ttoBig != null) tmp = ttoBig.getValue();
      defaultEmailTo += tmp;
    }
    //
    if (!recipientsAreEditable) {
      //recipients field read only
      if (defaultEmailTo.length() > (3 * emailCols)) {
        defaultEmailTo = defaultEmailTo.substring(0, 3 * emailCols - 1);
        defaultEmailTo += "\n. . . ";
      }
      if (formFieldAlreadyAdded) tto.setValue(defaultEmailTo);
      else tto = new StaticTextElement(NLS_CONTACT_TO, defaultEmailTo);
    } else {
      //recipients field editable
      if (!formFieldAlreadyAdded) ttoBig = new TextAreaElement(NLS_CONTACT_TO, 2, emailCols);
      //recipients field filled with addresses and even editable
      ttoBig.setValue(defaultEmailTo);
      ttoBig.setMandatory(true);
    }
    if (!formFieldAlreadyAdded) {
      if (tto != null) {
        addFormElement("tto", tto);
      } else if (ttoBig != null) {
        addFormElement("tto", ttoBig);
      }
    }
  }

  /**
   * @param editable
   */
  private void setRecipientsEditableTo(boolean editable) {
    /*
     * ON INIT: recipientsAreEditable == false; if addEmailTo(...) is called
     * before setRecipientsEditableTo(...) tto != null && ttoBig == null if
     * addEmailTo(...) is called after setRecipientsEditableTo(TRUE) tto == null &&
     * ttoBig != null setRecipientsEditableTo(..) must ensure that: - either tto ==
     * null or ttoBig == null - switching from tto <-> ttoBig removesFormElement
     */
    if (recipientsAreEditable != editable) {
      //a change
      recipientsAreEditable = editable;
      if (ttoBig == null && tto == null) {
        //no recipients added yet, nothing to do, immediate return
        return;
      }
      //POSTCOND: either tto or ttoBig is != null
      //recipientsAreEditable == false && editable == true:
      //-> copy tto -> ttoBig, set tto = null, remove tto, add ttoBig
      if (editable) {
        ttoBig = new TextAreaElement(NLS_CONTACT_TO, 2, emailCols);
        ttoBig.setValue(tto.getValue());
        tto = null;
        //removeFormElement(tto);
        addFormElement("tto", ttoBig);
      } else {
        tto = new StaticTextElement(NLS_CONTACT_TO, ttoBig.getValue());
        ttoBig = null;
        //removeFormElement(ttoBig);
        addFormElement("tto", tto);
      }
    }
  }

  /**
   * @param defaultBody
   */
  public void setBody(String defaultBody) {
    tbody = new TextAreaElement(NLS_CONTACT_BODY, 10, 60, defaultBody);
    tbody.setReadOnly(readOnly);
    if(!readOnly){
      tbody.setMandatory(true);
    }
    addFormElement("tbody", tbody);
  }

  /**
   * @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
   */
  public boolean validate() {
    if(readOnly){
      return true;
    }
    boolean subjectOk = tsubject.notEmpty("error.field.not.empty");
    boolean bodyOk = tbody.notEmpty("error.field.not.empty");
    // the body message may not be longer than about 4 pages or 10000
    // characters
    bodyOk = bodyOk && tbody.notLongerThan(10000, "input.toolong");
    boolean toOk = false;
    if (tto != null) {
      toOk = tto.notEmpty("error.field.not.empty");
    } else {
      toOk = ttoBig.notEmpty("error.field.not.empty");
      // limit of recipients about 700 (1 emailaddress medial 40
      // characters)
      toOk = toOk && ttoBig.notLongerThan(30000, "input.toolong");
    }
    boolean fromOk = tfrom.notEmpty("error.field.not.empty");
    return subjectOk && bodyOk && toOk && fromOk;
  }

  /**
   * @return
   */
  public String getEmailFrom() {
    return tfrom.getValue();
  }

  /**
   * a List with ContactLists as elements is returned
   *
   * @return
   */
  public List getEmailToContactLists() {
    ArrayList retVal = new ArrayList();
    Enumeration enumeration = this.contactLists.elements();
    while (enumeration.hasMoreElements()) {
      retVal.add(enumeration.nextElement());
    }
    return retVal;
  }

  /**
   * retrieve the contact list names from the to field, and map them back to the
   * stored contact lists names.
   *
   * @return
   */
  public String getEmailTo() {
    String retVal = "";
    String value;
    if (tto != null) value = tto.getValue();
    else value = ttoBig.getValue();

    String sep = "";
    int i = 0;
    int j = -1;
    i = value.indexOf("<", j + 1);
    j = value.indexOf(">", j + 2);
    while (i > -1 && j > 0) {
      String contactListName = value.substring(i + 1, j);
      i = value.indexOf("<", j + 1);
      j = value.indexOf(">", j + 2);
      if (contactLists.containsKey(contactListName)) {
        ContactList found = (ContactList) contactLists.get(contactListName);
        retVal += sep + found.toString();
        sep = ", ";
      }
    }
    return retVal;
  }

  /**
   * @return
   */
  public String getSubject() {
    return tsubject.getValue();
  }

  /**
   * @return email body text
    */
   public String getBody() {
       return tbody.getValue();
  }
  
}
TOP

Related Classes of org.olat.modules.co.ContactForm

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.