SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

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



CREATE PROCEDURE [forums_system_Import_Forum]
	@ForumID [int],
	@ForumGroupID [int] = 0,
	@ParentID [int] = 0,
	@DateCreated [datetime],
	@IsActive [bit] = 0,
	@IsModerated [bit] = 1,
	@SortOrder [int] = 0,
	@Name [nvarchar](256) = '',
	@Description [nvarchar](3000) = ''
AS
BEGIN

Declare @RoleID uniqueidentifier
SET @RoleID = NULL
Select Top 1 @RoleID = RoleID FROM aspnet_roles where LoweredRoleName = 'everyone'
if(@RoleID is null)
RETURN

IF EXISTS(SELECT * FROM forums_Forums WHERE ForumID = @ForumID)
	-- Update the forum information
	UPDATE 
		forums_Forums 
	SET
		Name = @Name,
		Description = @Description,
		ParentID = @ParentID,
		DateCreated = @DateCreated,
		ForumGroupID = @ForumGroupID,
		IsModerated = @IsModerated,
		IsActive = @IsActive,
		SortOrder = @SortOrder
	WHERE 
		ForumID = @ForumID
ELSE
BEGIN
	SET IDENTITY_INSERT forums_Forums ON
	INSERT INTO 
		forums_Forums (
			ForumID,
			ForumGroupID, 
			ParentID, 
			DateCreated,
			IsActive,
			IsModerated,
			SortOrder, 
			Name, 
			Description
			)
		VALUES (
			@ForumID,
			@ForumGroupID,
			@ParentID,
			@DateCreated,
			@IsActive,
			@IsModerated,
			@SortOrder,
			@Name,
			@Description
			)
	SET IDENTITY_INSERT forums_Forums OFF

	INSERT INTO 
		forums_ForumPermissions 
		(ForumID, RoleID, [View], [Read], Post, Reply, Edit, [Delete], Sticky, Announce, CreatePoll, Vote, Moderate, Attachment)
	VALUES
		(@ForumID, @RoleID, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0)

END

END



GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

