<%
/******************************************************************************
 * Copyright (c) 2000-2004 e-Deal
 *
 * e-Deal S.A.
 * 55 rue Brillat Savarin
 * 75013 Paris
 * France
 *
 * T: +33 (0)1 53 80 80 30
 * F: +33 (0)1 73 01 69 77
 * http://www.e-deal.com
 * 
 * La diffusion de ce code source sous quelque forme que ce soit sans
 * l'autorisation de E-DEAL est interdite.
 *
 * Vous êtes autorisés à modifier ce code source uniquement pour votre usage
 * propre et sous réserve que les mentions de copyright demeurent intactes.
 *
 * Ce code est fourni en l'état. Aucune garantie d'aucune sorte, explicite ou
 * implicite n'est donnée. En aucun cas E-DEAL ne pourra être tenu pour
 * responsable des dommages pouvant résulter de l'utilisation de ce code
 * source.
 *
 *
 ******************************************************************************/
%>
<%@page session="true" import="com.edeal.frontline.*, com.edeal.frontline.navigation.*,java.util.*"%>
<jsp:useBean class="com.edeal.frontline.MessagesBean" id="Messages" scope="request"/>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<%
	final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory.getLog(getClass());
	FlRequest flRequest  = new FlRequest(request);
	FlContext context = flRequest.getContext();
	DataDictionary dico = context.getDataDictionary();
	String solObjID = null;
	try {
		ObjectsBean solObj = dico.getTable("sql", "Solicitation");
		solObjID = solObj.getID();
	} catch (FrontlineException e) {
	}

	String[] fields = {"MsgID", "MsgSubject", "MsgCtyID:RefVal", "MsgCode"};
	String[][] query = {{"MsgCode like " + Utils.formatToSQL(context, "SOL%")}};
	String[] orderBy = {"MsgCode"};
	Vector vectMsg = MessagesBean.listSummary(context, fields, query, orderBy);
	Hashtable hashMsg = new Hashtable();
	if(vectMsg != null && vectMsg.size() > 0) {
		for(int i = 0; i < vectMsg.size(); i++) {
			Hashtable row = (Hashtable)vectMsg.elementAt(i);
			String msgCode = (String)row.get("MsgCode");
			String msgCtyVal = (String)row.get("MsgCtyID:RefVal");
	
			if(msgCode != null && msgCtyVal != null) {
				if("TEXT/PLAIN".equalsIgnoreCase(msgCtyVal)) {
					hashMsg.put(msgCode + "TEXT/PLAIN", row);
				} else {
					hashMsg.put(msgCode + "TEXT/HTML", row);
				}
			}				
		}
	}

	String[][] msgType = {{"SOL1", Utils.getMessage(session, "L62D9", "Notification acteur"), Utils.getMessage(session, "L0052", "Acteur texte"), Utils.getMessage(session, "L0053", "Acteur html"),"Acteur texte","Acteur html"},
		{"SOL2", Utils.getMessage(session, "L62DA", "Notification solliciteur"), Utils.getMessage(session, "F6135", "Solliciteur texte"), Utils.getMessage(session, "F6137", "Solliciteur html"),"Solliciteur texte","Solliciteur html"}};
%>




<script type="text/javascript" language="javascript">
function showMsg(){
	document.getElementById('msgBody').style.display = 'block';
}

function doEdit(code, cty, id) {
	
	<%--window.location = '<fl:webapp/>edit_messages.fl?1=1' + (id ? '&id=' + id : '') + '&code=' + code + '&cty=' + encodeURIComponent(cty) + '&object=<%= Utils.formatToJavascript(solObjID) %>';--%>
	if (!id || id==''||id==null){
		window.location = '<%=flRequest.computeURL("/create_notificationMsg.fl?1=1")%>'+'&code='+code+'&cty='+cty+'&msgBody=true';
	}else{
		window.location = '<%=flRequest.computeURL("/create_notificationMsg.fl?1=1")%>'+'&id='+id+'&msgBody=true';
	}
}

function doDelete(id) {
	var str = '<fl:link url="/delete_object_ajax.fl?type=Messages&id='+id+'"/>';
	doAjax(str, refresh);
}

