<%
/******************************************************************************
 * Copyright (c) 2000-2005 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 language="java"%>
<%@ page import="com.edeal.frontline.navigation.FlRequest" %>
<%@ page import="com.edeal.frontline.Utils" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="com.edeal.frontline.DataDictionary" %>
<%@ page import="com.edeal.frontline.FlContext" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.edeal.frontline.navigation.WebUtils" %>
<%@ page import="com.edeal.frontline.ActorBean" %>
<%@ page import="com.edeal.frontline.ObjectsBean" %>
<%@ page import="com.edeal.frontline.AccessDeniedException" %>
<%@ page import="com.edeal.frontline.FlLocale" %>
<%@ page import="com.edeal.frontline.FrontlineException" %>
<%@ page import="com.edeal.frontline.*" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.edeal.frontline.controller.actions.SelectAction" %>
<%@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();

// List code
String lc = flRequest.getRequestParameterOrAttribute("lc");
if (lc == null) {
	lc = flRequest.getRequestParameterOrAttribute("code");
}
if (lc == null) {
	lc = "DocList";
}
request.setAttribute("lc", lc);

// search criteria
String sc = flRequest.getRequestParameterOrAttribute("sc");
// object uri
String ou = flRequest.getRequestParameterOrAttribute("ou");
// id
String objTitle = flRequest.getRequestParameterOrAttribute("obj.title");
String SfwID = flRequest.getRequestParameterOrAttribute("SfwID");

Map searchCriteria = new Hashtable();
if (sc != null) {
	searchCriteria = WebUtils.parseQueryString(sc);
}
request.setAttribute("selectedTab", "0");

Vector vectDocType = dico.getRefList(session, "DocType");
Vector vectFam = dico.getRefList(session, "DocCatID");

String[] fields= {"ObjID","ObjDisplay"};
String[] orderBy = {"ObjDisplay"};
String radicals = context.getContextParameter("Marketing.RefDocument.Objects");

if (radicals == null) {
	logger.error("Option \"Marketing.RefDocument.Objects\" has not been specified, ALL objects will be listed");
	radicals = "";
}

Vector rads = Utils.explodeList(radicals, ";");
String[] tmpArray = new String[rads.size()];
for (int i=0; i < rads.size(); i++) {
	tmpArray[i] = "ObjRadical="+Utils.formatToSQL(context, (String)rads.elementAt(i));
}

Vector list = new Vector();
if (rads.size() > 0) {
	String[][] query = {tmpArray};
	list =ObjectsBean.listSummary(context,fields,query,orderBy,true);
} else {
	list =ObjectsBean.listSummary(context,fields,orderBy);
}

String idAdmin = dico.getRefIdByCode("DocRolesID","ADMIN");

/*Recherche des roles de l'acteur*/
String actorID = (String) session.getAttribute("actorID");
ActorBean actor = null;
Vector roles = new Vector();
try {
	actor = new ActorBean(actorID,context);
	roles = actor.getRoles();
} catch(FrontlineException ex) {
}
/*****************************/
/* Est ce un administrateur  */
boolean isAdmin = false;
if (roles != null && roles.contains(idAdmin)) {
	isAdmin = true;
}

Vector vectNb = (Vector) searchCriteria.get("$$nb");
String nb = null;
if (vectNb != null ) nb = (String) vectNb.firstElement();

Vector vectTitle = (Vector) searchCriteria.get("DocTitle");

String docTitle = null;
if (vectTitle != null ) {
	docTitle = (String) vectTitle.firstElement();
}

Vector vectActID = (Vector) searchCriteria.get("DocActID");
String actID = flRequest.getSearchCriteria("DocActID");
if (vectActID != null ) {
	actID = (String) vectActID.firstElement();
}

String actName = null;
if (actID != null) {
	ObjectsBean objBean = dico.getTable("sql","Actor");
	actName = objBean.toString(session,actID);
}


Vector vectCatID = (Vector) searchCriteria.get("DocCatID");
String docCatID = null;
if (vectCatID != null ) {
	docCatID = (String) vectCatID.firstElement();
}

Vector vectType = (Vector) searchCriteria.get("DocType");
String docType = null;
if (vectType != null ) {
	docType = (String) vectType.firstElement();
}

Vector vectObjID = (Vector) searchCriteria.get("DocObjID");
String docObjID = null;
if (vectObjID != null ) {
	docObjID = (String) vectObjID.firstElement();
}

