<%@ language="SQLRPGLE" sqlopt="COMMIT(*NONE) SRTSEQ(*LANGIDUNQ)" modopt="SRTSEQ(*LANGIDUNQ)" %>
<%
H DECEDIT('0,') DATEDIT(*YMD.)  COPYRIGHT('System og Metode (c), 2007')
H TEXT('mnu: Build menu per User (JSON)')
H BNDDIR('MNUUTL')
H BNDDIR('ICEUTILS')
 * ------------------------------------------------------------------------------------ *
 * Program ....: mnu999l
 * Function ...: Build User Menu
 * Author .....: Claus Rytter Larsen, System og Metode Aps - December 2007
 * ------------------------------------------------------------------------------------ *
FMNUMNUL3  IF   E           K Disk    Rename(MNUMNU0:MNUMNU3) prefix(x)
FMNUMNUL4  IF   E           K Disk    Rename(MNUMNU0:MNUMNU4)
FMNUGMRL1  IF   E           K Disk    Rename(MNUGMR0:MNUGMR1)
FMNUUSRL1  IF   E           K Disk    Rename(MNUUSR0:MNUUSR1)
FMNULNGL1  IF   E           K Disk    Rename(MNULNG0:MNULNG1)
 * ------------------------------------------------------------------------------------ *
/include qasphdr,iceUtils
 * ------------------------------------------------------------------------------------ *
d wMNUMTX       e ds                  extname(MNUMTX)

d wSYSID          s                   like(MNUSYSID)
d userId          s              5  0
d len             s              3  0
d node            s              9  0
d path            s             24    varying
d search          s            256    varying
d sqlCmd          s           5000    varying
d initCmd         s            512    varying
d sgvpath         s            128    varying
d tabClosable     s              5    varying
d criticalAppl    s              3
d menuGroup       s              5    varying
d hideChildren    s              5    varying
d closeAllChildr  s              5    varying
d openInWindow    s              5    varying
d windowWidth     s             10i 0
d windowHeight    s             10i 0
d openInNewWin    s              5    varying
d server          s             10

d sep             s              1
d leaf            s              5    varying
d added           s               n

d i               s              5  0
d mnuUserClass    s              3  0
d mnuLanguage     s              3
d userGroup       s              3  0 
d userGroups      s            256    varying
d icon            s            256    varying
d text            s            256    varying

d buildMenu       pr
d                                5  0 value
d checkChildren   pr              n
d                                5  0
d addMenu         pr
 * ------------------------------------------------------------------------------------ *
 /Include qSrc,mnuUtlP
 * ------------------------------------------------------------------------------------ *
 * Initialize
 * ------------------------------------------------------------------------------------ *
/free
  SetContentType('text/html; charset=windows-1252');

  node   = formNum('node');
  path   = form('path');
  search = form('search');
  wSYSID = formnum('system');

  len = %len(%trim(path));
  select;
  when len = 0;
    path = %trim(path);
  when %subst(%trim(path):len:1) = '/';
    path = %subst(%trim(path):1:len-1);
  other;
    path = %trim(path);
  endsl;

  userid       = sesGetVarNum('sys.usr.ID');
  userGroups   = sesGetVar('sys.usr.group');
  mnuUserClass = sesGetVarNum('sys.usr.class');
  mnuLanguage  = sesGetVar('sys.usr.language');
  chain mnuLanguage MNULNG1;
  if not %found;
    mnuLanguage = 'dft';
  endif;

  // get user
  chain userid MNUUSR1;
  
  server = GetServerVar('SERVER_ID');
  initCmd = getInitCmd(server:wSYSID);

%>[<%

  sep = '';

  if search <> '';
    exsr sqlOpn;
    exsr sqlFetch;
    dow (sqlCod = 0);
      addMenu();
      %>}<%
      exsr sqlFetch;
    enddo;
    exsr sqlClose;
  else;
    buildMenu(node);
  endif;

%>]<%

  return;
 // -------------------------------------------------------------------------- *
 // Open sql cursor
 // -------------------------------------------------------------------------- *
begsr sqlOpn;
  sqlCmd  = 'Select MNUID,MTXTXT,MNUUCLID,MNUPTH,MNULNK,'
          + ' MNUSELICO'
          + ' from MNUMTX'
          + ' inner join MNUMNU'
          + ' on  MNUID    = MTXMNUID'
          + ' and MNUSYSID = ' + %char(wSYSID)
          + ' where MTXLNGID = ''' + mnuLanguage + ''''
          + ' and upper(MTXTXT) like ''%'
          + %trim(uppercase(search)) + '%'''
          + ' order by MTXTXT';
/end-free
c/Exec SQL
c+    Prepare pList from :sqlCmd
c/End-Exec
/free
/end-free
C/Exec SQL
c+    Declare List cursor for pList
c/End-Exec
/free
/end-free
c/Exec SQL
c+     Open List
c/End-Exec
/free
endsr;
 // -------------------------------------------------------------------------- *
 // Fetch row fom sql cursor
 // -------------------------------------------------------------------------- *
begsr sqlFetch;
/end-free
c/Exec SQL
c+      Fetch list into :MNUID, :MTXTXT, :MNUUCLID, :MNUPTH,
                        :MNULNK, :MNUSELICO
c/End-Exec
/free
endsr;
 // -------------------------------------------------------------------------- *
 // Close sql cursor
 // -------------------------------------------------------------------------- *
begsr sqlClose;
/end-free
c/Exec SQL
c+      Close list
c/End-Exec
/free
endsr;
/end-free
 * --------------------------------------------------------------------
 * Check if menu have children
 * --------------------------------------------------------------------
p buildMenu       b                   export

