﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION [dbo].[fis_getONEPersonWidgets]
(	
	@useraccount_id varchar(10)
)
RETURNS @RET TABLE 
(
	[short_name] [varchar](50) NOT NULL,
	[account] [varchar](50) NOT NULL,
	[type] [nvarchar](50) NOT NULL,
	[name] [nvarchar](255) NOT NULL,
	[description] [nvarchar](1000) NULL,
	[sql] [nvarchar](2000) NULL,
	[sql_admin_columns] [nvarchar](500) NULL,
	[sql_datatypes] [nvarchar](255) NULL,
	[url] [nvarchar](500) NULL,
	[html] [nvarchar](max) NULL,
	[visible_groups] [varchar](1000) NULL,
	[visible] [tinyint] NOT NULL,
	[auth_module] [varchar](50) NULL,
	[rendering_options] [nvarchar](255) NULL,
	[server_options] [nvarchar](2000) NULL,
	[daycache] [bit] NOT NULL,
	[personalized] [bit] NOT NULL,
	[safemode] [bit] NOT NULL,
	[variables] [varchar](1000) NULL,
	[order] int
)
AS
BEGIN
	--DECLARE @account varchar(50) = '';   -- kept in function for reuse purposes
	

	INSERT INTO @RET
	SELECT w.*
			, ROW_NUMBER() OVER (ORDER BY s.[display_order], s.[DateCreated] DESC, w.[name]) as 'order'
		FROM [dbo].[fis_widgets] as w JOIN [dbo].[fis_widget_subscriptions] as s ON w.[short_name] = s.[widget_name]
		WHERE --(s.[account] = @account OR s.[account] = '*')
			(s.[person_guid] =  @useraccount_id OR s.[person_guid] = '*')
			AND (ISNULL(w.[visible],0) = 0 OR ([dbo].[fis_getModuleAuth](ISNULL(w.[auth_module], 'fis_widgets'), @useraccount_id) >= w.[visible]))
	--UNION
	--	SELECT * FROM [dbo].[fis_getONEPersonWidgets_ownevents](@account, @person_guid)
	
	RETURN 
END