<% /*
    $Revision$
    $Id$

    File:           model_regions.inc

    Description:    Regio model.
    Parameters:
    Context:

    Notes:
*/

%>
<!-- #include file="../Shared/discxalg3d.inc"-->
<!-- #include file="../ALG/alg.inc" -->
<!-- #include file="./model_districts.inc" -->
<%
model_regions =
{
    table: "alg_regio",
    primary: "alg_regio_key",
    records_name: "regions",
    record_name: "region",
    fields: {"id"     : { dbs: "alg_regio_key",          typ: "key",     filter: "exact" },
             "name"   : { dbs: "alg_regio_omschrijving", typ: "varchar", label: L("lcl_estate_regio_descr")},
             "deleted": { dbs: "alg_regio_verwijder",    typ: "datetime"}
            },
    includes: { "districts": { model: model_districts, joinfield: "region" }
              },

    _check_authorization: function(params, method)
        {
            var scope = params.filter.scope || "fe";
            var autfunction = (scope == "fe"? "WEB_ALGUSE" : "WEB_ALGMAN");
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

            var onrgoedlvl = "RG";
            if (method == "GET")
            {
                if (params.filter.id)
                {
                    var reg_key = params.filter.id;
                    var this_alg = alg.func_enabled(reg_key, onrgoedlvl);
                    user.auth_required_or_abort(this_alg.readman || this_alg.readuse);
                }
            }
            if (method == "PUT" || method == "DELETE")
            {
                var reg_key = params.filter.id;
                var this_alg = alg.func_enabled(reg_key, onrgoedlvl);
                user.auth_required_or_abort(this_alg.writeman || (this_alg.writeuse));
            }
            if (method == "POST")
            {
                var reg_key = -1
                var this_alg = alg.func_enabled(reg_key, onrgoedlvl);
                user.auth_required_or_abort(this_alg.writeman);
            }
            params.func_enabled = this_alg || {};
        },
    _pre_analyze_fields: function (params, jsondata) /* analyseer inkomende jsondata voor PUT en POST */
        {
        },
    REST_GET: function _GET(params, jsondata)
        {
            model_regions._check_authorization(params, "GET");
            var query = api2.sqlfields(params, model_regions);
            if (!params.filter.show_deleted)
                query.wheres.push("alg_regio_verwijder IS NULL");

            if (params.authparams.ALGreadlevel > -1)
            {   // Er is een scope-beperking van kracht
                query.wheres.push("alg_regio_key IN"
                                + " (SELECT alg_regio_key FROM fac_v_my_regions"
                                +   " WHERE prs_perslid_key=" + user_key
                                +   "   AND niveau=" + params.authparams.ALGreadlevel + ")"
                                  )
            }

            if (params.filter.isNew)
                query.wheres.push("alg_regio_key = -1");  // Forceer leeg record.

            var wheres = api2.sqlfilter(params, model_regions);
            query.wheres = query.wheres.concat(wheres);

            var sql = "SELECT " + query.selects.join(", ")
                    + "  FROM " + query.tables.join(", ")
                    + (query.wheres.length ? " WHERE " + query.wheres.join(" AND " ) : "")
                    + " ORDER BY alg_regio_key, alg_regio_omschrijving";
            var json = api2.sql2json (params, sql, model_regions);

            return json;
        },
    REST_PUT: function (params, jsondata, the_key) /* update region */
        {
            model_regions._check_authorization(params, "PUT");
            if (!jsondata.id) jsondata.id = the_key;
            var reg_key = the_key;
            var dbfields = api2.update_fields(params, model_regions, jsondata); // Build updater
            var wheres = [" alg_regio_key = " + reg_key];
            var regUpd = buildTrackingUpdate("alg_regio", wheres.join(" AND " ), dbfields, { noValidateToken: true });

            var warning = "";
            var err = Oracle.Execute(regUpd.sql, true);
            if (err.friendlyMsg)
                warning = err.friendlyMsg;

            return { key: reg_key, warning: warning };
        },
    REST_POST: function (params, jsondata) /* new region */
        {
            params.isNew = true;
            model_regions._check_authorization(params, "POST");

            var dbfields = api2.update_fields(params, model_regions, jsondata); // Build updater
            dbfields["id"] = {dbs: "alg_regio_key", typ: "key", seq: "alg_s_alg_regio_key" };
            var regIns = buildInsert("alg_regio", dbfields, { noValidateToken: true });
            var reg_key = regIns.sequences["alg_regio_key"];

            var warning = "";
            var err = Oracle.Execute(regIns.sql, true);
            if (err.friendlyMsg)
                warning = err.friendlyMsg;

            return { key: reg_key, warning: warning };
        },
    REST_DELETE: function (params, the_key) /* delete region */
        {
            model_regions._check_authorization(params, "DELETE");
            var regio_key = params.filter.id;
            var sql = "UPDATE alg_regio"
                    + "   SET alg_regio_verwijder = SYSDATE"
                    + " WHERE alg_regio_key = " + regio_key;

            var err = Oracle.Execute(sql, true);
            if (err.friendlyMsg)
                params.warning = err.friendlyMsg;

            return { key: regio_key, warning: warning };
        }
}
%>