📄 emaillist.jsp
字号:
<%@ include file="/includes/header.jsp" %>
<%@ page import="com.sourcetap.sfa.ui.*" %>
<%@ page import="com.sourcetap.sfa.util.*" %>
<%@ page import="com.sourcetap.sfa.replication.GenericReplicator" %>
<%@ page import="org.ofbiz.service.*" %>
<%@ page import="org.ofbiz.entity.*" %>
<%@ page import="org.ofbiz.entity.condition.*" %>
<%@ page import="org.ofbiz.base.util.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="org.ofbiz.content.webapp.ftl.*" %>
<LINK rel="stylesheet" type="text/css" href="/sfa/css/report.css">
<jsp:useBean id="dispatcher" type="org.ofbiz.service.LocalDispatcher" scope="application" />
<CENTER>
<%
String action = request.getParameter("action");
String templateId = UtilFormatOut.checkNull(request.getParameter("templateId"));
String accountId = UtilFormatOut.checkNull(request.getParameter("accountId"));
String emailType= UtilFormatOut.checkNull(request.getParameter("emailType"));
String templateName = UtilFormatOut.checkNull(request.getParameter("templateName"));
String fromAddress = UtilFormatOut.checkNull(request.getParameter("fromAddress"));
String toAddress = UtilFormatOut.checkNull(request.getParameter("toAddress"));
String ccAddress = UtilFormatOut.checkNull(request.getParameter("ccAddress"));
String bccAddress = UtilFormatOut.checkNull(request.getParameter("bccAddress"));
String subject = UtilFormatOut.checkNull(request.getParameter("subject"));
String contentType = UtilFormatOut.checkNull(request.getParameter("contentType"));
String body = UtilFormatOut.checkNull(request.getParameter("body"));
String listId = UtilFormatOut.checkNull(request.getParameter("listId"));
try
{
if ( ( action == null ) || ( action.length() == 0) )
action = "start";
if ( action.equals("saveTemplate") )
{
GenericValue gv = new GenericValue(delegator.getModelEntity("EmailTemplate"));
gv.setDelegator(delegator);
if ( templateId.length() < 1 )
templateId = GenericReplicator.getNextSeqId("EmailTemplate", delegator);
if ( accountId.length() < 1 )
accountId = userInfo.getAccountId();
if ( emailType.length() < 1)
emailType = "CONTACT_EMAIL";
gv.set("templateId", templateId);
gv.set("accountId", accountId);
gv.set("emailType", emailType);
gv.set("templateName", templateName);
gv.set("fromAddress", fromAddress);
gv.set("toAddress", toAddress);
gv.set("ccAddress", ccAddress);
gv.set("bccAddress", bccAddress);
gv.set("subject", subject);
gv.set("contentType", contentType);
gv.set("body", body);
delegator.storeAll(UtilMisc.toList(gv));
action = "start";
}
if ( action.equals("deleteTemplate" ))
{
delegator.removeByAnd("EmailTemplate", UtilMisc.toMap("templateId", templateId));
action = "start";
}
if ( action.equals("start") )
{
List templates = delegator.findByAnd( "EmailTemplate", UtilMisc.toMap("accountId", userInfo.getAccountId()), UtilMisc.toList("templateName"));
List contactLists = delegator.findByCondition( "UiList",
new EntityConditionList( UtilMisc.toList( new EntityExpr("partyId", EntityOperator.EQUALS, userInfo.getPartyId()),
new EntityExpr("partyId", EntityOperator.EQUALS, userInfo.getAccountId())), EntityOperator.OR ), null, UtilMisc.toList("listName") );
if ( contactLists.size() < 1 )
{
%>
<br><br>Before You can use the mass email option, you need to create a list of people to contact.
<BR>This can be done from the Contact or Lead Query screens by setting the Save List As Option.
<%
}
else if ( templates.size() < 1 )
{
%>
<br><br>Before You can use the mass email option, you need to create an email template to be used.
<br><a href=/sfa/control/emailList?action=newTemplate>Click Here</a> to create a new template.
<%
}
else
{
%>
<FORM action="/sfa/control/emailList" method=post>
<input type="hidden" name=action value=getList>
<BR>Choose a Template, or <a href=emailList?action=newTemplate>click here</a> to create a new template
<BR>
<TABLE class=its>
<THEAD><TR><TD>Template Name</TD><TD>Action</TD></TR></THEAD><TBODY>
<%
Iterator templateI = templates.iterator();
int i = 0;
while ( templateI.hasNext() )
{
i++;
String rowClass = (( i % 2 ) == 0 ) ? "even" : "odd";
String checked = ( i==1) ? " checked " : "";
GenericValue templateGV = (GenericValue) templateI.next();
out.write("<TR class=" + rowClass + "><TD><INPUT TYPE=RADIO NAME=templateId "+ checked + " VALUE='" + templateGV.getString("templateId") + "'>" + templateGV.getString("templateName") + "</TD>");
out.write("<TD><a href=emailList?action=editTemplate&templateId=" + templateGV.getString("templateId") + ">Edit?</a> " +
"<a href=emailList?action=newTemplate&templateId=" + templateGV.getString("templateId") + ">Duplicate?</a> " +
"<a href=emailList?action=deleteTemplate&templateId=" + templateGV.getString("templateId") + ">Delete?</a>" +
"</TD></TR>\n");
}
%>
</TBODY>
</TABLE>
<BR>
<TABLE class=its>
<TR class=odd><TD>Choose a List to Email To</TD><TD><%=buildDropDown(contactLists, UtilMisc.toMap("NAME", "listId", "VALUE_FIELD", "listId", "DISPLAY_FIELD", "listName", "SELECT", listId))%></TD></TR>
<TR class=even><TD>Override Send To Address (you can enter an email address for testing purposes)</TD><TD><input name=overrideAddress size=20></TD></TR>
</TABLE>
<input type=submit value=Send name="Send Emails">
</FORM>
<%
}
}
else if ( action.equals("newTemplate") || action.equals("editTemplate") )
{
if ( templateId.length() > 0 )
{
GenericValue templateGV = delegator.findByPrimaryKey("EmailTemplate", UtilMisc.toMap("templateId", templateId));
if ( templateGV == null )
throw new IllegalArgumentException("Unable to load template");
emailType = templateGV.getString("emailType");
templateName = templateGV.getString("templateName");
fromAddress = templateGV.getString("fromAddress");
toAddress = templateGV.getString("toAddress");
ccAddress = templateGV.getString("ccAddress");
bccAddress = templateGV.getString("bccAddress");
subject = templateGV.getString("subject");
contentType = templateGV.getString("contentType");
body = templateGV.getString("body");
}
if ( action.equals("newTemplate") )
templateId = "";
List contactFields = delegator.findByAnd("UiScreenSectionFields", UtilMisc.toMap("sectionId", "8"), UtilMisc.toList("displayOrder"));
List leadFields = delegator.findByAnd("UiScreenSectionFields", UtilMisc.toMap("sectionId", "10010.10000"), UtilMisc.toList("displayOrder"));
%>
<form name=form1 action="/sfa/control/emailList" method=post>
<input type=hidden name=action value=saveTemplate>
<input type=hidden name=templateId value=<%=templateId%>>
<input type=hidden name=accountId value=<%=accountId%>>
<TABLE style="width='100%';border-width=0;border-style=none;margin=0;">
<TR><TD border=none style="width='70%';border-right=1;border-right-style=solid;">
<TABLE style="width='100%';border-width=0;border-style=none;margin=0;">
<TR><TD>Template Name:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=templateName value='<%=templateName%>'></TD></TR>
<TR><TD>From Address:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=fromAddress value='<%=fromAddress%>'></TD></TR>
<TR><TD>To Address:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=toAddress value='<%=toAddress%>'></TD></TR>
<TR><TD>CC Address:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=ccAddress value='<%=ccAddress%>'></TD></TR>
<TR><TD>BCC Address:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=bccAddress value='<%=bccAddress%>'></TD></TR>
<TR><TD>Subject:</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME=subject value='<%=subject%>'></TD></TR>
<TR><TD>Content Type:</TD><TD><%=buildStringDropDown(UtilMisc.toList("text/plain", "text/html"), UtilMisc.toMap("NAME", "contentType", "SELECTED", contentType))%></TD></TR>
<TR><TD>Body:</TD><TD><TEXTAREA ROWS=20 COLS=80 name=body><%=body%></TEXTAREA></TD></TR>
<TR><TD colspan=2><center><input type=submit value=save name=save></center></TD></TR>
</TABLE>
</TD><TD width='30%'>
<TABLE style="width='100%';border-width=0;border-style=none;margin=0;">
<TR><TD COLSPAN=2>The email template can use fields that will be filled in from data at execution time. To see the text required to insert a
field into the template, choose the Template type, then click on a field in the Available fields list. The code that needs to
be inserted into the list will be shown in Field Value, and you can then cut and paste it into your template. </TD></TR>
<SCRIPT>
function switchOptions (select, listType) {
select.options.length = 0;
if ( listType == "Contact" )
{
<%
Iterator fldIter = contactFields.iterator();
while ( fldIter.hasNext() )
{
GenericValue field = (GenericValue) fldIter.next();
String fieldName = field.getString("attributeName");
String displayLabel = field.getString("displayLabel");
%>
var opt = new Option('<%=displayLabel%>', '<%=fieldName%>');
select.options[select.options.length] = opt;
<%
}
%>
}
else
{
<%
Iterator fldIter2 = leadFields.iterator();
while ( fldIter2.hasNext() )
{
GenericValue field = (GenericValue) fldIter2.next();
String fieldName = field.getString("attributeName");
String displayLabel = field.getString("displayLabel");
%>
var opt = new Option('<%=displayLabel%>', '<%=fieldName%>');
select.options[select.options.length] = opt;
<%
}
%>
}
}
</SCRIPT>
<TR><TD>Template Type:</TD><TD><SELECT NAME=templateType onchange="this.form.fieldValue.value='';switchOptions(this.form.fieldList, this.options[this.selectedIndex].value)"><OPTION VALUE=Contact SELECTED>Contact</OPTION><OPTION VALUE=Lead>Lead</OPTION></SELECT></TD></TR>
<TR><TD>Field Value:</TD><TD><INPUT NAME=fieldValue size=30></TD></TR>
<TR><TD>Available Fields</TD><TD><SELECT NAME=fieldList size=20 onchange="this.form.fieldValue.value='${' + this.form.templateType.value + '.' + this.options[this.selectedIndex].value + '}'">
<%
Iterator iter = contactFields.iterator();
while ( iter.hasNext() )
{
GenericValue field = (GenericValue) iter.next();
String fieldName = field.getString("attributeName");
String displayLabel = field.getString("displayLabel");
%>
<OPTION value=<%=fieldName%>><%=displayLabel%></OPTION>
<%
}
%>
</SELECT></TD></TR>
</TABLE>
</TD></TR>
</TABLE>
</FORM>
<%
}
else if ( action.equals("getList") )
{
UIList uiList = new UIList( listId, delegator );
GenericValue templateGV = delegator.findByPrimaryKey("EmailTemplate", UtilMisc.toMap("templateId", templateId));
if ( templateGV == null )
throw new IllegalArgumentException("Unable to load template");
emailType = templateGV.getString("emailType");
templateName = templateGV.getString("templateName");
fromAddress = templateGV.getString("fromAddress");
toAddress = templateGV.getString("toAddress");
ccAddress = templateGV.getString("ccAddress");
bccAddress = templateGV.getString("bccAddress");
subject = templateGV.getString("subject");
contentType = templateGV.getString("contentType");
body = templateGV.getString("body");
uiList.loadQueryInfo( request, response, uiCache );
String primaryEntityName = uiList.getPrimaryEntityName();
List results = uiList.getListItems( userInfo, 5000);
Iterator iter = results.iterator();
int numEmails = 0;
int numErrors = 0;
int numSkipped = 0;
String overrideAddress = UtilFormatOut.checkNull(request.getParameter("overrideAddress"));
if ( overrideAddress.length() > 0 )
{
toAddress = overrideAddress;
ccAddress = "";
bccAddress = "";
}
while ( iter.hasNext() )
{
Map emailCtx = new HashMap();
GenericValue contactGV = (GenericValue) iter.next();
Map templateContext = UtilMisc.toMap(primaryEntityName, contactGV);
/*
String templateStr = "<html><body>This is a test email. Hello ${Contact.firstName} ${Contact.lastName} in ${Contact.Address_state} ${foo1}</body> </html> ";
String subjectStr = "Test Message to ${Contact.firstName} ${Contact.lastName}";
StringReader templateReader = new StringReader(templateStr);
// process the template with the given data and write
// the email body to the String buffer
Writer writer = new StringWriter();
FreeMarkerWorker.renderTemplate("emailTemplate", templateReader, templateContext, writer);
// extract the newly created body for the notification email
String notificationBody = writer.toString();
*/
String sendTo = FlexibleStringExpander.expandString(toAddress, templateContext);
if ( ( sendTo == null ) || (sendTo.length() < 1))
numSkipped++;
else
{
emailCtx.put("body", FlexibleStringExpander.expandString(body, templateContext));
emailCtx.put("templateName", "");
emailCtx.put("templateData", templateContext);
emailCtx.put("sendTo", FlexibleStringExpander.expandString(toAddress, templateContext));
emailCtx.put("sendCc", ccAddress);
emailCtx.put("sendBcc", bccAddress);
emailCtx.put("contentType", contentType);
emailCtx.put("sendFrom", fromAddress);
emailCtx.put("subject", FlexibleStringExpander.expandString(subject, templateContext));
// send off the email async so we will retry on failed attempts
try {
dispatcher.runSync("sendGenericNotificationEmail", emailCtx);
numEmails++;
//out.write("email sent");
} catch (GenericServiceException e) {
Debug.logError(e, "Problem sending mail", "emailList");
numErrors++;
//out.write("Error sending email");
}
}
}
%>
<BR>
<TABLE class=its>
<THEAD><TR><TD>Result</TD><TD>Count</TD></TR></THEAD><TBODY>
<TR class=odd><TD>Emails Sent Successfully</TD><TD><%=numEmails%></TD></TR>
<TR class=even><TD>Emails Skipped because no valid to Address</TD><TD><%=numSkipped%></TD></TR>
<TR class=odd><TD>Emails Not Sent due to Error</TD><TD><%=numErrors%></TD></TR>
</TBODY>
</TABLE>
<BR>
<a href=emailList>click here</a> to send another email.
<BR>
<%
}
} catch (GenericEntityException e)
{
e.printStackTrace();
Debug.logError("error saving report" + e.getMessage(), "emailList");
out.write("There was an unexpected Error. Please try again: " + e.getMessage());
}
%>
</CENTER>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -