<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

     File:               rs_columns_save.asp
     Description:        Save user-defined columns for non-scaffolding lists.
     Parameters:
     Globals:
     Context:            used by resultset_table_v2.js
     Note:
*/ %>
<%
var JSON_Result = true;
%>
<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/save2db.inc"-->
<%
protectRequest.validateToken();
var code = getFParam("code", "");
var cols = getFParamArray("columns", []);
var resetcols = getFParamInt("resetcols", 0);
if (cols.length > 0)
{
  savePerslidCols(code, cols);
}
else if (resetcols == 1)
{
  resetCols(code)
}

function resetCols(code)
{
  var sql = "DELETE prs_perslid_tabs"
          + " WHERE prs_perslid_tabs_code = " + safe.quoted_sql(code)
          + "   AND prs_perslid_key = " + user_key;
  Oracle.Execute(sql);
}

function savePerslidCols(code, cols)
{
    function perslidTabsInsert(code)
    {
        var sql = "SELECT prs_perslid_tabs_key"
                + "  FROM prs_perslid_tabs"
                + " WHERE prs_perslid_tabs_code = " + safe.quoted_sql(code)
                + "   AND prs_perslid_key = " + user_key;
        var oRs = Oracle.Execute(sql);
        if (!oRs.eof)
        {
            var tabs_key = oRs("prs_perslid_tabs_key").Value;
        }
        else
        {
            var fields = [ {  dbs: "prs_perslid_tabs_key",     typ: "key",      seq: "prs_s_prs_perslid_tabs_key" }
                         , {  dbs: "prs_perslid_tabs_code",    typ: "varchar",  val: code                         }
                         , {  dbs: "prs_perslid_key",          typ: "key",      val: user_key,                    foreign: "prs_perslid" }
                         ];
            var tabsIns = buildInsert("prs_perslid_tabs", fields);
            var tabs_key = tabsIns.sequences["prs_perslid_tabs_key"];
            var sql = tabsIns.sql;
            Oracle.Execute(sql);
        }
        oRs.Close();

        return tabs_key;
    }

    function perslidColsInsert(tabs_key, index, col)
    {
        var field   = col.split("|")[0];
        var visible = col.split("|")[1];
        var combine = col.split("|")[2];
        var fields = [ {  dbs: "prs_perslid_cols_key",          typ: "key",     seq: "prs_s_prs_perslid_cols_key" }
                     , {  dbs: "prs_perslid_tabs_key",          typ: "key",     val: tabs_key,                    foreign: "prs_perslid_tabs" }
                     , {  dbs: "prs_perslid_cols_volgnr",       typ: "number",  val: index                        }
                     , {  dbs: "prs_perslid_cols_column_name",  typ: "varchar", val: field                        }
                     , {  dbs: "prs_perslid_cols_visible",      typ: "varchar", val: visible                      }
                     , {  dbs: "prs_perslid_cols_combine",      typ: "number",  val: combine                      }
                     ];
        var colsIns = buildInsert("prs_perslid_cols", fields);
        var sql = colsIns.sql;
        Oracle.Execute(sql);
    }

    function perslidColsUpdate(tabs_key, index, col)
    {
        var field   = col.split("|")[0];
        var visible = col.split("|")[1];
        var combine = col.split("|")[2];
        var fields = [ { dbs: "prs_perslid_cols_volgnr",       typ: "number",  val: index   }
                     , { dbs: "prs_perslid_cols_column_name",  typ: "varchar", val: field   }
                     , { dbs: "prs_perslid_cols_visible",      typ: "varchar", val: visible }
                     , { dbs: "prs_perslid_cols_combine",      typ: "number",  val: combine }
                     ];
        var sqlCol = buildUpdate("prs_perslid_cols", fields)
                   + "       prs_perslid_tabs_key = " + tabs_key
                   + "   AND prs_perslid_cols_column_name = " + safe.quoted_sql(field);
        Oracle.Execute(sqlCol);
    }

    function perslidColsDelete(col)
    {
        var sqlCol = "DELETE FROM prs_perslid_cols"
                   + " WHERE prs_perslid_tabs_key = " + col.tabs_key
                   + "   AND prs_perslid_cols_key = " + col.cols_key;
        Oracle.Execute(sqlCol);
    }

    //
    var sql = "SELECT c.prs_perslid_tabs_key"
            + "     , c.prs_perslid_cols_key"
            + "     , c.prs_perslid_cols_column_name"
            + "  FROM prs_perslid_tabs t, prs_perslid_cols c"
            + " WHERE t.prs_perslid_tabs_code = " + safe.quoted_sql(code)
            + "   AND c.prs_perslid_tabs_key = t.prs_perslid_tabs_key"
            + "   AND t.prs_perslid_key = " + user_key
            + " ORDER BY c.prs_perslid_tabs_key"
            + "        , c.prs_perslid_cols_volgnr"
            + "        , c.prs_perslid_cols_column_name";
    var oRs = Oracle.Execute(sql);
    var db_cols = [];
    while (!oRs.eof)
    {
      db_cols.push( { tabs_key: oRs("prs_perslid_tabs_key").Value
                    , cols_key: oRs("prs_perslid_cols_key").Value
                    , col_name: oRs("prs_perslid_cols_column_name").Value
                    }
                  );
      oRs.MoveNext();
    }
    oRs.Close();

    if (cols.length == 0)
    {   // Overzicht bevat geen kolommen meer
        if (db_cols.length > 0)
        {   // Er zijn nog wel instellingen voor dit overzicht.
            // Verwijder alle prs_perslid_cols en de prs_perslid_tabs van dit overzicht.
            resetCols(code);
        }
    }
    else
    {   // Het overzicht bevat kolommen.
        // Als er nog geen instellingen voor dit overzicht bestaat dan eerst de overzichtsnaam toevoegen
        var tabs_key = (db_cols.length == 0 ? perslidTabsInsert(code) : db_cols[0].tabs_key);

        // Wijzig bestaande kolommen en voeg nieuwe kolommen toe in de opgeslagen instellingen.
        for (var index = 0; index < cols.length; index++)
        {
            var col_found = false;
            var field = cols[index].split("|")[0];
            for (r = 0; r < db_cols.length; r++)
            {
                if (db_cols[r].col_name == field)
                {
                    perslidColsUpdate(tabs_key, index, cols[index]);
                    col_found = true;
                    break;
                }
            }

            if (!col_found)
            {
                perslidColsInsert(tabs_key, index, cols[index]);
            }
        }
        // Verwijder vervallen kolommen.
        for (var r = 0; r < db_cols.length; r++)
        {
            var col_found = false;
            for (var index = 0; index < cols.length; index++)
            {
                var field = cols[index].split("|")[0];
                if (db_cols[r].col_name == field)
                {
                    col_found = true;
                    break;
                }
            }

            if (!col_found)
            {
                perslidColsDelete(db_cols[r]);
            }
        }
    }
}

Response.Write(JSON.stringify({ success: true }));
%><% ASPPAGE_END(); %>
