<% /*

    $Revision$
    $Id$

    File:           mld_fe_menu.inc
    Description:    Alternatieve interface om (eenvoudig) een melding te kunnen kiezen
                    Veronderstelt wel dat er niet al te schrikbarend veel meldingen zijn.
    Parameters:     lvl: het groeperingslevel,
                    0=vakgroeptype
                    1=vakgroep (default)
                    2=mld_stdmeldinggroep
                    n= als 1
                    urole: indien fo, dan wordt MLDFOF autorisatie gebruikt ipv MLDUSE
    Context:        Dit is de gedeelde code van /MLD/mld_fe_menu.asp en /PDA/melding_menu.asp
    Note:


*/ %>

<!-- #include file="../prs/prs.inc" -->

<%
mld_fe_menu = {
    /*  Parameters:
        level   Het groeperingslevel (0 = vakgroeptype, 1 = vakgroep (default), 2 = mld_stdmeldinggroep)
        urole   urole
        filters { srtdisc_key_arr, disc_key_arr, stdmgroep_key_arr }
    */
    get_menu_sql:
        function (level, urole, filters)
        {
            filters = filters || {};
            filters.srtdisc_key_arr     = filters.srtdisc_key_arr   || [];
            filters.disc_key_arr        = filters.disc_key_arr      || [];
            filters.stdmgroep_key_arr   = filters.stdmgroep_key_arr || [];

            var sql = "SELECT sm.mld_stdmelding_key,"
                    +           lcl.xsqla("sm.mld_stdmelding_omschrijving","sm.mld_stdmelding_key") + ","
                    +           lcl.xsqla("sm.mld_stdmelding_hint","sm.mld_stdmelding_key") + ","
                    +           lcl.xsqla("d.ins_discipline_omschrijving", "d.ins_discipline_key") + ","
                    +           lcl.xsqla("sd.ins_srtdiscipline_omschrijving", "sd.ins_srtdiscipline_key") + ","
                    +           lcl.xsqla("mg.mld_stdmeldinggroep_naam","mg.mld_stdmeldinggroep_key") + ","
                    + "       sm.mld_stdmeldinggroep_key,"
                    + "       mg.mld_stdmeldinggroep_image,"
                    + "       mg.mld_stdmeldinggroep_color,"
                    + "       d.ins_discipline_key,"
                    + "       sd.ins_srtdiscipline_key,"
                    + "       sd.ins_srtdiscipline_prefix,"
                    + "       CASE WHEN sd.ins_srtdiscipline_prefix IS NULL OR " + filters.srtdisc_key_arr.length + " = 1"
                    + "            THEN ''"
                    + "            ELSE sd.ins_srtdiscipline_prefix || '-'"
                    + "       END || " + lcl.xsql("d.ins_discipline_omschrijving", "d.ins_discipline_key") + " full_discname,"
                    + "       sd.ins_srtdiscipline_color,"
                    + "       d.ins_discipline_color,"
                    + "       sm.mld_stdmelding_color,"
                    + "       COALESCE(sm.mld_stdmelding_color, d.ins_discipline_color, sd.ins_srtdiscipline_color) color,"
                    + "       sd.ins_srtdiscipline_image,"
                    + "       d.ins_discipline_image,"
                    + "       sm.mld_stdmelding_image,"
                    + "       sm.mld_stdmelding_externurl"
                    + "  FROM mld_stdmelding sm,"
                    + "       mld_stdmeldinggroep mg,"
                    + "       mld_discipline d,"
                    + "       ins_srtdiscipline sd,"
                    + "       fac_v_webgebruiker fvw,"
                    + "       fac_functie f"
                    + " WHERE sm.mld_stdmelding_notfrontend" + (urole == "fo" ? " IN (0, 1)" : " = 0")
                    + "   AND sm.mld_stdmelding_verwijder IS NULL"
                    + "   AND d.ins_discipline_key = sm.mld_ins_discipline_key"
                    + "   AND sd.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                    + "   AND fvw.ins_discipline_key = d.ins_discipline_key"
                    + "   AND sm.mld_stdmeldinggroep_key = mg.mld_stdmeldinggroep_key (+)"
                    + "   AND fvw.fac_functie_key = f.fac_functie_key"
                    + "   AND fvw.fac_gebruiker_alg_level_write < 9"
                    + "   AND fvw.fac_gebruiker_prs_level_write < 9"
                    + "   AND f.fac_functie_code = " + safe.quoted_sql(urole == "fo" ? "WEB_MLDFOF" : "WEB_MLDUSE")
                    + "   AND sd.ins_srtdiscipline_key <>" + S("kto_srtdiscipline_key")
                    + "   AND fvw.prs_perslid_key = " + user_key
                    + "   AND (sm.mld_stdmelding_vervaldatum IS NULL OR"
                    + "        sm.mld_stdmelding_vervaldatum > SYSDATE)";

            if (lvl == 2) // Geen lege groepen tonen
                sql += "  AND sm.mld_stdmeldinggroep_key IS NOT NULL";

            if (filters.disc_key_arr.length)
                sql += "  AND d.ins_discipline_key IN (" + filters.disc_key_arr.join(",") + ")";

            if (filters.srtdisc_key_arr.length)
                sql += "  AND d.ins_srtdiscipline_key IN (" + filters.srtdisc_key_arr.join(",") + ")";

            if (filters.stdmgroep_key_arr.length)
                sql += "  AND sm.mld_stdmeldinggroep_key IN (" + filters.stdmgroep_key_arr.join(",") + ")";

            switch (lvl) {
                case 2:
                    sql += "   ORDER BY " + lcl.xsql("mg.mld_stdmeldinggroep_naam", "mg.mld_stdmeldinggroep_key")
                                        + " , sm.mld_stdmelding_volgnr, "
                                        + lcl.xsql("sm.mld_stdmelding_omschrijving", "sm.mld_stdmelding_key");
                    break;
                case 0:
                    sql += "   ORDER BY " + lcl.xsql("sd.ins_srtdiscipline_omschrijving", "sd.ins_srtdiscipline_key")
                                        + " , sm.mld_stdmelding_volgnr, "
                                        + lcl.xsql("sm.mld_stdmelding_omschrijving", "sm.mld_stdmelding_key");
                    break;
                case 1:     // Dit is de default
                default:
                    sql += "   ORDER BY d.ins_discipline_volgnr NULLS LAST"
                                    + ", 13" // = full_discname
                                    + ", sm.mld_stdmelding_volgnr"
                                    + ", " +lcl.xsql("sm.mld_stdmelding_omschrijving", "sm.mld_stdmelding_key");
            }
            return sql;
        },
    get_mobile_menu_sql:
        function (level, urole, filters)
        {
            filters = filters || {};
            filters.srtdisc_key_arr     = filters.srtdisc_key_arr   || [];
            filters.disc_key_arr        = filters.disc_key_arr      || [];
            filters.stdmgroep_key_arr   = filters.stdmgroep_key_arr || [];

            if (level == 9)
                return this.get_menu_sql(level, urole, filters);

            var select  = "";
            var from    = "";
            var where, groupby;

            if (level == 0)
            {
                select += "sd.ins_srtdiscipline_key,"
                        + "MIN(d.ins_discipline_volgnr),"
                        +  lcl.xsql("sd.ins_srtdiscipline_omschrijving", "sd.ins_srtdiscipline_key") + " descr,"
                        + "sd.ins_srtdiscipline_image image,"
                        + "sd.ins_srtdiscipline_color color";
                where   = "       sd.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                        + "   AND d.ins_discipline_key IN sm.mld_ins_discipline_key";
                groupby = "sd.ins_srtdiscipline_key,"
                        +  lcl.xsql("sd.ins_srtdiscipline_omschrijving", "sd.ins_srtdiscipline_key") + ","
                        + "sd.ins_srtdiscipline_image,"
                        + "sd.ins_srtdiscipline_color"
                        + " ORDER BY 3, 2";
            }
            else if (level == 1)
            {
                select += "d.ins_discipline_key,"
                        + "d.ins_discipline_volgnr,"
                        + "CASE WHEN sd.ins_srtdiscipline_prefix IS NULL OR " + filters.srtdisc_key_arr.length + " = 1"
                        + "     THEN ''"
                        + "     ELSE sd.ins_srtdiscipline_prefix || '-'"
                        + "END || " + lcl.xsql("d.ins_discipline_omschrijving", "d.ins_discipline_key") + " descr,"
                        + "d.ins_discipline_image image,"
                        + "d.ins_discipline_color color";
                where   = "       sd.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                        + "   AND d.ins_discipline_key IN sm.mld_ins_discipline_key";
                groupby = "d.ins_discipline_key,"
                        + "d.ins_discipline_volgnr,"
                        + "CASE WHEN sd.ins_srtdiscipline_prefix IS NULL OR " + filters.srtdisc_key_arr.length + " = 1"
                        + "     THEN ''"
                        + "     ELSE sd.ins_srtdiscipline_prefix || '-'"
                        + "END || " + lcl.xsql("d.ins_discipline_omschrijving", "d.ins_discipline_key")+ ","
                        + "d.ins_discipline_image,"
                        + "d.ins_discipline_color"
                        + " ORDER BY 2, 3";
            }
            else if (level == 2)
            {
                select += "mg.mld_stdmeldinggroep_key,"
                        + "MIN(d.ins_discipline_volgnr),"
                        +  lcl.xsql("mg.mld_stdmeldinggroep_naam", "mg.mld_stdmeldinggroep_key") + " descr,"
                        + "mg.mld_stdmeldinggroep_image image,"
                        + "mg.mld_stdmeldinggroep_color color"
                from    = "mld_stdmeldinggroep mg,";
                where   = "       sd.ins_srtdiscipline_key = d.ins_srtdiscipline_key"
                        + "   AND mg.mld_stdmeldinggroep_key IN sm.mld_stdmeldinggroep_key"
                        + "   AND d.ins_discipline_key IN sm.mld_ins_discipline_key";
                groupby = "mg.mld_stdmeldinggroep_key,"
                        +  lcl.xsql("mg.mld_stdmeldinggroep_naam", "mg.mld_stdmeldinggroep_key") + ","
                        + "mg.mld_stdmeldinggroep_image,"
                        + "mg.mld_stdmeldinggroep_color"
                        + " ORDER BY 3, 2";
            }

            var sql = "WITH stdmelding AS (SELECT mld_ins_discipline_key, mld_stdmeldinggroep_key"
                    + "                      FROM mld_stdmelding sm"
                    + "                     WHERE mld_stdmelding_verwijder IS NULL"
                    +      (urole == "fe" ? " AND mld_stdmelding_notfrontend = 0" : "")
                    +         (level == 2 ? " AND mld_stdmeldinggroep_key IS NOT NULL" : "")
                    + (filters.stdmgroep_key_arr.length ?
                                            " AND mld_stdmeldinggroep_key IN (" + filters.stdmgroep_key_arr.join(",") + ")" : "")
                    + "                       AND (mld_stdmelding_vervaldatum IS NULL"
                    + "                            OR mld_stdmelding_vervaldatum > SYSDATE))"
                    + "SELECT " + select
                    + "  FROM " + from
                    + "       mld_discipline d,"
                    + "       ins_srtdiscipline sd,"
                    + "       fac_v_webgebruiker fvw,"
                    + "       fac_functie f,"
                    + "       stdmelding sm"
                    + " WHERE " + where
                    + "   AND fvw.ins_discipline_key = d.ins_discipline_key"
                    + "   AND fvw.fac_functie_key = f.fac_functie_key"
                    + "   AND fvw.fac_gebruiker_alg_level_write < 9"
                    + "   AND fvw.fac_gebruiker_prs_level_write < 9"
                    + "   AND f.fac_functie_code = " + safe.quoted_sql(urole == "fo" ? "WEB_MLDFOF" : "WEB_MLDUSE")
                    + "   AND sd.ins_srtdiscipline_key <>" + S("kto_srtdiscipline_key")
                    + "   AND fvw.prs_perslid_key = " + user_key;

            if (filters.disc_key_arr.length)
                sql += "  AND d.ins_discipline_key IN (" + filters.disc_key_arr.join(",") + ")";

            if (filters.srtdisc_key_arr.length)
                sql += "  AND d.ins_srtdiscipline_key IN (" + filters.srtdisc_key_arr.join(",") + ")";

            sql += " GROUP BY " + groupby;

            return sql;
        }
}

%>
