<% /*
    $Revision$
    $Id$

    File:           model_fac_faq.inc

    Description:    fac_faq model.
    Parameters:
    Context:        Kennisbank

    Notes:          Levert FAQ's, meldingen etc,etc die het zoekwoord bevatten.
                    &include=stdmeldingfaqs,algfaqs,custom_fields
*/

%>
<!-- #include file="../fac/fac.inc" -->
<!-- #include file="../api2/model_mld_stdmeldingfaq.inc" -->
<!-- #include file="../api2/model_alg_algfaq.inc" -->
<!-- #include file="../api2/model_faq_kenmerk.inc"-->
<!-- #include file="../api2/model_custom_fields.inc"-->
<%
function model_fac_faq()
{
    var s = getQParamSafe("s", "");
    var incl_arr = getQParamArray("include", []);
    var supper = s.toUpperCase();

    this.table = "fac_faq";
    this.primary = "fac_faq_key";
    this.records_name = "knowledgebase";
    this.record_name = "knowledgebase";
    this.record_title = L("fac_faq");
    this.records_title = L("fac_faq_m");
    this.module = "FAQ";
    this.autfunction = "WEB_FAQMGT";

    this.fields = {
        "id": {
            "dbs": "fac_faq_key",
            "label": L("lcl_faq_kennisbank_nr"),
            "typ": "key",
            "required": true,
            "seq": "fac_s_fac_faq_key"
        },
        "facdiscipline": {
            "dbs": "faq_ins_discipline_key",
            "label": L("faq_discipline_key"),
            "typ": "key",
            "insertonly": true,
            "foreign": {
                "tbl": "faq_v_aanwezigdiscipline",
                "key": "ins_discipline_key",
                "desc": "ins_discipline_omschrijving"
            },
            "showtransit": true
        },
        "product": {
            "dbs": "fac_faq_question",
            "label": L("lcl_faq_question"),
            "typ": "varchar"
        },
        "extra": {
            "dbs": "fac_faq_answer",
            "label": L("lcl_faq_answer"),
            "typ": "varchar"
        },
        "extrainfo": {
            "dbs": "fac_faq_answer2",
            "label": L("lcl_faq_answer2"),
            "typ": "varchar"
        },
        "source": {
            "dbs": "fac_faq_source",
            "label": L("lcl_faq_bron"),
            "typ": "varchar"
        },
        "url": {
            "dbs": "fac_faq_url",
            "label": L("lcl_faq_link"),
            "typ": "varchar"
        },
        "level": {
            "dbs": "fac_faq_level",
            "label": L("lcl_faq_level"),
            "typ": "number"
        },
        "owner": {
            "dbs": "prs_perslid_key",
            "label": L("lcl_faq_ingevoerd"),
            "typ": "key",
            "foreign": "PRS_PERSLID"
        },
        "publicationdate": {
            "dbs": "fac_faq_datum",
            "label": L("lcl_faq_datum"),
            "typ": "date"
        },
        "rank": {
            "dbs": "fac_faq_rank",
            "label": L("lcl_ins_volgnr"),
            "typ": "number"
        },
        "language": {
            "dbs": "fac_faq_lang",
            "label": L("lcl_faq_lang"),
            "typ": "varchar"
        },
        "hint": {
            "dbs": "fac_faq_hint",
            "label": L("lcl_faq_hint"),
            "typ": "varchar"
        },
        "displaymode": {
            "dbs": "fac_faq_displaymode",
            "label": L("lcl_faq_displaymode"),
            "typ": "number"
        },
        "authorization": {
            "dbs": "fac_functie_key",
            "label": L("lcl_usrrap_functie"),
            "typ": "key",
            "foreign": "fac_functie"
        },
        "expirydate": {
            "dbs": "fac_faq_vervaldatum",
            "label": L("lcl_faq_vervaldatum"),
            "typ": "datetime"
        }
    };

    this.includes = {
        "issuetypefaqs": {
            "model": new model_mld_stdmeldingfaq(),
            "joinfield": "faq"
        },
        "placefaqs": {
            "model": new model_alg_algfaq(),
            "joinfield": "faq"
        },
        "custom_fields" : {
            "model": new model_custom_fields(this, new model_faq_kenmerk({ internal: true }), { readman: true, readuse: true }),
            "joinfield": "flexparentkey",
            "enable_update": true
        }
    };


    var where_auth = " (  fac_faq.fac_functie_key IS NULL"
                   + " OR fac_faq.fac_functie_key IN "
                   + "    (SELECT w.fac_functie_key"
                   + "       FROM fac_v_webgebruiker w"
                   + "      WHERE w.prs_perslid_key = " + user_key
                   + "    )"
                   + " )";

    var where_lang = " (  fac_faq.fac_faq_lang = " + safe.quoted_sql(user.lang())
                   + " OR fac_faq.fac_faq_lang IS NULL"
                   + " )"

    var gparams = {
            GET: {
                tables: [],
                wheres: [
                    where_auth, where_lang,
                    "fac_faq.fac_faq_datum < SYSDATE"
                ],
                orderbys: [
                    "fac_faq_rank DESC",
                    "fac_faq_datum DESC"
                ]
            }
        };

    var authparamsBOF = user.checkAutorisation("WEB_FAQBOF", true); // ziet alles
    var authparamsFOF = user.checkAutorisation("WEB_FAQFOF", true); // ziet ook FO-items
    var authparamsUSE = user.checkAutorisation("WEB_FAQUSE", true); // ziet ook FO-items
    if (!authparamsBOF)
    {
        var lvl = 0;
        if (authparamsFOF)
          lvl = 2;
        if (authparamsUSE)
          lvl += 1;
        gparams.GET.wheres.push("BITAND (fac_faq_level," + lvl +") <> 0");
    }


    var fieldstrarr = ["UPPER(fac_faq_hint)"];
    if (S("faq_search_hints_only") <= 0)
    {
        fieldstrarr.push("UPPER(fac_faq_question)", "UPPER(fac_faq_answer)", "UPPER(fac_faq_answer2)");
        if (inArray("custom_fields", incl_arr) )
        {   // Alle velden van de flexkenmerken staan in flex.
            fieldstrarr.push("UPPER(flex.omschrijving)", "UPPER(flex.waarde)");
        }
        if (inArray("placefaqs", incl_arr) )
        {   // Voor zoeken in locatie en onroerendgoed nog tabellen erbij joinen.
            fieldstrarr.push("UPPER(loc.alg_locatie_omschrijving)", "UPPER(onr.alg_plaatsaanduiding)");
            gparams.GET.tables.push("alg_locatie loc", "alg_v_onroerendgoed_gegevens onr");
            gparams.GET.wheres.push("alg_algfaq.alg_locatie_key = loc.alg_locatie_key(+)",
                                    "alg_algfaq.alg_onroerendgoed_keys = onr.alg_onroerendgoed_keys(+)"
                                   );
        }
        if (inArray("issuetypefaqs", incl_arr) )
        {   // Voor zoeken in discipline en standaardmeldingen nog tabellen erbij joinen.
            fieldstrarr.push("UPPER(dis.ins_discipline_omschrijving)", "stm.mld_stdmelding_upper");
            gparams.GET.tables.push("mld_discipline dis", "mld_stdmelding stm");
            gparams.GET.wheres.push("mld_stdmeldingfaq.ins_discipline_key = dis.ins_discipline_key(+)",
                                    "mld_stdmeldingfaq.mld_stdmelding_key = stm.mld_stdmelding_key(+)"
                                   );
        }
    }
    gparams.GET.wheres.push(fac.createOrClause2(fieldstrarr, supper));

    this.REST_GET    = generic_REST_GET(this, gparams);
}
%>