<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$
*/ %>

<%
DOCTYPE_Disable = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/selector.inc" -->

<%
// Build an article list
var urole = getQParamSafe("urole");
var fronto = urole == "fo";
var backo = urole == "bo";
var minfo = urole == "mi"; // NOT APPLICABLE?
var frontend = (urole == "fe" || (!fronto && !backo && !minfo));

var autfunction_arr = getQParamArray("autfunction", []); // Check of user rechten heeft op meegegeven artikelen
var srtdisc_key_arr = getQParamIntArray("srtdisc_key_arr", []); // Vakgroeptype (Soort melding) van de melding.
var disc_key_arr = getQParamIntArray("disc_key_arr", []); // Productgroep (discipline): -2 als lege waarde meegegeven omdat -1 een geldige waarde is
var stdm_arr = getQParamIntArray("stdm_str", []); // Stdmelding string (eventueel initiële waarden)
var noExpired = getQParamInt("noExpired", 0) == 1;
var withExternal = getQParamInt("withExternal", 0) == 1;
var faq = getQParamInt("faq", 0) == 1; // alleen waar faqs van zijn
var offerte = getQParamInt("offerte", -1);

var stdmsql_aut = "SELECT ins_discipline_key"
            + "  FROM fac_v_webgebruiker g"
            + ", fac_functie f"
            + " WHERE g.prs_perslid_key = " + user_key
            + " AND f.fac_functie_key = g.fac_functie_key"
            + " AND f.fac_functie_code IN (" + safe.quoted_sql_join(autfunction_arr) + ")"
            + " AND g.fac_gebruiker_alg_level_read < 9"; // MGE: Als alg_level_read = 9 dan komt waarde niet meer voor in fac_v_webgebruiker. Regel zou dus weg kunnen.

var stdmsql = "SELECT DISTINCT sm.mld_stdmelding_key"
        + ", " + lcl.xsqla('sm.mld_stdmelding_omschrijving','sm.mld_stdmelding_key')
        + ", sm.mld_ins_discipline_key"
        + " FROM mld_stdmelding sm"
        + ", mld_discipline d"
        + (offerte >= 0
          ? ", ins_srtdiscipline sd"
            + ", mld_typeopdr_srtdiscipline tsd"
            + ", mld_typeopdr mto"
          : "")
        + " WHERE sm.mld_ins_discipline_key = d.ins_discipline_key"
        + (offerte >= 0
          ? " AND d.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
            + " AND sd.ins_srtdiscipline_key = tsd.ins_srtdiscipline_key"
            + " AND (tsd.ins_discipline_key IS NULL OR tsd.ins_discipline_key = d.ins_discipline_key)"
            + " AND tsd.mld_typeopdr_key = mto.mld_typeopdr_key"
            + " AND mto.mld_typeopdr_isofferte = " + offerte
          : "")
        + " AND sm.mld_stdmelding_verwijder IS NULL"
        + " AND d.ins_discipline_verwijder IS NULL"
        + (!withExternal ? " AND sm.mld_stdmelding_externurl IS NULL" : "")
        + (frontend
          ? " AND sm.mld_stdmelding_notfrontend = 0"
          : "")
        // srtdisc wordt niet afgedwongen via afhankelijkheid in het geval van discipline is alle (-1). Dus gewoon meenemen.
        + (srtdisc_key_arr.length != 0
          ? " AND d.ins_srtdiscipline_key IN (" + srtdisc_key_arr.join(",") + ")"
          : "")
        + (disc_key_arr.length != 0
          ? " AND sm.mld_ins_discipline_key IN (" + disc_key_arr.join(",") + ")"
          : "")
        + (faq == 1
          ? " AND EXISTS (SELECT ''"
            + "             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)))" // Alleen waar faqs van zijn
          : "")
        + (autfunction_arr.length != ""
          ? " AND sm.mld_ins_discipline_key IN (" + stdmsql_aut + ")"
          : "")
        + (noExpired
           // Vervallen stdmeldingen niet tonen
           ? "   AND (sm.mld_stdmelding_vervaldatum IS NULL"
             + "        OR sm.mld_stdmelding_vervaldatum > SYSDATE)"
           : "")
        + " ORDER BY "+ lcl.xsql('sm.mld_stdmelding_omschrijving','sm.mld_stdmelding_key');

FCLTselectorOptions(stdmsql,
                    { initKey:  stdm_arr.join(","),
                      multi: true,
                      size: 3,
                      emptyOption: L("lcl_search_generic")
                    });
%>

<% ASPPAGE_END(); %>
