<% /*
    $Revision$
    $Id$

    File:           model_mld_melding_tag.inc

    Description:    Model voor mld_melding_tag (mijlpalen, releasekalender)

    Context:        Er wordt goed gelet op MLDTAG-schrijfrechten, maar leesrechten lijkt nog alles of niks. TODO denk ik.

    Notes:    Hoe gaan we normaal om met verwijderdatum? Wat mij betreft is weg=weg, en dat is standaard?
*/
%>
<%

function model_mld_melding_tag()
{
    this.records_name = "issuetags";
    this.record_name = "issuetag";
    this.table = "mld_melding_tag";
    this.primary = "mld_melding_tag_key";
    this.autfunction = "WEB_MLDTAG";
    this.soft_delete = "mld_melding_tag_verwijder";
    this.record_title = L("mld_melding_tag");
    this.records_title = L("mld_melding_tag_m");

    var tag_key = getQParamInt("id", -1);
    var auth_mode = getQParamSafe("mode", "show") === "edit" ? "write" : "read";

    this.fields = {
        "id": {
            "dbs": "mld_melding_tag_key",
            "label": "Key",
            "typ": "key",
            "hidden_fld": true,
            "required": true,
            "seq": "mld_s_mld_melding_tag_key",
            "defaultvalue": null
        },
        "disciplinetype": {
            "dbs": "ins_srtdiscipline_key",
            "label": L("ins_srtdiscipline"),
            "typ": "key",
            "xhidden_fld": true,
            "required": true,
            "foreign": { // tags zijn alleen relevant als er standaardmeldingen zijn die tags gebruiken
                "tbl": "(SELECT ins_srtdiscipline_key"
                     + "      , ins_srtdiscipline_omschrijving"  // lcl
                     + "   FROM ins_srtdiscipline"
                     + "  WHERE ins_srtdiscipline_module = 'MLD'"
                     + "    AND ins_srtdiscipline_verwijder IS NULL"
                     + "    AND ins_srtdiscipline_key IN "
                     + "        (SELECT ins_srtdiscipline_key"
                     + "           FROM mld_discipline"
                     + "          WHERE ins_discipline_key IN "
                     + "                (SELECT mld_ins_discipline_key"
                     + "                   FROM mld_stdmelding"
                     + "                  WHERE mld_stdmelding_taggable > 0)"
                     + "            AND ins_discipline_key IN"
                     + "                (SELECT w.ins_discipline_key"
                     + "                   FROM fac_v_webgebruiker w"
                     + "                  WHERE w.prs_perslid_key = " + user_key
                     + "                    AND fac_functie_key = " + F(this.autfunction)
                     + "                    AND w.fac_gebruiker_prs_level_" + auth_mode + " < 9 AND w.fac_gebruiker_alg_level_" + auth_mode + " < 9))"
                     + ")",
                "key": "ins_srtdiscipline_key",
                "desc": "ins_srtdiscipline_omschrijving"
            }
        },
        "name": {
            "dbs": "mld_melding_tag_naam",
            "label": L("mld_melding_tag_naam"),
            "typ": "varchar",
            "required": true
        },
        "description": {
            "dbs": "mld_melding_tag_oms",
            "label": L("mld_melding_tag_oms"),
            "typ": "varchar",
            "translate": true
        },
        "issuegroup": {
            "dbs": "mld_stdmeldinggroep_key",
            "typ": "key",
            "label": L("lcl_mld_meldinggroep"),
            "foreign": { // alleen meldinggroepen van standaardmeldingen die tags gebruiken
                "tbl": "(SELECT g.mld_stdmeldinggroep_key"
                     + ", " + lcl.xsqla("g.mld_stdmeldinggroep_naam", "g.mld_stdmeldinggroep_key")
                     + " FROM mld_stdmeldinggroep g"
                     + " WHERE g.mld_stdmeldinggroep_key IN "
                     + "       (SELECT mld_stdmeldinggroep_key "
                     + "          FROM mld_stdmelding "
                     + "         WHERE mld_stdmelding_taggable > 0"
                     + "           AND mld_ins_discipline_key IN "
                     + "              (SELECT w.ins_discipline_key "
                     + "                 FROM fac_v_webgebruiker w"
                     + "                  WHERE w.prs_perslid_key = " + user_key
                     + "                    AND fac_functie_key = " + F(this.autfunction)
                     + "                    AND w.fac_gebruiker_prs_level_" + auth_mode + " < 9 AND w.fac_gebruiker_alg_level_" + auth_mode + " < 9)))",
                "key": "mld_stdmeldinggroep_key",
                "desc": "mld_stdmeldinggroep_naam"
            },
            "emptyoption": "",
            "showtransit": true,
            "multiedit": true
        },
        "person": {
            "dbs": "prs_perslid_key",
            "label": L("mld_melding_tag_person"),
            "typ": "key",
            "foreign": "prs_perslid",
            "defaultvalue" : user_key /* werk dit? */
        },
        "begindate": {
            "dbs": "mld_melding_tag_datum_van",
            "label": L("mld_melding_tag_startdatum"),
            "typ": "date",
            "multiedit": true
        },
        "accdate": {
            "dbs": "mld_melding_tag_datum_acc",
            "label": L("mld_melding_tag_accdatum"),
            "typ": "date",
            "multiedit": true
        },
        "enddate": {
            "dbs": "mld_melding_tag_datum_tot",
            "label": L("mld_melding_tag_einddatum"),
            "typ": "date",
            "required": true,
            "multiedit": true
        }
    };

    this.autclause = function _autclause(rw) {
        rw = (rw === "write" ? "write" : "read"); // Whitelisten voor de zekerheid
        return "     ins_srtdiscipline_key IN"
             + "         (SELECT ins_srtdiscipline_key"
             + "            FROM mld_discipline"
             + "           WHERE     ins_discipline_key IN"
             + "                         (SELECT mld_ins_discipline_key"
             + "                            FROM mld_stdmelding"
             + "                           WHERE mld_stdmelding_taggable > 0)"
             + "                 AND ins_discipline_key IN"
             + "                         (SELECT w.ins_discipline_key"
             + "                            FROM fac_v_webgebruiker w"
             + "                           WHERE     w.prs_perslid_key = " + user_key
             + "                                 AND fac_functie_key = " + F(this.autfunction)
             + "                                 AND w.fac_gebruiker_prs_level_" + rw + " < 9"
             + "                                 AND w.fac_gebruiker_alg_level_" + rw + " < 9))"
             + " AND (   mld_stdmeldinggroep_key IS NULL"
             + "      OR (mld_stdmeldinggroep_key IN"
             + "              (SELECT mld_stdmeldinggroep_key"
             + "                 FROM mld_stdmelding"
             + "                WHERE     mld_stdmelding_taggable > 0"
             + "                      AND mld_ins_discipline_key IN"
             + "                              (SELECT w.ins_discipline_key"
             + "                                 FROM fac_v_webgebruiker w"
             + "                                WHERE     w.prs_perslid_key = " + user_key
             + "                                      AND fac_functie_key = " + F(this.autfunction)
             + "                                      AND w.fac_gebruiker_prs_level_" + rw + " < 9"
             + "                                      AND w.fac_gebruiker_alg_level_" + rw + " < 9))))";
    }

    this.REST_GET = generic_REST_GET(this, { "GET": { "wheres": this.autclause() }});
    var autparams = user.checkAutorisation(this.autfunction);
    var canWrite  = (autparams.PRSwritelevel < 9 && autparams.ALGwritelevel < 9);

    /* Dit lost het multi-gebeuren nog niet op .. */

    if (canWrite) {
        if (tag_key > -1) {
            var sql = "SELECT 'canWrite'"
                    + "  FROM mld_melding_tag"
                    + " WHERE mld_melding_tag_key = " + tag_key
                    + "   AND " + this.autclause("write");
            var oRs = Oracle.Execute(sql);
            if (!oRs.EOF) { // Ik heb schrijfrechten op deze specifieke tag
                this.REST_POST   = generic_REST_POST(this);
                this.REST_PUT    = generic_REST_PUT(this);
                this.REST_DELETE = generic_REST_DELETE(this);
            }
            oRs.Close();
        } else {
            this.REST_POST   = generic_REST_POST(this);
            // Geen multi-acties, wel door kunnen klikken
            this.list = {
                "default_action": "scf_show",
                "default_url": "appl/mgt/mld_melding_tag.asp?id={0}"
            };
        }
    }
}
%>