Name

sn_cd.cd_Utils

Description

Generic functionality for the Content Publishing

Script

var cd_Utils = Class.create();

cd_Utils.HR_PROFILE_TABLE = "sn_hr_core_profile";
cd_Utils.DISPLAY_CONFIG_TABLE = "sn_cd_user_display_configuration";

cd_Utils.prototype = {
  initialize: function() {
  },

  checkUserHasRole: function(role) {
  	if(GlidePluginManager.isActive('com.sn_hr_core'))
  		return new sn_hr_core.hr_Utils().checkUserHasRole(role);
  	else
  		return gs.getUser().getRoles().indexOf(role) > -1;
  },

  /*  Method to determine if the user should be displayed in the orgchart
  	Parameter input: sys_id of the user from sys_user table
  	Return type: Boolean, true: indicates the user should be displayed, and false otherwise
  */
  canDisplayUser: function (userSysId) {
  	if(gs.nil(userSysId))
  		return false;
  	if(gs.getUserID() == userSysId) //User should always be able to see him/herself on the orgchart
  		return true;
  	var toDisplay = true;
  	var isMatch = false;
  	var userDisplayConfigGr = new GlideRecord(cd_Utils.DISPLAY_CONFIG_TABLE);
  	userDisplayConfigGr.addActiveQuery();
  	userDisplayConfigGr.addQuery('is_default',false);
  	userDisplayConfigGr.orderBy("order");
  	userDisplayConfigGr.query();
  	while (userDisplayConfigGr.next()) {
  		if (new sn_cd.cd_Audience().isUserInAudience(userDisplayConfigGr.getValue('audience'), userSysId, false)){
  			isMatch = true;
  			toDisplay = this._isDisplayConditionMet(userSysId, userDisplayConfigGr);
  			break;
  		}
  	}
  	// If none of the audience criteria are matched check for the fallback/default record.
  	if(!isMatch){
  		var displayConfigGr =  new GlideRecord(cd_Utils.DISPLAY_CONFIG_TABLE);
  		displayConfigGr.addQuery('is_default', true);
  		displayConfigGr.orderBy('sys_updated_on'); // in case there are multiple default records (e.g. if CD installed before HR), get the last updated
  		displayConfigGr.setLimit(1);
  		displayConfigGr.query();
  		if(displayConfigGr.next())
  			toDisplay = this._isDisplayConditionMet(userSysId, displayConfigGr);
  	}
  	return toDisplay;
  },

  /**************************************************************************
  *                                                                         *
  *                         Private Methods Below                           *
  *                                                                         *
  ***************************************************************************/


  /* Method to check if the current date is greater or lesser than the condition date
  Parameter Inputs: userSysId,userDisplayConfigGr
  userSysId: The sys_id of the user from the sys_user table
  userDisplayConfigGr: Glide Record from sn_cd_user_display_configuration table

  Return type: Boolean, true: indicates the current date is before or on condition date, false otherwise
  */
  _isDisplayConditionMet: function (userSysId, userDisplayConfigGr) {
  	var userGr = this._getUserGr(userSysId, userDisplayConfigGr.table);
  	if (!gs.nil(userGr)) {
  		var offSetDays = userDisplayConfigGr.getValue("offset_in_days");
  		var conditionTime = userGr.getValue(userDisplayConfigGr.getValue("date"));
  		if(gs.nil(conditionTime))
  			return false;
  		var offSetTime = new GlideDateTime(conditionTime);
  		offSetDays = userDisplayConfigGr.getValue('date_offset_type') == "after" ? offSetDays : -1 * offSetDays;
  		offSetTime.addDaysLocalTime(offSetDays);
  		return new GlideDateTime().after(offSetTime);
  	}
  	return false;
  },

  //function to return the usergr
  _getUserGr: function (sysUserId, table) {
  	var userGr = new GlideRecord(table);
  	if (table == cd_Utils.HR_PROFILE_TABLE){
  		if(userGr.get('user', sysUserId)){
  			var showUserWhenEployementDatePassed = gs.getProperty('sn_cd.show_employment_ended_users');
  			if(showUserWhenEployementDatePassed == 'false'){
  				var employmentEndDate = userGr.getValue('employment_end_date');
  				if(gs.nil(employmentEndDate))
  					return userGr;
  				employmentEndDate = new GlideDateTime(employmentEndDate);
  				// do not return user if employement end date is passed
  				if(new GlideDateTime().after(employmentEndDate))
  					return null;
  			}
  			return userGr;
  		}
  		return null;
  	}
  	else
  		return (userGr.get(sysUserId)) ? userGr : null;
  },

  type: 'cd_Utils'
};

Sys ID

147242e2876323004d3b851c97cb0b84

Offical Documentation

Official Docs: