www.pudn.com > Tustena_CRM_OS_3.0.2_Stable.zip > groups.ascx.cs


/// TUSTENA PUBLIC LICENSE v1.0 
///  
/// Portions Copyright (c) 2003-2005 Digita S.r.l. All Rights Reserved. 
/// 
/// Tustena CRM is a trademark of: 
/// Digita S.r.l. 
/// Viale Enrico Fermi 14/z 
/// 31011 Asolo (Italy) 
/// Tel. +39-0423-951251 
/// Mail. info@digita.it 
/// 
/// This file contains Original Code and/or Modifications of Original Code 
/// as defined in and that are subject to the Tustena Public Source License 
/// Version 1.0 (the 'License'). You may not use this file except in 
/// compliance with the License. Please obtain a copy of the License at 
/// http://www.tustena.com/TPL/ and read it before using this 
// file. 
/// 
/// The Original Code and all software distributed under the License are 
/// distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
/// EXPRESS OR IMPLIED, AND DIGITA S.R.L. HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
/// INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
/// FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
/// Please see the License for the specific language governing rights and 
/// limitations under the License. 
/// 
/// YOU MAY NOT REMOVE OR ALTER THIS COPYRIGHT NOTICE! 
///  
 
using System; 
using System.Data; 
using System.Resources; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Digita.Tustena.Base; 
using Digita.Tustena.Database; 
 
namespace Digita.Tustena.Admin 
{ 
	public class groups : UserControl 
	{ 
		protected DataGrid Groups_Grid; 
		protected ListBox ListGroups; 
		protected ListBox ListDip; 
		protected Label LblError; 
		protected TextBox GroupText; 
		protected Button Btn_FwwAll; 
		protected Button Btn_Fww; 
		protected Button Btn_Rww; 
		protected Button Btn_RwwAll; 
		protected Table Groups_Table; 
		protected LinkButton Submit; 
		protected LinkButton NewBtn; 
 
		#region Codice generato da Progettazione Web Form 
 
		protected override void OnInit(EventArgs e) 
		{ 
			InitializeComponent(); 
			base.OnInit(e); 
		} 
 
		private void InitializeComponent() 
		{ 
			this.Load += new EventHandler(this.Page_Load); 
		} 
 
		#endregion 
 
		private UserConfig UC = new UserConfig(); 
		public static ResourceManager rm = (ResourceManager) HttpContext.Current.Application["RM"]; 
 
		private void Page_Load(object sender, EventArgs e) 
		{ 
			UC = (UserConfig) HttpContext.Current.Session["userconfig"]; 
 
			if (!Page.IsPostBack) 
			{ 
				Btn_FwwAll.ToolTip = rm.GetString("Agrtxt1"); 
				Btn_Fww.ToolTip = rm.GetString("Agrtxt2"); 
				Btn_Rww.ToolTip = rm.GetString("Agrtxt3"); 
				Btn_RwwAll.ToolTip = rm.GetString("Agrtxt4"); 
 
				Groups_Grid.Columns[0].HeaderText = rm.GetString("Agrtxt7"); 
				Groups_Grid.Columns[1].HeaderText = rm.GetString("Agrtxt8"); 
				Submit.Text = rm.GetString("Agrtxt10"); 
 
				FillGrid(); 
				FillListGroups(); 
				ViewState["IDGroup"] = "-1"; 
 
				Groups_Grid.Visible = true; 
				NewBtn.Visible = true; 
				NewBtn.Text = rm.GetString("Agrtxt12"); 
				Groups_Table.Visible = false; 
			} 
 
		} 
 
		private void FillListGroups() 
		{ 
			ListGroups.Items.Clear(); 
			ListGroups.DataTextField = "Description"; 
			ListGroups.DataValueField = "id"; 
			ListGroups.DataSource = DatabaseConnection.CreateDataset("select * from Groups order by id"); 
			ListGroups.DataBind(); 
		} 
 
		private void FillGrid() 
		{ 
			Groups_Grid.DataSource = DatabaseConnection.CreateDataset("select * from Groups order by id"); 
			Groups_Grid.DataBind(); 
		} 
 
		private void FillTable(string id) 
		{ 
			ViewState["IDGroup"] = id; 
			DataSet ds; 
			ds = DatabaseConnection.CreateDataset("select * from Groups where id=" + id); 
			DataRow dr = ds.Tables[0].Rows[0]; 
			GroupText.Text = dr["Description"].ToString(); 
			string[] arryD = dr["Dependency"].ToString().Split('|'); 
			dr.Delete(); 
			ds.Clear(); 
			string query = String.Empty; 
			foreach (string ut in arryD) 
			{ 
				query += "id=" + ut + " or "; 
			} 
			query = query.Substring(7, query.Length - 17); 
			ListDip.DataTextField = "Description"; 
			ListDip.DataValueField = "id"; 
			ListDip.DataSource = DatabaseConnection.CreateDataset(String.Format("select * from Groups where {0}", query)); 
			ListDip.DataBind(); 
		} 
 
		public void NewBtnClick(Object sender, EventArgs e) 
		{ 
			Groups_Table.Visible = true; 
			GroupText.Text = String.Empty; 
			NewBtn.Visible = false; 
			Groups_Grid.Visible = false; 
 
		} 
 
		public void Submit_Click(Object sender, EventArgs e) 
		{ 
			if (StaticFunctions.IsBlank(ViewState["IDGroup"].ToString()) || (GroupText.Text != null && GroupText.Text.Length == 0)) 
				return; 
 
			string sqlString = "select id from Groups where ID =" + int.Parse(ViewState["IDGroup"].ToString()); 
			using (DigiDapter dg = new DigiDapter(sqlString)) 
			{ 
 
 
 
				dg.Add("Description", GroupText.Text); 
				string dep = "|"; 
				foreach (ListItem im in ListDip.Items) 
				{ 
					dep += im.Value.ToString() + "|"; 
				} 
				if (dep.Length < 2) dep = "|" + UC.AdminGroupId.ToString() + "|"; 
				if (dep.IndexOf("|" + UC.AdminGroupId.ToString() + "|") < 0) dep = "|" + UC.AdminGroupId.ToString() + dep; 
				dg.Add("Dependency", dep); 
				dg.Execute("Groups", "id=" + int.Parse(ViewState["IDGroup"].ToString())); 
			} 
			ViewState["IDGroup"] = "-1"; 
			GroupText.Text = String.Empty; 
			ListDip.Items.Clear(); 
			FillGrid(); 
			FillListGroups(); 
			Groups_Grid.Visible = true; 
			Groups_Table.Visible = false; 
			NewBtn.Visible = true; 
		} 
 
		public void CategoryGridItemCommand(object source, DataGridCommandEventArgs e) 
		{ 
			string delStr; 
			switch (e.CommandName) 
			{ 
				case "Edit": 
					FillTable(((Literal) e.Item.FindControl("IDCat")).Text); 
					FillGrid(); 
					FillListGroups(); 
					Groups_Grid.Visible = false; 
					NewBtn.Visible = false; 
					Groups_Table.Visible = true; 
					break; 
				case "Delete": 
					delStr = "delete from Groups where ID =" + int.Parse(((Literal) e.Item.FindControl("IDCat")).Text); 
					DatabaseConnection.DoCommand(delStr); 
					Groups_Grid.EditItemIndex = -1; 
					FillGrid(); 
					FillListGroups(); 
					break; 
			} 
		} 
 
		public void Groups_Grid_ItemDataBound(Object sender, DataGridItemEventArgs e) 
		{ 
			switch (e.Item.ItemType) 
			{ 
				case ListItemType.Item: 
				case ListItemType.AlternatingItem: 
					Trace.Warn("", ((Literal) e.Item.Cells[0].FindControl("IDCat")).Text); 
					Literal LtrDip; 
					string id = ((Literal) e.Item.Cells[0].FindControl("IDCat")).Text; 
					DataSet ds; 
					ds = DatabaseConnection.CreateDataset("select * from Groups where id=" + int.Parse(id)); 
					DataRow dr = ds.Tables[0].Rows[0]; 
					string[] arryD = dr["Dependency"].ToString().Split('|'); 
					dr.Delete(); 
					ds.Clear(); 
					string query = String.Empty; 
					foreach (string ut in arryD) 
					{ 
						if (ut.Length > 0) query += "id=" + ut + " or "; 
					} 
					query = query.Substring(0, query.Length - 3); 
					ds = DatabaseConnection.CreateDataset("select Description from Groups where " + query); 
					string result = String.Empty; 
					foreach (DataRow dr1 in ds.Tables[0].Rows) 
					{ 
						result += dr1["Description"] + ", "; 
					} 
					LtrDip = (Literal) e.Item.Cells[1].FindControl("LtrDip"); 
					if (result.Length > 2) 
					{ 
						result = result.Substring(0, result.Length - 2); 
					} 
					LtrDip.Text = result; 
 
					LinkButton DeleteButton = (LinkButton) e.Item.Cells[2].FindControl("LnkDel"); 
					DeleteButton.Text = rm.GetString("Delete"); 
					DeleteButton.Attributes.Add("onclick", "return confirm('" + rm.GetString("Agrtxt6") + "');"); 
					LinkButton EditButton = (LinkButton) e.Item.Cells[2].FindControl("LnkEdt"); 
					EditButton.Text = rm.GetString("Modify"); 
					if (result.Length < 1) 
					{ 
						DeleteButton.Visible = false; 
						EditButton.Visible = false; 
					} 
					break; 
			} 
		} 
 
		public void Transfer_Listbox(ListBox fromListBox, ListBox toListBox) 
		{ 
			bool exists; 
			bool done = false; 
			foreach (ListItem li in fromListBox.Items) 
			{ 
				exists = false; 
				if (li.Selected) 
				{ 
					foreach (ListItem li1 in toListBox.Items) 
					{ 
						if (li.Value == li1.Value) 
						{ 
							exists = true; 
							break; 
						} 
					} 
					if (!exists) 
					{ 
						toListBox.Items.Add(li); 
						done = true; 
					} 
				} 
			} 
			if (done) toListBox.SelectedItem.Selected = false; 
		} 
 
		public void Remove_ListBox(ListBox fromListBox) 
		{ 
			ListBox MyLB = new ListBox(); 
			foreach (ListItem li in fromListBox.Items) 
			{ 
				if (li.Selected) 
				{ 
					MyLB.Items.Add(li); 
				} 
			} 
			foreach (ListItem li in MyLB.Items) 
			{ 
				fromListBox.Items.Remove(li); 
			} 
		} 
 
		public void Btn_Fww_Click(Object sender, EventArgs e) 
		{ 
			if (ListGroups.SelectedIndex > -1) 
			{ 
				Transfer_Listbox(ListGroups, ListDip); 
			} 
		} 
 
		public void Btn_FwwAll_Click(Object sender, EventArgs e) 
		{ 
			foreach (ListItem li in ListGroups.Items) 
			{ 
				li.Selected = true; 
			} 
			Transfer_Listbox(ListGroups, ListDip); 
		} 
 
		public void Btn_Rww_Click(Object sender, EventArgs e) 
		{ 
			if (ListDip.SelectedIndex > -1) 
			{ 
				Remove_ListBox(ListDip); 
 
			} 
		} 
 
		public void Btn_RwwAll_Click(Object sender, EventArgs e) 
		{ 
			foreach (ListItem li in ListDip.Items) 
			{ 
				li.Selected = true; 
			} 
			Remove_ListBox(ListDip); 
		} 
	} 
}