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

    File:               prs_edit_perslid_multi_save.asp
    Description:        save script van prs_edit_perslid_multi.asp
    Globalen:
    Context:
    Note:
*/
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/save2db.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<!-- #include file="./prs.inc" -->

<%
protectRequest.validateToken();
var prs_key_arr = getQParamIntArray("prs_keys");
var warning = "";
var result = {};
var fields = [];
var hasInGids = false;
if (Request.Form("prs_visibility").Count == 1 && getFParam("check_prs_visibility", "off") == "on")
  fields.push({ dbs: "prs_perslid_visibility", typ: "number",  frm: "prs_visibility", track: L("lcl_prs_person_visibility") });
if (Request.Form("has_ingids").Count == 1 && getFParam("check_ingids", "off") == "on")
  hasInGids = true;
if (Request.Form("prs_afdkey").Count == 1 && getFParam("check_prs_afdkey", "off") == "on")
  fields.push({ dbs: "prs_afdeling_key", typ: "key",  frm: "prs_afdkey", foreign: "prs_afdeling", track: L("lcl_prs_organisatie") });
if (Request.Form("prs_srtkey").Count == 1 && getFParam("check_prs_srtkey", "off") == "on")
  fields.push({ dbs: "prs_srtperslid_key", typ: "key",  frm: "prs_srtkey", foreign: "prs_srtperslid", track: L("lcl_prs_person_function") });
if (Request.Form("fac_prkey").Count == 1 && getFParam("check_fac_prkey", "off") == "on")
  fields.push({ dbs: "fac_profiel_key", typ: "key",  frm: "fac_prkey", foreign: "fac_profiel", track: L("lcl_prs_profile") });
if (Request.Form("prs_lang").Count == 1 && getFParam("check_prs_lang", "off") == "on")
  fields.push({dbs: "prs_perslid_lang", typ: "varchar", frm: "prs_lang", track: L("lcl_lcl_taal") });
if (Request.Form("prs_tz").Count == 1 && getFParam("check_prs_tz", "off") == "on")
  fields.push({dbs: "prs_perslid_timezone", typ: "varchar",  frm: "prs_tz", track: L("lcl_prs_person_timezone")});

for (var i = 0; i < prs_key_arr.length; i++)
{ // Ik mag de persoon wijzigen dus voer wijziging uit
  var prs_key = prs_key_arr[i];
  var trackarray = [];
  var prsauthparams = prs.checkAutorisation(prs_key);
  if (prsauthparams.writeman || prsauthparams.writeuse || prsauthparams.writeself)
  {
    if (fields.length || hasInGids && prsauthparams.writesys)
    {
      var fields_copy = JSON.parse(JSON.stringify(fields));
      if (hasInGids && prsauthparams.writesys) {
        fields_copy.push({ dbs: "prs_perslid_ingids", typ: "check",  frm: "ingids", track: L("lcl_prs_in_guide") });
      }
      var prsUpd = buildTrackingUpdate("prs_perslid", " prs_perslid_key = " + prs_key, fields_copy);
      trackarray = prsUpd.trackarray;
      // Submit the changed data
      var err = Oracle.Execute(prsUpd.sql, true);
      if (err.friendlyMsg)
          warning = err.friendlyMsg;
    }

    var currentKenmerkenSQL = "SELECT kl.prs_kenmerk_key"
                            + "     , kl.prs_kenmerklink_waarde"
                            + "     , k.prs_kenmerk_toonbaar"
                            + "     , k.fac_functie_key"
                            + "  FROM prs_kenmerklink kl"
                            + "     , prs_kenmerk k"
                            + " WHERE k.prs_kenmerk_key = kl.prs_kenmerk_key"
                            + "   AND prs_kenmerklink_verwijder IS NULL"
                            + "   AND prs_link_key = " + prs_key;

    var allKenmerkenSQL = " SELECT " + lcl.xsql('k.prs_kenmerk_omschrijving', 'k.prs_kenmerk_key') + " kenmerk_omschrijving"
                        + "      , k.prs_kenmerk_kenmerktype    kenmerk_kenmerktype"
                        + "      , k.prs_kenmerk_systeem        kenmerk_systeem"
                        + "      , k.prs_kenmerk_toonbaar       kenmerk_readonly"
                        + "      , k.prs_kenmerk_uniek          kenmerk_uniek"
                        + "      , fac_kenmerkdomein_key"
                        + "   FROM prs_kenmerk k "
                        + "  WHERE k.prs_kenmerk_verwijder IS NULL"
                        + "    AND k.prs_kenmerk_key = "; /* wordt in saveFlexkenmerken uitgebreid */

    var flextrack = saveFlexKenmerken(prs_key, { kenmerkTable: "prs_kenmerklink",
                                                 kenmerkParentKey: "prs_link_key",
                                                 kenmerkWaarde: "prs_kenmerklink_waarde",
                                                 kenmerkKey: "prs_kenmerk_key",
                                                 kenmerkToonbaar: "prs_kenmerk_toonbaar",
                                                 currentKenmerkenSQL: currentKenmerkenSQL,
                                                 allKenmerkenSQL: allKenmerkenSQL,
                                                 requestQF: Request.Form,
                                                 module: "PRS",
                                                 moduleName: "prs_kenmerklink_niveau",
                                                 moduleVal: "P",
                                                 isNew: false,
                                                 multiMode: true,
                                                 tracking: true,
                                                 flexPath: "PRS/P"
                                               });

    if (trackarray.length || (flextrack && flextrack.length))
    {
      var update_string = L("lcl_prs_is_prsupd") + "\n" + trackarray.join("\n") + (trackarray.length ? "\n" : "");
      if (flextrack && flextrack.length)
        update_string += flextrack.join("\n");
      shared.trackaction("PRSUPD", prs_key, update_string);
    }
  }
}

result = { success : true, refresh: true };

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

ASPPAGE_END();
%>
