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

    File:           mgt_genericsql.asp

    Description:    Ingang om generiek sql SELECT statement uit te voeren
    Context:        Rechtstreeks of vanuit logfile

    Notes:
*/
%>
<!-- #include file="../shared/common.inc" -->
<!-- #include file="../Shared/iface.inc" -->
<%
FCLTHeader.Requires({ plugins: ["jQuery"] })

__Logging = 0; // Is te laat...
    Application("otap_environment") == "O" || user.checkAutorisation("WEB_FACFAC");
    var sql = getFParam("sql", getQParam("sql", ""));

/*
//Onze SQL-statement zo maar naar een cloud-service sturen mag vast niet van ISO-27001
*/

%>
<html>
  <head>
<%  FCLTHeader.Generate({outputmode: 0}); %>
    <style>
        table.rstable td {
            border-left: 1px solid #bbb;
        }
        body#searchbody td {
            line-height:1.2em!important;
        }
        .message {
            background-color: orange;
            color: black;
            padding: 2px 5px 4px 5px;
            height: 20px;
            vertical-align: middle;
        }
    </style>

    <script>
<%      if (sql == "opener")
        {
            sql = "";
%>
            $(function()
                {
                    if (window.opener && window.opener.genericsql)
                    {
                        $("#sql").val(window.opener.genericsql);
                        $("form").submit();
                    }
                });
<%      }
%>
        $(function ()
            {
                <% if (getQParamInt("autoexec", 0) == 1) { %>
                    doSubmit();
                <% } else { %>
                    doFormat();
                <% } %>

           }
         );
        function doSubmit()
        {
            doFormat();
            document.forms.u2.submit();
        }

        function doFormat()
        {
            var sql = $("#sql").val();
            // Als er exact 1 SELECT, FROM en WHERE is doen we een beetje formatting
            if ( sql.indexOf("\n") == -1 &&
                (sql.match(/SELECT/ig) || []).length == 1 &&
                (sql.match(/FROM/ig) || []).length == 1 &&
                (sql.match(/WHERE/ig) || []).length <= 1
               )
            {
                sql = "_"+sql.trim();
                sql = sql.replace(/_SELECT/i, "SELECT");
                sql = sql.replace(/ FROM/i, "\nFROM");
                sql = sql.replace(/ WHERE/i, "\nWHERE");
                sql = sql.replace(/ ORDER BY/i, "\nORDER BY");
                sql = sql.replace(/ GROUP BY/i, "\nGROUP BY");
                sql = sql.replace(/ AND/ig, "\n    AND");
                sql = sql.replace(/ , /ig, ", ");
            }
            $("#sql").val(sql);
        }
    </script>
  </head>

  <body id="searchbody">
    <form id="u2" name="u2" method="post" action="mgt_genericsql_list.asp" target="workFrame">
        <% SEARCH_PAGE_START({ header: L("lcl_menu_fac_anysql") });
        BLOCK_START("the-sql", "", { icon: "fa-database", wide: true, nopadding: true });
          RWTEXTAREATR("sql", "fldtxt", "", sql, { html: "style='height:200px' spellcheck='false'", maxlength: 999999, no_bb_codes: true });
        BLOCK_END();
        var buttons = [ {title: L("lcl_usrrap_uitvoeren"), action: "doSubmit()", id: "bSearch", icon: "fa-fclt-save" } ];
        SIMPLE_BLOCK_START();
        CreateButtons(buttons, { entersubmit: true, showIcons: true });
        SIMPLE_BLOCK_END();
        SEARCH_PAGE_END();
%>
      </form>
    <div id="result">
      <iframe width="100%" height="100%"
              src="../Shared/empty.asp"
              name="workFrame" id="workFrame"
              onload="FcltMgr.iframeLoaded(this)"
              frameborder="0" scrolling="no">
      </iframe>
    </div>
  </body>
</html><% ASPPAGE_END(); %>
