<% /*
    $Revision$
    $Id$

    File:               msg_list.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/json2.js" -->
<!-- #include file="../Shared/resultset_table_v2.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 msg_list(pautfunction, params)
{
  var autfunction = "WEB_MSGBOF";
  var authparams = user.checkAutorisation(autfunction);
  var canAdd = (authparams.PRSwritelevel < 9);

  var urole = params.urole;
  var fronto = (urole == "fo");
  var backo = (urole == "bo");
  var minfo = (urole == "mi"); // NOT APPLICABLE?
  var frontend = (urole == "fe" || (!fronto & !backo & !minfo));

  // 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 dateon = params.dateon;
  var dist_key = params.dist_key;
  var loc_key = params.loc_key;
  var bld_key = params.bld_key;

  var sql_date = null;

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

<html>
  <head>
    <% FCLTHeader.Generate({outputmode:outputmode}) %>
    <script type="text/javascript">
      urole = "<%=urole%>";

      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("msg_delete_portal.asp",
                data,
                FcltCallbackRefresh,
                "json");
        });
      }

      function msgEdit(row)
      {
        $(row).addClass('dirty');
        var msgData = eval('(' + row.getAttribute("ROWDATA") + ')');
        var url = "appl/MSG/msg_portal_edit.asp?nieuws_key=" + msgData.nieuws_key + "&urole=" + urole;
        FcltMgr.openDetail(url, "<%=L("lcl_message")%>" + ' ' + msgData.nieuws_key);
      }
    </script>
  </head>
  <body id="listbody">
<%
    var sqln = "SELECT fac_nieuws_titel titel,"
             + "       fac_nieuws_key nieuws_key,"
             + "       fac_nieuws_onrgoed_niveau niveau,"
             + "       fac_nieuws_omschrijving oms,"
             + "       descript,"
             + "       locatie,"
             + "       fac_nieuws_van van,"
             + "	     fac_nieuws_tot tot,"
             + "       fac_nieuws.prs_perslid_key pkey,"
             + "       " + S("prs_pers_string") + " naam,"
             + "       fac_nieuws_aanmaak aanmaak"
             + "  FROM fac_nieuws,"
             + "       prs_perslid p,"
             + "       (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_omschrijving,"
             + "               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"
             + " ) SCOPE"
             + " WHERE fac_nieuws.prs_perslid_key = p.prs_perslid_key(+)"
             + "   AND fac_nieuws_onrgoed_key = SCOPE.thekey(+)"
             + "   AND fac_nieuws_onrgoed_niveau = SCOPE.thelevel(+)";

    if (bld_key)
    {
      sqln += " AND SCOPE.thekey = " + bld_key
            + " AND SCOPE.thelevel =  'G' ";
    }
    else if (loc_key)
    {
      sqln += " AND (  (fac_nieuws_onrgoed_niveau = 'L' AND SCOPE.thekey = " + loc_key + ")"
            + "     OR (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(fac_nieuws_titel) LIKE " + safe.quoted_sql_wild("%" + searchtext + "%")
            + " OR UPPER(fac_nieuws_omschrijving) LIKE " + safe.quoted_sql_wild("%" + searchtext + "%")+")";
    }

    if (dateon)
    {
      sqln += " AND " + sql_date + " BETWEEN FAC_NIEUWS_VAN AND FAC_NIEUWS_TOT";
    }

    sqln += " ORDER BY fac_nieuws_van DESC, fac_nieuws_tot DESC";

    buttons = [];
    var addurl = "appl/msg/msg_message.asp?urole=" + urole;
    // TODO: check schrijfrechten
    buttons.push({ icon: "plus.png", title: L("lcl_add"), action: "FcltMgr.openDetail('" + addurl + "', '" + L("lcl_add") + "')" });

    var rst = new ResultsetTable({sql:sqln,
                                   keyColumn: "nieuws_key",
                                   ID: "factable",
                                   title: L("lcl_msg_search"),
                                   rowData: fnrowData,
                                   outputmode: outputmode,
                                   showAll: showall,
                                   filterParams: params,
                                   buttons: buttons
                                  });

    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", hasActions: true}));
    rst.addColumn(new Column({caption: L("lcl_location"), content: "locatie"}));
    rst.addColumn(new Column({caption: L("lcl_msg_alg_omschr"), content: "descript"}));
    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: "msgEdit", caption: L("lcl_change"), isDefault:  true});
    rst.addAction({ action: "msgDelete", caption: L("lcl_delete"), multi: true, multiOnce: true});

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

