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

    File:               fac_edit_faq_save
    Description:        SUBMIT-form

    Parameters:
    Context:
    Note:

     SUBMIT-form
*/ %>

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

<%
FCLTHeader.Requires({ plugins:["jQuery"],
                      js: []
                    }) %>
<%
var faq_key = getQParamInt( "faq_key", -1 );
var isNew = (faq_key == -1);

var faq_disc = getFParamInt("fac_disc"); // Verplicht
var this_faq = fac.func_enabled_faq(faq_key, { "disc_key": faq_disc }); // Wat heb ik zoal aan rechten op deze specifieke kennisbankitem.

var faqVisibleZ = getFParamInt("faqVisibleZ", 0) == 1;
var faqVisibleP = getFParamInt("faqVisibleP", 0) == 1;
var faqScreenM  = getFParamInt("faqScreenM", 0) == 1;
var faqPopupM   = getFParamInt("faqPopupM", 0)  == 1;
var faqScreenO  = getFParamInt("faqScreenO", 0) == 1;
var faqPopupO   = getFParamInt("faqPopupO", 0)  == 1;
var faqScreenA  = getFParamInt("faqScreenA", 0) == 1;
var faqPopupA   = getFParamInt("faqPopupA", 0)  == 1;
var fac_faq_visibility = (faqVisibleZ? 1 : 0) + (faqVisibleP? 2 : 0);
var fac_faq_displaymode = (faqScreenM? 1 : 0) + (faqPopupM? 2 : 0) + (faqScreenO? 4 : 0) + (faqPopupO? 8 : 0) + (faqScreenA? 16 : 0) + (faqPopupA? 32 : 0);

if (fac_faq_visibility == 0) {
  abort_with_warning(L("lcl_faq_level0"));
}

var fields =  [ { dbs: "fac_faq_question",       typ: "varchar",  frm: "fac_question",                        track: L("lcl_faq_question"), len: 200 },
                { dbs: "fac_faq_answer",         typ: "varchar",  frm: "fac_answer",                          track: L("lcl_faq_answer") },
                { dbs: "fac_faq_answer2",        typ: "varchar",  frm: "fac_answer2",                         track: L("lcl_faq_answer2") },
                { dbs: "fac_faq_source",         typ: "varchar",  frm: "fac_bron",                            track: L("lcl_faq_bron"), len: 60 },
                { dbs: "fac_faq_url",            typ: "varchar",  frm: "fac_url",                             track: L("lcl_faq_link") },
                { dbs: "fac_faq_level",          typ: "number",   val: fac_faq_visibility,                    track: L("lcl_faq_level") },
                { dbs: "fac_faq_displaymode",    typ: "number",   val: fac_faq_displaymode,                   track: L("lcl_faq_displaymode") },
                { dbs: "prs_perslid_key",        typ: "key",      val: user_key, foreign: "prs_perslid",      track: L("lcl_faq_ingevoerd") },
                { dbs: "fac_faq_vervaldatum",    typ: "date",     frm: "date_to",                             track: L("lcl_faq_vervaldatum") },
                { dbs: "fac_faq_lang",           typ: "varchar",  frm: "fac_faq_lang",                        track: L("lcl_faq_lang") },
                { dbs: "fac_functie_key",        typ: "key",      frm: "fac_functie", foreign: "fac_functie", track: L("lcl_usrrap_functie") },
                { dbs: "fac_faq_hint",           typ: "varchar",  frm: "fac_hint",                            track: L("lcl_faq_hint"), len: 1000 },
                { dbs: "faq_ins_discipline_key", typ: "key",      val: faq_disc, foreign: "faq_discipline",   track: L("faq_discipline_key") }
              ];

// Publicatiedatum alleen muteerbaar met WEB_FAQBOF
if (this_faq.canBOwrite)
  fields.push({   dbs: "fac_faq_datum",          typ: "date",     frm: "date_from",           track: L("lcl_faq_datum") });

var warning = "";