function refresh(arrIndex) {
	if (eDealXmlhttpArr[arrIndex].readyState == 4) {
		window.location = '<%=flRequest.computeURL("/create_notification.fl")%>';
	}
}

function doImport(pCode) {
	window.open('<fl:webapp/>/list_messages_for_import.fl?1=1' + '&next=<%=flRequest.computeURL("/create_notification.fl")%>&object=<%= Utils.formatToJavascript(solObjID) %>&code=' + encodeURIComponent(pCode), 'mail_import', 'width=500,height=400,scrollbars,resizable');
}

function doSubmit() {
	var f = document.forms[0];	
	var str	= '<scr' + 'ipt language="javascript">window.location="<fl:link url="/create_notification.fl"/>";</s' + 'cript>';
	f.$$returnedHtmlHead.value = str;
	f.submit();
}
</script>
<fl:bigTabs objectTitle="<%=Utils.getMessage(session, \"L62D8\", false, true, \"Notification\")%>">
<table class="container" cellpadding="0">
	<tr class="fieldline">
		<%for(int i = 0; i < msgType.length; i++) { 
			if(i==1){
		%>
		<td class="separation"><div class="separation"></div></td>
		<%
			}
		%>
		<td class="column2">
			<table class="container" cellpadding="0">
				<tr>			
					<%		
							Hashtable row = (Hashtable)hashMsg.get(msgType[i][0] + "TEXT/PLAIN");													
							if(row == null) {							
					%>
										<td class="dataLabel"><div><label title="<%= msgType[i][4] %>"><%= msgType[i][2] %></label></div></td>
										<td>&nbsp;</td>
										<td valign="middle">
											<table border="0" cellpadding="0" cellspacing="0">
												<tr>
													<td><a href="javascript:doEdit('<%= msgType[i][0] %>', 'TEXT/PLAIN');"><img src="<fl:webapp/>/icons/ico/zoom.gif" alt="<fl:getMessage code="A0008" alt="modifier"/>" border="0" width="15" height="15"></a></td>
													<td>&nbsp;</td>
													<td><img src="<fl:webapp/>/icons/ico/rechercher.gif" border="0" width="17" height="17"></td>
													<td>&nbsp;</td>
													<td><img src="<fl:webapp/>/icons/ico/supprimer.gif" border="0" width="17" height="17"></td>
												</tr>
											</table>
										</td>
					<%		} else { 					
					%>
										<td class="dataLabel"><div><label title="<%= msgType[i][4] %>"><%= msgType[i][2] %></label></div></td> 
										<%
										String subject ="";
									    subject = (String) row.get("MsgSubject");																		
										%>
										
										<td class="dataValue" id="<%=row.get("MsgID")+"_label"%>"><div title="<%= subject%>"><%= subject%></div></td>
										<td>
											<table cellpadding="0" cellspacing="0">
												<tr>												
													<td><a href="javascript:doEdit('<%= msgType[i][0] %>', 'TEXT/PLAIN', '<%= row.get("MsgID") %>');"><img src="<fl:webapp/>/icons/ico/zoom.gif" alt="<fl:getMessage code="A0008" alt="modifier"/>" border="0" width="15" height="15"></a></td>
													<td>&nbsp;</td>
													<td><img src="<fl:webapp/>/icons/ico/rechercher.gif" border="0" width="17" height="17"></td>
													<td>&nbsp;</td>
													<td><a href="javascript:doDelete('<%= row.get("MsgID") %>')"><img src="<fl:webapp/>/icons/ico/supprimer.gif" alt="<fl:getMessage code="A0007" alt="supprimer"/>" border="0" width="17" height="17"></a></td>
												</tr>
											</table>
										</td>
					<%		} %>					
				</tr>
				<tr valign="top">
					<%	
							row = (Hashtable)hashMsg.get(msgType[i][0] + "TEXT/HTML");							
							if(row == null) {													
					%>
										<td class="dataLabel"><div><label title="<%= msgType[i][5] %>"><%= msgType[i][3] %></label></div></td>
										<td width="80%">&nbsp;</td>
										<td valign="middle">
											<table border="0" cellpadding="0" cellspacing="0">
												<tr>
													<td><a href="javascript:doEdit('<%= msgType[i][0] %>', 'TEXT/HTML');"><img src="<fl:webapp/>/icons/ico/zoom.gif" alt="<fl:getMessage code="A0008" alt="modifier"/>" border="0" width="17" height="17"></a></td>
													<td>&nbsp;</td>																							
													<td><a href="javascript:doImport('<%= msgType[i][0] %>')"><img src="<fl:webapp/>/icons/ico/rechercher.gif" alt="<fl:getMessage code="A0015" alt="importer"/>" border="0" width="17" height="17"></td>
													<td>&nbsp;</td>
													<td><img src="<fl:webapp/>/icons/ico/supprimer.gif" border="0" width="17" height="17"></td>
												</tr>
											</table>
										</td>
					<%		} else { 					
					%>                   <%
										String subject ="";
									    subject = (String) row.get("MsgSubject");
										%>
										
										<td class="dataLabel"><div><label title="<%= msgType[i][5]%>"><%= msgType[i][3] %></label></div></td>
										<td class="dataValue" id="<%=row.get("MsgID")+"_label"%>"><div title="<%= subject%>"><%= subject%></div></td>
										<td style="width:10%">
											<table border="0" cellpadding="0" cellspacing="0">
												<tr>
													<td><% if("MIME".equalsIgnoreCase((String)row.get("MsgCtyID:RefVal"))) { %><img src="<fl:webapp/>/icons/ico/zoom.gif" border="0" width="17" height="17"><% } else { %><a href="javascript:doEdit('<%= msgType[i][0] %>', 'TEXT/HTML', '<%= row.get("MsgID") %>');"><img src="<fl:webapp/>/icons/ico/zoom.gif" alt="<fl:getMessage code="A0008" alt="modifier"/>" border="0" width="15" height="15"><% } %></a></td>
													<td>&nbsp;</td>
													<td><img src="<fl:webapp/>/icons/ico/rechercher.gif" border="0" width="17" height="17"></td>
													<td>&nbsp;</td>
													<td><a href="javascript:doDelete('<%= row.get("MsgID") %>')"><img src="<fl:webapp/>/icons/ico/supprimer.gif" alt="<fl:getMessage code="A0007" alt="supprimer"/>" border="0" width="17" height="17"></a></td>
												</tr>
											</table>
										</td>
					<%						
					} %>
				</tr>				
			</table>
		</td>
		<% }%>
	</tr>
	<tr class="ligne_separation"><td></td></tr>
