<% /*
     $Revision$
     $Id$

     File:          audithistory.inc
     Description:   Algemene interface om audit history tabblad te openen.
*/ %>

<%
if (JSON_Result)
    __SafeLog("<span style='background-color:#0ff'>JSON_Result staat aan, dan is audithistory.inc includen erg onlogisch.</span>");

function AUDIT_HISTORY_BUTTON(pbuttons, params)
{
  var tablekey_array = params.tablekeyarray;
  var table_ref = params.tableref;
  var table_name = params.tablename;
  var keys = params.keys;
  var veldnamen = params.veldnamen;
  var oms = params.oms;
  var oms_veldnaam = params.oms_veldnaam;
  var id_veld = params.id_veld;
  var id_oms = params.id_oms;
  var sql = "";
  if (table_ref)
  {
    var where = [];
    for (var i = 0; i < table_ref.length; i++)
    {
      where.push("fa.fac_audit_tabelkey IN (SELECT fa2.fac_audit_tabelkey"
               + "                            FROM fac_audit fa2"
               + "                           WHERE fa2.fac_audit_tabelnaam = " + safe.quoted_sql(table_name)
               + "                             AND fa2.fac_audit_veldnaam = " + safe.quoted_sql(table_ref[i].field)
               + "                             AND fa2.fac_audit_waarde_oud = " + safe.quoted_sql(String(table_ref[i].fieldkey)) + ")");
    }
    sql = "SELECT DISTINCT fa.fac_audit_tabelkey"
        + "  FROM fac_audit fa"
        + " WHERE " + where.join(" AND ");
  }
  else if (oms_veldnaam && oms)
  {
      sql = "SELECT fac_audit_tabelkey"
          + "  FROM fac_audit"
          + " WHERE fac_audit_tabelnaam = " + safe.quoted_sql(table_name)
          + "   AND fac_audit_veldnaam = " + safe.quoted_sql(oms_veldnaam)
          + "   AND fac_audit_waarde_oud = " + safe.quoted_sql(oms);
  }
  else if (id_veld && id_oms)
  {
    sql = "SELECT " + table_name + "_key"
        + "  FROM " + table_name
        + " WHERE " + table_name + "_verwijder IS NOT NULL"
        + "   AND " + id_veld + " = " + safe.quoted_sql(id_oms);
  }

  var allshowed = true;
  if (sql != "")
  {
    var oRs = Oracle.Execute(sql + " ORDER BY 1 DESC");
    while(!oRs.eof)
    {
      tablekey_array.push(oRs(0).Value);
      oRs.Movenext();
      if (tablekey_array.length == 100) // In een "IN" statement mogen maar maximaal 1000 keys. Maximaal nemen we hier de meest recente 100 keys.
      {
        allshowed = false; // Niet alle keys zitten nu in tablekey_array. Niet alle audit tracking kan dan worden opgevraagd.
        break;
      }
    }
  }
%>
  <script  type="text/javascript">
    function audit_history()
    {
<%    var url = "appl/mgt/fac_audit.asp?mode=list&table={0}&record={1}".format(table_name, tablekey_array);
      var proturl = protectQS.create(url);
%>
      FcltMgr.openDetail("<%=proturl%>", L("lcl_history") + " " + "<%=safe.jsstring(params.historytitle)%>");
    }
  </script>
<%

  if (tablekey_array.length > 0)
  {
    sql = "SELECT COUNT(*)"
        + "  FROM fac_audit"
        + " WHERE fac_audit_tabelnaam = " + safe.quoted_sql(table_name)
        + "   AND fac_audit_tabelkey IN (" + tablekey_array.join(",") + ")";
    oRs = Oracle.Execute(sql);
    var cnt = oRs(0).Value;
    oRs.Close();
    if (cnt > 0)
      pbuttons.push({title: L("lcl_history") + " ({0})".format(cnt + (allshowed? "" : "+")), // Een "+" geeft aan dat er meer audit tracking regels zijn dan is weergegeven.
                     action:"audit_history()",
                     icon: "fa-history",
                     id: "btn_audit_history",
                     importance: 3 });
  }
}
%>