<% /*
     $Revision$
     $Id$
*/ %>

<%
//
//  Include file (ASP) used for adding a 3D ALG (place) and PRS (organisation) authorization clause
//  to an SQL statement 'psql' that is being build
//  It requires some column names that represent the columns of discipline, place and organisation
//  levels in this SQL statement, and return the modified psql variable
//  Since it creates several UNIONs based upon the current sql statement,
//  it must be called at the end of the sql creation, i.e. sql must be complete (apart from order by)
//  Variabele pdisc is used (if present) to restrict the condition to only the disciplines in this (comma separated) list
//
//  Every builded ALG union is given to the funcion algxprs(), which is copying the union in
//  different unions extended with the different PRS extentions.
//  Another possibility was to build de PRS unions first, and than copying it and extend it with
//  the different ALG extention.
//  Parameter palgprs is used to tell which 3D authorisation should be used:
//    Only ALG = 0
//    Only PRS = 1
//    Both ALG and PRS = 2
//

var bedr = afd1 = afd2 = afd3 = afd4 = afd5 = "";

function algxprs(psql, oRsPArr,
                 pdiscipline_column_3d,
                 pbedrijf_column_3d,
                 pafdeling_column_3d,
                 pautfunction,
                 psql_group_by)
{
  __SafeLog('discipline_column_3d = ' + pdiscipline_column_3d
   +'<br>bedrijf_column_3d    = ' + pbedrijf_column_3d
   +'<br>afdeling_column_3d   = ' + pafdeling_column_3d
   +'<br>autfunction          = ' + pautfunction
   +'<br>oRsPArr.length        = ' + oRsPArr.length);

  //__Log('InputSQL: <small>' + psql + '</small>');

  // ==============================
  // === BUILD PRS_AUTORISATION ===
  // ==============================

  // Iterate through all disciplines

  var fullSQL = "" // Build a UNION for each INS_DISCIPLINE

  var i = 0;
  while (i < oRsPArr.length)
  {
    var discsOfThisLevel = '';
    var thisLevel = oRsPArr[i].level;
    while (i < oRsPArr.length && oRsPArr[i].level == thisLevel)
    {
      discsOfThisLevel += (discsOfThisLevel == '' ? oRsPArr[i].disc : ', ' + oRsPArr[i].disc);
      i ++;
    }
    fullSQL += (fullSQL == ""
               ? ""
               : (psql_group_by? psql_group_by : "") + " UNION ALL ")
             + psql + " AND " + pdiscipline_column_3d + " IN (" + discsOfThisLevel + ")";
    __Log(pautfunction+' for disciplines ' + discsOfThisLevel + ' authorized at PRS level ' + thisLevel);

    if (thisLevel == -1)
    { // Done
    }
    else if (thisLevel== 0) { fullSQL += " AND " + pbedrijf_column_3d  + "  =  " + bedr }
    else if (thisLevel== 1) { fullSQL += " AND " + pafdeling_column_3d + "1 =  " + afd1 }
    else if (thisLevel== 2) { fullSQL += " AND " + pafdeling_column_3d + "2 =  " + afd2 }
    else if (thisLevel== 3) { fullSQL += " AND " + pafdeling_column_3d + "3 =  " + afd3 }
    else if (thisLevel== 4) { fullSQL += " AND " + pafdeling_column_3d + "4 =  " + afd4 }
    else if (thisLevel== 5) { fullSQL += " AND " + pafdeling_column_3d + "5 =  " + afd5 }
    else __Log("Waarschuwing: niveau nog niet ondersteund: " + thisLevel)
  }
  __Log("end algxprs()");
  // we return the extended version of psql, or, if NO discipline was read authorized we prohibit the selection

  return (fullSQL != "" ? fullSQL : psql + " AND 1 = 2");
}

function prshasrestrict(pautfunction)
{
  var sql = "SELECT COUNT(ins_discipline_key)"
          + " FROM fac_v_webgebruiker wg, fac_functie f"
          + " WHERE wg.prs_perslid_key = " + user_key
          + "   AND f.fac_functie_key = wg.fac_functie_key"
          + (typeof pautfunction == "object" && pautfunction instanceof Array
            ? " AND f.fac_functie_code IN (" + safe.quoted_sql_join(pautfunction) + ")"
            : " AND f.fac_functie_code = " + safe.quoted_sql(pautfunction))
          + "   AND wg.fac_gebruiker_prs_level_read > -1";

  var oRs = Oracle.Execute(sql);

  if (!oRs.eof)
  {
    var count = oRs(0).Value;
    if (count > 0) return true;
  }
  oRs.Close();
  return false;
}

