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

    File:           fac_locale_data.asp
    Status:
    Description:    Bewerkscherm voor locale-data
                    bewerk alle talen van een bepaalde datakolom

    Parameters:     kolomnaam ("RES_RUIMTE_NR") en
                    kolomkeyval (613)
                    kolomkeydata ('Vergaderzaal 3a') wordt ter info getoond.
                                We hebben helaas niet genoeg kennis om dit zelf in de database
                                te achterhalen

    Context:        Vanuit Facmgt (specifieke kolom) en vanuit fac_locale_list
    Note:           Er bestaat geen show-variant. Zou te weinig meerwaarde hebben

*/ %>
<!--#include file="../Shared/common.inc" -->
<!--#include file="../Shared/iface.inc" -->

<%
FCLTHeader.Requires({plugins:["suggest","jQuery"], js: ["jquery-ui.js"]})

var submitting = getQParamInt("submit", 0) == 1;
var kolomnaam = getQParam("kolomnaam");
var kolomkeyval = getQParamInt("kolomkeyval");
var kolomkeydata = getQParam("kolomkeydata", ""); // db_lang tekst
var lbl = getQParam("lbl", kolomnaam);
var typ = getQParam("typ", "input").toLowerCase();

var db_lang = S("db_lang");
var chosen_lang = Session("user_lang");

var autfunction = "WEB_LCLSYS";
if (kolomnaam == "fac_usrdata_omschr" || kolomnaam == "fac_usrtab_omschrijving")
{
    var sql = "SELECT f.fac_functie_code"
            + "  FROM fac_usrtab t "
            + "     , fac_functie f"
            + (kolomnaam == "fac_usrdata_omschr" ? ", fac_usrdata d" : "")
            + " WHERE t.fac_functie_key = f.fac_functie_key"
            + (kolomnaam == "fac_usrdata_omschr"
              ? " AND d.fac_usrtab_key = t.fac_usrtab_key AND fac_usrdata_key = " + kolomkeyval
              : " AND t.fac_usrtab_key = " + kolomkeyval
              );
    var oRs = Oracle.Execute(sql);
    if (!oRs.eof)
        autfunction = oRs("fac_functie_code").Value;
    oRs.Close();
}
var authparams = user.checkAutorisation(autfunction);

// Sporadisch komt een kolom wel eens groter voor in een importtabel. Dan veilig kiezen
// Melding 53648: zonder de RULE hint is dit vaak *stervens* traag
var sql = "SELECT /*+ RULE */ MIN(data_length)"
        + "  FROM user_tab_columns"
        + " WHERE column_name = " + safe.quoted_sql_upper(kolomnaam)
var oRs = Oracle.Execute(sql);
var maxlen = oRs(0).Value;
oRs.Close();
%>

<html>
  <head>
<%
    FCLTHeader.Generate();

    if (submitting) {
        protectRequest.validateToken();
        for (i in lcl.languages)
        {
            if (i != db_lang)
            {
                var tekst = getFParam("lcl_tekst_" + i, "");
                if (tekst == "")
                {
                    var sql = "DELETE FROM FAC_LOCALE"
                            + " WHERE fac_locale_lang = " + safe.quoted_sql(i)
                            + "   AND fac_locale_kolomnaam = " + safe.quoted_sql_upper(kolomnaam)
                            + "   AND fac_locale_kolomkeyval = " + kolomkeyval
                }
                else
                {
                    var sql = "SELECT 1 FROM FAC_LOCALE"
                            + " WHERE fac_locale_lang = " + safe.quoted_sql(i)
                            + "   AND fac_locale_kolomnaam = " + safe.quoted_sql_upper(kolomnaam)
                            + "   AND fac_locale_kolomkeyval = " + kolomkeyval;
                    oRs = Oracle.Execute(sql)
                    if (oRs.Eof)
                    {
                        var sql = "INSERT INTO FAC_LOCALE (fac_locale_lang, fac_locale_kolomnaam, fac_locale_kolomkeyval, fac_locale_tekst)"
                                + " VALUES('" + i + "', " + safe.quoted_sql_upper(kolomnaam) + ", " + kolomkeyval + ","
                                + safe.quoted_sql(tekst, maxlen) + ")";
                    }
                    else
                    {
                        var sql = "UPDATE FAC_LOCALE SET fac_locale_tekst = " + safe.quoted_sql(tekst, maxlen)
                                + " WHERE fac_locale_lang = " + safe.quoted_sql(i)
                                + "   AND fac_locale_kolomnaam = " + safe.quoted_sql_upper(kolomnaam)
                                + "   AND fac_locale_kolomkeyval = " + kolomkeyval;
                    }
                    oRs.Close();
                }
                Oracle.Execute(sql);
            }
        }

%>
      <script type="text/javascript">
        FcltMgr.closeDetail( window, { success: true });
      </script>
<%
      Response.End;
    }
