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

    File:        fac_faq_list.asp
    Status:      prototype
    Description: zoekt op basis van de zoekvelden in de kennisbank
    Parameters:  search: zoekstring
                 urole: voor FO is meer info beschikbaar dan voor de fe (default)
                        op basis van fac_faq_level
    Context:     Vanuit fac_faq_search.asp
    */
%>
<!--#include file="../Shared/common.inc" -->
<!--#include file="../Shared/getkenmerksql.inc" -->
<!--#include file="../Shared/resultset_table_v2.inc" -->
<!--#include file="fac.inc" -->

<%
FCLTHeader.Requires({js: []});

var searchtext = getQParam("searchtext", null);
var disc_key_arr = getQParamIntArray("discmulti", []); // Productgroep
var stdm_key_arr = getQParamIntArray("stdmmulti", []); // Melding (Stdmelding)
var fac_faq_level = getQParamInt("fac_faq_level", -1); // Zichtbaar voor
var fac_faq_lang = getQParam("fac_faq_lang", null); // Taal
if (fac_faq_lang == "-1") fac_faq_lang = null;

disc_key_arr = (disc_key_arr.length > 0 ? disc_key_arr : null);
stdm_key_arr = (stdm_key_arr.length > 0 ? stdm_key_arr : null);

var params = {searchtext: searchtext,
              disc_key_arr: disc_key_arr,
              stdm_key_arr: stdm_key_arr,
              fac_faq_level: fac_faq_level,
              fac_faq_lang: fac_faq_lang};

var urole = getQParamSafe("urole", "fe");
var fronto = (urole == "fo");
var outputmode = getQParamInt("outputmode", 0); //  0 = screen, 1 = print, 2 = excel etc
var print = (outputmode == 1 || outputmode == 5);
var excel = (outputmode == 2 || outputmode == 6);
var showall = getQParamInt("showall", 0) == 1;
var wholeword = getQParam("wholeword", "off") == "on";

var authparamsBOF = user.checkAutorisation("WEB_FAQBOF", true); // ziet alles
var authparamsFOF = user.checkAutorisation("WEB_FAQFOF", true); // ziet ook FO-items (en eigen items)
var authparamsUSE = user.checkAutorisation("WEB_FAQUSE", true); // ziet ook FE-items (en eigen items)
user.auth_required_or_abort(authparamsBOF || authparamsFOF || authparamsUSE);

