<%
'
'    Filename: fac_persoongroepgrid.asp
'    Generated with CodeCharge 2.0.5
'    ASP 2.0 & Templates.ccp build 11/30/2001
'

'-------------------------------
' 
' fac_persoongroepgrid CustomIncludes Event begin
%>
<!-- #INCLUDE FILE="Common.asp" -->
<%
' fac_persoongroepgrid CustomIncludes Event end


'===============================
' Save Page and File Name available into variables
'-------------------------------
sFileName = "fac_persoongroepgrid.asp"
sTemplateFileName = "fac_persoongroepgrid.html"
'===============================


'===============================
' fac_persoongroepgrid CustomSecurity Event begin
checkAutorisation("WEB_PRSMSU")
' fac_persoongroepgrid CustomSecurity Event end
'===============================

'===============================
' fac_persoongroepgrid Open Event begin
' fac_persoongroepgrid Open Event end
'===============================

'===============================
' fac_persoongroepgrid OpenAnyPage Event begin
' fac_persoongroepgrid OpenAnyPage Event end
'===============================

'===============================
'Save the name of the form and type of action into the variables
'-------------------------------
sAction = GetParam("FormAction")
sForm = GetParam("FormName")
'===============================

' fac_persoongroepgrid Show begin

'===============================
' Display page
'-------------------------------
' Load HTML template for this page
'-------------------------------
LoadTemplate sAppPath & sTemplateFileName, "main"
'-------------------------------
' Load HTML template of Header and Footer
'-------------------------------
'-------------------------------
SetVar "FileName", sFileName


'-------------------------------
' Step through each form
'-------------------------------
PersoonSearch_Show
PersoonGrid_Show
'-------------------------------
' Process page templates
'-------------------------------
Parse "main", False
'-------------------------------
' Output the page to the browser
'-------------------------------
Response.write PrintVar("main")

' fac_persoongroepgrid Show end

'-------------------------------
' Destroy all object variables
'-------------------------------

' fac_persoongroepgrid Close Event begin
' fac_persoongroepgrid Close Event end

cn.Close
Set cn = Nothing
UnloadTemplate
'===============================

'===============================
' Display Grid Form
'-------------------------------
Sub PersoonGrid_Show()
'-------------------------------
' Initialize variables  
'-------------------------------
  Dim rs
  Dim sWhere : sWhere = ""
  Dim sOrder : sOrder = ""
  Dim sSQL : sSQL = ""
  Dim sFormTitle: sFormTitle = "Autorisaties"
  Dim HasParam : HasParam = false
  Dim iSort : iSort = ""
  Dim iSorted : iSorted = ""
  Dim sDirection : sDirection = ""
  Dim sSortParams : sSortParams = ""
  Dim iRecordsPerPage : iRecordsPerPage = 50
  Dim iCounter : iCounter = 0
  Dim iPage : iPage = 0
  Dim bEof : bEof = False
  Dim iTmpI : iTmpI = 0
  Dim iTmpJ : iTmpJ = 0
  Dim sCountSQL : sCountSQL = ""

  SetVar "TransitParams", "fFAC_GROEP_KEY=" & ToURL(GetParam("fFAC_GROEP_KEY")) & "&fPRS_AFDELING_KEY=" & ToURL(GetParam("fPRS_AFDELING_KEY")) & "&fPRS_PERSLID_NAAM=" & ToURL(GetParam("fPRS_PERSLID_NAAM")) & "&fPRS_PERSLID_OSLOGIN=" & ToURL(GetParam("fPRS_PERSLID_OSLOGIN")) & "&"
  SetVar "FormParams", "fFAC_GROEP_KEY=" & ToURL(GetParam("fFAC_GROEP_KEY")) & "&fPRS_AFDELING_KEY=" & ToURL(GetParam("fPRS_AFDELING_KEY")) & "&fPRS_PERSLID_NAAM=" & ToURL(GetParam("fPRS_PERSLID_NAAM")) & "&fPRS_PERSLID_OSLOGIN=" & ToURL(GetParam("fPRS_PERSLID_OSLOGIN")) & "&"

'-------------------------------
' Build WHERE statement
'-------------------------------
  pfPRS_AFDELING_KEY = GetParam("fPRS_AFDELING_KEY")
  if IsNumeric(pfPRS_AFDELING_KEY) and not isEmpty(pfPRS_AFDELING_KEY) then pfPRS_AFDELING_KEY = ToSQL(pfPRS_AFDELING_KEY, "Number") else pfPRS_AFDELING_KEY = Empty
  if not isEmpty(pfPRS_AFDELING_KEY) then
    HasParam = true
    sWhere = sWhere & "P.PRS_AFDELING_KEY=" & pfPRS_AFDELING_KEY
  end if


  if HasParam then
    sWhere = " AND (PRS_PERSLID_OSLOGIN IS NOT NULL) AND (" & sWhere & ")"
  else
    sWhere = " AND PRS_PERSLID_OSLOGIN IS NOT NULL"
  end if
  
'-------------------------------
' Build ORDER BY statement
'-------------------------------
  sOrder = " order by P.PRS_PERSLID_NAAM Asc"
  iSort = GetParam("FormPersoonGrid_Sorting")
  iSorted = GetParam("FormPersoonGrid_Sorted")
  sDirection = ""
  if IsEmpty(iSort) then
    SetVar "Form_Sorting", ""
  else
    if iSort = iSorted then 
      SetVar "Form_Sorting", ""
      sDirection = " DESC"
      sSortParams = "FormPersoonGrid_Sorting=" & iSort & "&FormPersoonGrid_Sorted=" & iSort & "&"
    else
      SetVar "Form_Sorting", iSort
      sDirection = " ASC"
      sSortParams = "FormPersoonGrid_Sorting=" & iSort & "&FormPersoonGrid_Sorted=" & "&"
    end if
    if iSort = 1 then sOrder = " order by P.PRS_PERSLID_KEY" & sDirection
    if iSort = 2 then sOrder = " order by P.PRS_PERSLID_NAAM" & sDirection
    if iSort = 3 then sOrder = " order by P.PRS_PERSLID_OSLOGIN" & sDirection
    if iSort = 4 then sOrder = " order by P1.PRS_AFDELING_OMSCHRIJVING" & sDirection
  end if

'-------------------------------
' Build base SQL statement
'-------------------------------
  sSQL = "select P.PRS_AFDELING_KEY as P_PRS_AFDELING_KEY, " & _
    "P.PRS_PERSLID_KEY as P_PRS_PERSLID_KEY, " & _
    "P.PRS_PERSLID_NAAM as P_PRS_PERSLID_NAAM, " & _
    "P.PRS_PERSLID_OSLOGIN as P_PRS_PERSLID_OSLOGIN, " & _
    "P1.PRS_AFDELING_KEY as P1_PRS_AFDELING_KEY, " & _
    "P1.PRS_AFDELING_OMSCHRIJVING as P1_PRS_AFDELING_OMSCHRIJVING " & _
    " from PRS_V_AANWEZIGPERSLID P, PRS_V_AANWEZIGAFDELING P1" & _
    " where P1.PRS_AFDELING_KEY=P.PRS_AFDELING_KEY  "
'-------------------------------

'-------------------------------
' PersoonGrid Open Event begin
tmpWhere = ""
pPRS_PERSLID_NAAM = getParam("fPRS_PERSLID_NAAM")
if not isEmpty(pPRS_PERSLID_NAAM) then
  HasParam = true
  tmpWhere = tmpWhere & " UPPER(P.PRS_PERSLID_NAAM) like " & ToSQL("%"& UCase(pPRS_PERSLID_NAAM) &"%", "Text")
end if

pPRS_PERSLID_OSLOGIN = getParam("fPRS_PERSLID_OSLOGIN")
if not isEmpty(pPRS_PERSLID_OSLOGIN) then
  if not (tmpWhere = "") then tmpWhere = tmpWhere & " and "
  HasParam = true
  tmpWhere = tmpWhere & " UPPER(P.PRS_PERSLID_OSLOGIN) like " & ToSQL("%"& UCase(pPRS_PERSLID_OSLOGIN) &"%", "Text")
end if

pFAC_GROEP_KEY = getParam("fFAC_GROEP_KEY")
if not isEmpty(pFAC_GROEP_KEY) then
  if not (tmpWhere = "") then tmpWhere = tmpWhere & " and "
  hasParam = true
  tmpWhere = tmpWhere & ToSQL(pFAC_GROEP_KEY, "Number") & " in (select gg.fac_groep_key from fac_gebruikersgroep gg where gg.prs_perslid_key=P.PRS_PERSLID_KEY) "
end if


if tmpWhere<>"" then
  sWhere = sWhere & " AND (" & tmpWhere & ")"
end if
' PersoonGrid Open Event end
'-------------------------------

'-------------------------------
' Assemble full SQL statement
'-------------------------------
  sSQL = sSQL & sWhere & sOrder
  if sCountSQL = "" then
    iTmpI = instr(lcase(sSQL), "select")
    iTmpJ = instr(lcase(sSQL), "from") - 1
    sCountSQL = replace(sSQL, mid(sSQL, iTmpI + 6, iTmpJ - 6), " count(*) ")
    iTmpI = instr(lcase(sCountSQl), "order by")
    if iTmpI > 1 then sCountSQL = left(sCountSQL, iTmpI - 1)
  end if
'-------------------------------

SetVar "FormTitle", sFormTitle

'-------------------------------
' Process the parameters for sorting
'-------------------------------
  SetVar "SortParams", sSortParams
'-------------------------------

'-------------------------------
' Open the recordset
'-------------------------------
  openrs rs, sSQL
'-------------------------------

'-------------------------------
' Process empty recordset
'-------------------------------
  if rs.eof then
    set rs = nothing
    SetVar "DListPersoonGrid", ""
    Parse "PersoonGridNoRecords", False
    SetVar "PersoonGridNavigator", ""
    Parse "FormPersoonGrid", False
    exit sub
  end if
'-------------------------------

'-------------------------------
' Initialize page counter and records per page
'-------------------------------
  iRecordsPerPage = 50
  iCounter = 0
'-------------------------------

'-------------------------------
' Process page scroller
'-------------------------------
  iPage = GetParam("FormPersoonGrid_Page")
  openrs rsCount, sCountSQL
  dResult = CLng(rsCount.fields(0).value) / iRecordsPerPage
  iPageCount = int(dResult)
  if iPageCount < dResult then iPageCount = iPageCount + 1
  SetVar "PersoonGridPageCount", iPageCount
  set rsCount = nothing
  if IsEmpty(iPage) then
    iPage = 1
  else
    if iPage = "last" then iPage = iPageCount
  end if
  while not rs.eof and iCounter < (iPage-1)*iRecordsPerPage
    rs.movenext
    iCounter = iCounter + 1
  wend
  iCounter = 0
'-------------------------------

'-------------------------------
' Display grid based on recordset
'-------------------------------
  while not rs.EOF  and iCounter < iRecordsPerPage
'-------------------------------
' Create field variables based on database fields
'-------------------------------
    fldPRS_AFDELING_KEY = GetValue(rs, "P1_PRS_AFDELING_OMSCHRIJVING")
    fldPRS_PERSLID_KEY = GetValue(rs, "P_PRS_PERSLID_KEY")
    fldPRS_PERSLID_NAAM_URLLink = "fac_persoongroeprecord.asp"
    fldPRS_PERSLID_NAAM_PRS_PERSLID_KEY = GetValue(rs, "P_PRS_PERSLID_KEY")
    fldPRS_PERSLID_NAAM = GetValue(rs, "P_PRS_PERSLID_NAAM")
    fldPRS_PERSLID_OSLOGIN = GetValue(rs, "P_PRS_PERSLID_OSLOGIN")
'-------------------------------
' PersoonGrid Show begin
'-------------------------------