Vector vectIsArchive = (Vector) searchCriteria.get("DocIsArchive");
String docIsArchive = "0";
if (vectIsArchive != null ) {
	docIsArchive = (String) vectIsArchive.firstElement();
}

String refID = null;
%>
<script type="text/javascript" language="javascript" src="<fl:webapp/>/js/jsOnload.jsp"></script>
<script language="javascript">
var query = '&gridName=DocList';

function doSubmit() {
	f = document.forms[0];
	var query='';
	var nb = f.$$nb.options[f.$$nb.selectedIndex].value;
	query += '$$nb=' + nb;
	
	// If we're doing an autocomplete operation, we need to catch
	// the return key when selecting an object by keyboard	
	try {
		if (acCurHighlightedDiv != null) {
			return false;
		}
	} catch (error) {
	}
	
	if (f.DocTitle.value != '<fl:getMessage code="L2071" alt="<Titre>" js="true"/>') {
		query += "&DocTitle=" + f.DocTitle.value;
	}
	
	if (f.DocActID.value != '') {
		query += "&DocActID=" + f.DocActID.value;
	}
	
	if (f.DocCatID.value != '') {
		query += "&DocCatID="+f.DocCatID.value;
	}
	
	if (f.DocObjID.value != '') {
	    query += "&DocObjID=" + f.DocObjID.value;
	}
	
	if (f.DocType.value != '') {
		query += "&DocType=" + f.DocType.value;	
	}

	<% if (isAdmin) { %>
		if (f.DocIsArchive[0].checked == true) {
			query += "&DocIsArchive=1" 
		 }
    <% } %>
    
   	f.sc.value = query;
	f.submit();
}

function critOnKeyPress(event) {
	var keyPress = 0;
	try {
		keyPress = event.which;
	} catch (error) {
	}
	
	if (keyPress == 0) {
		try {
			keyPress = window.event.keyCode;
		} catch (error) {
		}
	}
	
	// Someone pressed the enter key, AND we're not AC'ing
	if (keyPress == 13 && acCurHighlightedDiv == null) {
		doSubmit();
	}
}

function doOnLoad() {
	window.document.forms[0].onsubmit = doSubmit;
	document.onkeypress = critOnKeyPress;
}
</script>
<form action="<fl:link url="/list_document.fl" menu="true"/>" method="get">
<input type="hidden" name="lc" value="<%= Utils.formatToWeb(lc, true) %>">
<input type="hidden" name="sc" value="">
<input type="hidden" name="ou" value="<%= Utils.formatToWeb(ou, true) %>">
<input type="hidden" name="gridName" value="DocList">
<input type="hidden" name="cu" value="/list_document_criteria.fl">
<input type="hidden" name="lu" value="/list_document.fl">
<input type="hidden" name="obj.title" value="<%= Utils.formatToWeb(objTitle, true) %>">
<input type="hidden" name="criteria.title" value="<fl:getMessage code="L648B" alt="Recherche document"/>">
<% if (SfwID != null) { %>
<input type="hidden" name="SfwID" value="<%= SfwID %>">
<% } %>
<fl:bigTabs criteriaTitle="<%= Utils.getMessage(session, \"L648B\", \"Recherche document\") %>" listTitle="<%= Utils.getMessage(session, \"L63E2\", \"Liste des documents\") %>">
	<table class="container" cellpadding="0">
		<tr>
			<td>
				<div class="criteriaTableDiv">
					<input class="criteriaField" id="DocTitle" name="DocTitle" type="text" value="<%= (docTitle!=null)? Utils.formatToWeb(docTitle,false): Utils.getMessage(session,"L2071","<Titre>") %>" onfocus="if(this.value == '<fl:getMessage code="L2071" alt="<Titre>" js="true"/>') this.value = ''" size="40" maxlength="100" onBlur="if(this.value == '') this.value = '<fl:getMessage code="L2071" alt="<Titre>" js="true"/>'">					
					
					<input type="hidden" id="DocActID" name="DocActID" value="<%= (actID!=null)? Utils.formatToWeb(actID,false):""%>">
					<input id="lblDocActID" type="text" class="criteriaField leftMargin10" name="lblDocActID" value="<%= (actName!=null)? Utils.formatToWeb(actName,false):Utils.getMessage(session, "L63D9", false, true, "<Auteur>")%>" size="40" autocomplete="off" onfocus="displayOnFocus(this);">
					<a href="javascript:var win = window.open('<fl:webapp/>/select.fl?type=Actor&field=DocActID', 'SelectAuteur', 'menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=yes,width=<%= SelectAction.SELECT_WINDOW_WIDTH %>,height=500')" tabindex="3"><img src="<fl:webapp/>/icons/ico/rechercher.gif" border="0" width="17" height="17"></a>
					<a href="javascript:var f = window.document.forms[0]; f.DocActID.value = ''; f.lblDocActID.value = '<fl:getMessage code="L63D9" alt="<Auteur>" js="true"/>'; void('')" tabindex="4"><img src="<fl:webapp/>/icons/ico/supprimer.gif" border="0" width="17" height="17"></a>
					<script language="javascript">runOnTime("installFobAC(document.forms[0].lblDocActID);", 200);</script>			