function make_faq_list (searchtext, ororand)
{
      var supper = searchtext.toUpperCase();

      var sql = "SELECT f.fac_faq_key"
              + "     , f.fac_faq_question"
              + "     , f.fac_faq_answer"
              + "     , f.fac_faq_datum"
              + "     , f.fac_faq_source"
              + "     , f.fac_faq_url"
              + "     , f.fac_faq_lang"
              + "     , f.fac_faq_level"
                  + " , (SELECT MIN(" + lcl.xsql('sm.mld_stdmelding_omschrijving', 'sm.mld_stdmelding_key')
                  + "              )"
                  + "      FROM mld_stdmeldingfaq mf"
                  + "         , mld_stdmelding sm"
                  + "     WHERE mf.mld_stdmelding_key = sm.mld_stdmelding_key(+)"
                  + "       AND mf.fac_faq_key = f.fac_faq_key"
                  + "   ) mld_stdmelding_omschrijving"
                  + " , (SELECT MIN(CASE"
                  + "               WHEN sd.ins_srtdiscipline_prefix IS NULL"
                  + "               THEN ''"
                  + "               ELSE sd.ins_srtdiscipline_prefix || '-'"
                  + "               END"
                  + "               ||" + lcl.xsql('m1.ins_discipline_omschrijving', 'm1.ins_discipline_key')
                  + "              )"
                  + "      FROM mld_v_aanwezigdiscipline m1"
                  + "         , ins_srtdiscipline sd"
                  + "         , mld_stdmeldingfaq mf"
                  + "     WHERE m1.ins_srtdiscipline_key = sd.ins_srtdiscipline_key"
                  + "       AND m1.ins_discipline_key = mf.ins_discipline_key"
                  + "       AND mf.fac_faq_key = f.fac_faq_key"
                  + "   ) vakgroep"
              + "     , fac_faq_hint"
                  + ",  (SELECT " + S("prs_pers_string") + " naam"
                  + "      FROM prs_perslid p"
                  + "     WHERE p.prs_perslid_key = f.prs_perslid_key"
                  + "   ) aangepastdoor"
              + "     , (SELECT count(*)"
                  + "     FROM mld_stdmeldingfaq mf"
                  + "    WHERE mf.fac_faq_key = f.fac_faq_key"
                  + "   ) mld_stdmelding_count"
              + "  FROM fac_faq f"
              + " WHERE 1 = 1";

      if (ororand)
      {
         sql += "   AND (" + fac.createOrOrClause("UPPER(fac_faq_question)", supper, wholeword)
              + "        OR " + fac.createOrOrClause("UPPER(fac_faq_answer)", supper, wholeword)
              + "        OR " + fac.createOrOrClause("UPPER(fac_faq_hint)", supper, 0, wholeword)+ ")";
      }
      else
      {
         sql += "   AND " + fac.createOrClause2(["UPPER(fac_faq_question)","UPPER(fac_faq_answer)","UPPER(fac_faq_hint)"], supper, wholeword);
      }

      if (stdm_key_arr)
        sql += " AND EXISTS (SELECT ''"
                         + "   FROM mld_stdmeldingfaq"
                         + "  WHERE mld_stdmelding_key IN (" + stdm_key_arr + ") "
                         + "    AND fac_faq_key = f.fac_faq_key"
                         + ")";
      else
        if (disc_key_arr)
          sql += " AND EXISTS (SELECT ''"
                           + "   FROM mld_stdmeldingfaq"
                           + "  WHERE ins_discipline_key IN (" + disc_key_arr + ")"
                           + "    AND fac_faq_key = f.fac_faq_key"
                           + ")";



      if (fac_faq_level > 0)
        sql += " AND BITAND(f.fac_faq_level, " + fac_faq_level +") = " + fac_faq_level;

      if (fac_faq_lang != null)
        sql += " AND f.fac_faq_lang LIKE " + safe.quoted_sql(fac_faq_lang);
      else
      {
        if (!authparamsBOF && !authparamsFOF)
            sql += " AND (f.fac_faq_lang = " + safe.quoted_sql(user.lang()) + " OR f.fac_faq_lang IS NULL)";
      }

      if (!authparamsBOF)
      {
        var bits = 0;
        if (authparamsFOF)
            bits |= (urole=="fo"?3:2);
        if (authparamsUSE)
            bits |= 1;
        sql += " AND BITAND (fac_faq_level, " + bits + ") <> 0";
        if (!authparamsFOF)
            sql += " AND (fac_faq_datum < SYSDATE OR prs_perslid_key = " + user_key + ")"; // zelf ingevoerd zie je wel terug
      }

      sql += getKenmerkSql("FAQ", "f.fac_faq_key"); // Zoeken op kenmerken, indien geavanceerd ingevuld

      sql += " ORDER BY fac_faq_rank DESC NULLS LAST"
           + "        , fac_faq_datum DESC"
           + "        , fac_faq_question";

    if (!ororand)
    {
        var testRs = Oracle.Execute(sql);
        if (testRs.Eof)
        {
            make_faq_list (searchtext, true);
            return;
        }
        testRs.Close();
    }

    function fnQuestion(oRs)
    {
      return "<div class='faqq'>" + safe.html(oRs("fac_faq_question").value) + "</div>";
    }

    function fnLevel(oRs)
    {
        var r = "?";
        switch (oRs("fac_faq_level").value)
        {
            case 1: r = L("lcl_faq_level1"); break;
            case 2: r = L("lcl_faq_level2"); break;
            case 3: r = L("lcl_faq_level3"); break;
        }
        return r;
    }

    function fnSource(oRs)
    {
      var lref = "";
      if (oRs("fac_faq_url").value)
      {
        if (oRs("fac_faq_url").Value.slice(0,4) == "http") // extern
          var url = oRs("fac_faq_url").Value;
        else // in cust subdir
          var url = custpath + "/" + oRs("fac_faq_url").Value;

        lref = "<a target=_new href='" + safe.htmlattr(url) + "'>" + safe.html(oRs("fac_faq_source").value) + "</a>";
      }
      else
        lref = safe.html(oRs("fac_faq_source").value);
      return lref;
    }

    function fnVakgroep(oRs)
    {
      var vakgroep = "";
      if (oRs("mld_stdmelding_count").value > 1)
      {
        vakgroep = L("lcl_faq_stdmelding_m").format(oRs("mld_stdmelding_count").value);
      }
      else
      {
        vakgroep = oRs("vakgroep").value;
      }
      return safe.html(vakgroep);
    }

    function fnStdmelding(oRs)
    {
      var stdmelding = "";
      if (oRs("mld_stdmelding_count").value == 1)
      {
        stdmelding = oRs("mld_stdmelding_omschrijving").value;
      }
      return safe.html(stdmelding);
    }


       var canAdd = authparamsFOF && authparamsFOF.PRSwritelevel < 9 && authparamsFOF.ALGwritelevel < 9;

        buttons = [];
        if (canAdd)
        {
          var addurl = "appl/fac/fac_faq.asp";
          buttons = [{ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "', {callback: FcltMgr.reload })" }]
        }
        var rst = new ResultsetTable({ sql: sql,
                                       keyColumn: "fac_faq_key",
                                       flexModule: "FAQ",
                                       flexId: "fac_faq_key",
                                       ID: "faqtable",
                                       title: L("lcl_faq_items"),
                                       outputmode: outputmode,
                                       showAll: showall,
                                       filterParams: params,
                                       buttons: buttons
                                      });
        rst.addColumn(new Column({hasActions: true, caption: L("lcl_faq_question"), content: fnQuestion}));
        rst.addColumn(new Column({caption: L("lcl_faq_answer"), combine: true, content: "fac_faq_answer"}));

        rst.addColumn(new Column({caption: L("lcl_faq_bron"), content: fnSource}));
        rst.addColumn(new Column({caption: L("lcl_faq_datum"), content:"fac_faq_datum", datatype: "date"}));

        // BOF altijd, FOF bij printen
        if (authparamsBOF || (authparamsFOF && outputmode != 0))
        {
            rst.addColumn(new Column({caption: L("lcl_vakgroup"), content: fnVakgroep}));
            rst.addColumn(new Column({caption: L("lcl_complain"), content: fnStdmelding, combine: true}));
            if (fac_faq_lang == null)
              rst.addColumn(new Column({caption: L("lcl_faq_lang"), content: "fac_faq_lang"}));
            if (fac_faq_level < 0)
              rst.addColumn(new Column({caption: L("lcl_faq_level"), content: fnLevel}));
        }
        if (outputmode != 0 && (authparamsBOF || authparamsFOF))
        { // Met printen wel tonen?
          rst.addColumn(new Column({caption: L("lcl_faq_hint"), content: "fac_faq_hint"}));
          rst.addColumn(new Column({caption: L("lcl_faq_link"), content: "fac_faq_url"}));
          rst.addColumn(new Column({caption: L("lcl_faq_ingevoerd"), content: "aangepastdoor"}));
        }

        rst.addAction({ action: "faq_edit", caption: L("lcl_edit"), isDefault: true } );
        var cnt = rst.processResultset();

    return cnt;
}
%>
<html>
  <head>
   <% FCLTHeader.Generate({ outputmode: outputmode }); %>
<script>
   function faq_edit(row) {
       var faq_key = row.getAttribute("ROWKEY");

       FcltMgr.openDetail("appl/fac/fac_faq.asp?urole=<%=urole%>&faq_key=" + faq_key, "<%=L("lcl_faq_item")%>");
   }
</script>
</head>
<body id="listbody">
<%
if (searchtext != null)
{
    var cnt = make_faq_list (searchtext);
}
%>
</body>
</html>