</table>

<%
	String id = null;
	try {
		id = Messages.getID();
	} catch (FieldNotInitializedException fnie) {
	}

	String pCode = flRequest.getParameter("code");
	String pCty = flRequest.getParameter("cty");
	String pNext = flRequest.getParameter("next");
	String pTarget = flRequest.getParameter("target");
	//String pObject = flRequest.getParameter("object");
	String msgBody = flRequest.getParameter("msgBody");	

	MessagesBean obj = (MessagesBean)request.getAttribute("Messages");

	
	if (pCode!=null){
		obj.setMsgCode(pCode);
	}
	
	if (pCty!=null){
		obj.setMsgCty(pCty);
	}

	
	String objTableID = null;
	try{
		objTableID = obj.getMsgObjID();
	}catch(FieldNotInitializedException fnie){}
	
	String typeID = null;
	
	try{
		typeID = obj.getMsgCtyID();
	}catch(FieldNotInitializedException fnie){
		try{
		typeID = dico.getRefIdByCode("MsgCtyID", pCty);
		}catch(FrontlineException fe){
			typeID="";
		}
	}
	
	if (typeID!=null){
		obj.setMsgCtyID(typeID);
	}
	
	
	String[] objFields = { "ObjID", "ObjDisplay", "ObjSql" };
	String[][] objQuery = {{ "ObjSql in (" + Utils.formatToSQL(context, "Person") + ", " + Utils.formatToSQL(context, "Solicitation") + ", " + Utils.formatToSQL(context, "Journalist") + ", " + Utils.formatToSQL(context, "Silhouette") + ", " + Utils.formatToSQL(context, "Actor") + ")" }};
	String[] objOrderBy = { "ObjDisplay" };
	Vector objList;
	
	try {
		objList = ObjectsBean.listSummary(session, objFields, objQuery, objOrderBy);
	} catch (FrontlineException e) {
		objList = new Vector();
	}
