Esempio n. 1
0
		public static void AppendButtons(string sVIEW_NAME, Guid gASSIGNED_USER_ID, Control ctl, bool bIsMobile, DataRow rdr, L10N L10n, CommandEventHandler Page_Command)
		{
			if ( ctl == null )
			{
				SplendidError.SystemWarning(new StackTrace(true).GetFrame(0), "AppendButtons ctl is not defined");
				return;
			}
			//ctl.Controls.Clear();

			Hashtable hashIDs = new Hashtable();
			// 05/06/2010   Use a special Page flag to override the default IsPostBack behavior. 
			bool bIsPostBack = ctl.Page.IsPostBack;
			bool bNotPostBack = false;
			if ( ctl.TemplateControl is SplendidControl )
			{
				bNotPostBack = (ctl.TemplateControl as SplendidControl).NotPostBack;
				bIsPostBack = ctl.Page.IsPostBack && !bNotPostBack;
			}
			bool bShowUnassigned = Crm.Config.show_unassigned();
			DataTable dt = SplendidCache.DynamicButtons(sVIEW_NAME);
			if ( dt != null )
			{
				foreach(DataRow row in dt.Rows)
				{
					Guid   gID                 = Sql.ToGuid   (row["ID"                ]);
					int    nCONTROL_INDEX      = Sql.ToInteger(row["CONTROL_INDEX"     ]);
					string sCONTROL_TYPE       = Sql.ToString (row["CONTROL_TYPE"      ]);
					string sMODULE_NAME        = Sql.ToString (row["MODULE_NAME"       ]);
					string sMODULE_ACCESS_TYPE = Sql.ToString (row["MODULE_ACCESS_TYPE"]);
					string sTARGET_NAME        = Sql.ToString (row["TARGET_NAME"       ]);
					string sTARGET_ACCESS_TYPE = Sql.ToString (row["TARGET_ACCESS_TYPE"]);
					bool   bMOBILE_ONLY        = Sql.ToBoolean(row["MOBILE_ONLY"       ]);
					bool   bADMIN_ONLY         = Sql.ToBoolean(row["ADMIN_ONLY"        ]);
					string sCONTROL_TEXT       = Sql.ToString (row["CONTROL_TEXT"      ]);
					string sCONTROL_TOOLTIP    = Sql.ToString (row["CONTROL_TOOLTIP"   ]);
					string sCONTROL_ACCESSKEY  = Sql.ToString (row["CONTROL_ACCESSKEY" ]);
					string sCONTROL_CSSCLASS   = Sql.ToString (row["CONTROL_CSSCLASS"  ]);
					string sTEXT_FIELD         = Sql.ToString (row["TEXT_FIELD"        ]);
					string sARGUMENT_FIELD     = Sql.ToString (row["ARGUMENT_FIELD"    ]);
					string sCOMMAND_NAME       = Sql.ToString (row["COMMAND_NAME"      ]);
					string sURL_FORMAT         = Sql.ToString (row["URL_FORMAT"        ]);
					string sURL_TARGET         = Sql.ToString (row["URL_TARGET"        ]);
					string sONCLICK_SCRIPT     = Sql.ToString (row["ONCLICK_SCRIPT"    ]);
					// 07/28/2010   We need a flag to exclude a button from a mobile device. 
					bool   bEXCLUDE_MOBILE     = false;
					try
					{
						bEXCLUDE_MOBILE = Sql.ToBoolean(row["EXCLUDE_MOBILE"]);
					}
					catch
					{
					}
					// 03/14/2014   Allow hidden buttons to be created. 
					bool   bHIDDEN             = false;
					try
					{
						bHIDDEN = Sql.ToBoolean(row["HIDDEN"]);
					}
					catch
					{
					}

					// 09/01/2008   Give each button an ID to simplify validation. 
					// Attempt to name the control after the command name.  If no command name, then use the control text. 
					string sCONTROL_ID = String.Empty;
					if ( !Sql.IsEmptyString(sCOMMAND_NAME) )
					{
						sCONTROL_ID = sCOMMAND_NAME;
					}
					else if ( !Sql.IsEmptyString(sCONTROL_TEXT) )
					{
						sCONTROL_ID = sCONTROL_TEXT;
						if ( sCONTROL_TEXT.IndexOf('.') >= 0 )
						{
							sCONTROL_ID = sCONTROL_TEXT.Split('.')[1];
							sCONTROL_ID = sCONTROL_ID.Replace("LBL_", "");
							sCONTROL_ID = sCONTROL_ID.Replace("_BUTTON_LABEL", "");
						}
					}
					if ( !Sql.IsEmptyString(sCONTROL_ID) )
					{
						// 09/01/2008   Cleanup the ID. 
						sCONTROL_ID = sCONTROL_ID.Trim();
						sCONTROL_ID = sCONTROL_ID.Replace(' ', '_');
						sCONTROL_ID = sCONTROL_ID.Replace('.', '_');
						sCONTROL_ID = "btn" + sCONTROL_ID.ToUpper();
						// 12/16/2008   Add to hash after cleaning the ID. 
						if ( hashIDs.Contains(sCONTROL_ID) )
							sCONTROL_ID = sVIEW_NAME.Replace('.', '_') + "_" + nCONTROL_INDEX.ToString();
						if ( !hashIDs.Contains(sCONTROL_ID) )
							hashIDs.Add(sCONTROL_ID, null);
						else
							sCONTROL_ID = String.Empty;  // If ID still exists, then don't set the ID. 
					}

					// 03/21/2008   We need to use a view to search for the rows for the ColumnName. 
					// 11/22/2010   Convert data reader to data table for Rules Wizard. 
					//DataView vwSchema = null;
					//if ( rdr != null )
					//	vwSchema = new DataView(rdr.GetSchemaTable());

					string[] arrTEXT_FIELD = sTEXT_FIELD.Split(' ');
					object[] objTEXT_FIELD = new object[arrTEXT_FIELD.Length];
					for ( int i=0 ; i < arrTEXT_FIELD.Length; i++ )
					{
						if ( !Sql.IsEmptyString(arrTEXT_FIELD[i]) )
						{
							objTEXT_FIELD[i] = String.Empty;
							if ( rdr != null ) // && vwSchema != null
							{
								//vwSchema.RowFilter = "ColumnName = '" + Sql.EscapeSQL(arrTEXT_FIELD[i]) + "'";
								//if ( vwSchema.Count > 0 )
								// 11/22/2010   Convert data reader to data table for Rules Wizard. 
								if ( rdr.Table.Columns.Contains(arrTEXT_FIELD[i]) )
									objTEXT_FIELD[i] = Sql.ToString(rdr[arrTEXT_FIELD[i]]);
							}
						}
					}
					if ( String.Compare(sCONTROL_TYPE, "Button", true) == 0 )
					{
						Button btn = new Button();
						ctl.Controls.Add(btn);
						if ( !Sql.IsEmptyString(sCONTROL_ID) )
							btn.ID = sCONTROL_ID;
						if ( !Sql.IsEmptyString(sARGUMENT_FIELD) )
						{
							if ( rdr != null ) // && vwSchema != null )
							{
								//vwSchema.RowFilter = "ColumnName = '" + Sql.EscapeSQL(sARGUMENT_FIELD) + "'";
								//if ( vwSchema.Count > 0 )
								if ( rdr.Table.Columns.Contains(sARGUMENT_FIELD) )
									btn.CommandArgument = Sql.ToString(rdr[sARGUMENT_FIELD]);
							}
						}

						btn.Text            = "  " + L10n.Term(sCONTROL_TEXT) + "  ";
						btn.CssClass        = sCONTROL_CSSCLASS;
						btn.Command        += Page_Command;
						btn.CommandName     = sCOMMAND_NAME;
						btn.OnClientClick   = sONCLICK_SCRIPT;
						// 11/21/2008   On post back, we need to re-create the buttons, but don't change the visiblity flag. 
						// The problem is that we don't have the record at this early stage, so we cannot properly evaluate gASSIGNED_USER_ID. 
						// This is not an issue because .NET will restore the previous visibility state on post back. 
						if ( !bIsPostBack )
						{
							// 07/28/2010   We need a flag to exclude a button from a mobile device. 
							// 03/14/2014   Allow hidden buttons to be created. 
							btn.Visible         = (!bEXCLUDE_MOBILE || !bIsMobile) && (bMOBILE_ONLY && bIsMobile || !bMOBILE_ONLY) && (bADMIN_ONLY && Security.isAdmin || !bADMIN_ONLY) && !bHIDDEN;
							if ( btn.Visible && !Sql.IsEmptyString(sMODULE_NAME) && !Sql.IsEmptyString(sMODULE_ACCESS_TYPE) )
							{
								int nACLACCESS = Taoqi.Security.GetUserAccess(sMODULE_NAME, sMODULE_ACCESS_TYPE);
								// 08/11/2008 John.  Fix owner access rights. 
								// 10/27/2008 Brian.  Only show button if show_unassigned is enabled.
								// 11/21/2008   We need to make sure that an owner can create a new record. 
								btn.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								if ( btn.Visible && !Sql.IsEmptyString(sTARGET_NAME) && !Sql.IsEmptyString(sTARGET_ACCESS_TYPE) )
								{
									// 08/11/2008 John.  Fix owner access rights.
									nACLACCESS = Taoqi.Security.GetUserAccess(sTARGET_NAME, sTARGET_ACCESS_TYPE);
									// 11/21/2008   We need to make sure that an owner can create a new record. 
									btn.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								}
							}
						}
						if ( !Sql.IsEmptyString(sCONTROL_ACCESSKEY) )
						{
							btn.AccessKey = L10n.AccessKey(sCONTROL_ACCESSKEY);
						}
						if ( !Sql.IsEmptyString(sCONTROL_TOOLTIP) )
						{
							btn.ToolTip = L10n.Term (sCONTROL_TOOLTIP);
							if ( btn.ToolTip.Contains("[Alt]") )
							{
								if ( btn.AccessKey.Length > 0 )
									btn.ToolTip = btn.ToolTip.Replace("[Alt]", "[Alt+" + btn.AccessKey + "]");
								else
									btn.ToolTip = btn.ToolTip.Replace("[Alt]", String.Empty);
							}
						}
						btn.Attributes.Add("style", "margin-right: 3px;");
					}
					else if ( String.Compare(sCONTROL_TYPE, "HyperLink", true) == 0 )
					{
						HyperLink lnk = new HyperLink();
						ctl.Controls.Add(lnk);
						if ( !Sql.IsEmptyString(sCONTROL_ID) )
							lnk.ID          = sCONTROL_ID;
						lnk.Text        = L10n.Term(sCONTROL_TEXT);
						lnk.NavigateUrl = String.Format(sURL_FORMAT, objTEXT_FIELD);
						lnk.Target      = sURL_TARGET;
						lnk.CssClass    = sCONTROL_CSSCLASS;
						// 11/21/2008   On post back, we need to re-create the buttons, but don't change the visiblity flag. 
						// The problem is that we don't have the record at this early stage, so we cannot properly evaluate gASSIGNED_USER_ID. 
						// Not setting the visibility flag is not an issue because .NET will restore the previous visibility state on post back. 
						if ( !bIsPostBack )
						{
							// 07/28/2010   We need a flag to exclude a button from a mobile device. 
							// 03/14/2014   Allow hidden buttons to be created. 
							lnk.Visible     = (!bEXCLUDE_MOBILE || !bIsMobile) && (bMOBILE_ONLY && bIsMobile || !bMOBILE_ONLY) && (bADMIN_ONLY && Security.isAdmin || !bADMIN_ONLY) && !bHIDDEN;
							if ( lnk.Visible && !Sql.IsEmptyString(sMODULE_NAME) && !Sql.IsEmptyString(sMODULE_ACCESS_TYPE) )
							{
								int nACLACCESS = Taoqi.Security.GetUserAccess(sMODULE_NAME, sMODULE_ACCESS_TYPE);
								// 08/11/2008 John.  Fix owner access rights.
								// 10/27/2008 Brian.  Only show button if show_unassigned is enabled.
								// 11/21/2008   We need to make sure that an owner can create a new record. 
								lnk.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								if ( lnk.Visible && !Sql.IsEmptyString(sTARGET_NAME) && !Sql.IsEmptyString(sTARGET_ACCESS_TYPE) )
								{
									// 08/11/2008 John.  Fix owner access rights.
									nACLACCESS = Taoqi.Security.GetUserAccess(sTARGET_NAME, sTARGET_ACCESS_TYPE);
									// 10/27/2008 Brian.  Only show button if show_unassigned is enabled.
									// 11/21/2008   We need to make sure that an owner can create a new record. 
									lnk.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								}
							}
						}
						if ( !Sql.IsEmptyString(sONCLICK_SCRIPT) )
						{
							lnk.Attributes.Add("onclick", sONCLICK_SCRIPT);
						}
						if ( !Sql.IsEmptyString(sCONTROL_ACCESSKEY) )
						{
							lnk.AccessKey = L10n.AccessKey(sCONTROL_ACCESSKEY);
						}
						if ( !Sql.IsEmptyString(sCONTROL_TOOLTIP) )
						{
							lnk.ToolTip = L10n.Term(sCONTROL_TOOLTIP);
							if ( lnk.ToolTip.Contains("[Alt]") )
							{
								if ( lnk.AccessKey.Length > 0 )
									lnk.ToolTip = lnk.ToolTip.Replace("[Alt]", "[Alt+" + lnk.AccessKey + "]");
								else
									lnk.ToolTip = lnk.ToolTip.Replace("[Alt]", String.Empty);
							}
						}
						// 04/04/2008   Links need additional spacing.
						lnk.Attributes.Add("style", "margin-right: 3px; margin-left: 3px;");
					}
					else if ( String.Compare(sCONTROL_TYPE, "ButtonLink", true) == 0 )
					{
						Button btn = new Button();
						ctl.Controls.Add(btn);
						if ( !Sql.IsEmptyString(sCONTROL_ID) )
							btn.ID              = sCONTROL_ID;
						btn.Text            = "  " + L10n.Term(sCONTROL_TEXT) + "  ";
						btn.CssClass        = sCONTROL_CSSCLASS;
						// 03/21/2008   Keep the command just in case we are in a browser that does not support javascript. 
						btn.Command        += Page_Command;
						btn.CommandName     = sCOMMAND_NAME;
						// 08/22/2010   Provide a way to override the default URL behavior and run javascript. 
						if ( !Sql.IsEmptyString(sONCLICK_SCRIPT) )
							btn.OnClientClick   = String.Format(sONCLICK_SCRIPT, objTEXT_FIELD);
						else
							btn.OnClientClick   = "window.location.href='" + Sql.EscapeJavaScript(String.Format(sURL_FORMAT, objTEXT_FIELD)) + "'; return false;";
						// 11/21/2008   On post back, we need to re-create the buttons, but don't change the visiblity flag. 
						// The problem is that we don't have the record at this early stage, so we cannot properly evaluate gASSIGNED_USER_ID. 
						// Not setting the visibility flag is not an issue because .NET will restore the previous visibility state on post back. 
						if ( !bIsPostBack )
						{
							// 07/28/2010   We need a flag to exclude a button from a mobile device. 
							// 03/14/2014   Allow hidden buttons to be created. 
							btn.Visible     = (!bEXCLUDE_MOBILE || !bIsMobile) && (bMOBILE_ONLY && bIsMobile || !bMOBILE_ONLY) && (bADMIN_ONLY && Security.isAdmin || !bADMIN_ONLY) && !bHIDDEN;
							if ( btn.Visible && !Sql.IsEmptyString(sMODULE_NAME) && !Sql.IsEmptyString(sMODULE_ACCESS_TYPE) )
							{
								int nACLACCESS = Taoqi.Security.GetUserAccess(sMODULE_NAME, sMODULE_ACCESS_TYPE);
								// 08/11/2008 John.  Fix owner access rights.
								// 10/27/2008 Brian.  Only show button if show_unassigned is enabled.
								// 11/21/2008   We need to make sure that an owner can create a new record. 
								btn.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								if ( btn.Visible && !Sql.IsEmptyString(sTARGET_NAME) && !Sql.IsEmptyString(sTARGET_ACCESS_TYPE) )
								{
									// 08/11/2008 John.  Fix owner access rights.
									nACLACCESS = Taoqi.Security.GetUserAccess(sTARGET_NAME, sTARGET_ACCESS_TYPE);
									// 10/27/2008 Brian.  Only show button if show_unassigned is enabled.
									// 11/21/2008   We need to make sure that an owner can create a new record. 
									btn.Visible = (nACLACCESS > ACL_ACCESS.OWNER) || (nACLACCESS == ACL_ACCESS.OWNER && ((Security.USER_ID == gASSIGNED_USER_ID) || (!bIsPostBack && rdr == null) || (rdr != null && bShowUnassigned && Sql.IsEmptyGuid(gASSIGNED_USER_ID))));
								}
							}
						}
						if ( !Sql.IsEmptyString(sCONTROL_ACCESSKEY) )
						{
							btn.AccessKey = L10n.AccessKey(sCONTROL_ACCESSKEY);
						}
						if ( !Sql.IsEmptyString(sCONTROL_TOOLTIP) )
						{
							btn.ToolTip = L10n.Term (sCONTROL_TOOLTIP);
							if ( btn.ToolTip.Contains("[Alt]") )
							{
								if ( btn.AccessKey.Length > 0 )
									btn.ToolTip = btn.ToolTip.Replace("[Alt]", "[Alt+" + btn.AccessKey + "]");
								else
									btn.ToolTip = btn.ToolTip.Replace("[Alt]", String.Empty);
							}
						}
						btn.Attributes.Add("style", "margin-right: 3px;");
					}
				}
			}
		}