Files
Facilitor/APPL/API2/model_reports.inc
Jos Groot Lipman 8decd00522 Alle API's via nieuwe stijl fields
svn path=/Website/trunk/; revision=24435
2015-03-13 13:34:37 +00:00

243 lines
10 KiB
PHP

<% /*
$Revision$
$Id$
File: model_reports.inc
Description: rapport model voor *de data* van een specifiek rapport.
Zal rapporten uitvoeren dus. Het *bewerken* van rapporten gaat via model_reportsx
Parameters:
Context:
Notes:
*/
%>
<!-- #include file="../Shared/json2.js" -->
<!-- #include file="./model_reportsx.inc" -->
<!-- #include file="../fac/fac_usrrap.inc" -->
<%
function model_reports_template()
{
this.table = "te bepalen";
this.primary = null;
this.records_name = "te bepalen";
this.record_name = "te bepalen";
this.fields = {/* te bepalen */};
this.autfunction = null;
this.REST_GET = function _GET(params, jsondata)
{
if (this.autfunction)
user.checkAutorisation(this.autfunction); // leesrechten is wel het minste
// let op: fields bevat hier al alleen de zichtbare velden.
// zie has_functie_key voor de oplossing
// params.filter = params.filter || {};
// params.filter.fclt_3d_locatie_key = user.alg_locatie_key({withcurrent:true});
// params.filter.fclt_3d_gebouw_key = user.alg_gebouw_key({withcurrent:true});
// params.filter.fclt_3d_verdieping_key = user.alg_verdieping_key({withcurrent:true});
// params.filter.fclt_3d_user_key = user_key;
// // TODO: echte 3D erbij?
var groupbys = [];
if (params.columns && params.groupby)
{
if (params.columns.length != params.groupby.length)
INTERNAL_ERROR_GROUP_MISMATCH;
for (var fld in this.fields)
{
if (!inArray(fld, params.columns))
{
if (this.list && this.list.default_url && fld == "id")
groupbys.push(this.fields["id"].dbs);
else
delete this.fields[fld]; // Onnodige velden niet ophalen
}
}
for (var i=0; i < params.columns.length; i++)
{
var fld = params.columns[i];
var field = this.fields[fld];
field.keepdbsforfilter = true;
switch (params.groupby[i] || "G")
{
case "G":
groupbys.push(field.dbs);
break;
case "S":
field.sql = "SUM(" + field.dbs + ")";
field.label = L("lcl_usrrap_label_SUM").format(field.label);
field.total = true;
break;
case "C":
field.sql = "COUNT(" + field.dbs + ")";
field.label = L("lcl_usrrap_label_CNT").format(field.label);
field.total = true;
break;
case "A":
field.sql = "AVG(" + field.dbs + ")";
field.label = L("lcl_usrrap_label_AVG").format(field.label);
field.newtyp = "float";
break;
case "H":
if (field.typ == "date" || field.typ == "datetime")
field.sql = "TO_CHAR(" + field.dbs + ", 'YYYY-MM-DD HH24')||':00'";
else
field.sql = "TO_CHAR(" + field.dbs + ", 'HH24')||':00'";
field.newtyp = "varchar";
groupbys.push(field.sql);
field.label = L("lcl_usrrap_label_HOUR").format(field.label);
break;
case "D":
field.sql = "TRUNC(" + field.dbs + ", 'DD')";
field.newtyp = "date";
groupbys.push(field.sql);
field.label = L("lcl_usrrap_label_DAY").format(field.label);
break;
case "W":
field.sql = "TO_CHAR(" + field.dbs + ", 'YYYY-IW')";
field.newtyp = "varchar";
groupbys.push(field.sql);
field.label = L("lcl_usrrap_label_WEEK").format(field.label);
break;
case "M":
field.sql = "TO_CHAR(" + field.dbs + ", 'YYYY-MM')";
field.newtyp = "varchar";
groupbys.push(field.sql);
field.label = L("lcl_usrrap_label_MONTH").format(field.label);
break;
case "Y":
field.sql = "TO_CHAR(" + field.dbs + ", 'YYYY')";
field.newtyp = "varchar";
groupbys.push(field.sql);
field.label = L("lcl_usrrap_label_YEAR").format(field.label);
break;
}
}
}
var query = api2.sqlfields(params, this);
var wheres = api2.sqlfilter(params, this);
query.wheres = query.wheres.concat(wheres);
if (this.has_functie_key)
{
var authparams = user.checkAutorisation("WEB_PRSSYS", true);
if (!authparams)
{
query.wheres.push("(fac_functie_key IN"
+ " (SELECT w.fac_functie_key"
+ " FROM fac_v_webgebruiker W"
+ " WHERE w.prs_perslid_key = " + user_key
+ " ) OR fac_functie_key IS NULL)");
}
}
var maxcnt = getQParamInt("showall", 0) == 1?S("qp_maxrows2"):S("qp_maxrows");
query.wheres.push("ROWNUM <= " + (maxcnt + 1)); // Eentje extra om overflow-melding te triggeren in resultsettable
var sql = "SELECT " + query.selects.join(", ")
+ " FROM " + query.tables.join(", ")
+ (query.wheres.length ? " WHERE " + query.wheres.join(" AND " ) : "")
// TODO: 3D en FCLT_KEY filtering en zo
if (groupbys.length && groupbys.length != query.selects.count)
sql += " GROUP BY " + groupbys.join(", ");
var order = [];
for (var i=0; i < query.selects.length; i++)
order.push(i+1)
sql += " ORDER BY " + order.join(", ");
for (var fld in this.fields)
{ // Overrules voor groeperingen
this.fields[fld].typ = this.fields[fld].newtyp || this.fields[fld].typ;
}
var json = api2.sql2json (params, sql, this);
return json;
};
this.REST_PUT = false;
this.REST_POST = false;
this.REST_DELETE = false;
this.search = { title: L("lcl_usrrap_report") }
}
// Voltooi model_reports_template voor een bepaald rapport
function model_reports(fac_usrrap_key)
{
var reportsx = new model_reportsx(fac_usrrap_key, { include: ["columns"] });
var usrrap = reportsx.data;
user.auth_required_or_abort(usrrap);
var model = new model_reports_template();
model.table = usrrap.viewname;
if (usrrap.styling && usrrap.styling.id & 8) // Tabelized
model.table = "T_" + usrrap.viewname;
model.record_name = usrrap.viewname.substring(0,1) + usrrap.viewname.substring(1).toLowerCase();
model.records_name = model.record_name + "s";
model.records_title = usrrap.name;
model.list = { columns : [], canGroup: true };
var keyfield = null;
if (usrrap.urllink)
{
var mtc = usrrap.urllink.match(/(.*)\{(.*)\}/);
if (!mtc || mtc.length != 3)
shared.simpel_page("Urllink <em>{0}</em> in rapport moet formaat 'xxxx/yyy.asp?key={keyveld}' hebben.".format(usrrap.urllink));
model.list.default_url = mtc[1];
keyfield = mtc[2];
}
if (usrrap.authorisation)
{
var sql = "SELECT fac_functie_code FROM fac_functie WHERE fac_functie_key = " + usrrap.authorisation.id;
var oRs = Oracle.Execute(sql);
model.autfunction = oRs("fac_functie_code").Value;
oRs.Close();
}
for (var i = 0; i < usrrap.columns.length; i++)
{
var field = { dbs: usrrap.columns[i].name,
label: safe.html(usrrap.columns[i].caption), // (lcl) Labels worden altijd geacht html-safe te zijn
typ: usrrap.columns[i].datatype.id, // Voor overulen van gewone LCL's ligt de verantwoordelijkheid
group: usrrap.columns[i].group, // bij een beheerder maar rapportages wordt misschien wel fe
visible: usrrap.columns[i].visible.id
};
if (field.typ == "number" && field.dbs.match(/_key$/i)) // Vooral de FCLT_3D filters
field.typ = "key";
if (usrrap.columns[i].filter)
{
if (usrrap.columns[i].filter.id == "A")
{
field.filter = { "date": "range",
"datetime": "range",
"time": "range",
"varchar": "like"
}[ field.typ ] || "exact";
}
else
{
field.filter = api2.splitLOV("E;exact;L;like;R;range")[usrrap.columns[i].filter.id]
}
}
var fld = field.dbs.toLowerCase();
if (fld == "fac_functie_key")
model.has_functie_key = true;
if (fld == keyfield)
fld = "id";
model.fields[fld] = field;
if (usrrap.columns[i].visible.id == 'V')
model.list.columns.push(fld);
}
__Log(model);
return model;
}
%>