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

    File:        fac_locale_list.asp
    Description: zoekt op basis van de zoekvelden in de vertaaltabel
    Parameters:  search: zoekstring
                 dialect
                 taal
    Context:     Vanuit fac_locale_search.asp
    */
%>

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

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

var autfunction = "WEB_LCLSYS";
var authparams = user.checkAutorisation(autfunction);

var vDialect_key = getQParamInt("sDialect", -1);
var vTaal = getQParam("sTaal", "");
var vSearchString = getQParam("sString", "");
var vSearchGroep = getQParam("sGroep", "");
var outputmode = getQParamInt("outputmode", 0);
var showall = getQParamInt("showall", 0) == 1;
var SorteerKolommen = [];
var vWhere = "";
var rst_keyColumn = "";
var rst_nameColumn = "";

// Bepaal de verschillende talen of dialecten die er gebruikt worden
// en maak daar kolommen voor.
if (vSearchGroep == "lang")
{
  rst_keyColumn = "fac_locale_default";
  rst_nameColumn = L("lcl_lcl_default");
  if (vTaal)
  {
    vWhere = " WHERE fac_locale_lang = " + safe.quoted_sql_upper(vTaal);
  }
  sqlo = "SELECT distinct fac_locale_lang"
       + "     , fac_locale_lang fac_label"
       + "  FROM fac_v_locale_kolom"
       + vWhere
       + " ORDER BY fac_label";
  SorteerKolommen.push(" l.fac_locale_kolomkeyval");

  var oRs1 = Oracle.Execute (sqlo);
  while (!oRs1.eof)
  {
    if (oRs1("fac_locale_lang").value == null)
      waarde = "";
    else
      waarde = oRs1("fac_locale_lang").value;
    SorteerKolommen.push(" Max(Decode(l.fac_locale_lang"
                        + ", " + safe.quoted_sql(waarde)
                        + ", l.fac_locale_tekst, null)) "
                        + "COL" + SorteerKolommen.length);
    oRs1.moveNext();
  }
  oRs1.Close();

  sqln = "SELECT l.fac_locale_default"
       + "     , l.fac_locale_kolomnaam"
       + "     , " + SorteerKolommen.join(',')
       + "  FROM fac_v_locale_kolom l"
       + " WHERE 1 = 1"
  if (vSearchString != "")
  {
    sqln += " AND (L.fac_locale_default, l.fac_locale_kolomkeyval) IN "
         +  "(SELECT fac_locale_default, fac_locale_kolomkeyval "
         +  " FROM fac_v_locale_kolom F "
         +  " WHERE (  (UPPER(F.fac_locale_tekst) LIKE "+safe.quoted_sql_wild("%"+vSearchString+"%") + ") "
         +         "OR (UPPER(F.fac_locale_default)  LIKE "+safe.quoted_sql_wild("%"+vSearchString+"%") + ") "
         +         ") "
         +  ") ";
  }
  sqln += " GROUP BY l.fac_locale_default, l.fac_locale_kolomnaam, l.fac_locale_kolomkeyval ";
}
else
{
  rst_keyColumn = "fac_localeitems_dialect_id";
  rst_nameColumn = L("lcl_lcl_naam");
  if (vDialect_key > 0)
  {
    vWhere = " AND l.fac_locale_dialect_key = " + vDialect_key;
  }
  sqlo = "SELECT distinct l.fac_locale_dialect_key"
       + "     , "+ lcl.xsql('d.ins_srtdiscipline_omschrijving','d.ins_srtdiscipline_key') +" fac_label"
       + "  FROM fac_v_locale_lcl l, ins_srtdiscipline d"
       + " WHERE l.fac_locale_dialect_key = d.ins_srtdiscipline_key"
       + vWhere
       + " ORDER BY fac_label";
  SorteerKolommen.push("fac_localeitems_lcl");

  var oRs1 = Oracle.Execute (sqlo);
  while (!oRs1.eof)
  {
    if (oRs1("fac_locale_dialect_key").value == null)
      waarde = "null";
    else
      waarde = oRs1("fac_locale_dialect_key").value;
    SorteerKolommen.push("Max(Decode(l.fac_locale_dialect_key, "
                        + waarde
                        + ", l.fac_locale_tekst, null)) "
                        + "COL" + SorteerKolommen.length);
    oRs1.moveNext();
  }
  oRs1.Close();

  sqln = "SELECT l.fac_localeitems_dialect_id"
       + "     , l.fac_locale_lang"
       + "     , " + SorteerKolommen.join(',')
       + "  FROM fac_v_locale_lcl l"
       + "     , ins_srtdiscipline d"
       + " WHERE l.fac_locale_dialect_key = d.ins_srtdiscipline_key"
  if (vSearchString != "")
  {
    sqln += " AND (l.fac_locale_dialect_key, l.fac_locale_lang) IN "
         +  "(SELECT fac_locale_dialect_key, fac_locale_lang "
         +  " FROM fac_v_locale_lcl F "
         +  " WHERE (  (UPPER(F.fac_locale_tekst) LIKE " + safe.quoted_sql_wild("%" + vSearchString + "%") + ") "
         +         "OR (UPPER(F.fac_localeitems_lcl)  LIKE " + safe.quoted_sql_wild("%" + vSearchString + "%") + ") "
         +         ") "
         +  ") ";
  }
  sqln += " GROUP BY l.fac_localeitems_dialect_id, l.fac_locale_lang, l.fac_localeitems_lcl";
}
%>

<html>
  <head>
    <% FCLTHeader.Generate(); %>
    <script type="text/javascript">
      function localeedit(row)
      {
        $(row).addClass('dirty');
        var facData = JSON.parse(row.getAttribute("ROWDATA"));
        $(row).addClass('dirty');

<%      if (vSearchGroep == 'lang')
        {
%>
          url = 'appl/fac/fac_locale_data.asp?kolomnaam='    + facData.fac_kolomnaam
                                          + '&kolomkeyval='  + facData.fac_kolomkeyval
                                          + '&kolomkeydata=' + escape(facData.fac_kolomkeydata);
          FcltMgr.openDetail(url, L("lcl_lcl_vertaling"));
<%      }
        else
        {
%>
          if (facData.fac_dialect_key != -1)
          {
            url = 'appl/fac/fac_locale_dialect.asp?dialect_id='   + facData.fac_dialect_id
                                               + '&dialect_key='  + facData.fac_dialect_key
                                               + '&lang='         + facData.fac_dialect_lang;
            FcltMgr.openDetail(url, L("lcl_adress"));
          }
<%      } %>
      }
    </script>
  </head>

  <body id="listbody">
<%
    function fnrowData(oRs)
    {
      if (vSearchGroep == 'lang')
      {
        var kolomnaam    = oRs("fac_locale_kolomnaam").value;
        var kolomkeyval  = oRs("fac_locale_kolomkeyval").value;
        var kolomkeydata = oRs("fac_locale_default").value;

        if (kolomnaam    == null) kolomnaam    = -1;
        if (kolomkeyval  == null) kolomkeyval  = -1;
        if (kolomkeydata == null) kolomkeydata = -1;

        var data = {fac_kolomnaam:kolomnaam,
                    fac_kolomkeyval:kolomkeyval,
                    fac_kolomkeydata:kolomkeydata};
      }
      else
      {
        var dialect_id   = oRs("fac_localeitems_dialect_id").value;
        var dialect_key  = vDialect_key; // de parameter
        var dialect_lang = oRs("fac_locale_lang").value;

        if (dialect_id   == null) dialect_id   = -1;
        if (dialect_lang == null) dialect_lang = 'NL';

        var data = {fac_dialect_id:dialect_id,
                    fac_dialect_key:dialect_key,
                    fac_dialect_lang:dialect_lang};
      }
      return JSON.stringify(data);
    }

    function fnNaam (oRs)
    {
      if (vSearchGroep == "lang")
      {
        return "<div class='locn'>"+oRs("fac_locale_default").value+"</div>";
      }
      else
      {
        return "<div class='locn'>"+oRs("fac_localeitems_lcl").value+"</div>";
      }
    }

    var rst = new ResultsetTable({ sql: sqln,
                                   keyColumn: rst_keyColumn,
                                   ID: "localetable",
                                   rowData: fnrowData,
                                   showAll: showall,
                                   buttons: []
                                });

    rst.addColumn(new Column({hasActions: true, caption:L("lcl_lcl_default"), content:fnNaam}));

    // Toon het variabel aantal kolommen voor Taal/Dialect
    oRs1 = Oracle.Execute(sqlo);
    var nn = 1;
    while (!oRs1.eof)
    {
      rst.addColumn(new Column({caption: oRs1("fac_label").value, content: "COL"+nn}));
      nn++;
      oRs1.moveNext();
    }
    oRs1.Close();

    rst.addAction({action: "localeedit", caption: L("lcl_edit"), isDefault: true});
    rst.processResultset();
%>
  </body>
</html>

<% ASPPAGE_END(); %>
