<%
/******************************************************************************
 * 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.
 *
 *
 ******************************************************************************/
 
 /* a.stouky@e-deal.ma : 16/08/06
	Champ principal: Libellé
	Paragraphe de champ sur 2 colonnes
		1ère colonne:
			Logiciel, nom affiché, Valorisation, par défaut
		2ème colonne:
			Page par défaut
	Enlever les champs icones
	Diminuer la taille des liens et les espacements entre les icônes dans les smalltabs (modification)
 */
%><%@page session="true"%>
<%@ page import="com.edeal.frontline.navigation.FlRequest" %>
<%@ page import="com.edeal.frontline.FlContext" %>
<%@ page import="com.edeal.frontline.Utils" %>
<%@ page import="com.edeal.frontline.FieldNotInitializedException" %>
<%@ page import="com.edeal.frontline.DataDictionary" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.edeal.frontline.MenuBean" %>
<%@ page import="com.edeal.frontline.MenuElementBean" %>
<%@ page import="java.util.Hashtable" %>
<%@taglib uri="http://www.e-deal.com/taglib/fl" prefix="fl" %>
<jsp:useBean class="com.edeal.frontline.MenuBean" id="Menu" scope="request"/>
<%	
	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 copyMenID = (String)request.getAttribute("copyMenID");
	if (copyMenID != null) {
		try {
			Menu = new MenuBean(copyMenID, context);
			request.setAttribute("Menu", Menu);
		} catch (Exception e) {
		}
	}

	String id = null;
	try { id = Menu.getID(); }
	catch (FieldNotInitializedException e) {}
	
	Vector nodes = new Vector();
	Vector leaves = new Vector(); 
	//if this is not a creation
	if (id != null) {
		//get first level menuelements (nodes)
		String[] fields1 = {"MelID", "MelLabel", "MelLabelIsCode", "MelType", "MelIconName", "MelPosition", "MelLink"};
		String[][] query1 = {{"MelMenID = " + Utils.formatToSQL(context, Menu.getMenID()), 
					"MelParentID = null"}};
		String[] orderBy1 = {"MelPosition"};
		nodes = MenuElementBean.listSummary(context, fields1, query1, orderBy1);
		
		//get second level menuelements (leaves) ordered by their parent's position
		String[] fields2 = {"MelID", "MelLabel", "MelLabelIsCode", "MelIconName", "MelParentID", "MelPosition", "MelLink","MelParentID:MelPosition"};
		String[][] query2 = {{"MelMenID = " + Utils.formatToSQL(context, Menu.getMenID()), 
					"MelParentID <> null"}};
		String[] orderBy2 = {"MelParentID:MelPosition", "MelPosition"};
		leaves = MenuElementBean.listSummary(context, fields2, query2, orderBy2);
	}
	String menMainLink = null;
	try { menMainLink = Menu.getMenMainLink(); }
	catch (FieldNotInitializedException e) {}