<%
if (vectFam != null && !vectFam.isEmpty()) {
%>					<select class="criteriaField leftMargin10" name="DocCatID">
						<option value=""><fl:getMessage code="L635D" alt="<Famille>"/></option>
<%
	for (int i = 0; i < vectFam.size(); i++) {
		Vector row = (Vector)vectFam.elementAt(i);
%>						<option value="<%= Utils.formatToWeb((String)row.elementAt(0), true) %>" <%= ((String)row.elementAt(0)).equals(docCatID)? "selected": ""%> ><%= Utils.formatToWeb((String)row.elementAt(1), false) %></option>
<%
	}
%>					</select>
<%
}
%>					<br><br>
					<select class="criteriaField" name="DocObjID">
						<option value=""><fl:getMessage code="L63D8" alt="<Objet>"/></option>
<%
for (int i=0; i < list.size(); i++) {
	Hashtable currObj = (Hashtable) list.elementAt(i);
	String objID = (String) currObj.get("ObjID");
	String objDisplay = (String) currObj.get("ObjDisplay");
%>						<option value="<%=objID%>" <%= ((String)objID).equals(docObjID)? "selected" : ""%> ><%=objDisplay%></option>
<%
}
%>					</select>
<%
if (vectDocType != null && !vectDocType.isEmpty()) { 
%>					<select class="criteriaField leftMargin10" name="DocType">
						<option value=""><fl:getMessage code="L60E4" alt="<Type>"/></option>
<%
	for (int i = 0; i < vectDocType.size(); i++) {
		Vector row = (Vector)vectDocType.elementAt(i);
%>						<option value="<%= Utils.formatToWeb((String)row.elementAt(0), true) %>" <%= ((String)row.elementAt(0)).equals(docType)? "selected" : "" %> ><%= Utils.formatToWeb((String)row.elementAt(1), false) %></option>
<%
	}
%>					</select>
<%
}
if (isAdmin) {
%>					<span class="criteriaLabel leftMargin10"><fl:getMessage code="F602E" alt="Archive"/></span>
					<input class="leftMargin10 radio" type="radio" id="DocIsArchive_0" name="DocIsArchive" value="1" <%= docIsArchive.equals("1")? "checked" : "" %>><label class="radioLabel" for="DocIsArchive_0"><fl:getMessage code="L2005" alt="Oui"/></label>
					<input class="leftMargin10 radio" type="radio" id="DocIsArchive_1" name="DocIsArchive" value="0" <%= docIsArchive.equals("0") ? "checked" : "" %>><label class="radioLabel" for="DocIsArchive_1"><fl:getMessage code="L2006" alt="Non"/></label>
<%
}
%>				</div>
			</td>
			<td class="separation"><div class="separation"></div></td>
			<td valign="top" style="width: 15%;">
				<table class="container" cellpadding="0">					
					<tr>
						<td colspan="2" style="text-align: left;">
							<select name="$$nb">
								<option value="10" <%= nb == null || "10".equals(nb) ? "selected" : "" %> >10&nbsp;/&nbsp;<fl:getMessage code="M60CC" alt="page" /></option>
								<option value="20" <%= "20".equals(nb) ? "selected" : "" %> >20&nbsp;/&nbsp;<fl:getMessage code="M60CC" alt="page" /></option>
								<option value="50" <%= "50".equals(nb) ? "selected" : "" %> >50&nbsp;/&nbsp;<fl:getMessage code="M60CC" alt="page" /></option>
								<option value="All" <%= "All".equals(nb) ? "selected" : "" %> ><fl:getMessage code="L302C" alt="Toutes"/></option>
							</select>
						</td>
					</tr>
					<tr class="ligne_separation"><td></td></tr>
					<tr>
						<td>
							<a href="javascript:doSubmit();"><img src="<fl:webapp/>/icons/ico/valid_green2_long.gif"></a>

						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</fl:bigTabs>
</form>