function discx3d(psql,
                 pdiscipline_column_3d,
                 pregio_column_3d,
                 pdistrict_column_3d,
                 plocatie_column_3d,
                 pgebouw_column_3d,
                 pverdieping_column_3d,
                 pruimte_column_3d,
                 pbedrijf_column_3d,
                 pafdeling_column_3d,
                 pautfunction,
                 pdisc, // "" for unknown
                 palgprs,
                 psql_group_by,
                 minlevel)
{
  __SafeLog('discipline_column_3d = '+pdiscipline_column_3d
   +'<br>regio_column_3d      = '+pregio_column_3d
   +'<br>district_column_3d   = '+pdistrict_column_3d
   +'<br>locatie_column_3d    = '+plocatie_column_3d
   +'<br>gebouw_column_3d     = '+pgebouw_column_3d
   +'<br>verdieping_column_3d = '+pverdieping_column_3d
   +'<br>ruimte_column_3d     = '+pruimte_column_3d
   +'<br>bedrijf_column_3d    = '+pbedrijf_column_3d
   +'<br>afdeling_column_3d   = '+pafdeling_column_3d
   +'<br>autfunction          = '+pautfunction
   +'<br>disc                 = '+pdisc
   +'<br>algprs               = '+palgprs);
  __SafeLog('GroupBySQL: <small>' + (psql_group_by? psql_group_by : "") + '</small>');

  //__Log('InputSQL: <small>' + psql + '</small>');

  var oRsO = Oracle.Execute ("SELECT prs_bedrijf_key, prs_afdeling_key1, prs_afdeling_key2"
                             + ", prs_afdeling_key3, prs_afdeling_key4, prs_afdeling_key5"
                             + " FROM prs_v_afdeling_boom b, prs_perslid p"
                             + " WHERE b.prs_afdeling_key = p.prs_afdeling_key"
                             + " AND p.prs_perslid_key = " + user_key);
  if (!oRsO.eof)
  {
    bedr = (!(oRsO("prs_bedrijf_key").Value == ""   || oRsO("prs_bedrijf_key").Value == null))?   oRsO("prs_bedrijf_key").Value :-1;
    afd1 = (!(oRsO("prs_afdeling_key1").Value == "" || oRsO("prs_afdeling_key1").Value == null))? oRsO("prs_afdeling_key1").Value : -1;
    afd2 = (!(oRsO("prs_afdeling_key2").Value == "" || oRsO("prs_afdeling_key2").Value == null))? oRsO("prs_afdeling_key2").Value : -1;
    afd3 = (!(oRsO("prs_afdeling_key3").Value == "" || oRsO("prs_afdeling_key3").Value == null))? oRsO("prs_afdeling_key3").Value : -1;
    afd4 = (!(oRsO("prs_afdeling_key4").Value == "" || oRsO("prs_afdeling_key4").Value == null))? oRsO("prs_afdeling_key4").Value : -1;
    afd5 = (!(oRsO("prs_afdeling_key5").Value == "" || oRsO("prs_afdeling_key5").Value == null))? oRsO("prs_afdeling_key5").Value : -1;
  }

  var fullSQL = "";
  var oRs;
  if (palgprs == 0 || palgprs == 2)
  { // ==============================
    // === BUILD ALG_AUTORISATION ===
    // ==============================
    oRs = Oracle.Execute ("SELECT alg_regio_key FROM alg_v_my_region" +
                          " WHERE prs_perslid_key = " + user_key) // Level 0
    var rekeys = "" // List of my region keys
    while (!oRs.eof)
    {
        rekeys += (rekeys == ""? "" : ", ") + oRs(0)
        oRs.MoveNext()
    }
    oRs = Oracle.Execute ("SELECT alg_district_key FROM alg_v_my_district" +
                          " WHERE prs_perslid_key = " + user_key) // Level 1
    var dkeys = "" // List of my district keys
    while (!oRs.eof)
    {
      dkeys += (dkeys == ""? "" : ", ") + oRs(0)
      oRs.MoveNext()
    }
    oRs = Oracle.Execute ("SELECT alg_locatie_key FROM alg_v_my_location" +
                          " WHERE prs_perslid_key = " + user_key) // Level 2
    var lkeys = "" // List of my location keys
    while (!oRs.eof)
    {
      lkeys += (lkeys == ""? "" : ", ") + oRs(0)
      oRs.MoveNext()
    }
    oRs = Oracle.Execute ("SELECT alg_gebouw_key FROM alg_v_my_building" +
                          " WHERE prs_perslid_key = " + user_key) // Level 3
    var gkeys = "" // List of my building keys
    while (!oRs.eof)
    {
      gkeys += (gkeys == ""? "" : ", ") + oRs(0)
      oRs.MoveNext()
    }
    oRs = Oracle.Execute ("SELECT alg_verdieping_key FROM alg_v_my_floor" +
                          " WHERE prs_perslid_key = " + user_key) // Level 4
    var fkeys = "" // List of my floor keys
    while (!oRs.eof)
    {
      fkeys += (fkeys == ""? "" : ", ") + oRs(0)
      oRs.MoveNext()
    }
    oRs = Oracle.Execute ("SELECT alg_ruimte_key FROM alg_v_my_room" +
                          " WHERE prs_perslid_key = " + user_key) // Level 4
    var rkeys = "" // List of my room keys
    while (!oRs.eof)
    {
      rkeys += (rkeys == ""? "" : ", ") + oRs(0)
      oRs.MoveNext()
    }

    // Iterate through all disciplines
    var sqlDA =   "SELECT i.ins_discipline_key, fac_gebruiker_alg_level_read, isd.ins_srtdiscipline_alg"
               + " FROM fac_v_webgebruiker wg, fac_functie f, ins_tab_discipline i, ins_srtdiscipline isd"
               + " WHERE wg.prs_perslid_key = " + user_key
               + "   AND f.fac_functie_key = wg.fac_functie_key"
               + "   AND f.fac_functie_code = " + safe.quoted_sql(pautfunction)
               + "   AND wg.fac_gebruiker_alg_level_read<9"
               + "   AND wg.ins_discipline_key = i.ins_discipline_key"
               + "   AND i.ins_srtdiscipline_key = isd.ins_srtdiscipline_key(+)"
               + "   AND i.ins_discipline_verwijder is NULL";
    var sqlDP =   "SELECT i.ins_discipline_key, fac_gebruiker_prs_level_read, isd.ins_srtdiscipline_alg"
               + " FROM fac_v_webgebruiker wg, fac_functie f, ins_tab_discipline i, ins_srtdiscipline isd"
               + " WHERE wg.prs_perslid_key = " + user_key
               + "   AND f.fac_functie_key = wg.fac_functie_key"
               + "   AND f.fac_functie_code = " + safe.quoted_sql(pautfunction)
               + "   AND wg.fac_gebruiker_prs_level_read < 9"
               + "   AND wg.ins_discipline_key = i.ins_discipline_key"
               + "   AND i.ins_srtdiscipline_key = isd.ins_srtdiscipline_key(+)"
               + "   AND i.ins_discipline_verwijder is NULL";
    if (pdisc != '')
    {
      sqlDA += " AND i.ins_discipline_key IN (" + pdisc + ")";
      sqlDP += " AND i.ins_discipline_key IN (" + pdisc + ")";
    }
    if (minlevel>0) // RES filtert extra hard
    {
      sqlDA += " AND i.ins_discipline_min_level = " + minlevel;
      sqlDP += " AND i.ins_discipline_min_level = " + minlevel;
    }
    sqlDA += " ORDER BY 2, 3, 1";  // we need this
    sqlDP += " ORDER BY 2, 3, 1";  // we need this
    oRs = Oracle.Execute(sqlDA);  // used for ALG
    var oRsP = Oracle.Execute(sqlDP); // used for PRS function algxprs()
    var oRsPArr = [];
    while (!oRsP.eof)
    {
        oRsPArr.push({ disc: oRsP("ins_discipline_key").Value, level: oRsP("fac_gebruiker_prs_level_read").Value });
        oRsP.MoveNext();
    }
    oRsP.close();

    fullSQL = "" // Build a UNION for each INS_DISCIPLINE

    while (!oRs.eof)
    {
      var discsOfThisLevel = '';
      var thisLevel = oRs(1).Value;
      var srtalg = oRs(2).Value;
      while ((!oRs.eof) && (oRs(1).Value == thisLevel) && (oRs(2).Value == srtalg))
      {
        discsOfThisLevel += (discsOfThisLevel == ''? oRs(0) : ', ' + oRs(0));
        oRs.MoveNext()
      }
      pfullSQL = psql + " AND "+ pdiscipline_column_3d + " IN (" + discsOfThisLevel + ")";
      __Log(pautfunction + ' for disciplines ' + discsOfThisLevel + ' authorized at ALG level ' + thisLevel);

      // if (incidentele bestellingen) then no ALG autorisation (srtdisc==5)
      if (thisLevel == -1 || srtalg == 0)
      { // Done
      }
      else if (thisLevel == 0) { pfullSQL += " AND " + pregio_column_3d + " IN (" + ((!rekeys == "")? rekeys : "-1") + ")" }
      else if (thisLevel == 1) { pfullSQL += " AND " + pdistrict_column_3d + " IN (" + ((!dkeys == "")? dkeys : "-1") + ")" }
      else if (thisLevel == 2) { pfullSQL += " AND " + plocatie_column_3d + " IN (" + ((!lkeys == "")? lkeys : "-1") + ")" }
      else if (thisLevel == 3) { pfullSQL += " AND " + pgebouw_column_3d + " IN (" + ((!gkeys == "")? gkeys : "-1") + ")" }
      else if (thisLevel == 4) { pfullSQL += " AND " + pverdieping_column_3d + " IN (" + ((!fkeys == "")? fkeys : "-1") + ")" }
      else if (thisLevel == 5) { pfullSQL += " AND " + pruimte_column_3d + " IN (" + ((!rkeys == "")? rkeys : "-1") + ")" }
      else __Log("Waarschuwing: niveau nog niet ondersteund: " + thisLevel)
      // Look for every ALG union to the PRS authorisation
      if (oRsPArr.length)
      {
        fullSQL += (fullSQL == ""
                   ? ""
                   : " UNION ALL ")
                 + ((palgprs == 2)
                   ? algxprs(pfullSQL, oRsPArr, pdiscipline_column_3d,
                             pbedrijf_column_3d, pafdeling_column_3d,
                             pautfunction, psql_group_by)
                   : pfullSQL)
                 + (psql_group_by
                   ? psql_group_by
                   : "");
      }
    }
    oRs.close();
  }
  else if (palgprs == 1)
  {
    // Iterate through all disciplines
    sqlD = "SELECT i.ins_discipline_key, fac_gebruiker_prs_level_read, i.ins_srtdiscipline_key"
         + " FROM fac_v_webgebruiker wg, fac_functie f, ins_tab_discipline i"
         + " WHERE wg.prs_perslid_key="+user_key
         + "   AND f.fac_functie_key=wg.fac_functie_key"
         + "   AND f.fac_functie_code="+ safe.quoted_sql(pautfunction)
         + "   AND wg.fac_gebruiker_prs_level_read<9"
         + "   AND wg.ins_discipline_key = i.ins_discipline_key(+)"
         + "   AND i.ins_discipline_verwijder is NULL";
    if (pdisc != '')
    {
      sqlD += " AND i.ins_discipline_key IN (" + pdisc + ")";
    }
    sqlD += " ORDER BY 2, 1";  // we need this
    var oRsP = Oracle.Execute(sqlD); // used for PRS function algxprs()
    var oRsPArr = [];
    while (!oRsP.eof)
    {
        oRsPArr.push({ disc: oRsP("ins_discipline_key").Value, level: oRsP("fac_gebruiker_prs_level_read").Value });
        oRsP.MoveNext();
    }
    oRsP.close();

    if (oRsPArr.length)
    {
      fullSQL = algxprs(psql, oRsPArr, pdiscipline_column_3d, pbedrijf_column_3d, pafdeling_column_3d,
                        pautfunction, psql_group_by);
    }
  }
  else
  {
    __Log("Error: Unknown 3D authorisation");
  }
  // we return the extended version of psql, or, if NO discipline was read authorized we prohibit the selection
  return (fullSQL != "" ? fullSQL : psql + " AND 1 = 3");
}
%>