%>
<head>
<script type="text/javascript" language="javascript">

	function doRefresh() {
		var f = document.forms[0];
		presentFieldsValue();
		f.action = "<fl:link url="<%= \"/refresh_interaction.fl?random=\"+ Math.random()+ (id != null ? \"&id=\" + id : \"\") %>"/>";
		doExitConf = false;
		f.submit();
	}
	
	function doSelectLabel(field) {
		window.open('<fl:link url="/select_label.fl?1=1"/>&field=' + field, 'select_label', 'width=1000,height=500,menubar=no,status=no,resizable=yes');
	}
	
	function doSubmit() {
		var f = document.forms[0];
		if (f.MenTitle.value == "") {
			window.alert("<fl:getMessage code="M608B" alt="Vous devez spécifier un nom affiché." js="true"/>");
			f.MenTitle.focus();
			return false;
		}
	
		presentFieldsValue();
		return true;
	}
	
	function doAddChild(menID,parentID, position) {
		var strLink = '<fl:link url="/create_menuelement.fl?MenID='+menID +'&ParentID=' + parentID +'&MelPosition=' + position+'" list="false"/>';
		window.open(strLink, 'MenuElement', 'menubar=no,status=no,toolbar=no,scrollbars=auto,resizable=yes,width=800,height=250');
	
	}
	
	function doRead(id) {
		var strLink = '<fl:link url="/read_menuelement.fl?id='+id+'" list="false"/>';
		window.open(strLink, 'MenuElement', 'menubar=no,status=no,toolbar=no,scrollbars=auto,resizable=yes,width=800,height=250');
	}
	
	function doEdit(id) {
		var strLink = '<fl:link url="/edit_menuelement.fl?id='+id+'" list="false"/>';
		window.open(strLink, 'MenuElement', 'menubar=no,status=no,toolbar=no,scrollbars=auto,resizable=yes,width=800,height=250');
	
	}

 	function doAdd(menID) {
		f = document.forms[0];
		var position = f.position.value;
		var strLink = '<fl:link url="/create_menuelement.fl?MenID='+menID+'&MelPosition=' + position+'" list="false"/>';
		window.open(strLink, 'MenuElement', 'menubar=no,status=no,toolbar=no,scrollbars=auto,resizable=yes,width=810,height=250');
	}

	function doDel(eltID) {
		if (confirm('<fl:getMessage code="M2004" alt="Etes-vous sûr de vouloir supprimer?" js="true"/>')) {
			doAjax('<fl:link url="/delete.fl?type=MenuElement&id=' + eltID + '"/>&successPage=' + encodeURIComponent('/edit_menu.fl?id=<%= id%>'));
		}
	}

	function doSelectMainType(index) {
		var f = document.forms[0];
		f.MenMainType[index].checked = true;
	}

	function doMenMainListChange() {
		doSelectMainType(0);	
	}

	function doRemoveLabel(field){
		var f = window.document.forms[0];
		if (f.elements[field] && f.elements[field].value) {
			f.elements[field].value = '';
		}
		
		if (f.elements['lbl' + field] && f.elements['lbl' + field].value) {
			f.elements['lbl' + field].value = '';
		}
	}
</script>
</head>
<fl:form action="/save_menu.fl" bean="Menu">
<fl:bigTabs objectTitle="<%= Utils.getMessage(session, \"L624C\", \"Menu\") %>" listTitle="<%= Utils.getMessage(session, \"L624E\", \"Liste des menus\") %>" ou="<%= \"/edit_menu.fl\" + (id != null ? \"?id=\" + id : \"\") %>">
	<table class="container" cellpadding="0">
		<tr>
			<td colspan="3">
				<table class="container" cellpadding="0">
					<tr class="fieldline">
						<td class="labelTitreFiche">
							<fl:label field="MenLabel" labelClassName="titreFiche"/>
						</td>
						<td class="fieldTitreFiche">
							<fl:input property="MenLabel" unicityMsgCode="M618F" mandatoryMsgCode="M6059"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr><td colspan="3" class="ligne_separation"/></tr>
		<tr>
			<td class="column2">
				<table class="container">
					<fl:fieldEdit property="MenSfwID" mandatoryMsgCode="M6089"/>
					<tr class="fieldline">
						<td id="MenTitle_label" class="dataLabel">
							<fl:label code="L6015"/>
						</td>									
						<td id="MenTitle_value" class="dataValue"><div>
							<% 	String fullTitle = null;
								String codeTitle = null;
								try { 
										codeTitle = Menu.getMenTitle() ;
										fullTitle = Utils.getMessage(session, codeTitle, "Libellé indisponible"); 
								} catch (Exception e) {}
							%>
							<table class="fieldTable" cellpadding="0">
							<tr>
								<td class="principalText">
									<input name="lblMenTitle" type="text" size="24" value="<%= (fullTitle!= null)? fullTitle : "" %>">
									<input name="MenTitle" type="hidden" value="<%= (codeTitle!= null)? codeTitle : "" %>">
								</td>
								<td class="postText">
									<a href="javascript:doSelectLabel('MenTitle')"><img src="<fl:webapp/>/icons/ico/rechercher.gif" alt="" class="icon"></a>
									<a href="javascript:doRemoveLabel('MenTitle')"><img src="<fl:webapp/>/icons/ico/supprimer.gif" alt="" class="icon"></a>
								</td>
							</tr>
							</table></div>
						</td>
					</tr>
					<fl:fieldEdit property="MenValue"/>
					<fl:fieldEdit property="MenDefault"/>
				</table>
			</td>
			<td class="separation"><div class="separation"></div></td>
			<td class="column2">
				<table>
					<tr>
						<td id="MenMainType_label" class="dataLabel">
							<fl:getMessage code="F60A9" alt="Page par défaut"/>
						</td>									
						<td id="MenMainType_value" class="dataValue">
							<input type="radio" name="MenMainType" value="0" <%= menMainLink == null ? "checked" : "" %>><fl:getMessage code="L6228" alt="Liste"/>
							<br>
							<fl:input property="MenMainList"/>
						</td>
					</tr>
					<tr class="fieldline">
						<td id="MenMainLink_label" class="dataLabel">
							&nbsp;
						</td>									
						<td id="MenMainLink_value" class="dataValue">
							<input type="radio" name="MenMainType" value="1" <%= menMainLink != null ? "checked" : "" %>><fl:getMessage code="L6180" alt="Lien"/>
							<br>
							<fl:input onchange="doSelectMainType(1)" property="MenMainLink" cols="70" rows="2"/>
						</td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
