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

    File:        fac_edit_nieuws_save.asp
    Description: Opslaan van een bewerkte portal message
    Parameters:  nieuws_key (optioneel) voor bewerken
    Context:     save bestand van fac_edit_nieuws.asp
    Note:

*/ %>
<%
var JSON_Result = true;
%>

<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/save2db.inc" -->
<!-- #include file="../Shared/FlexFiles.inc" -->
<!-- #include file="../MSG/msg.inc" -->
<%
  protectRequest.validateToken();
  var nieuws_key = getQParamInt("nieuws_key", -1); // Quesrystring
  var parent_key = getFParamInt("parent_key", -1); // Form
  var nieuws_show = getFParamBits("nieuws_show", getFParamInt("nieuws_show", 3));
  var authparams = (nieuws_show & 4)
                    ? user.checkAutorisation("WEB_MSGUSE")
                    : user.checkAutorisation("WEB_MSGBOF");
  if (nieuws_show & 4) {
    var msg_info = msg.func_enabled(nieuws_key);
    if (nieuws_key == -1 && parent_key > 0) {         // Ik ben een Reply
      user.auth_required_or_abort(msg_info.canRead);  // daarvoor zijn alleen leesrechten vereist
    } else {
      user.auth_required_or_abort(msg_info.canChangeMsg);
    }
  }

  var district_key = getFParamInt("districtkey", -1);
  var location_key = getFParamInt("locatiekey", -1);
  var bld_key = getFParamInt("gebouwkey", -1);
  var verdieping_key = getFParamInt("verdiepingkey", -1);

  var groep_list = getFParamIntArray("msggroup", []);

  if (nieuws_show != 4 || !hasFParam("parent_key")) {
    var subject = "" + getFParam("subject"); /* Voor de verplichting */
  }
  var message = "" + getFParam("ta_message");

  var vandatum = getFParamDate("datefrom", null); // periode tot
  var totdatum = getFParamDate("dateto", null); // Periode van

  var fields = [ { dbs: "fac_nieuws_titel",        typ: "varchar", frm: "subject",    len: 100 },
                 { dbs: "fac_nieuws_image",        typ: "varchar", frm: "image",      len: 255 },
                 { dbs: "fac_nieuws_groep_key",    typ: "key",     frm: "nwsgroup"},
                 { dbs: "fac_nieuws_omschrijving", typ: "varchar", frm: "ta_message", len: 4000 },
                 { dbs: "fac_nieuws_van",          typ: "date",    frm: "datefrom" },
                 { dbs: "fac_nieuws_tot",          typ: "date",    frm: "dateto" },
                 { dbs: "fac_nieuws_show",         typ: "number",  val: nieuws_show }
               ];
  if (nieuws_show == 4) {
    fields.push({ dbs: "fac_nieuws_parentkey",    typ: "key",     frm: "parent_key" });
  }
  if (nieuws_key === -1) {
    fields.push({ dbs: "mld_melding_key",         typ: "key",     frm: "mld_melding_key" });
    fields.push({ dbs: "prs_perslid_key",         typ: "key",     val: user_key });
  }

  if (verdieping_key != -1) {
    niv = 'V';
    key = verdieping_key;
  } else if (bld_key != -1) {
     niv = 'G';
     key = bld_key;
  } else if (location_key != -1) {
     niv = 'L';
     key = location_key;
  } else if  (district_key != -1) {
     niv = 'D';
     key = district_key;
  } else {
     niv = "";
     key = -1; // for ALL users
  }

  var req;
  var result;
  var warning = "";
  fields.push({ dbs: "fac_nieuws_onrgoed_niveau", typ: "varchar", val: niv },
              { dbs: "fac_nieuws_onrgoed_key",    typ: "key",     val: key });

  if(nieuws_key == -1){
    fields.push({ dbs: "fac_nieuws_key", typ: "key", seq: "fac_s_fac_nieuws_key"});
    //geen groep (1 nieuw portalitem)
    if(groep_list.length == 0){
      req = buildInsert("fac_nieuws", fields);
      var err = Oracle.Execute(req.sql, true);
    }
    //1 of meerdere groepen worden gekozen (nieuwe portalitems)
    for (i=0; i<groep_list.length; i++)
    {
      fields.push({ dbs: "fac_groep_key", typ: "key", val: (groep_list[i] > -1 ? groep_list[i] : " NULL")});
      req = buildInsert("fac_nieuws", fields);
      var err = Oracle.Execute(req.sql, true);
      if (err.friendlyMsg){
        warning = err.friendlyMsg;
      }
      fields.pop();
    }
    var nkey = req.sequences["fac_nieuws_key"];
    move_new_bb_files("NEWS", nkey, getFParamStringArray("image", []));
    move_new_bb_files("MSG", nkey, getFParamStringArray("bb_files", []));
    result = {success: true, nieuws_key: nkey, close: false, keepForm: !!warning, warning: warning};
  } else {
    //update van bestaand portalitem (1 vernieuwd portalitem)
    fields.push({ dbs: "fac_groep_key", typ: "key", frm: "msggroup" });
    req = buildUpdate("fac_nieuws", fields) + " fac_nieuws_key = " + nieuws_key;
    var err = Oracle.Execute(req, true);
    if (err.friendlyMsg){
      warning = err.friendlyMsg;
    }
    result = {success: true, nieuws_key: nieuws_key, close: false, keepForm: !!warning, warning: warning};
  }
  if (nieuws_show == 4) { /* TODO, pagina refreshed direct, dus -> tonen op nieuwe pagina */
    result.toaster = nieuws_key == -1
                      ? (hasFParam("parent_key")
                        ? "Nieuwe reactie geplaatst"
                        : "Nieuw bericht geplaatst")
                      : "Bericht gewijzigd";
  }

  Response.Write(JSON.stringify(result));
%>
<% ASPPAGE_END(); %>
