Skip to content
Snippets Groups Projects
contentProcess.js 5.84 KiB
Newer Older
import("KeywordRegistry_basic");
import("Sql_lib");
import("Contact_lib");
import("system.datetime");
import("system.logging");
import("Util_lib");
import("system.translate");
Tobias Feldmann's avatar
Tobias Feldmann committed
import("system.result");
Tobias Feldmann's avatar
Tobias Feldmann committed
import("Context_lib");

if (vars.exists("$param.ObjectType_param") && vars.get("$param.ObjectType_param") 
    && vars.exists("$param.ObjectRowId_param") && vars.get("$param.ObjectRowId_param"))
{
    var contextList = JSON.parse(vars.get("$param.ObjectType_param"));
    var contactId = JSON.parse(vars.get("$param.ObjectRowId_param"));
    var data = _get360Data(contactId, contextList);
    
    // #1075280 added null-check to prevent misbehaviour
    if(vars.get("$local.idvalues") != null)
    {
       data = data.filter(function (row){
           return vars.get("$local.idvalues").includes(row[0]);
       });
    }
    
 * collects all data for the 360Degree tree.
 * @param {String} pContactId, the main Contactid that will be used for the Connections
 * @param {String[]} pContextList, a list of Contexts that should be displayed
 *  
 *  @return {String[][]} the resulting data
function _get360Data(pContactId, pContextList)
{   
    var resultList = [];
    var filter = vars.get("$sys.filter");
    for(var context in pContextList) 
        if(pContextList[context].hasOwnProperty("setGroupBy") && pContextList[context].hasOwnProperty("groupByKeyword"))
            var groupKeyword = Utils.objectFromMap(new Map(KeywordUtils.getEntryArray(pContextList[context]["groupByKeyword"])));

        var res = ContextUtils.getContextDataViaReadEntity(context, pContextList[context], filter, pContactId);
        if(res.length > 0)
            var roleObj = {}; //helper obejct
            if(context == "Salesproject" && !ContactUtils.isOrganisation(pContactId))//we want to add the roles that contacts have in each salesproject to the description
            {
                var roles = newSelect(["OBJECTMEMBER.OBJECT_ROWID", KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.MemberRole(), "MEMBERROLE")])
                                        .from("OBJECTMEMBER")
                                        .where("OBJECTMEMBER.OBJECT_TYPE", "Salesproject")
                                        .and("OBJECTMEMBER.CONTACT_ID", contactId)
                                        .orderBy("OBJECTMEMBER.OBJECT_ROWID")
                                        .table();
                //one select outside of the for each for performance, we assign the roles to roleObj so we can add them in the for each accordingly
                for (let i = 0; i < roles.length; i++)
                {
                    var salesProjectId, role;
                    [salesProjectId, role] = roles[i];
                    if(!roleObj.hasOwnProperty(salesProjectId))
                    {
                        roleObj[salesProjectId] = [];
                    }
                    roleObj[salesProjectId].push(role)
                }
            }
            
                var uid = JSON.stringify({
                    "id": row["#UID"], 
                    "type": context
                });
                var targetid = row["#UID"];
                var title = row["#CONTENTTITLE"];
                var description = row["#CONTENTDESCRIPTION"];
                var dataDate = row["DATE_NEW"];
                var active = row["ACTIVE"];
                if(pContextList[context].hasOwnProperty("setGroupBy"))
                    group = row[pContextList[context]["setGroupBy"]]
                

                if(pContextList[context].hasOwnProperty("setGroupBy"))
                {
                    if( pContextList[context].hasOwnProperty("groupByKeyword") && groupKeyword)
                        group = groupKeyword[row[pContextList[context]["setGroupBy"]]]
                    else
                        group = row[pContextList[context]["setGroupBy"]]
                }
                
                if(context == "Salesproject" && !ContactUtils.isOrganisation(pContactId))//add roles to the description
                {
                    description = description + " | " + translate.text("Role") + ": "
                    if(roleObj.hasOwnProperty(salesProjectId))
                    {
                        var roles = roleObj[salesProjectId];
                        for (let i = 0; i < roles.length; i++)
                        {
                            if(i != 0)
                            {
                                description = description + ", ";
                            }
                            description = description + roles[i];
                        }
                    }
                }
                
                resultList.push([
                    uid,                                          // UID
                    targetid,                                     // TARGET_ID
                    context,                                      // TARGET_CONTEXT
                    translate.text(pContextList[context]),        // TARGET_CONTEXT.displayValue
                    title,                                        // TITLE
                    description,                                  // DESCRIPTION
                    dataDate,                                     // DATE
                    datetime.toDate(dataDate, "yyyy"),            // YEAR
                    ContextUtils.getEntity(context),              // ENTITY_NAME
                    translate.text(group),                        // GROUP
                    active                                        // ACTIVE
                    ]); 
            });
        }
    }
    var sortArr = [9, false]
    resultList = ArrayUtils.sortMulti(resultList, sortArr)
Heinz Boesl's avatar
Heinz Boesl committed
    return resultList;