Created
July 7, 2020 19:25
-
-
Save thisnameissoclever/8d1d0da8e587bfb8582a47a589e17cf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data.orgChart = data.orgChart || {}; | |
data.orgChart.tiers = data.orgChart.tiers || []; | |
data.orgChart.i18n = { | |
avatar: gs.getMessage("{0} avatar"), | |
directReport: gs.getMessage("{0} direct report"), | |
directReports: gs.getMessage("{0} direct reports"), | |
minimumCharacters: gs.getMessage("Please enter {0} or more characters") | |
}; | |
var loopDetected = false; | |
var i = 0; | |
var max = 1; | |
var mainList = ['sys_id', 'photo', 'name', 'title', 'department', 'manager', 'location.city', 'location.state', 'location.country']; | |
var CONST = { | |
minList: ['sys_id', 'photo', 'name', 'title'], | |
table: { | |
attachment: "sys_attachment", | |
live: "live_profile", | |
ocConfig: "sn_cd_config_org_chart", | |
user: "sys_user" | |
}, | |
userColumn: "" | |
}; | |
var cdUtil = new sn_cd.cd_Utils(); | |
var ocUtil = ocUtil || { | |
getGR: getGR, | |
getAvatar: getAvatar, | |
getHierarchy: getHierarchy, | |
getManager: getManager, | |
getReportGR: getReportGR, | |
getReports: getReports, | |
getUserInitial: getUserInitial, | |
prepData: prepData, | |
updateData: updateData, | |
searchUser: searchUser, | |
getReporteeDetails:getReporteeDetails | |
}; | |
var searchedPerson = $sp.getParameter('p'); | |
var orgchartConfig = ocUtil.getGR({}, CONST.table.ocConfig); | |
// Check sn_hr_core_config_orgchart for config record, else use static default | |
if (orgchartConfig && orgchartConfig.next()) { | |
data.orgChart.config = ['', '', '', '']; | |
CONST.table.profile = orgchartConfig.getValue('profile_table'); | |
CONST.userColumn = orgchartConfig.getValue('user'); | |
// Check the table if extended from sys_user | |
var checkTable = new GlideTableHierarchy(CONST.table.user).getAllExtensions(); | |
CONST.isSysUserExtended = checkTable.indexOf(CONST.table.profile) > -1; | |
Object.keys(orgchartConfig).map(function(o) { | |
if (/show/.test(o) && +orgchartConfig.getValue(o)) { | |
var index = +o.match(/\d$/); | |
var val = orgchartConfig.getValue('detail_' + index); | |
data.orgChart.config.splice(index-1, 1, val); | |
mainList.push(val); | |
} | |
}); | |
// filter array to remove empty val | |
data.orgChart.config = data.orgChart.config.filter(String); | |
} else { | |
// if no config is available, use default value | |
CONST.table.profile = CONST.table.user; | |
data.orgChart.config = ['email', 'phone', 'mobile_phone', 'location']; | |
mainList = mainList.concat(data.orgChart.config); | |
} | |
/* handle hrprofile as search param | |
* get profile glide record, then use record.user to get another sys_user glide record | |
*/ | |
if ($sp.getParameter('hrprofile')) { | |
searchedPerson = ocUtil.getGR($sp.getParameter('hrprofile'), "sn_hr_core_profile"); | |
// if user cannot be found, show message and use current user | |
if (!searchedPerson || !searchedPerson.user) { | |
searchedPerson = gs.getUserID(); | |
gs.addInfoMessage(gs.getMessage("Could not find user {0}", $sp.getParameter('hrprofile'))); | |
} else | |
searchedPerson = searchedPerson.user.toString(); | |
searchedPerson = ocUtil.getGR(searchedPerson); | |
searchedPerson = searchedPerson.getUniqueValue(); | |
} | |
if (input && input.orgChart.action === "updateChart") | |
ocUtil.updateData(input.orgChart.searchNewUser, input.orgChart.noManager); | |
else if (input && input.orgChart.action === "searchForPerson") | |
ocUtil.searchUser(input.orgChart.searchTerm, input.orgChart.pg); | |
else | |
ocUtil.updateData((searchedPerson || gs.getUserID()), -1); | |
function getGR (param, table) { | |
var gr = new GlideRecord(table || CONST.table.user); | |
gr.addActiveQuery(); | |
if (typeof param !== 'string') { | |
Object.keys(param).map(function(o) { | |
gr.addQuery(o, param[o]); | |
}); | |
gr.orderBy(gr.getDisplayName()); | |
gr.query(); | |
} else { | |
// avoid .get() since it includes inactive users for admins | |
gr.addQuery('sys_id', param); | |
gr.query(); | |
gr.next(); | |
} | |
return gr; | |
} | |
function getAvatar (id) { | |
var liveProfGR = this.getGR({'document': id},CONST.table.live); | |
if (!liveProfGR || !liveProfGR.hasNext()) | |
return null; | |
liveProfGR.next(); | |
var sysAttrGR = this.getGR({'table_sys_id': liveProfGR.sys_id}, CONST.table.attachment); | |
if (!sysAttrGR || !sysAttrGR.hasNext()) | |
return null; | |
sysAttrGR.next(); | |
return sysAttrGR.sys_id + ".iix"; | |
} | |
function getHierarchy (id) { | |
var person = ocUtil.getGR(id); | |
if (!person.canRead() || !person.getRowCount()) | |
return {}; | |
var personObj = ocUtil.prepData(person); | |
personObj.shouldShow = cdUtil.canDisplayUser(person.getUniqueValue()); | |
personObj.directReports = ocUtil.getReports(person); | |
personObj.reportCount = personObj.directReports.length; | |
// if tiers array contain duplicate user, stop recursion in getManager() | |
function checkForCircularLogic (tier) { | |
var tierPerson = tier.sys_id.value; | |
if (id == tierPerson) | |
loopDetected = true; | |
} | |
data.orgChart.tiers.map(checkForCircularLogic); | |
if (!loopDetected && personObj.shouldShow) | |
data.orgChart.tiers.push(personObj); | |
return personObj; | |
} | |
function getManager (person) { | |
if (person.manager && person.manager.value && !loopDetected) { | |
var managerObj = this.getHierarchy(person.manager.value); | |
this.getManager(managerObj); | |
} | |
} | |
/* Get the details of the reportees if he/she matches the criteria. | |
If they dont, the function goes on to check if his/her reportees matches the criteria | |
and fetches their reports */ | |
function getReports(person) { | |
var resultObj = []; | |
var directReportObj = []; | |
var hiddenUserReportsObj = []; | |
var reports = this.getReportGR(person); | |
if (!reports) | |
return null; | |
while (reports.next()) { | |
if (cdUtil.canDisplayUser(reports.getUniqueValue())) { | |
directReportObj = ocUtil.getReporteeDetails(reports); | |
if(directReportObj) | |
resultObj = resultObj.concat(directReportObj); | |
} else { // if reports dont match the configuration, fetch their reports using recursion | |
hiddenUserReportsObj = ocUtil.getReports(reports); | |
if (hiddenUserReportsObj) | |
resultObj = resultObj.concat(hiddenUserReportsObj); | |
} | |
} | |
return resultObj; | |
} | |
//Get the reportee details and their count, and return it if it matches the configuration. | |
function getReporteeDetails(employeeGr) { | |
var isEmployeeDisplayed = cdUtil.canDisplayUser(employeeGr.getUniqueValue()); | |
var reportsGr = this.getReportGR(employeeGr); | |
var employeePrepData = {}; | |
var reportCount = 0; | |
while (reportsGr.next()) { | |
if (cdUtil.canDisplayUser(reportsGr.getUniqueValue())) | |
reportCount += 1; | |
else { // if configuration is not met get the report details and the count through recursion until they are met. | |
var reporteeDetails = ocUtil.getReporteeDetails(reportsGr); | |
if(reporteeDetails.reportCount){ | |
var reportCounter = parseInt(reporteeDetails.reportCount); | |
if (!gs.nil(reportCounter)) | |
reportCount = reportCount + reportCounter; | |
} | |
} | |
} | |
if (isEmployeeDisplayed || cdUtil.canDisplayUser(reportsGr.getUniqueValue())){ | |
employeePrepData = this.prepData(employeeGr); | |
employeePrepData.reportCount = reportCount; | |
} | |
return employeePrepData; | |
} | |
function getReportGR (p) { | |
p = typeof p === "string" ? p : p.getUniqueValue(); | |
return this.getGR({manager: p}); | |
} | |
function getUserInitial (d) { | |
var initial = d.name.getDisplayValue().trim(); | |
var cutOff = 3; | |
if (initial) { | |
if (/[^\u0000-\u007F]/.test(initial)) | |
cutOff = 2; | |
else | |
// sanitize name: i.e. "john 'aka' smith" to "john smith" or "o'neil" to "oneil" | |
initial = initial.replace(/\W[\w\s]+\W(?!\w)|[^a-z\s]/gi, ""); | |
initial = initial.match(/[^\u0000-\u007F]+|\b[a-z]/gi).join(""); | |
} else | |
initial = "--"; | |
return (initial.length > cutOff) ? initial.substr(0, cutOff) : initial; | |
} | |
function prepData (gr, list) { | |
var data = {}; | |
data.avatar = this.getAvatar(gr.getUniqueValue()); | |
data.initial = this.getUserInitial(gr); | |
// if no record in the specified table (from config), use default list | |
(CONST[list] || mainList).map(function(o) { | |
var mainFieldList = ['sys_id', 'photo', 'name', 'title', 'department', 'manager', 'location.city', 'location.state', 'location.country']; | |
// if not from mainList, use the specified table GR (from config) | |
if (mainFieldList.indexOf(o) == -1 && CONST.userColumn) | |
gr = getFromParentGr(gr); | |
var ge = gr.getElement(o); | |
data[o] = { | |
display_value : ge ? ge.getDisplayValue() : "", | |
label: ge ? ge.getLabel() : "", | |
value: ge ? ge.toString() : "", | |
type: ge ? ge.getED().getInternalType() : "" | |
}; | |
}); | |
return data; | |
} | |
function updateData (id, noManager) { | |
if (noManager < 0) { | |
ocUtil.getManager(ocUtil.getHierarchy(id)); | |
data.orgChart.tiers.reverse(); | |
} | |
} | |
function searchUser (term, pg) { | |
data.orgChart.searchRes = []; | |
var res = new GlideRecord(CONST.table.user); | |
res.addActiveQuery(); | |
res.addQuery("123TEXTQUERY321", term); | |
res.chooseWindow(10 * pg - 10, 10 * pg); | |
res.query(); | |
if (!res.hasNext()) | |
return; | |
data.orgChart.searchResCount = res.getRowCount(); | |
while (res.next()){ | |
if(cdUtil.canDisplayUser(res.getUniqueValue())) | |
data.orgChart.searchRes.push(this.prepData(res, 'minList')); | |
else | |
data.orgChart.searchResCount -- ; | |
} | |
} | |
// get the user GR of specified table (from config) | |
function getFromParentGr (gr) { | |
var queryObj = {}; | |
queryObj[CONST.userColumn] = gr.getUniqueValue(); | |
var parentGr = ocUtil.getGR(queryObj, CONST.table.profile); | |
if (parentGr.next()) | |
gr = parentGr; | |
return gr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment