<% /*
    $Revision$
    $Id$

    File:           model_fac_widget.inc

    Description:

    Context:

    Notes:
*/
%>
<!-- #include file="../fac/fac_menu.inc" -->
<!-- #include file="../fac/fac_widget.inc" -->
<%
function model_fac_widget(onpage_mode)
{
    this.records_name   = "widgets";
    this.record_name    = "widget";
    this.table          = "fac_widget";
    this.primary        = "fac_widget_key";
    this.autfunction    = ["WEB_WGTMAN", "WEB_PRSSYS"];
    var hasPRSSYS       = user.has("WEB_PRSSYS");
    var advancedAuth    = hasPRSSYS || user.has("WEB_WGTMAN");
    var fac_widget_sql      = mode == "show" || mode == "list"; // In deze modes joint de query met de fac_widget tabel, en moeten we dus ook op widget_type matchen
    var scope_ref_fields    = !hasPRSSYS                        // Met PRSSYS mag je alles zien
                              && (!advancedAuth                 // Met WGTUSE altijd scopen
                                || !fac_widget_sql);            // Met WGTMAN mag je alleen ongescoped /zien/ (de definitie), niet wijzigen (de inhoud)

    // In onpage_mode wordt in buildWidgetTypeLOV() geregeld welke types je mag wijzigen.
    // Met WEB_WGTUSE is dat alleen type WGT zodat je de huidige widget kan inwisselen voor een bestaande andere
    // Die variant gaat uiteindelijk via een custom REST_PUT/POST niet langs mode == "save" waardoor het huidige record niet wordt aangepast.
    if (onpage_mode && mode != "save")
        this.autfunction.push("WEB_WGTUSE");
    this.record_title = L("fac_widget");
    this.records_title = L("fac_widget_m");

    this.fields = {
        "id": {
            "dbs": "fac_widget_key",
            "label": L("lcl_key"),
            "typ": "key",
            "required": true,
            "seq": "fac_s_fac_widget_key",
            "defaultvalue": null
        },
        "type": {
            "dbs": "fac_widget_type",
            "label": L("fac_widget_type"),
            "typ": "varchar",
            "LOV": buildWidgetTypeLOV(),
            "defaultvalue": (onpage_mode ? "WGT" : "FAC"),
            "hidden_fld": (onpage_mode ? false : S("fac_usrrap_mode") == 0),
            "required": true
        },
        "name" : {
            "dbs": "fac_widget_naam",
            "label": L("fac_widget_naam"),
            "typ": "varchar",
            "required": true,
            "filter": "like",
            "translate": true
        },
        "ref_key_MK": {
            "dbs": "fac_widget_typerefkey",
            "label": L("fac_widget_reftype_MK"),
            "typ": "key",
            "foreign": { "tbl": "("+fillMenuArray(1, { "sqlOnly": true, "level": "1" })+")",
                         "key": "fac_menu_key",
                         "desc": "DECODE(groep "
                                     + ", 0, "+ safe.qL("lcl_menu_pers")
                                     + ", 1, "+ safe.qL("lcl_menu_prof")
                                     + ", 2, "+ safe.qL("lcl_menu_config")
                                     + ", 3, "+ safe.qL("lcl_menu_mobile")
                                     + ", 7, "+ safe.qL("lcl_menu_app")+" ) || ': ' || lcl.x('label', fac_menu_key, label)",
                         "where": "groep IN (0, 1, 2, 3, 7)"    // 0=personal, 1=professional, 2=management, 3=mobile, 4=?, 5=pinned tab, 6=favoriten menu, 7=App
                                + (fac_widget_sql ? " AND fac_widget.fac_widget_type = 'MK'" : ""),
                         "order": "groep, fac_menu_volgnr" },
            "hidden_fld": S("fac_usrrap_mode") == 0,
            "required": true
        },
        "ref_key_MI": {
            "dbs": "fac_widget_typerefkey",
            "label": L("fac_widget_reftype_MI"),
            "typ": "key",
            "foreign": { "tbl": "("+fillMenuArray(1, { "sqlOnly": true, "level": "2" })+")",
                         "key": "fac_menu_key",
                         "desc": "DECODE(groep "
                                     + ", 0, "+ safe.qL("lcl_menu_pers")
                                     + ", 1, "+ safe.qL("lcl_menu_prof")
                                     + ", 2, "+ safe.qL("lcl_menu_config")
                                     + ", 3, "+ safe.qL("lcl_menu_mobile")
                                     + ", 7, "+ safe.qL("lcl_menu_app")+" ) || ': ' || lcl.x('label', fac_menu_key, label)",
                         "where": "groep IN (0, 1, 2, 3, 7)"    // 0=personal, 1=professional, 2=management, 3=mobile, 4=?, 5=pinned tab, 6=favoriten menu, 7=App
                                + (fac_widget_sql ? " AND fac_widget.fac_widget_type = 'MI'" : ""),
                         "order": "groep, fac_menu_volgnr" },
            "hidden_fld": S("fac_usrrap_mode") == 0,
            "required": true
        },
        "ref_key_NWS": {
            "dbs": "fac_widget_typerefkey",
            "label": L("fac_widget_reftype_NWS"),
            "typ": "key",
            "foreign": { "tbl": "fac_nieuws_groep",
                         "key": "fac_nieuws_groep_key",
                         "desc": "fac_nieuws_groep_omschrijving",
                         "where": (fac_widget_sql ? "fac_widget.fac_widget_type = 'NWS'" : "1=1") },
            "hidden_fld": S("fac_usrrap_mode") == 0,
            "emptyoption": "",
            "required": false
        },
        "ref_key_RAP": {
            "dbs": "fac_widget_typerefkey",
            "label": L("fac_widget_reftype_RAP"),
            "typ": "key",
            "foreign": { "tbl": "fac_usrrap",
                         "key": "fac_usrrap_key",
                         "desc": "fac_usrrap_omschrijving",
                         "where": (fac_widget_sql ? "fac_widget.fac_widget_type = 'RAP'" : "1=1")
                                + (scope_ref_fields
                                    ? "     AND (   fac_functie_key IS NULL"
                                        + "      OR fac_functie_key IN (SELECT w.fac_functie_key"
                                        + "                               FROM fac_v_webgebruiker W"
                                        + "                              WHERE w.prs_perslid_key = " + user_key + "))"
                                        + " AND BITAND (COALESCE (fac_usrrap_functie, 0), 32) = 0"
                                    : "") },
            "hidden_fld": S("fac_usrrap_mode") == 0,
            "emptyoption": "",
            "required": true
        },
        "ref_key_FAQ": {
            "dbs": "fac_widget_typerefkey",
            "label": L("fac_widget_reftype_FAQ"),
            "typ": "key",
            "foreign": { "tbl": "fac_faq",
                         "key": "fac_faq_key",
                         "desc": "SUBSTR(fac_faq_question, 0, 40) || ' - [' || fac_faq_key || ']'",
                         "where": (fac_widget_sql ? "fac_widget.fac_widget_type = 'FAQ'" : "1=1")
                                + (scope_ref_fields
                                    ? "     AND (   fac_functie_key IS NULL"
                                        + "      OR fac_functie_key IN (SELECT w.fac_functie_key"
                                        + "                               FROM fac_v_webgebruiker W"
                                        + "                              WHERE w.prs_perslid_key = " + user_key + "))"
                                    : "") },
            "hidden_fld": S("fac_usrrap_mode") == 0 || S("faq_enabled") == 0,
            "emptyoption": "",
            "required": true
        },
        "content": {
            "dbs": "fac_widget_content",
            "label": L("fac_widget_content"),
            "typ": "memo",
            "hidden_fld": S("fac_usrrap_mode") == 0,
            "required": true
        },
        "linkurl": {
            "dbs": "fac_widget_url",
            "label": L("fac_widget_url"),
            "typ": "varchar",
            "required": true
        },
        "title": {
            "dbs": "fac_widget_title",
            "label": L("fac_widget_title"),
            "translate": true,
            "typ": "varchar",
            "filter": "like",
            "friendlyname": true
        },
        "refreshtime": {
            "dbs": "fac_widget_refreshtime",
            "label": L("fac_widget_refreshtime"),
            "typ": "number",
            "multiedit": true
        },
        "widgetid": {
            "dbs": "fac_widget_id",
            "label": L("fac_widget_id"),
            "typ": "varchar"
        },
        "widgetheight": {
            "dbs": "fac_widget_height",
            "label": L("fac_widget_height"),
            "typ": "number",
            "multiedit": true,
            "hidden_fld": true
        },
        "widgetwidth": {
            "dbs": "fac_widget_width",
            "label": L("fac_widget_width"),
            "typ": "number",
            "multiedit": true,
            "hidden_fld": true
        },
        "created": {
            "dbs": "fac_widget_aanmaak",
            "label": L("fac_widget_aanmaak"),
            "typ": "date",
            "hidden_fld": true,
            "readonly": true
        },
        "authorization": {
            "dbs": "fac_functie_key",
            "label": L("fac_widget_autorisatie"),
            "typ": "key",
            "foreign": "fac_functie",
            "foreignfiltercode": "SELF",
            "multiedit": true
        }
    }

    var authFAQ;
    function buildWidgetTypeLOV()
    {
        var widget_type_LOV = "";
        if (!advancedAuth)
            widget_type_LOV = L("fac_widget_noauth_typeWGT");
        else if (S("fac_usrrap_mode") == 1)
        {
            widget_type_LOV = L("fac_widget_typeLOV");
            if (onpage_mode)
                widget_type_LOV = L("fac_widget_typeWGT") + ";" + widget_type_LOV;

            if (S("fac_html_strictness") == 0) {
                widget_type_LOV += ";" + L("fac_widget_typeRAW");
            }

            if (S("faq_enabled") == 1)
            {
                authFAQ = user.func_enabled2("FAQ", { prs_key: user_key, isOptional: true });
                if (authFAQ && authFAQ.anyfound)
                    widget_type_LOV += ";" + L("fac_widget_typeFAQ");
            }

            var authCAD = user.func_enabled2("CAD", { prs_key: user_key, isOptional: true }); // TODO; ALG scoping
            if (authCAD && authCAD.anyfound)
                widget_type_LOV += ";" + L("fac_widget_typeFLR");
        }
        else
        {
            widget_type_LOV = L("fac_widget_noauth_typeLOV");
            if (onpage_mode)
                widget_type_LOV = L("fac_widget_noauth_typeWGT") + ";" + widget_type_LOV;
        }
        return widget_type_LOV;
    }

    addfaqauth: if (S("fac_usrrap_mode") == 1 && S("faq_enabled") == 1)
    {
        if (authFAQ && (authFAQ.canRead("WEB_FAQBOF") || authFAQ.canRead("WEB_FAQMGT")))
            break addfaqauth; // Dan mag ik alles zien

        var lvl_bits = 0;
        if (authFAQ && authFAQ.canRead("WEB_FAQUSE"))
            lvl_bits += 1;
        if (authFAQ && authFAQ.canRead("WEB_FAQFOF"))
            lvl_bits += 2;

        this.fields.ref_key_FAQ.foreign.where += " AND BITAND (fac_faq_level, " + lvl_bits + ") <> 0";
    }

    // 4 velden gebruiken 1 onderliggend DB-veld, bij de im-/export slaan we die weer plat anders AiAi dubbele kolomnaam
    if (mode == "api2" || mode == "import")
    {
        delete this.fields.ref_key_MI;
        delete this.fields.ref_key_MK;
        delete this.fields.ref_key_NWS;
        delete this.fields.ref_key_RAP;
        delete this.fields.ref_key_FAQ;
        this.fields.ref_key = {
            "dbs": "fac_widget_typerefkey",
            "typ": "key"
        }
    }

    if (onpage_mode)
    {
        this.fields.widgets = {
            "dbs": "fac_widget_key",
            "label": L("fac_widget"),
            "typ": "key",
            "foreign": { "tbl": "fac_widget",
                         "key": "fac_widget_key",
                         "desc": "fac_widget_naam",
                         "where": "(   fac_functie_key IS NULL"
                                + " OR fac_functie_key IN (SELECT w.fac_functie_key"
                                + "                          FROM fac_v_webgebruiker W"
                                + "                         WHERE w.prs_perslid_key = " + user_key + "))"
                                + (S("fac_usrrap_mode") == 0 ? " AND fac_widget_type = 'FAC'" : ""),
                         "order": "fac_widget_public DESC, fac_widget_naam" },
            "required": true
        }
    }

    var gparams = {
        "GET": {
            "wheres": []
        }
    }

    if (S("fac_usrrap_mode") == 0)
        gparams.GET.wheres.push("fac_widget.fac_widget_type = 'FAC'");

    if (!hasPRSSYS) // Dan alleen de widgets waar je rechten op hebt
    {
        gparams.GET.wheres.push("(   fac_widget.fac_functie_key IS NULL"
                              + " OR fac_widget.fac_functie_key IN (SELECT w.fac_functie_key"
                              + "                                     FROM fac_v_webgebruiker W"
                              + "                                    WHERE w.prs_perslid_key = " + user_key + "))");
    }

    if (advancedAuth)
    {
        if (S("fac_usrrap_mode") == 1)
        {
            this.fields.public = {
                "dbs": "fac_widget_public",
                "label": L("fac_widget_public"),
                "typ": "check0",
                "multiedit": true,
                "defaultvalue": 0
            }
        }
    }
    else
    {
        this.fields.authorization.hidden_fld = true;
        if ("widgets" in this.fields) // = Widgetbibliotheek in de onpage_mode
            this.fields.widgets.foreign.where += " AND (   fac_widget_public = 1"
                                               + "      OR fac_widget_key IN"
                                               + "              (SELECT fac_widget_key"
                                               + "                 FROM fac_widget_onpage o, fac_widget_page p"
                                               + "                WHERE o.fac_widget_page_key = p.fac_widget_page_key"
                                               + "                  AND p.prs_perslid_key = " + user_key + "))";
    }

    this.REST_GET   = generic_REST_GET(this, gparams);
    this.REST_POST  = generic_REST_POST(this);
    this.REST_PUT   = generic_REST_PUT(this);
    if (!onpage_mode)
    {
        this.REST_DELETE = generic_REST_DELETE(this);
    }
}
%>