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

    File:           fac_edit_xsl.asp
    Description:    Bewerken van een xsl
    Parameters:
        xmlnode     "reservering" o.i.d.
    Context:
    Note:           PRSSYS autorisatie is nodig
                    Voorwaarden:
                        - De klant cust.xsl moet opgesplitst zijn in meerdere bestanden, eentje
                          per xmlnode, dus er moet een reservering.xsl zijn voor reserveringen etc.
                        - Als er een reservering_concept.xsl is wordt die geopend, anders reservering.xsl
                        - Opslaan gaat *altijd* naar reservering_concept.xsl
                    TOOD: Knop om te promoveren/ door te voeren

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

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

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

var xmlnode = getQParam("xmlnode", "");
if (xmlnode) // safety check
{
    var sql = "SELECT 1"
            + "  FROM fac_srtnotificatie"
            + "   WHERE fac_srtnotificatie_xmlnode = " + safe.quoted_sql(xmlnode);
    var oRs = Oracle.Execute(sql);
    user.auth_required_or_abort(!oRs.Eof); // hij moet bestaan als srtnotificatie
    oRs.Close()
}

%>
<html>
<head>
   <title>XSLT Transform</title>
<%
    FCLTHeader.Generate();
%>
    <script type="text/javascript">
        // We laten het testen altijd door de server doen.
        function revertXSL()
        {
            if (confirm(L("lcl_xsl_revert") + "?"))
            {
                $.post($("form[name=xsledit]")[0].action + "?revert=1", $("[name=xsledit]").serialize(), FcltCallbackRefresh, "json");
            }
        }
        function saveXSL(promote)
        {
            if (!promote || confirm(L("lcl_xsl_doorvoeren_confirm")))
            {
                $.post($("form[name=xsledit]")[0].action + (promote?"?promote=1":""), $("[name=xsledit]").serialize(), FcltCallbackRefresh, "json");
            }
        }
        function previewXSL()
        {
            // Een controle op een ingevulde key om te testen zou nog mogen
            $.post($("form[name=xsledit]")[0].action + "?preview=1", $("[name=xsledit]").serialize(),
                FcltCallbackAndThen(function (data) {
                    if (data.success)
                    {
                        // XML krijgt eigen window, die wil je vaak laten staan
                        $("form[name=xslview]")[0].target = ($("#mode :selected").val()=='xml'?"RapPreview":"XmlPreview")
                        $("form[name=xslview]")[0].submit();
                    }
                    }),
             "json");
        }

        function onOtherFile()
        {
            window.location.href = "<%=Request.ServerVariables("SCRIPT_NAME")%>?xmlnode=" + $("#sDocuments").val();
        }
        $().ready(
            function () {
                $("#concept,#sDocuments").change(onOtherFile);
            })
    </script>
</head>
<body id="editbody">
<%
    buttons = [ { title: L("lcl_xsl_revert"), icon: "undo.png", action: "revertXSL()", id: "xslrevert" },
                { title: L("lcl_xsl_submit"), icon: "opslaan.png", action: "saveXSL()", id: "xslsave" },
                { title: L("lcl_xsl_preview"), icon: "xslpreview.png", action: "previewXSL()", id: "xslsave" },
                { title: L("lcl_xsl_doorvoeren"), icon: "accept.png", action: "saveXSL(1)", id: "xslcommit" }
               ];
    IFRAMER_HEADER(L("lcl_xsledit_frame"), buttons); %>

<label xclass="label"><%=L("lcl_xsledit_bon")%>:</label><select id='sDocuments'>
<% if (!xmlnode) { %>
    <option value=""><%=L("lcl_xsledit_selectnode")%></option>
<%
  }
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var sql = "SELECT DISTINCT(fac_srtnotificatie_xmlnode)"
            + "  FROM fac_srtnotificatie";
    var oRs = Oracle.Execute(sql);
    while (!oRs.Eof)
    {
        var vXmlnode = oRs("fac_srtnotificatie_xmlnode").Value;
        var vFilename = Server.MapPath(custpath + "/xsl/" + oRs("fac_srtnotificatie_xmlnode").Value + ".xsl");
        if (fso.FileExists(vFilename))
        {

%>    <option value="<%=safe.htmlattr(vXmlnode)%>" <%=vXmlnode==xmlnode?"selected='1'":""%>><%=safe.html(vXmlnode)%></option>
<%
        }
         oRs.MoveNext();
    }
    oRs.Close()
%></select>
    <% if (xmlnode) {

    var xslsheet = Server.MapPath(custpath + "\\xsl\\" + safe.filename(xmlnode) + "_concept.xsl");
    if (!fso.FileExists(xslsheet)) // dan de default beweken
    {
        var xslsheet = Server.MapPath(custpath + "\\xsl\\" + safe.filename(xmlnode) + ".xsl");
        var teaser = L("lcl_xsledit_original");
    }
    else
    {
        var teaser = L("lcl_xsledit_concept_date");
    }
    var f = fso.GetFile(xslsheet);
    teaser = teaser.format(toDateTimeString(new Date(f.DateLastModified)));
%>
<div id="edit">
  <%=teaser%>
  <form name="xsledit" action="fac_edit_xsl_save.asp" id="xsledit">
    <input type='hidden' name='xmlnode' value='<%=safe.htmlattr(xmlnode)%>'>
    <textarea name="xsldata" id="xsldata">
<%
    var streamResult = new ActiveXObject("ADODB.Stream")
    streamResult.Type = 2; // Text
    streamResult.Open();
    streamResult.CharSet = "utf-8";
    streamResult.LoadFromFile(xslsheet);

    Response.Write(safe.textarea(streamResult.ReadText(-1).replace(/\t/g,'    '))); // Anders is de tabgrootte 8 karakters
    streamResult.Close();
    // Let op: geen newline voor de volgende < / textarea>
%></textarea>
<% IFACE.FORM_END(); %>
  </form>
  <form name="xslview" action="fac_preview_xsl.asp" target='RapPreview' id="xslview">
      <input type='hidden' name='xmlnode' value='<%=xmlnode%>'>
      <label class="label"><%=L("lcl_xsledit_key")%>:</label><input name="key" class="required">
<%
      var sql = "SELECT fac_srtnotificatie_code, fac_srtnotificatie_code"
              + "  FROM fac_srtnotificatie"
              + " WHERE fac_srtnotificatie_xmlnode = " + safe.quoted_sql(xmlnode)
              + " ORDER BY 1";
      FCLTselector("srtnoti", sql, {  emptyOption: L("lcl_xsledit_nosrtnoti") });

    %><select name='mode' id='mode'>
        <option value=''><%= L("lcl_xsledit_nomode") %></option>
        <option value='xml'><%= L("lcl_xsledit_xmlmode") %></option>
        <option value='email'><%= L("lcl_xsledit_emailmode") %></option>
        <option value='summary'><%= L("lcl_xsledit_summarymode") %></option>
        <option value='kto'><%= L("lcl_xsledit_ktomode") %></option>
      </select>
<% IFACE.FORM_END(); %>
  </form>
</div>
<% } %>
</body>
</html>