'-------------------------------
' PersoonGrid Show Event begin
fldFAC_GROEP_KEY = getFacGroepByPrsLidKey(fldPRS_PERSLID_KEY)

fldPRS_PERSLID_NAAM = buildName(fldPRS_PERSLID_KEY)
' PersoonGrid Show Event end
'-------------------------------

'-------------------------------
' Replace Template fields with database values
'-------------------------------
    
      SetVar "PRS_PERSLID_KEY", ToHTML(fldPRS_PERSLID_KEY)
      SetVar "PRS_PERSLID_NAAM", ToHTML(fldPRS_PERSLID_NAAM)
      SetVar "PRS_PERSLID_NAAM_URLLink", fldPRS_PERSLID_NAAM_URLLink
      SetVar "PrmPRS_PERSLID_NAAM_PRS_PERSLID_KEY", ToURL(fldPRS_PERSLID_NAAM_PRS_PERSLID_KEY)
      SetVar "PRS_PERSLID_OSLOGIN", ToHTML(fldPRS_PERSLID_OSLOGIN)
      SetVar "PRS_AFDELING_KEY", ToHTML(fldPRS_AFDELING_KEY)
      SetVar "FAC_GROEP_KEY", fldFAC_GROEP_KEY
    Parse "DListPersoonGrid", True

'-------------------------------
' PersoonGrid Show end
'-------------------------------

'-------------------------------
' Move to the next record and increase record counter
'-------------------------------
    rs.MoveNext
    iCounter = iCounter + 1
  wend
'-------------------------------


'-------------------------------
' PersoonGrid Navigation begin
'-------------------------------
  bEof = rs.eof
  if rs.eof and iPage = 1 then
	SetVar "PersoonGridNavigator", ""
  else
    iCounter = 1
    iHasPages = iPage
    iDisplayPages = 0
    iNumberOfPages = 10
    iHasPages = iPageCount
    if (iHasPages - iPage) < int(iNumberOfPages/2) then
      iStartPage = iHasPages - iNumberOfPages
    else
      iStartPage = iPage - iNumberOfPages + int(iNumberOfPages/2)
    end if
    if iStartPage < 0 then iStartPage = 0
    for iPageCount = iStartPage + 1 to iPage - 1
      SetVar "NavigatorPageNumber", iPageCount
      SetVar "NavigatorPageNumberView", iPageCount
      Parse "PersoonGridNavigatorPages", true
      iDisplayPages = iDisplayPages + 1
    next
    SetVar "NavigatorPageSwitch", "_"
    SetVar "NavigatorPageNumber", iPage
    SetVar "NavigatorPageNumberView", iPage
    Parse "PersoonGridNavigatorPages", true
    iDisplayPages = iDisplayPages + 1
    SetVar "NavigatorPageSwitch", ""
    iPageCount = iPage + 1
    while iDisplayPages < iNumberOfPages and iStartPage + iDisplayPages < iHasPages
      SetVar "NavigatorPageNumber", iPageCount
      SetVar "NavigatorPageNumberView", iPageCount
      Parse "PersoonGridNavigatorPages", true
      iDisplayPages = iDisplayPages + 1
      iPageCount = iPageCount + 1
    wend
    if bEof then
      SetVar "PersoonGridNavigatorLastPage", "_"
    else
      SetVar "NextPage", (iPage + 1)
    end if
    if iPage = 1 then
      SetVar "PersoonGridNavigatorFirstPage", "_"
    else
      SetVar "PrevPage", (iPage - 1)
    end if
    SetVar "PersoonGridCurrentPage", iPage
    Parse "PersoonGridNavigator", False
  end if
'-------------------------------
' PersoonGrid Navigation end
'-------------------------------

'-------------------------------
' Finish form processing
'-------------------------------
  set rs = nothing
  SetVar "PersoonGridNoRecords", ""
  Parse "FormPersoonGrid", False

'-------------------------------
' PersoonGrid Close Event begin
' PersoonGrid Close Event end
'-------------------------------
End Sub
'===============================