if (faq_key > 0)
{
  user.auth_required_or_abort(this_faq.canChange);

  var faqUpd = buildTrackingUpdate("fac_faq", " fac_faq_key = " + faq_key, fields);
  var err = Oracle.Execute(faqUpd.sql, true);
  if (err.friendlyMsg)
    warning = err.friendlyMsg;
}
else
{
  user.auth_required_or_abort(this_faq.canAdd);
  fields.push({  dbs: "fac_faq_key",         typ: "key",      seq: "fac_s_fac_faq_key" });
  var regIns = buildInsert("fac_faq", fields);
  var faq_key = regIns.sequences["fac_faq_key"];
  sql = regIns.sql;
  var err = Oracle.Execute(sql, true);
  if (err.friendlyMsg)
    warning = err.friendlyMsg;

  move_new_bb_files("FAQA", faq_key, getFParamStringArray("bb_files1", []));
  move_new_bb_files("FAQB", faq_key, getFParamStringArray("bb_files2", []));
}

// HIER WORDEN DE SAVEFLEXKENMERKEN GEREGELD
currentKenmerkenSQL = "SELECT kf.faq_kenmerk_key"
                    + "     , kf.faq_kenmerkwaarde_waarde"
                    + "     , k.faq_kenmerk_toonbaar"
                    + "     , k.fac_functie_key"
                    + "  FROM faq_kenmerkwaarde kf"
                    + "     , faq_kenmerk k"
                    + " WHERE k.faq_kenmerk_key = kf.faq_kenmerk_key"
                    + "   AND faq_kenmerkwaarde_verwijder IS NULL"
                    + "   AND fac_faq_key = " + faq_key;

var allKenmerkenSQL = " SELECT " + lcl.xsql('k.faq_kenmerk_omschrijving', 'k.faq_kenmerk_key') + " kenmerk_omschrijving"
                     + "      , k.faq_kenmerk_kenmerktype    kenmerk_kenmerktype"
                     + "      , k.faq_kenmerk_systeem        kenmerk_systeem"
                     + "      , k.faq_kenmerk_toonbaar       kenmerk_readonly"
                     + "      , k.faq_kenmerk_uniek          kenmerk_uniek"
                     + "      , fac_kenmerkdomein_key"
                     + "   FROM faq_kenmerk k "
                     + "  WHERE k.faq_kenmerk_verwijder IS NULL"
                     + "    AND k.faq_kenmerk_key = "; /* wordt in saveFlexkenmerken uitgebreid */

var flextrack = saveFlexKenmerken(faq_key,
                                  { kenmerkTable: "faq_kenmerkwaarde",
                                    kenmerkParentKey : "fac_faq_key",
                                    kenmerkWaarde: "faq_kenmerkwaarde_waarde",
                                    kenmerkKey: "faq_kenmerk_key",
                                    kenmerkToonbaar: "faq_kenmerk_toonbaar",
                                    currentKenmerkenSQL: currentKenmerkenSQL,
                                    allKenmerkenSQL: allKenmerkenSQL,
                                    requestQF: Request.Form,
                                    isNew: isNew,
                                    tracking: true,
                                    module: "FAQ",
                                    flexPath: "FAQ/F"
                                  });
if (faq_key > 0)
{
  if (faqUpd && (faqUpd.trackarray.length || flextrack.length))
  { // Tracking alleen op wijzigingen
    shared.trackaction("FAQUPD", faq_key, faqUpd.trackarray.concat(flextrack).join("\n"));
  }
}

%>
<html>
  <head>
    <%  FCLTHeader.Generate() %>
    <script type="text/javascript">
        $(function ()
        {
          FcltMgr.closeDetail(window, { faq_key: <%=faq_key%>,
                                        warning: "<%=safe.jsstring(warning)%>",
                                        keepForm: <%=warning?"true":"false"%>,
                                        refresh: true
                                      });
        });
    </script>
  </head>
  <body>
  </body>
</html>
<% ASPPAGE_END(); %>