<% 	int nodePosition = 0;
if (id != null) {
%>
<table class="container" cellpadding="0">
		<tr class="ligne_separation"><td></td></tr>
		<tr>
			<td class="titrePartie"><fl:getMessage code="L624F" alt="Eléments de menu"/></td>
		</tr>
		<tr class="ligne_separation_couleur"><td></td></tr>
		<tr class="ligne_separation"><td></td></tr>
</table>
	<link href="<fl:webapp/>/css/smalltabs_css.jsp" rel="stylesheet" type="text/css">
	<div style="width: 100%; overflow: auto;">
	<fl:smallTabs name="MenuELT">
	<%
	
	//for each node
	for (int i=0 ; i<nodes.size() ; i++) {
		Hashtable node = (Hashtable)nodes.elementAt(i);
		int Fposition = 0;
		int leavesIdx = 0;
		String nodeID = (String)node.get("MelID");
		String nodeLabel = (String)node.get("MelLabel");
		Boolean nodeLabelIsCode = (Boolean)node.get("MelLabelIsCode");
		String nodeIconName = (String)node.get("MelIconName");
		String nodeType = (String)node.get("MelType");
		String nodeLink = (String)node.get("MelLink");
		String title = (nodeLink != null ? "title='" + Utils.formatToJavascript(nodeLink) + "'" : "");
		nodePosition = 0;
		if (node.get("MelPosition") != null)
			nodePosition = ((Number)node.get("MelPosition")).intValue();
		String nodeLabelDisplay = (nodeLabelIsCode.equals(Boolean.FALSE) ? nodeLabel : Utils.getMessage(session, nodeLabel, "Libellé indisponible"));
		if (nodeType != null && nodeType.equals("NODE")){
			while (leavesIdx < leaves.size()) {
				Hashtable leaf = (Hashtable)leaves.elementAt(leavesIdx);
				String leafParentID = (String)leaf.get("MelParentID");
				int leafPosition = 0;
				if (leafParentID.equals(nodeID) && leaf.get("MelPosition") != null) {
					leafPosition = ((Number)leaf.get("MelPosition")).intValue();
					Fposition = leafPosition ;
				}
				leavesIdx++;
			}
		}
		//display info on first line
		%>
		<fl:sTab id="<%= nodeLabel %>" title="<%= nodeLabelDisplay %>">
		<table class="data_column" cellpadding="0" border="0">
		<tr class="fieldline">
			<td colspan="2">
				<table class="data_column" cellpadding="0" border="0">
					<tr>
					<td style="width: 2%">
						<a href="javascript:doEdit('<%=nodeID%>');">
							<img src="<fl:webapp/>/icons/ico/zoom.gif" alt="Edit">
						</a>
					</td>
					<td style="width: 2%">
						<a href="javascript:doDel('<%=nodeID%>');">
							<img src="<fl:webapp/>/icons/ico/supprimer.gif" alt="Sup">
						</a>
					</td>
					<td style="text-align:left;width: 60%" class="dataValue">
						<a href="javascript:doRead('<%=nodeID%>')"><%= nodeLabelDisplay %></a> (<%= nodePosition %>)
					</td>
<%			if (nodeType != null && nodeType.equals("NODE")){
%>					
					<td style="text-align:right;width: 20%" class="dataValue">
						<a href="javascript:doAddChild('<%= id %>','<%=nodeID%>','<%=  Fposition + 10 %>')">
							<fl:getMessage code="L6253" alt="Feuille"/> + 
						</a>
					</td>
<%			}
%>
					</tr>
				</table>
			</td>
		</tr>
<%			if (nodeType != null && nodeType.equals("NODE")){
%>	
		<tr class="ligne_separation_couleur"><td colspan="3"></td></tr>
<% }
%>
		<%
			//leavesIndex is used to navigate on the leaves vector
			int leavesIndex = 0;
			boolean atLeastOneLeaf = false;
			while (leavesIndex < leaves.size()) {
				Hashtable leaf = (Hashtable)leaves.elementAt(leavesIndex);
				String leafParentID = (String)leaf.get("MelParentID");
				leavesIndex++;
				//if this leaf belongs to current node, display info
				if (leafParentID.equals(nodeID)) {
					atLeastOneLeaf = true;
					String leafID = (String)leaf.get("MelID");
					String leafLabel = (String)leaf.get("MelLabel");
					Boolean leafLabelIsCode = (Boolean)leaf.get("MelLabelIsCode");
					String leafIconName = (String)leaf.get("MelIconName");
					String leafLink = (String)leaf.get("MelLink");
					title = (leafLink != null ? "title='" + Utils.formatToJavascript(leafLink) + "'" : "");
					int leafPosition = 0;
					if (leaf.get("MelPosition") != null)
						leafPosition = ((Number)leaf.get("MelPosition")).intValue();
					String leafLabelDisplay = (leafLabelIsCode.equals(Boolean.FALSE) ? leafLabel : Utils.getMessage(session, leafLabel, "Libellé indisponible"));
			%>
					<tr class="fieldline">							
							<td style="width: 10%"></td>
							<td>
							<table class="data_column" cellpadding="0" border="0">
								<tr>
								<td style="width: 1%">
									<a href="javascript:doEdit('<%=leafID%>');">
										<img src="<fl:webapp/>/icons/ico/zoom.gif" alt="Edit">
									</a>
								</td>
								<td style="width: 1%">
									<a href="javascript:doDel('<%=leafID%>');">
										<img src="<fl:webapp/>/icons/ico/supprimer.gif" alt="Sup">
									</a>
								</td>
								<% if (leafIconName != null) { %>
								<td style="width: 1%"><img src="<fl:webapp/>/icons/menu/<%= leafIconName %>"></td>
								<% } else { %>
								<td style="width: 1%"><img src="<fl:webapp/>/icons/menu/Default.gif"></td>
								<% } %>
								<td style="text-align:left;width: 80%" class="dataValue">
									<a href="javascript:doRead('<%=leafID%>')" <%=title%>><%= leafLabelDisplay %></a> (<%= leafPosition %>)</td>
								</tr>
							</table>
							</td>
	<%
				} else {
					continue;
				}
			}
%>
<%			if (!atLeastOneLeaf && nodeType != null && nodeType.equals("NODE")) {
					%><td colspan="2"><fl:getMessage code="L6259" alt="Aucune feuille"/></td><%
			}
		%>
							</tr>
					</table>
			</fl:sTab>
	<%	}
	%>
	</fl:smallTabs>
	</div>
<% } %>
		<input type="hidden" name="position" value="<%= nodePosition + 10 %>">
</fl:bigTabs>
</fl:form>