'===============================
' Display Search Form
'-------------------------------
Sub PersoonSearch_Show()
  Dim sFormTitle: sFormTitle = ""
  Dim sActionFileName: sActionFileName = "fac_persoongroepgrid.asp"
  Dim sfPRS_AFDELING_KEYDisplayValue: sfPRS_AFDELING_KEYDisplayValue = ""
  Dim sfFAC_GROEP_KEYDisplayValue: sfFAC_GROEP_KEYDisplayValue = ""

'-------------------------------
' PersoonSearch Open Event begin
' PersoonSearch Open Event end
'-------------------------------
      SetVar "FormTitle", sFormTitle
      SetVar "ActionPage", sActionFileName

'-------------------------------
' Set variables with search parameters
'-------------------------------
      fldfPRS_PERSLID_NAAM = GetParam("fPRS_PERSLID_NAAM")
      fldfPRS_AFDELING_KEY = GetParam("fPRS_AFDELING_KEY")
      fldfPRS_PERSLID_OSLOGIN = GetParam("fPRS_PERSLID_OSLOGIN")
      fldfFAC_GROEP_KEY = GetParam("fFAC_GROEP_KEY")

'-------------------------------
' PersoonSearch Show begin
'-------------------------------


'-------------------------------
' PersoonSearch Show Event begin
' PersoonSearch Show Event end
'-------------------------------
      SetVar "fPRS_PERSLID_NAAM", ToHTML(fldfPRS_PERSLID_NAAM)
      SetVar "PersoonSearchLBfPRS_AFDELING_KEY", ""
      SetVar "Selected", ""
      SetVar "ID", ""
      SetVar "Value", sfPRS_AFDELING_KEYDisplayValue
      Parse "PersoonSearchLBfPRS_AFDELING_KEY", True
      openrs rsfPRS_AFDELING_KEY, "select prs_afdeling_key, prs_afdeling_naam from prs_v_aanwezigafdeling order by prs_afdeling_naam"
      while not rsfPRS_AFDELING_KEY.EOF
        SetVar "ID", GetValue(rsfPRS_AFDELING_KEY, 0) : SetVar "Value", GetValue(rsfPRS_AFDELING_KEY, 1)
        if cstr(GetValue(rsfPRS_AFDELING_KEY, 0)) = cstr(fldfPRS_AFDELING_KEY) then SetVar "Selected", "SELECTED" else SetVar "Selected", ""
        Parse "PersoonSearchLBfPRS_AFDELING_KEY", True
        rsfPRS_AFDELING_KEY.MoveNext
      wend
      set rsfPRS_AFDELING_KEY = nothing
    
      SetVar "fPRS_PERSLID_OSLOGIN", ToHTML(fldfPRS_PERSLID_OSLOGIN)
      SetVar "PersoonSearchLBfFAC_GROEP_KEY", ""
      SetVar "Selected", ""
      SetVar "ID", ""
      SetVar "Value", sfFAC_GROEP_KEYDisplayValue
      Parse "PersoonSearchLBfFAC_GROEP_KEY", True
      openrs rsfFAC_GROEP_KEY, "select FAC_GROEP_KEY, FAC_GROEP_OMSCHRIJVING from FAC_GROEP order by 2"
      while not rsfFAC_GROEP_KEY.EOF
        SetVar "ID", GetValue(rsfFAC_GROEP_KEY, 0) : SetVar "Value", GetValue(rsfFAC_GROEP_KEY, 1)
        if cstr(GetValue(rsfFAC_GROEP_KEY, 0)) = cstr(fldfFAC_GROEP_KEY) then SetVar "Selected", "SELECTED" else SetVar "Selected", ""
        Parse "PersoonSearchLBfFAC_GROEP_KEY", True
        rsfFAC_GROEP_KEY.MoveNext
      wend
      set rsfFAC_GROEP_KEY = nothing
    

'-------------------------------
' PersoonSearch Show end
'-------------------------------

'-------------------------------
' PersoonSearch Close Event begin
' PersoonSearch Close Event end
'-------------------------------
      Parse "FormPersoonSearch", False
End Sub
'===============================

%>