<% /*
     $Revision$
     $Id$

    File:               stdmeldingFilter.inc
    Description:        functies voor stdmelding suggest velden
    Globals:
    Context:
    Note:
*/ %>


<%

function getFiltClauseStdmelding(pfiltcode, params, init)
{
  var lfiltClause = {from: "", where: ""};
  if (pfiltcode && pfiltcode != "")
  {
    switch(pfiltcode)
    {
      case "FE":   lfiltClause.where += " AND sm.mld_stdmelding_notfrontend = 0";
                   break;
      case "VDK":  // Alleen de vakgroepen die gewijzigd mogen worden mag je zien
                   lfiltClause.from += ", mld_disc_params dp";
                   lfiltClause.where += " AND dp.mld_ins_discipline_key = di.ins_discipline_key"
                                      + " AND dp.mld_disc_params_keten = 1";
                   break;
      default:     __Log("KIES JUISTE FILTERCODE!!!");
                   lfiltClause.where = " AND 1 = 2";
                   break;
    }
  }

  if (!params.show_expired && !init)
  { // Alleen bij vinkje "Toon vervallen" en bij init de vervallen stdmeldingen laten zien.
    lfiltClause.where += " AND (sm.mld_stdmelding_vervaldatum IS NULL"
                       + "      OR sm.mld_stdmelding_vervaldatum > SYSDATE)";
  }

  if (params.stdmelding_key && params.stdmelding_key > 0)
  {
    lfiltClause.where += "   AND sm.mld_stdmelding_key = " + params.stdmelding_key
  }
  else
  if (params.disc_key && params.disc_key != -1) // parent gegeven
  {
    lfiltClause.where += " AND di.ins_discipline_key = " + params.disc_key;
  }
  else if (params.srtdisc_key && params.srtdisc_key != -1) // parent-parent gegeven
  {
    lfiltClause.where += " AND di.ins_srtdiscipline_key = " + params.srtdisc_key;
  }

  if (params.autfunctionKey > -1) // Efficiente versie van FAC_V_MY_PR**S_AFDELINGEN_WRITE
  {
    if (user_key == params.perslidKey)
      var same_level = 9;
    else
    {
      var sql = "SELECT aut.fac_get_prs_same_level(" + user_key + ", " + params.perslidKey + ") theLevel FROM DUAL";
      var oRs = Oracle.Execute(sql);
      var same_level = oRs("theLevel").value;
      oRs.Close();
    }
    lfiltClause.where += " AND di.ins_discipline_key IN"
                       + " (SELECT ins_discipline_key FROM fac_v_webgebruiker fw"
                       + "   WHERE fac_functie_key = " + params.autfunctionKey
                       + "     AND fac_gebruiker_prs_level_write <= " + same_level
                       + "     AND fac_gebruiker_alg_level_write < 9" // extra: Vooral voor MLDUSE gebruikers met alleen PRS-leesrechten
                       + "     AND prs_perslid_key = " + user_key
                       + "     AND di.ins_discipline_key = fw.ins_discipline_key)"
  }

  if (params.ins_key > 0)
  {
     lfiltClause.where +="   AND EXISTS (SELECT 1"
                        + "        FROM ins_srtgroep isg,"
                        + "             ins_srtdeel isd,"
                        + "             ins_deel insd"
                        + "       WHERE insd.ins_deel_key = " + ins_key
                        + "         AND isd.ins_srtdeel_key = insd.ins_srtdeel_key"
                        + "         AND isg.ins_srtgroep_key = isd.ins_srtgroep_key"
                        + "         AND (   sm.ins_srtinst_niveau = 'S' AND ins_srtinst_key = insd.ins_srtdeel_key"
                        + "              OR sm.ins_srtinst_niveau = 'G' AND ins_srtinst_key = isg.ins_srtgroep_key"
                        + "              OR sm.ins_srtinst_niveau = 'D' AND ins_srtinst_key = isg.ins_discipline_key"
                        //       + "        OR msm.ins_srtinst_niveau IS NULL" // is wel héél algemeen
                        + "             )"
                        + "     )";

  }

  return lfiltClause;
}

function getExtraInfStdmelding(pextracode)
{
  var lExtraInf = ", sm.mld_ins_discipline_key extra";
  if (pextracode && pextracode != "")
  {
    switch(pextracode)
    {
      case 'KB': lExtraInf = ", (SELECT COUNT(*)"
                           + "     FROM mld_stdmeldingfaq sf"
                           + "    WHERE (sf.mld_stdmelding_key = sm.mld_stdmelding_key"
                           + "       OR (sf.ins_discipline_key = sm.mld_ins_discipline_key"
                           + "           AND sf.mld_stdmelding_key IS NULL))) extra";
                 break;
    }
  }
  return lExtraInf;
}
%>
