Skip to content
Snippets Groups Projects
contentProcess.js 4.25 KiB
import("system.tools");
import("system.logging");
import("system.db");
import("Sql_lib");
import("system.result");
import("system.vars");
import("system.calendars");
import("system.datetime");
import("system.eMath");
import("system.util");
import("system.neon");
import("system.entities")


var cond = SqlCondition.begin();
var appointmentUids;
var idscond;

if(vars.get("$local.idvalues") != null && vars.get("$local.idvalues") != "")
{
    var selectedids = vars.get("$local.idvalues");
    result.object([buildEntry(calendars.getEntry(selectedids, null, null), null)]);
}

else if(vars.getString("$param.LinkedAppointmentsFromDashlet_param"))
{
    var contactid = tools.getCurrentUser()["params"]["contactID"];
    
    cond.andPrepareIfSet("AB_APPOINTMENTLINK.OBJECT_ROWID", contactid)
    idscond = db.translateCondition(cond.build("1 = 1"));
   
    appointmentUids = db.table("select APPOINTMENT_ID from AB_APPOINTMENTLINK where " + idscond);
    result.object(buildEntriesFromUids(appointmentUids));
}

/**
 * Will be triggert if a module needs AppointmentFilter_view
 */
else if(vars.getString("$param.LinkedObjectId_param") != undefined)
{
    cond.andPrepareVars("AB_APPOINTMENTLINK.OBJECT_ROWID", "$param.LinkedObjectId_param")
    idscond = db.translateCondition(cond.build("1 = 1"));
   
    appointmentUids = db.table("select APPOINTMENT_ID from AB_APPOINTMENTLINK where " + idscond);
    result.object(buildEntriesFromUids(appointmentUids));
}
/**
 * Will be used, if the user is operating the calendar.
 */
else if(vars.exists("$param.Entry_param") && vars.get("$param.Entry_param"))
{
    var entry = JSON.parse(vars.getString("$param.Entry_param"));

    var masterEntry = null;
    if (vars.exists("$param.MasterEntry_param") && vars.get("$param.MasterEntry_param") != "") {
        masterEntry = JSON.parse(vars.getString("$param.MasterEntry_param"));
    }

    //@TODO Icon 
    result.object([
        buildEntry(entry, masterEntry)
    ]);
}

function buildEntriesFromUids(appointmentUids)
{
    var entryArray = new Array(appointmentUids.length);
    
    for(var i = 0; i < appointmentUids.length; i++)
        entryArray[i] = buildEntry(calendars.getEntry(appointmentUids[i], null, null), null);
    
    return entryArray;
}


function buildEntry(pEntry, pMasterentry)
{
    var uid = pEntry[calendars.ID];    
    var summary = pEntry[calendars.SUMMARY];
    var attendees = pEntry[calendars.AFFECTEDUSERS];
    var startdate = pEntry[calendars.DTSTART];
    var enddate = pEntry[calendars.DTEND];
    var links = pEntry[calendars.LINKS];
    var description = pEntry[calendars.DESCRIPTION];
    if(pEntry[calendars.ORGANIZER2] != undefined)
        var organizer = pEntry[calendars.ORGANIZER2]["paramvalue"];
    var status = pEntry[calendars.STATUS];
    var location = pEntry[calendars.LOCATION];
    var reminder = pEntry[calendars.REMINDER_DURATION];
    var remindercheck = pEntry[calendars.HASREMINDER]
    var classification = pEntry[calendars.CLASSIFICATION];
    var transparency = pEntry[calendars.TRANSPARENCY];
    var categories = pEntry[calendars.CATEGORIES];
    var isAllDay = pEntry["X-ADITO-ISALLDAYEVENT"] != null ? pEntry["X-ADITO-ISALLDAYEVENT"] : "FALSE";
    
    var masterBegin = pMasterentry != null ? pMasterentry[calendars.DTSTART] : null
    var masterEnd = pMasterentry != null ? pMasterentry[calendars.DTEND] : null
    
    // Recurrence
    var recurrenceID = pEntry[calendars.RECURRENCEID];
    var rrule = null;
    if (pMasterentry != null) { // Entry is a recurrence exception, therefore get rrule from master
        rrule = pMasterentry[calendars.RRULE] != null ? pMasterentry[calendars.RRULE][0] : null;
    } else {
        rrule = pEntry[calendars.RRULE] != null ? pEntry[calendars.RRULE][0] : null;
    }
    
    return [
            uid, 
            attendees.length, 
            startdate, 
            enddate, 
            summary, 
            organizer,
            attendees, 
            status, 
            links, 
            description, 
            location, 
            '', 
            isAllDay,
            classification,
            transparency, 
            categories, 
            reminder, 
            remindercheck, 
            rrule, 
            recurrenceID, 
            null, 
            masterBegin, 
            masterEnd
        ];
}