SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[cs_Sections_Get]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[cs_Sections_Get]
GO


CREATE PROCEDURE [dbo].cs_Sections_Get
(
	@SettingsID				int,
	@ApplicationType        smallint = 0,
	@AllowInactive			bit = 0,
	@IncludePostTypeCounts	bit = 0
)
AS
SET Transaction Isolation Level Read UNCOMMITTED
-- Return all the forums
SELECT  
	F.SectionID, F.SettingsID, F.IsActive, F.ParentID, F.GroupID, F.Name, F.NewsgroupName, F.Description, F.DateCreated, F.Url, F.IsModerated,
	F.DaysToView, F.SortOrder, F.TotalPosts, F.TotalThreads, F.DisplayMask, F.EnablePostStatistics, F.EnablePostPoints, F.EnableAutoDelete, F.EnableAnonymousPosting,
	F.AutoDeleteThreshold, F.MostRecentPostID, F.MostRecentThreadID, F.MostRecentThreadReplies, F.MostRecentPostSubject, F.MostRecentPostAuthor,
	F.MostRecentPostAuthorID, F.MostRecentPostDate, F.PostsToModerate, F.ForumType, F.IsSearchable, F.ApplicationType, F.ApplicationKey, F.Path,
	F.PropertyNames as SectionPropertyNames, F.PropertyValues as SectionPropertyValues, F.DefaultLanguage, F.DiskUsage

FROM
	cs_Sections F
WHERE
	SettingsID = @SettingsID AND
	--(IsActive = 1 OR 1 = @AllowInactive) AND
	 ApplicationType = @ApplicationType

--Declare @AnonID int
--exec cs_GetAnonymousUserID @SettingsID, @AnonID output

exec cs_Section_Permissions_Get @SettingsID, 0, @ApplicationType

--Return the counts of each post type
IF @IncludePostTypeCounts > 0
BEGIN
	SELECT 
		F.ApplicationKey, P.ApplicationPostType, Count(*) as PostCount
	FROM 
		cs_Sections F
		JOIN cs_Posts P on F.SectionID = P.SectionID
	WHERE
		F.SettingsID = @SettingsID AND
		F.ApplicationType = @ApplicationType
	GROUP BY F.ApplicationKey, P.ApplicationPostType
END
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT  EXECUTE  ON [dbo].[cs_Sections_Get]  TO [public]
GO

