dbo.mg_menu_tree

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

Name Data Type Length Description
@PARAMETER varchar -1

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.mg_menu_tree
@PARAMETER varchar(max)='1,0' ---roleid , publish
--with encryption
AS
set nocount on
 SET ANSI_NULLS, QUOTED_IDENTIFIER ON

declare @_list_id_available table ( 
app_menu_id int primary key not null
,pattern_id int
,name_title varchar(255)
,link_address varchar(255)
,icon_name varchar(255)
,menu_type varchar(255)
,level_row_id int
,child_count int
,children varchar(max)
,ipage varchar(255)
,iJdl varchar(255) 
,order_row int
,selected bit
 )

declare @level_row_id int
declare @habis bit
declare @max_level_row int
  
  set @level_row_id=0;  

  insert into @_list_id_available  

  SELECT        
     id * -1
  , 0
  , title
  , ''
  , 'windows.gif'
  , 'R'
  , @level_row_id
  ,  (SELECT  COUNT(*)       
   FROM            ccs_menu
   WHERE        (ccs_menu.menutype = ccs_menu_types.id) AND (published = 1) AND (parent_id = 0))
    as child_count 
  ,''
  ,''
  ,''
  ,[order]
  ,0
   FROM            
  ccs_menu_types  
  where ccs_menu_types.published_menutype=1
    order  by ccs_menu_types.[order]

  set @level_row_id=1

  insert into @_list_id_available   
  SELECT        
     id 
  , menutype * -1
   , title + case when nullif(ltrim(rtrim(params)),'') is null then '' else ' (' + ltrim(rtrim(params))+ ')' end as title
  , case when note='folder' then '' else link end
  , img
  , case when note='folder' then 'SF' else 'L' end 
  , @level_row_id
  ,  (SELECT  COUNT(*)       
   FROM            ccs_menu as submenu
   WHERE        (submenu.parent_id = xmenu.id) AND (published = 1) )
    as child_count 
  ,''
  ,''
  ,params
  ,ordering
  ,0
   FROM            
  ccs_menu  as xmenu
  where  exists(
  select * from @_list_id_available as xfilter
  where xmenu.menutype=xfilter.app_menu_id*-1
  ) and    (published = 1) AND (parent_id = 0)
    order  by ordering 


  set @habis=case when (select count(*) from @_list_id_available where level_row_id=@level_row_id )=0 then 1 else 0 end
    while @habis=0
  begin  
   set @level_row_id=@level_row_id+1;   
   insert into @_list_id_available 
   SELECT        
     id
   , parent_id
   , title + case when nullif(ltrim(rtrim(params)),'') is null then '' else ' (' + ltrim(rtrim(params))+ ')' end as title
   , case when note='folder' then '' else link end
   , img
   , case when note='folder' then 'SF' else 'L' end 
 
   , @level_row_id
   , case when  note='folder' then  (

    SELECT  COUNT(*)       
    FROM            ccs_menu as submenu
    WHERE        (submenu.parent_id = xmenu.id) AND (published = 1)

     ) else 0 end  as child_count 
   ,''
   ,''
   ,params
   ,ordering
   ,0
   FROM                
   ccs_menu  as xmenu
   WHERE     
    exists(select app_menu_id from (select app_menu_id from @_list_id_available where level_row_id=@level_row_id-1) as xfilmenu where xmenu.parent_id=xfilmenu.app_menu_id)
    order  by xmenu.ordering 
 
   set @habis=case when (select count(*) from @_list_id_available where level_row_id=@level_row_id )=0 then 1 else 0 end
   end 
       delete from @_list_id_available where (menu_type='R' or menu_type='SF') and child_count=0

    

declare @a varchar(max) 
select @a=  perm_menutype  from ccs_permissions
where role_id=@PARAMETER
set @a=nullif(ltrim(rtrim(@a)),'') 
if @a is not null
begin  
 update @_list_id_available
 set selected =1
 where exists ( 
  select cast(Data as int) from dbo.split( @a,',') as x
  where [@_list_id_available].app_menu_id =x.Data  
 )
end
    
  while @level_row_id>-1
  begin 
  update @_list_id_available
  set children=isnull(('[' + STUFF(( 
      SELECT  ',{"pid":"'  + dbo.JSONEscaped(x.app_menu_id) + '"'
       +  ',"key":'    + dbo.JSONEscaped(app_menu_id)
      + ',"title":"' + dbo.JSONEscaped(x.name_title) + '"'
       +  case when x.menu_type='SF' or x.menu_type='R' then ',"isFolder":true' else '' end 
      +  case when x.menu_type='SF' or x.menu_type='R' then ',"expand":true'  else '' end 
       +  case when x.menu_type='SF' or x.menu_type='R' then   '' else ',"url":"'  + dbo.JSONEscaped(isnull(x.link_address,'')) + '"' end 
       +  case when x.menu_type='SF' or x.menu_type='R' then   '' else case when x.selected=1 then  ',"select":true' else '' end end 
      
      + ',"icon":"' + dbo.JSONEscaped(  x.icon_name) + '"'
      + ',"tooltip":"' + dbo.JSONEscaped(x.iJdl) + '"'
      + ',"treetype":"' + dbo.JSONEscaped(x.menu_type) + '"'
       +  case when x.menu_type='SF' or x.menu_type='R'  then ',"children":' + x.children  else '' end 
      + '}'
       from @_list_id_available as x where x.pattern_id =[@_list_id_available].app_menu_id
       for xml  PATH(''), type
      ).value('.', 'varchar(max)'), 1, 1, '') + ']')  ,'')
  where ([@_list_id_available].menu_type ='F' or [@_list_id_available].menu_type ='SF' or [@_list_id_available].menu_type ='R') and [@_list_id_available].level_row_id=@level_row_id
  set @level_row_id=@level_row_id-1
   -- select * from @_list_id_available
   --where ([@_list_id_available].menu_type ='F' or [@_list_id_available].menu_type ='SF' or [@_list_id_available].menu_type ='R') and [@_list_id_available].level_row_id=@level_row_id
 end
  
  delete from @_list_id_available where not(pattern_id=0)

   
declare @rowJSON varchar(max)

set @rowJSON=('[' + STUFF(( 
 SELECT  ',{"pid":"' + dbo.JSONEscaped(app_menu_id)
 + '","title":"' + dbo.JSONEscaped(name_title)
 +  '","isFolder":true'   
 +  ',"expand":true'   
 +  ',"key":'    + dbo.JSONEscaped(app_menu_id)
+ ',"icon":"' +  ISNULL(icon_name, '' )
 + '","children":' + children  
+ '}'
 FROM @_list_id_available 
 order by order_row
for xml  PATH(''), type
).value('.', 'varchar(max)'), 1, 1, '') + ']')  

SET NOCOUNT OFF
select @rowJSON as treedata

GO

Used By

No items found