d                 pi
d mnu                            5  0 value

d wMnuMnu       e ds                  extname(MnuMnu) prefix(w)

/free
	setll (wSYSID:mnu) MNUMNU4;
	reade (wSYSID:mnu) MNUMNU4;
	Dow not %eof(MNUMNUL4);
    addMenu();
    if added;
      if checkChildren(MNUID);
        %>,children:[<%
        sep = '';
      else;
        %>}<%
      endif;

      wMNUSEQ = MNUSEQ;

      buildMenu(MNUID);

      chain (wSYSID:mnu:wMNUSEQ) MNUMNU4;

      if checkChildren(MNUID);
        %>]}<%
      endif;
    endif;

  	reade (wSYSID:mnu) MNUMNU4;
  enddo;

  return;
/end-free
p                 e
 * --------------------------------------------------------------------
 * Check if menu have children
 * --------------------------------------------------------------------
p addMenu         b

d                 pi

/free
  added = *off;
  if MNUUCLID > 10 and mnuUserClass >= MNUUCLID;
  // if mnuUserClass >= MNUUCLID;
    exsr addNode;
  else;
    for i = 1 to words(userGroups:',');
      userGroup = num(word(userGroups:i:','));
      Chain (userGroup: MNUID) MNUGMR1;
      if %found;
        exsr addNode;
        i = words(userGroups:',');
      endif;
    endfor;
  endif;

  return;
 // -------------------------------------------------------------------------- *
 // Add Menu item as tree node
 // -------------------------------------------------------------------------- *
Begsr addNode;
  added = *on;
  if checkChildren(MNUID);
    leaf = 'false';
  else;
    leaf = 'true';
  endif;
  
  sgvpath = '/'
          + GetServerVar('SERVER_ID')
          + '/sys/mnu/'+%char(MNUID)+'/menudata';

  menuGroup = globalGetVar(sgvpath+'@menuGroup':'off');
  if menuGroup = 'on';
    menuGroup = 'true';
  else;
    menuGroup = 'false';
  endif;

  hideChildren = globalGetVar(sgvpath+'@hideChildrenInGroup':'off');
  if hideChildren = 'on';
    hideChildren = 'true';
  else;
    hideChildren = 'false';
  endif;
  if hideChildren = 'true';
    leaf = 'true';
  endif;


  closeAllChildr = globalGetVar(sgvpath+'@closeAllChildrenInGroup':'off');
  if closeAllChildr = 'on';
    closeAllChildr = 'true';
  else;
    closeAllChildr = 'false';
  endif;

  text = getMnuTxtP(MNUID: mnuLanguage);

  if MNUSELICO <> '';
    icon = MNUSELICO;
  else;
    icon = '/system/snapOnIcon.aspx.png?keyword='
      		+ %trim(encodeJSONstr(text))
      		+ '&default=gear.png&insertRoot=true';
  endif;

  if MNULNK = '';
%><%=%trim(sep)%>{"id":"<%=%char(MNUID)%>","text":"<%=text%>","icon":"<%responseWrite(%trim(icon));%>","leaf":<%=leaf%>,"draggable":false,"menuGroup":<%=menuGroup%>,"closeAllChildren":<%=closeAllChildr%><%
  else;
  
    if MNUPTH = '';
      MNUPTH = path;
    endif;
    if initCmd <> '';
      if %scan('initFunc=':MNULNK) = 0;
        if %scan('?':MNULNK) = 0;
          MNULNK = %trim(MNULNK)
                  + '?initFunc=' + %trim(initCmd);
        else;
          MNULNK = %trim(MNULNK)
                  + '&initFunc=' + %trim(initCmd);
        endif;
      endif;
    endif;
    criticalAppl = globalGetVar(sgvpath+'@criticalApplication':'off');
    if criticalAppl = 'on';
      tabClosable = 'false';
    else;
      tabClosable = 'true';
    endif;

    openInWindow = globalGetVar(sgvpath+'@openInWindow':'off');
    if openInWindow = 'on';
      openInWindow = 'true';
      windowWidth  = globalGetVarNum(sgvpath+'@windowWidth':500);
      windowHeight = globalGetVarNum(sgvpath+'@windowHeight':500);
    else;
      openInWindow = 'false';
    endif;
    openInNewWin = globalGetVar(sgvpath+'@openInNewWindow':'off');
    if openInNewWin = 'on';
      openInNewWin = 'true';
    else;
      openInNewWin = 'false';
    endif;

%><%=%trim(sep)%>{"id":"<%=%char(MNUID)%>","text":"<%=text%>","icon":"<%responseWrite(%trim(icon));%>","leaf":<%=leaf%>,"draggable":true,"path":"<%responseWrite(encodeJSONstr(MNUPTH));%>","link":"<%responseWrite(encodeJSONstr(MNULNK));%>","tabClosable":<%=tabClosable%>,"menuGroup":<%=menuGroup%>,"closeAllChildren":<%=closeAllChildr%>,"openInNewWindow":<%=openInNewWin%>,"openInWindow":<%=openInWindow%><%
    if (openInWindow = 'true');
      %>,"windowWidth":<%=%char(windowWidth)%>,"windowHeight":<%=%char(windowHeight)%><%
    endif;
  endif;
  sep = ',';
endsr;
/end-free
p                 e
 * --------------------------------------------------------------------
 * Check if menu have children
 * --------------------------------------------------------------------
p checkChildren   b                   export

d                 pi              n
d mnu                            5  0

/free
  setll (mnu) MNUMNU3;
  if %equal(MNUMNUL3);
    return *on;
  else;
    return *off;
  endif;
/end-free
p                 e
%>
