<% /*
    $Revision$
    $Id$

    File:               fac_list_nieuws.inc
    Description:        Show an overview of news items in list-form,
                        which meets with the given requirements
    Parameters:
    Context:            Search action from msg_search.asp form
    Note:
*/ %>

<!-- #include file="../Shared/resultset_table_v2.inc" -->
<!-- #include file="../msg/msg.inc"-->

<%
FCLTHeader.Requires({ plugins:["jQuery"] })

function fnrowData(oRs)
{
  var nieuws_key = oRs("nieuws_key").value;

  var data = {nieuws_key: nieuws_key};

  return JSON.stringify(data);
}

function fnrowActionEnabler(oRs) {
  return {
    eShow: !(fac_nieuws_parentkey > 0)
  };
}

function fnHasReactions(oRs) {
  return oRs("nrreplies").Value > 0;
}

function msg_list(pautfunction, params)
{
  var autfunction = "WEB_MSGBOF";
  var authparams = user.checkAutorisation(autfunction);
  var canAdd = (authparams.PRSwritelevel < 9);

  // Voor resulttable de globalen zetten; zou ng anders moeten.
  var outputmode = params.outputmode; // print to 0 = screen, 1 = printer, 2 = excel, etc.
  var showall = params.showall;

  var searchtext = params.searchtext;
  var authgroup_arr = params.authgroup;
  var categorie_arr = params.categorie;
  var dateon = params.dateon;
  var nieuwsshow = params.nieuwsshow;
  var fac_nieuws_parentkey = params.fac_nieuws_parentkey;
  var tiny = params.tiny;
  var inline = params.inline;
  var dist_key = params.dist_key;
  var loc_key = params.loc_key;
  var bld_key = params.bld_key;
  var verdieping_key = params.verdieping_key;

  var sql_date = null;

  if (dateon)
  {
    var sql_date = dateon.toSQL(true);
  }
%>

<html>
  <head>
<%  if (!params.inline) {
      FCLTHeader.Generate({ outputmode: outputmode });
    } %>
    <script type="text/javascript">
      function inlineReactionsUrl(row) {
        var fac_nieuws_key = row.getAttribute("ROWKEY");
        return "fac_list_nieuws.asp?tiny=1&inline=1&fac_nieuws_parentkey=" + fac_nieuws_key;
      }

      function msgDelete(rowArray, isMulti)
      {
        FcltMgr.confirm(L("lcl_R_U_sure"), { autoconfirm: isMulti }, function() {
            var nieuwsKeyString = getKeyString(rowArray);
            var data = {
                nieuws_key: nieuwsKeyString
            };
            <% protectRequest.dataToken("data"); %>
            $.post("fac_delete_nieuws.asp",
                data,
                FcltCallbackRefresh,
                "json");
        });
      }

      function msgShow(row)
      {
        $(row).addClass('dirty');
        var msgData = JSON.parse(row.getAttribute("ROWDATA"));
        var url = "appl/MSG/fac_nieuws.asp?nieuws_key=" + msgData.nieuws_key;
        FcltMgr.openDetail(url, L("lcl_message") + ' ' + msgData.nieuws_key);
      }
    </script>
  </head>
  <body id="listbody">
<%
    var sqln = "SELECT n.fac_nieuws_titel titel,"
             + "       n.fac_nieuws_key nieuws_key,"
             + "       n.fac_nieuws_onrgoed_niveau niveau,"
             + "       n.fac_nieuws_omschrijving oms,"
             + "       n.fac_nieuws_show,"
             + "       scope.descript,"
             + "       scope.locatie,"
             + "       n.fac_nieuws_van van,"
             + "	   n.fac_nieuws_tot tot,"
             + "       n.prs_perslid_key pkey,"
             + "       " + S("prs_pers_string") + " naam,"
             + "       n.fac_nieuws_aanmaak aanmaak,"
             + "       n.fac_groep_key,"
             +         lcl.xsql("f.fac_groep_omschrijving", "f.fac_groep_key") + " fac_groep_naam,"
             +         lcl.xsql("fng.fac_nieuws_groep_omschrijving", "fng.fac_nieuws_groep_key") + " fac_nieuwsgroep,"
             + "       (SELECT COUNT (*)"
             + "          FROM fac_nieuws fn"
             + "         WHERE fn.fac_nieuws_parentkey = n.fac_nieuws_key"
             + "           AND fac_nieuws_omschrijving IS NOT NULL) nrreplies"
             + "  FROM fac_nieuws n,"
             + "       prs_perslid p,"
             + "       fac_nieuws_groep fng,"
             + "       fac_groep f,"
             + "       (SELECT a.alg_locatie_omschrijving descript,"
             + "               a.alg_locatie_key thekey,"
             + "               a.alg_district_key upkey,"
             + "               'L' thelevel,"
             + "               'Locatie' locatie"
             + "          FROM alg_locatie a"
             + "        UNION ALL"
             + "        SELECT d.alg_district_omschrijving,"
             + "               d.alg_district_key,"
             + "               d.alg_regio_key,"
             + "               'D',"
             + "               'District'"
             + "          FROM alg_district d"
             + "        UNION ALL "
             + "        SELECT l.alg_locatie_omschrijving||'-'||g.alg_gebouw_naam,"
             + "               g.alg_gebouw_key,"
             + "               g.alg_locatie_key,"
             + "               'G',"
             + "               'Gebouw'"
             + "          FROM alg_gebouw g, alg_locatie l"
             + "          WHERE g.alg_locatie_key = l.alg_locatie_key"
             + "        UNION ALL "
             + "        SELECT l.alg_locatie_omschrijving||'-'|| g.alg_gebouw_naam || ' ' || v.alg_verdieping_omschrijving,"
             + "               v.alg_verdieping_key,"
             + "               v.alg_gebouw_key,"
             + "               'V',"
             + "               'Verdieping'"
             + "          FROM alg_verdieping v, alg_gebouw g, alg_locatie l"
             + "          WHERE g.alg_gebouw_key = v.alg_gebouw_key"
             + "            AND l.alg_locatie_key = g.alg_locatie_key"
             + " ) SCOPE"
             + " WHERE n.prs_perslid_key = p.prs_perslid_key(+)"
             + "   AND n.fac_nieuws_onrgoed_key = SCOPE.thekey(+)"
             + "   AND n.fac_nieuws_onrgoed_niveau = SCOPE.thelevel(+)"
             + "   AND n.fac_nieuws_groep_key = fng.fac_nieuws_groep_key(+)"
             + "   AND n.fac_groep_key = f.fac_groep_key(+)"
             + "   AND n.fac_nieuws_omschrijving IS NOT NULL"; /* Filter de ##LIKE##'s eruit */

    if (verdieping_key)
    {
      sqln += " AND SCOPE.thekey = " + verdieping_key
            + " AND SCOPE.thelevel =  'V' ";
    }
    else if (bld_key)
    {
      sqln += " AND SCOPE.thekey = " + bld_key
            + " AND SCOPE.thelevel =  'G' ";
    }
    else if (loc_key)
    {
      sqln += " AND (  (n.fac_nieuws_onrgoed_niveau = 'L' AND SCOPE.thekey = " + loc_key + ")"
            + "     OR (n.fac_nieuws_onrgoed_niveau = 'G' AND SCOPE.upkey = "  + loc_key + ")"
            + "     )";
    }
    else if (dist_key)
    {
      sqln += " AND SCOPE.thekey = " + dist_key
            + " AND SCOPE.thelevel = 'D' ";
    }

    if (searchtext)
    {
      sqln += " AND (UPPER(n.fac_nieuws_titel) LIKE " + safe.quoted_sql_wild("%" + searchtext + "%")
            + " OR UPPER(n.fac_nieuws_omschrijving) LIKE " + safe.quoted_sql_wild("%" + searchtext + "%")+")";
    }

    if (dateon)
    {
      sqln += " AND " + sql_date + " BETWEEN n.fac_nieuws_van AND n.fac_nieuws_tot";
    }
    if (authgroup_arr && authgroup_arr.length)
    {
      sqln += " AND n.fac_groep_key IN (" + authgroup_arr.join(",") + ")";
    }
    if (categorie_arr && categorie_arr.length)
    {
      sqln += " AND n.fac_nieuws_groep_key IN (" + categorie_arr.join(",") + ")";
    }
    if (nieuwsshow > 0)
    {
      sqln += " AND BITAND(n.fac_nieuws_show, " + nieuwsshow + ") <> 0";
    }
    if (fac_nieuws_parentkey > -1) {
      sqln += " AND n.fac_nieuws_parentkey = " + fac_nieuws_parentkey;
    } else {
      sqln += " AND n.fac_nieuws_parentkey IS NULL";
    }

    sqln += " ORDER BY COALESCE(n.fac_nieuws_van, n.fac_nieuws_aanmaak) DESC";

    function fnNieuwsShow (oRs)
    {
      var result = [];
      for (var i = 1; i <= 15; i = i * 2) {
        if (oRs("fac_nieuws_show").Value & i) {
          result.push(msg.getmsgnieuwsshowtext(i));
        }
      }
      return result.join(" | ")
    }

    buttons = [];
    var addurl = "appl/msg/fac_nieuws.asp";
    // TODO: check schrijfrechten
    if (!inline) {
      buttons.push({ icon: "fa-plus", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + safe.jsstring(L("lcl_add")) + "')" });
    }

    var rst = new ResultsetTable({sql:sqln,
                                   keyColumn: "nieuws_key",
                                   ID: "fac_list_nieuws",
                                   title: L("lcl_msg_search"),
                                   rowData: fnrowData,
                                   rowActionEnabler: fnrowActionEnabler,
                                   inlineDetails: "inlineReactionsUrl",
                                   hasInlineDetails: fnHasReactions,
                                   inline: inline,
                                   outputmode: outputmode,
                                   showAll: showall,
                                   filterParams: params,
                                   buttons: buttons
                                });

    if (!tiny) {
      rst.addColumn(new Column({caption: L("lcl_msg_nieuws_show"), content: fnNieuwsShow, hasActions: true}));
      rst.addColumn(new Column({caption: L("lcl_msg_geldig_van"), content: "van"}));
      rst.addColumn(new Column({caption: L("lcl_msg_geldig_tot"), content: "tot"}));
      rst.addColumn(new Column({caption: L("lcl_msg_titel"), content: "titel"}));
      rst.addColumn(new Column({caption: L("fac_nieuws_groep"), content: "fac_nieuwsgroep"}));
      rst.addColumn(new Column({caption: L("fac_groep"), content: "fac_groep_naam"}));
      rst.addColumn(new Column({caption: L("lcl_location"), content: "locatie"}));
      rst.addColumn(new Column({caption: L("lcl_place"), content: "descript"}));
    } else {
      rst.addColumn(new Column({caption: L("lcl_msg_reply"), content: "oms", hasActions: true}));
    }
    rst.addColumn(new Column({caption: L("lcl_user"), content: "naam"}));
    rst.addColumn(new Column({caption: L("lcl_msg_nieuws_aanmaak"), content: "aanmaak"}));

    rst.addAction({ action: "msgShow", caption: L("lcl_change"), enabler: "eShow", isDefault: true});
    rst.addAction({ action: "msgDelete", caption: L("lcl_delete"), multi: true, multiOnce: true});

    var cnt = rst.processResultset();
%>
  </body>
</html>
<%}%>