%>


<%if ("true".equals(msgBody)){%>
<table class="container">	
	<tr>
		<td>
		<fl:form action="/save_messages.fl" bean="Messages">
		<input type="hidden" name="lblimportMsgID" value="" onchange="javascript:alert('here');">
		<input type="hidden" name="importMsgID" value="" onchange="javascript:importDone();">
		<input type="hidden" name="$$returnedHtmlHead" value="">
		<input type="hidden" name="$$returnedHtmlBody" value="Reload ..">
		<input type="hidden" name="MsgID" value="<fl:getProperty name="Messages" property="MsgID" alt=""/>">
		<input type="hidden" name="MsgCtyID" value="<%=typeID%>">
		<input type="hidden" name="MsgCode" value="<fl:getProperty name="Messages" property="MsgCode" alt=""/>">
		<input type="hidden" name="MsgCty" value="<fl:getProperty name="Messages" property="MsgCty" alt=""/>" >

		<table class="container" cellpadding="0">
			<tr>
				<td class="titrePartie" width="20%">
					<fl:getMessage code="F6002" alt="Messages"/>
				</td>					
			</tr>
		</table>
		<table class="container" cellpadding="0">
			<tr class="fieldline">
				<td class="labelTitreFiche">
					<fl:label field="MsgSubject" labelClassName="titreFiche"/>
				</td>
				<td class="fieldTitreFiche">
					<fl:input property="MsgSubject" mandatoryMsgCode="M3001" onblur="if (this.form.MsgSubject.value == '') this.form.MsgSubject.value = this.value;"/>
				</td>
			</tr>
			<tr class="ligne_separation"><td></td></tr>
			<tr class="ligne_separation_couleur"><td></td></tr>
			<tr class="ligne_separation"><td></td></tr>
		</table>
		<table class="container" cellpadding="0">
			<tr>
				<td class="column2">
					<table class="container" cellpadding="0">
						<fl:fieldRead name="Messages" property="MsgCtyID" alt=""/>
					</table>
				</td>
				<td class="separateur"><div class="separation"></div></td>
				<td class="column2">
					<table class="container" cellpadding="0">
						<tr class="fieldline">
							<td class="dataLabel">
								<label for="MsgObjID" id="MsgObjID_label"><fl:getMessage code="F1022" alt="Objet"/></label>
							</td>
							<td class="dataValue">
							<% if (objList != null && objList.size() > 0) { %>
								<select name="MsgObjID" >
									<option value=""></option>
									<% for (int i = 0; i < objList.size(); i++) {
										Hashtable row = (Hashtable)objList.elementAt(i);
										String objID = (String)row.get("ObjID");
										String objDisplay = (String)row.get("ObjDisplay");
										String objSql = (String)row.get("ObjSql");											
									%>
										<option value="<%= Utils.formatToWeb(objID, true) %>" <%= objID != null && objID.equals(objTableID) ? "selected" : "" %><%= id==null && objSql.equals("Solicitation") ? "selected" : ""%> ><%= objDisplay != null ? Utils.formatToWeb(objDisplay, false) : Utils.formatToWeb(objSql, false) %></option>
									<% } %>
								</select>
							<% } %>
							</td>
						</tr>
					</table>				
				</td>
			</tr>
			<tr class="ligne_separation"><td></td></tr>
		</table>
		<table class="container" cellpadding="0">			
			<tr>
				<td class="labelTitreFiche titreInterneSmalltabs"><fl:label field="MsgBody" labelClassName="titreInterneSmalltabs"/></td>
			</tr>
			<tr class="ligne_separation_couleur"><td></td></tr>
			<tr class="ligne_separation"><td></td></tr>
			<tr>
				<td class="dataValue noPaddingLeft"><fl:input property="MsgBody" nodiv="true"/></td>
			</tr>
			<tr class="ligne_separation"><td></td></tr>
		</table>		
		</fl:form>				
		</td>
	</tr>
</table>
<%}%>
</fl:bigTabs>