Files
Fuchs_Intranet/db/dbo.ocms_getFullTree.StoredProcedure.sql
T

131 lines
7.8 KiB
Transact-SQL

USE [site_fuchs]
GO
/****** Object: StoredProcedure [dbo].[ocms_getFullTree] Script Date: 02.12.2020 21:05:05 ******/
DROP PROCEDURE [dbo].[ocms_getFullTree]
GO
/****** Object: StoredProcedure [dbo].[ocms_getFullTree] Script Date: 02.12.2020 21:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ocms_getFullTree]
@locale varchar(5)
AS
BEGIN
/* FULL TREE is only intended for OCMS admin portal
Hidden items will be shown !
*/
SET NOCOUNT ON;
-- get basics
DECLARE @site bigint = (SELECT TOP(1) [ocms_iid] FROM [dbo].[ocms_items] as i WHERE i.[parent_iid] is null);
IF @site is null
THROW 61110, 'site does not exist', 1;
--get locales
DECLARE @locales TABLE([locale] varchar(5), [order] int);
INSERT INTO @locales
SELECT * FROM [dbo].[ocms_locales]();
-- calculate tree and paths
DECLARE @itemtree TABLE(
[ocms_iid] [bigint] NOT NULL,
[parent_iid] [bigint] NULL,
[ocms_pid] [bigint] NULL,
[name] [nvarchar](255) NULL,
[view] [bit] NOT NULL,
[template_id] [bigint] NULL,
[hide] [bit] NOT NULL,
[order] [int] NOT NULL,
[DateCreated] datetime null,
[DateModified] datetime null,
[level] int, [new_order] int
);
WITH cteLevels as (
SELECT t.*
, cast(0 as int) as [level]
FROM [dbo].[ocms_items] t
WHERE t.[ocms_iid] = @site
UNION ALL
SELECT t.*
, CAST(c.[level] + 1 as int) as [level]
FROM [dbo].[ocms_items] t
inner JOIN cteLevels c on t.[parent_iid] = c.[ocms_iid]
)
INSERT INTO @itemtree
SELECT t.*
, ROW_NUMBER() OVER (PARTITION BY [parent_iid] ORDER BY [order], [ocms_iid]) as [new_order]
FROM cteLevels as t;
--WHERE [hide] = 0;
-- output site properties
WITH SP As (
SELECT
p.*
, ROW_NUMBER() OVER (PARTITION BY p.[ocms_iid], p.[key] ORDER BY CAST((CASE WHEN p.[locale] = '*' THEN 1 ELSE 0 END) as int)) as 'rwi'
FROM @itemtree as it
JOIN [dbo].[ocms_properties] as p ON it.[ocms_iid] = p.[ocms_iid]
WHERE it.[level] = 0 --site
AND (p.[locale] = @locale OR p.[locale] = '*')
)
SELECT
it.[ocms_iid]
,p.[ocms_pid]
,ISNULL(tp.[key], p.[key]) as [key]
,IIF(ISNULL(tp.[mandatory],0) = 0, p.[value], ISNULL(p.[value],'')) as [value]
,tp.[type]
FROM @itemtree as it
FULL OUTER JOIN [dbo].[ocms_template_properties] as tp ON it.[template_id] = tp.[ocms_tid]
FULL OUTER JOIN SP as p ON p.[ocms_iid] = it.[ocms_iid] AND (tp.[key] = p.[key] OR tp.[key] is null)
AND p.[rwi] = 1 -- only the right language or fallback
WHERE it.[level] = 0;
-- output locales
SELECT * FROM @locales;
-- output children
SELECT
it.*
,p.[key] as 'property_key'
,p.[locale] as 'property_locale'
,tpl.[name] as 'template_name'
,ROW_NUMBER() OVER (PARTITION BY it.[parent_iid] ORDER BY IIF(it.[view] = 1, 0, 1), it.[ocms_pid], it.[ORDER], it.[ocms_iid]) as 'item_order'
FROM @itemtree as it
LEFT JOIN [dbo].[ocms_templates] as tpl on it.[template_id] = tpl.[ocms_tid]
LEFT JOIN [dbo].[ocms_properties] as p on it.[ocms_pid] = p.[ocms_pid]; --only arrays
-- output childrens' properties ONLY arrays
SELECT
it.[ocms_iid]
,p.[ocms_pid]
,ISNULL(tp.[key], p.[key]) as [key]
,IIF(ISNULL(tp.[mandatory],0) = 0, p.[value], ISNULL(p.[value],'')) as [value]
,p.locale
,tp.[type]
FROM @itemtree as it
JOIN [dbo].[ocms_template_properties] as tp ON it.[template_id] = tp.[ocms_tid]
FULL OUTER JOIN [dbo].[ocms_properties] as p ON p.[ocms_iid] = it.[ocms_iid] AND (tp.[key] = p.[key] OR tp.[key] is null)
WHERE ISNULL(ISNULL(tp.[key], p.[key]),'') <> ''
AND tp.[type] = 'array';
END
GO
ALTER AUTHORIZATION ON [dbo].[ocms_getFullTree] TO SCHEMA OWNER
GO