dbo.mg_menu_tree_acc

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_acc
@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
,menu_type2 varchar(255)
 )

declare @level_row_id int
declare @habis bit
declare @max_level_row int
  
declare @menu_active table (id int primary key)
declare @role_id int
SELECT   top 1 @role_id=     role_id
FROM            users
WHERE        (id = @PARAMETER)
  
declare @a varchar(max) 
select @a=  perm_menutype  from ccs_permissions
--where id=@role_id
where role_id=@role_id
set @a=nullif(ltrim(rtrim(@a)),'') 


if @PARAMETER='9999'
begin
insert into @menu_active  
 select id from ccs_menu 
end
else
begin 
 if @a is not null
 begin 
  insert into @menu_active 
  select cast(Data as int) from dbo.split( @a,',') as x  
 end  
 insert into @menu_active  
 select parent_id from (
 SELECT       case when  parent_id=0 then menutype *-1 else parent_id end as parent_id
 FROM            ccs_menu
 WHERE    
 parent_id is not null and
 exists (select * from @menu_active as xchild where ccs_menu.id=xchild.id  )
 )xdata 
 where 
   not exists  (select * from @menu_active as xchild2 where xdata.parent_id=xchild2.id  )
 group by parent_id

end



 

while  (@@ROWCOUNT >0)
BEGIN  

 insert into @menu_active  
 select parent_id from (
 SELECT       case when  parent_id=0 then menutype *-1 else parent_id end as parent_id
 FROM            ccs_menu
 WHERE   
 parent_id is not null and 
 exists (select * from @menu_active as xchild where ccs_menu.id=xchild.id  )
 )xdata 
 where 
   not exists  (select * from @menu_active as xchild2 where xdata.parent_id=xchild2.id  )
 group by parent_id


end
 
--select * from @menu_active

  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]
  ,menutype
   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
  ,''
   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)
    and exists(select * from @menu_active as xfil4 where xmenu.id=xfil4.id)
  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
   ,''
   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)
    and exists(select * from @menu_active as xfil4 where xmenu.id=xfil4.id)
    and xmenu.published =1
    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
 
    
  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 
      
      + ',"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
       order  by order_row ,x.name_title
       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
  end
   
   delete from @_list_id_available where not(pattern_id=0) or children =''

   
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 * from @_list_id_available

 order by order_row
GO

Used By

No items found