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

     File:          queuemail_schedule_save.asp
     Description:   Het echte bewaren van een geschedulede e-mail
     Parameters:
     Context:       Vanuit queuemail_schedule.asp
*/
var JSON_Result = true;

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

<%
protectRequest.validateToken();

var job_key = getQParamInt("key", -1);

var once = getFParamInt("fac_sched", 0) == 0;
var geturl = getFParam("geturl");

geturl += "&outputmode=" + getFParamInt("q_outputmode", 1);

if (once) // rechtstreeks in fac_notificatie
{
        Oracle.Execute("BEGIN fac.putnotificationprio("
                          + user_key
                          + ", " + getFParamInt("sName_key") // pto
                          + ", " + safe.quoted_sql(getFParam("subject"))
                          + ", 2" // pstatus, 2=EMAIL
                          + ", NULL" // pemail
                          + ", NULL" // pmobile
                          + ", 2"    // prio
                          + ", NULL "  // pmailfrom
                          + ", pqueue =>'DEFAULT'"
                          + ", pgeturl => " + safe.quoted_sql(geturl)
                          + ");"
                    + " END;");
}
else
{
    if (getFParamInt("fac_usrrap_key", -1) > 0) // alle datumvelden er uit
    {
        geturl += "&preferdefaultfilters=1"; // maakt dat model_reports.inc/addfixedfilters sterke voorkeur krijgt
                                             // voor voorgedefinieerde default datumfilters zoals :wp voor 'begin vorige week'
    }

    var fields =  [ { dbs: "fac_notificatie_job_oms",       typ: "varchar",  frm: "subject"   },
                    { dbs: "fac_notificatie_job_geturl",    typ: "varchar",  val: geturl      },
                    { dbs: "fac_notificatie_job_nextrun",   typ: "datetime", frm: "nextrun"   },
                    { dbs: "fac_notificatie_job_interval",  typ: "number",   frm: "interval"  },
                    { dbs: "prs_perslid_key",               typ: "key",      val: user_key    }, // eigenaar
                    { dbs: "prs_perslid_key_receiver",      typ: "key",      frm: "sName_key" },
                    { dbs: "fac_usrrap_key",                typ: "key",      frm: "fac_usrrap_key" }
                  ];
    if (job_key > 0)
    {
      jobUpd = buildTrackingUpdate("fac_notificatie_job", "fac_notificatie_job_key = " + job_key, fields);
      Oracle.Execute(jobUpd.sql);
    }
    else
    {
      fields.push({ dbs: "fac_notificatie_job_key", typ: "key",   seq: "fac_s_fac_notificatie_job_key" });
      var jobIns = buildInsert("fac_notificatie_job", fields);
      job_key = jobIns.sequences["ins_job_key"];
      Oracle.Execute(jobIns.sql);
      shared.trackaction("PRSLOG", getFParamInt("sName_key"), L("lcl_schedule_plan_created").format(getFParam("subject")));
    }
}

var result = {success: true};

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