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

    File            fac_report_calworker.asp
    Description:    Gebruikt vanuit shared/fullcalendar.asp
    Parameters:     usrrap_key: van een rapport
                    distinct: name van het veld wat we zoeken
                    xxx overige filtervelden
    Note:
*/
JSON_Result = true;
%>
<!-- #include file="../api2/api2.inc" -->
<!-- #include file="../api2/model_reports.inc" -->
<!-- #include file="../api2/model_reportsx.inc" -->
<!-- #include file="../scf/scaffolding.inc" -->
<%
    // hierboven is scaffolding.inc nodig omdat rapporten met default values
    // de functie scf.parseDefault nodig hebben

    // fullCalendar werkt een factor 1000 kleiner met hun datums dan wij.
    var date_from = new Date(getQParamInt("start") * 1000);
    var date_to = new Date(getQParamInt("end") * 1000);

    var usrrap_key = getQParamInt("usrrap_key");
    var rapport = model_reports(usrrap_key);
    var colurl = getQParam("colurl", null);

    var xxx_params = { filter : shared.qs2json(rapport), groupby: [] };

    var vanColname, totColname, titleColname;
    if ("title" in rapport.fields) {
        titleColname = "title";
    } else if ("titel" in rapport.fields) {
        titleColname = "titel";
    }

    for (var i in rapport.fields) {
        var fld = rapport.fields[i];
        if (!inArray(fld.visible, ["V", "S", "U", "C"])) { // V=Zichtbaar, S=Zichtbaar, verberg herhalingen, U=Klik-actie, "C"=Visible combine, "I"=onzichtbaar, "H"=verborgen
            continue;
        }
        if (inArray(fld.typ, ["date", "datetime"]) && fld.group.id === "G") {
            if (!vanColname) {
                vanColname = fld.dbs;
            } else {
                totColname = fld.dbs;
            }
        } else if (!titleColname && fld.typ == "varchar") {
            titleColname = fld.dbs;
        }
        if (titleColname && totColname) {
            break; // Klaar
        }
    }

    if (vanColname && !totColname) {
        totColname = vanColname;
    }

    if (vanColname === totColname && !xxx_params.filter[vanColname]) { // Er is maar 1 datum-kolom (allDay == true)
        if (!xxx_params.filter["end_" + vanColname]) {
            xxx_params.filter["end_" + vanColname] = date_to;
        }
        if (!xxx_params.filter["start_" + vanColname]) {
            xxx_params.filter["start_" + vanColname] = date_from;
        }
    } else {
        if (!xxx_params.filter[vanColname] && !xxx_params.filter["start_" + vanColname] && !xxx_params.filter["end_" + vanColname]) {
            xxx_params.filter["end_" + vanColname] = date_to;
        }
        if (!xxx_params.filter[totColname] && !xxx_params.filter["start_" + totColname] && !xxx_params.filter["end_" + totColname]) {
            xxx_params.filter["start_" + totColname] = date_from;
        }
    }

    xxx_params.filter.showall = 1;
    xxx_params.filter.limit = 9999;
    xxx_params.filter.scf_pivot = 0; // nooit

    var rap_data = rapport.REST_GET(xxx_params);

    var calresult = [];
    for (var i = 0; i < rap_data.length; i++)
    {
        var data = rap_data[i];
        var fclturl = null;
        if (colurl)
        {
            fclturl = data[colurl];
            // Voorkom dat hij binnen menu-wrapper wordt getoond
            if (fclturl && fclturl.indexOf("?u=") == 0) {
                fclturl = "?internal=1&" + fclturl.substring(1);
            }
        }
        else if (rapport.list.default_url && "id" in data)
        {
            fclturl = rapport.list.default_url.format(data.id);
        }

        calresult.push({ title:     data[titleColname] || "",
                         start:     data[vanColname].getTime() / 1000,
                         end:       data[totColname].getTime() / 1000,
                         jsstart:   data[vanColname],
                         jstot:     data[totColname],
                         fclturl:   fclturl,
                         color:     data.color,
                         textColor: data.textcolor,
                         allDay:       vanColname === totColname
                                    || data[vanColname].getTime() === new Date(data[vanColname]).setHours(0, 0, 0, 0) && data[vanColname].getTime() === data[totColname].getTime() // Beide data zijn middernacht en gelijk
                    });
    }
    // Ordenen op datum, handig om straks naar het eerste event te springen
    calresult.sort(function (a, b) {
        return a.start - b.start;
    });

    if ("title" in rapport.search && !rapport.title_locked) {
        calresult.unshift(rapport.search.title);
    }

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