<% /*
    $Revision$
    $Id$

    File:           model_apis.inc

    Description:    Api model.
    Parameters:
    Context:

    Notes:
*/

%>
<!--#include file="./api2_dispatch.inc"-->
<%
model_apis =
{
    table: "apis",
    primary: "id",
    records_name: "apis",
    record_name: "api",
    fields : { "id"           : { typ: "varchar",      label: "Api" },
               "module"       : { typ: "varchar",      label: "Module" },
               "name"         : { typ: "varchar",      label: "Api" }
             },

    REST_GET: function _GET(params)
        {
            var autfunction = "WEB_APIDOC"; // is dit niet erg streng?
            params.authparams = user.checkAutorisation(autfunction, null, null, true); // pessimistisch

            var safefieldnames = [];
            var lcl2fld = {};

            var api2_names = [];
            for (var dispatch in api2_mapper)
            {
                if (api2_mapper[dispatch].hidden)
                    continue;

                var filepath = api2_mapper[dispatch].filename;
                var filename = filepath.split('/').pop();
                var module = api2_mapper[dispatch].module || filename.substr(0, 3).toUpperCase();
                var apidata = { id: dispatch,
                                name: "<<" + dispatch + ">>",
                                modulecode: module,
                                module: L("lcl_module_" + module)
                              }
                if (api2_mapper[dispatch].lcl_name)
                    apidata.name = L(api2_mapper[dispatch].lcl_name);
                if ("docparam" in api2_mapper[dispatch])
                    apidata.docparam = api2_mapper[dispatch].docparam;
                if ("nodoc" in api2_mapper[dispatch])
                    apidata.nodoc = api2_mapper[dispatch].nodoc;

                var lclname = "{0}_m".format(filename.split(".")[0]); // alg_ruimte_m
                lcl2fld[lclname] = apidata;
                safefieldnames.push(safe.quoted_sql(lclname));

                api2_names.push(apidata);
            }

            // kijken of er een L() bestaat voor de api2_mapper filenaam
            if (safefieldnames.length)
            {
                //model.fields[fld].hint = "Hallo";
                var sql = "SELECT fac_locale_xsl_label, "
                        + "       COALESCE(fac_locale_xsl_cust, fac_locale_xsl_tekst) fac_locale_xsl_tekst"
                        + "  FROM fac_locale_xsl xsl"
                        + " WHERE fac_locale_xsl_lang = " + safe.quoted_sql(user_lang)
                        + "   AND fac_locale_xsl_module = 'ASP'"
                        + "   AND fac_locale_xsl_label IN (" + safefieldnames.join(", ") + ")";
                var oRs = Oracle.Execute(sql);
                while (!oRs.Eof)
                {
                    lcl2fld[oRs("fac_locale_xsl_label").Value].name = oRs("fac_locale_xsl_tekst").value;
                    oRs.MoveNext();
                }
                oRs.Close();
            }

            api2_names.sort(function (a, b) {
                if (a.modulecode == b.modulecode)
                    return a.id < b.id ? -1: 1;
                else
                    return a.modulecode < b.modulecode ? -1: 1;
            });
            // Graag zou ik er ook info instoppen over het model zelf.
            // Dan moet ik echter alle model_xxxx.inc bestanden includen wat ik niet wil.
            // Daarom moet je zelf maar /api2/xxxx.api aanroepen voor de details

            return api2_names;
        },

    search: { autosearch: true },
    list: { default_url: "api2/{0}.scf" }
//    list: { default_url: "api2/{0}.api?pretty=1" }
}
%>