USE [site_fuchs] GO /****** Object: StoredProcedure [dbo].[ocms_createView] Script Date: 02.12.2020 21:05:05 ******/ DROP PROCEDURE [dbo].[ocms_createView] GO /****** Object: StoredProcedure [dbo].[ocms_createView] Script Date: 02.12.2020 21:05:06 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: -- Create date: -- Description: -- ============================================= CREATE PROCEDURE [dbo].[ocms_createView] @name nvarchar(255), @parent_iid bigint AS BEGIN SET NOCOUNT ON; DECLARE @site bigint = (SELECT TOP(1) [ocms_iid] FROM [dbo].[ocms_items] as i WHERE i.[parent_iid] is null); DECLARE @template_id bigint = (SELECT TOP(1) [ocms_tid] FROM [dbo].[ocms_templates] as t WHERE t.[name] = 'view'); IF ISNULL(@name, '') = '' THROW 61010, 'name missing', 1; IF EXISTS (SELECT * FROM [dbo].[ocms_items] WHERE [name] = @name) THROW 61110, 'name already exists', 1; IF @site is null THROW 61110, 'site does not exist', 1; SELECT @parent_iid = [ocms_iid] FROM [dbo].[ocms_items] as i WHERE [ocms_iid] = @parent_iid AND (i.[parent_iid] is null OR i.[view] = 1); --apply site as fallback SET @parent_iid = ISNULL(@parent_iid, @site); --get last ocms_iid DECLARE @lastiid int = ISNULL ( (SELECT MAX(ISNULL(p.[ocms_iid], ap.[ocms_iid])) FROM [dbo].[ocms_items] as p FULL OUTER JOIN [ocms_archive__items] as ap on p.[ocms_iid] = ap.[ocms_iid]) ,0); -- get locales setting DECLARE @locales TABLE([locale] varchar(5), [order] int); INSERT INTO @locales SELECT * FROM [dbo].[ocms_locales](); -- add item DECLARE @output TABLE([ocms_iid] bigint); DECLARE @ocms_iid bigint; MERGE [dbo].[ocms_items] as TARGET USING (VALUES( @site ,NULL ,@name ,1 ,@template_id ,0 ,999 -- append always )) as SOURCE ([parent_iid] ,[ocms_pid] ,[name] ,[view] ,[template_id] ,[hide] ,[order]) ON TARGET.[name] = SOURCE.[name] WHEN NOT MATCHED BY TARGET THEN INSERT ([ocms_iid] ,[parent_iid] ,[ocms_pid] ,[name] ,[view] ,[template_id] ,[hide] ,[order] ,[DateCreated] ,[DateModified]) VALUES(@lastiid + 1, SOURCE.[parent_iid], SOURCE.[ocms_pid], SOURCE.[name], SOURCE.[view], SOURCE.[template_id], SOURCE.[hide], SOURCE.[order], GETUTCDATE(), GETUTCDATE()) OUTPUT inserted.[ocms_iid] INTO @output; SELECT TOP(1) @ocms_iid = [ocms_iid] FROM @output; -- add navigation -- update nav table UPDATE n SET n.[parent_iid] = i.[parent_iid] FROM [dbo].[ocms_navigation] as n JOIN [ocms_items] as i ON n.[ocms_iid] = i.[ocms_iid]; DECLARE @newnav [dbo].[ocms_type_navigation_base]; DECLARE @suffix int = 0; INSERT INTO @newnav SELECT @ocms_iid, [locale], @name + '_' + [locale] FROM @locales as l WHERE l.[locale] <> '*'; WHILE EXISTS (SELECT * FROM [dbo].[ocms_navigation] as cn JOIN @newnav as nn ON cn.[parent_iid] = @parent_iid AND cn.[alias] = nn.[alias]) BEGIN SET @suffix = @suffix + 1; UPDATE @newnav SET [alias] = @name + '_' + [locale] + CAST(@suffix as varchar(3)); END EXECUTE [dbo].[ocms_merge_navigation] @newnav; --output SELECT @ocms_iid; END GO ALTER AUTHORIZATION ON [dbo].[ocms_createView] TO SCHEMA OWNER GO