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

    File:               fac_current_tab_save.asp
    Description:        SUBMIT-form

    Parameters:
    Context:
    Note:
*/
var JSON_Result = true;
%>

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

<%

protectRequest.validateToken();

var taboption    = getFParam("taboption", "");
var fac_menu_key = getFParamInt("menu_key", -1);
var unpin        = fac_menu_key != -1;

if (taboption == "W")
{
    var this_wgt = wgt.func_enabled_wgt(-1); // Mag ik nieuwe widgets opslaan?
    user.auth_required_or_abort(this_wgt.canWrite);
}

function cleanurl(purl)
{
    var autourl = purl;
    // GUI_counters wissen voor gepinde tabbladen
    autourl = autourl.replace(/[\&|\?]from_menu_key=\d+/, "");
    autourl = autourl.replace(/[\&|\?]from_search_key=\d+/, "");
    autourl = autourl.replace(/[\&|\?]asWidget=\d/, "");
    autourl = autourl.replace(/[\&|\?]fcltfilters=.*/, "");
    autourl = autourl.replace(/[\&|\?]scf_transit=.*&scf_transitend=1/, "");
    autourl = autourl.replace(/[\&|\?]$/, "");

    __Log("Cleanurl: " + autourl);

    if (autourl.indexOf("../../") == 0)
        autourl = autourl.substr("../../".length + 1);

    if (autourl.indexOf(HTTP.urlzelf() + "/") == 0)
        autourl = autourl.substr(HTTP.urlzelf().length + 1);

    if (autourl.indexOf(rooturl + "/") == 0)
        autourl = autourl.substr(rooturl.length + 1);

    return autourl;
}

var result = { success: false };
var safe_tabgroep;
switch (taboption) {
    case "M": safe_tabgroep = 6; break;
    case "W": safe_tabgroep = 9; break;
    case "A":
    case "T":
    default: safe_tabgroep = 5; break;
}

if (unpin)
{
    if (taboption == "T" || taboption == "M")
    {   // Is het wel mijn eigen fav/tab?
        var sql = "SELECT 'dummy'"
                + "  FROM fac_menu"
                + " WHERE fac_menu_key = " + fac_menu_key
                + "   AND fac_menu_altgroep = " + safe_tabgroep
                + "   AND prs_perslid_key = " + user_key;
        var oRs = Oracle.Execute(sql);
        var found = !oRs.eof;
        oRs.Close();

        if (found)
        {
            // Bestaande autoopen, dus verwijderen, kan alleen bij taboption T of M
            var sql_d = "DELETE FROM fac_menu"
                    + " WHERE fac_menu_key = " + fac_menu_key;
            Oracle.Execute(sql_d, true);
            result.toaster = (taboption == "T" ? L("lcl_current_tab_unsaved") : L("lcl_current_fav_unsaved"));
            result.success = true;
        }
    }
}
else
{
    var tab_count       = getFParamInt("tab_count", 1);             // Wordt alleen bij taboption = "A" meegegeven
    var autoopenurl_arr = getFParamArray("autoopenurl", [], true);  // Elk element van "autoopenurl" niet splitsen (want er kunnen komma's in de titel staan)
    var autoopenttl_arr = getFParamArray("autoopenttl", [], true);  // Elk element van "autoopenttl" niet splitsen (want er kunnen komma's in de titel staan)
    var filters_arr     = getFParamArray("filters", [], true);      // Elk element van "filters" niet splitsen!
    var advfilters_arr  = getFParamArray("advfilters", [], true);   // Elk element van "advfilters" niet splitsen!
    result.menu_key_arr = [];

    for (var i = 0; i < tab_count; i++)
    {
        var autoopenurl = autoopenurl_arr[i];
        var autoopenttl = autoopenttl_arr[i];
        var filters     = filters_arr[i]    || "";
        var advfilters  = advfilters_arr[i] || "";
        /* Lege filters niet opslaan */
        if (filters === "{}") {
            filters = "";
        }
        if (advfilters === "{}") {
            advfilters = "";
        }

        if (!autoopenttl || !taboption)
            continue;

        autoopenurl = cleanurl(autoopenurl);

        // Nieuwe autoopen, dus toevoegen
        var fields =  [ { dbs: "fac_menu_key",      typ: "key",     seq: "fac_s_fac_menu_key" },
                        { dbs: "fac_menu_altlabel", typ: "varchar", val: autoopenttl,       len: 30 },
                        { dbs: "fac_menu_alturl",   typ: "varchar", val: autoopenurl },
                        { dbs: "fac_menu_altgroep", typ: "number",  val: safe_tabgroep },
                        { dbs: "fac_menu_level",    typ: "number",  val: "2" },
                        { dbs: "fac_menu_filter",   typ: "varchar", val: filters },
                        { dbs: "fac_menu_advfilter",typ: "varchar", val: advfilters }
                    ];
        if (taboption != "W") {
            fields.push({ dbs: "prs_perslid_key",   typ: "key",     val: user_key });
            fields.push({ dbs: "fac_menu_volgnr",   typ: "sql",     val:  "(SELECT 10 + COALESCE(MAX(fac_menu_volgnr), 0)"
                                                                        + "   FROM fac_menu"
                                                                        + "  WHERE fac_menu_altgroep = " + safe_tabgroep
                                                                        + "    AND prs_perslid_key = " + user_key
                                                                        + ")" });
        }

        var tabIns = buildInsert("fac_menu", fields);
        var fac_menu_key = tabIns.sequences["fac_menu_key"];
        var err = Oracle.Execute(tabIns.sql, true);
        if (err.friendlyMsg)
            result.warning = err.friendlyMsg;
        else
        {
            result.menu_key_arr.push(fac_menu_key);
            result.success  = true;
            result.saved    = true;
            if (taboption == "W")
                result.url = autoopenurl;
        }

        switch (taboption)
        {
            case "T": result.toaster = L("lcl_current_tab_saved"); break;
            case "M": result.toaster = L("lcl_current_fav_saved"); break;
            case "A": result.toaster = L("lcl_current_all_saved"); break;
            case "W": result.toaster = L("lcl_current_wgt_saved"); break;
        }
    }
}

Response.Write(JSON.stringify(result));

ASPPAGE_END();
%>