%>
    <script type="text/javascript">
        $(function()
        {
           $('textarea').resize(function () { FcltMgr.resized(window) } );
           $('textarea').autogrow();
        });
  </script>
  </head>
  <body class="fclt-modal" id="localebody">
      <% MODAL_START(); %>
      <form id="lclform" name="lclform" action="fac_locale_data.asp?submit=1&kolomnaam=<%=safe.url(kolomnaam)%>&kolomkeyval=<%=kolomkeyval%>" method="post">
<%
        MODAL_BLOCK_START("mldReject", lbl, { "icon": "fa-language" });

function langlabel(code)
{
    return '<img style="height:12px" alt="' + lcl.languages[code] + '" src=../pictures/' + code + '.png>&nbsp;' + lcl.languages[code];
}


        //kolomkeydata
        var talen_arr = [];
        for (i in lcl.languages)
        {
            if (i != db_lang)
                talen_arr.push("SELECT " + safe.quoted_sql(i) + " fac_locale_lang, {0} volg FROM DUAL".format(talen_arr.length));
        }

        if (db_lang != user.lang())
        {   // We hebben hier de clientside tekst en die kan al naar user.lang() vertaald zijn.
            // We hebben niet voldoende info om de echte DB-tekst op te halen dus dan maar
            // een generieke waarschuwing. In ieder geval beter dan *foute* informatie
            kolomkeydata = L("lcl_vertalingen_dblangwarn").format(db_lang);
        }
        if (typ == "input")
            ROFIELDTR("", langlabel(db_lang), kolomkeydata);
        else
            ROTEXTAREATR("fldtxt", langlabel(db_lang), kolomkeydata, { html: "rows='3'" });

        var talen = talen_arr.join(" UNION ");
        var sql = "SELECT l.fac_locale_kolomnaam"
                + "     , l.fac_locale_kolomkeyval"
                + "     , tl.fac_locale_lang"
                + "     , l.fac_locale_tekst"
                + "  FROM fac_locale l, ("+talen+") tl"
                + " WHERE l.fac_locale_kolomkeyval(+) = " + kolomkeyval
                + "   AND l.fac_locale_kolomnaam(+) = " + safe.quoted_sql_upper(kolomnaam)
                + "   AND l.fac_locale_lang(+) = tl.fac_locale_lang"
                + " ORDER BY tl.volg";
        var oRs = Oracle.Execute(sql);

        while (!oRs.eof)
        {
            var fldname = "lcl_tekst_"+oRs("fac_locale_lang").Value;
            if (typ == "input")
                RWFIELDTR(fldname, "fld",
                          langlabel(oRs("fac_locale_lang").Value),
                          oRs("fac_locale_tekst").Value, {maxlength: maxlen });
            else
            { %>
                <tr>
                  <td class="label"><label for="<%=safe.htmlattr(fldname)%>"><nobr><%=langlabel(oRs("fac_locale_lang").Value)%></nobr></label></td>
                  <td>
                    <textarea class="fldtxt" name="<%=safe.htmlattr(fldname)%>" rows="3"><%=safe.textarea(oRs("fac_locale_tekst").Value)%></textarea>
                  </td>
                </tr>
            <% }

            oRs.MoveNext();
        }
        oRs.Close();

        MODAL_BLOCK_END();

        var buttons =  [ {title: L("lcl_submit"), icon: "fa-fclt-save", action: "document.forms.lclform.submit()", importance: 1 },
                         {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail(window, { cancel: true } )", importance: 3}];
        SIMPLE_BLOCK_START();
            CreateButtons(buttons);
        SIMPLE_BLOCK_END();
IFACE.FORM_END();
%>
      </form>
      <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
