API Docs for: 1.0.0
Show:

File: scotts\classes\CWTCalendarUtil.js

var CWTCalendarUtil = Class.create();
/**
 * Utility for working with the Portal Calendar class.
 * 
 * @class CWTCalendarUtil
 * @constructor
 * @author Alexander Anderson (cwt_alexander)
 * @module ScriptIncludes
 * @see https://scottsdev.service-now.com/sys_script_include.do?sys_id=319b7db5db9af7405cacd7795e961959
 * @param {GlideRecord | PortalCalendar | String} calendar A direct glide reference or SysID for the
 * 		Calendar with which to interact.
 */
(function() {
	/**
	 * Values for a calendar to cast it to JSON.
	 * 
	 * Limited values for privacy concerns.
	 * @property toJSONMap
	 * @type Object
	 * @private
	 */
	var toJSONMap = {
		"u_description": "description",
		"u_number": "number",
		"u_name": "name"
	};
	
	/**
	 * Values for a calendar to cast it to JSON.
	 * 
	 * Limited values for privacy concerns.
	 * @property toJSONKeys
	 * @type Object
	 * @private
	 */
	var toJSONKeys = Object.keys(toJSONMap);
	
	/**
	 * Determine if a value should be considered true.
	 * 
	 * Due to a variety of ways of true/false values being returned by service now, this
	 * method exists to level set to a JavaScript boolean value for use.
	 * @method isFalse
	 * @private
	 * @static
	 * @param {Object | String | Boolean | Number} value
	 * @return {Boolean} True if the value is considered as trying to be "true", false otherwise.
	 */
	var isTrue = function(value) {
		return value == "1" || value == "true" || value === "t" || value === true;
	};
	
	/**
	 * Determine if a value should be considered false.
	 * 
	 * Due to a variety of ways of true/false values being returned by service now, this
	 * method exists to level set to a JavaScript boolean value for use.
	 * @method isFalse
	 * @private
	 * @static
	 * @param {Object | String | Boolean | Number} value
	 * @return {Boolean} True if the value is considered as trying to be "false", false otherwise.
	 */
	var isFalse = function(value) {
		return value == "0" || value == "false" || value === "f" || value === false;
	};
	
	/**
	 * Convert a GlideRecord to a standard Javascript object 
	 * @method convert
	 * @static
	 * @param {GlideRecord} record The record to convert to a standard Javascript object
	 * @return {Object} The records values as an independent object.
	 */
	var convert = CWTCalendarUtil.convert = function(record) {
		if(!record) {
			return undefined;
		}
		
		var keys = Object.keys(record),
			obj = {},
			buffer,
			x;
		
		for(x=0; x<keys.length; x++) {
			buffer = record.getValue(keys[x]);
			if(typeof(buffer) === "object") {
				obj[keys[x]] = convert(buffer);
			} else if(typeof(buffer) === "function") {
				// Skip functions
			} else {
				obj[keys[x]] = buffer;
			}
		}
		
		gs.log("Returning: " + JSON.stringify(obj, null, 4));
		return obj;
	};
	
	
	CWTCalendarUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	    "type": "CWTCalendarUtil",
		"initialize": function(calendar) {
			if(calendar) {
				if(calendar instanceof GlideRecord) {
					this.calendar = calendar;
				} else {
					this.calendar = new GlideRecord("u_portal_calendar");
					this.calendar.get(calendar);
				}
			}
		},
		/**
		 * The PortalCalendar that this utility is currently associated with for
		 * pulling values and performing checks.
		 * @property calendar
		 * @type GlideRecord | PortalCalendar
		 */
		
		/**
		 * Check if the current user has access to the currently declared calendar.
		 * @method hasAccess
		 */
		"hasAccess": function() {
			return CWTPortalUtil.hasPortalAccess(this.calendar);
		},
		
		/**
		 * Get the events associated with the current calendar (See: this.calendar).
		 * 
		 * @method getEvents
		 * @param {Boolean} [published] When true, only published events are retrieved.
		 * @param {Boolean} [featured] When true, only featured events are retrieved.
		 * @param {GlideDate} [lower] When specified, used as the minimum time for the
		 * 		starting date of an event.
		 * @param {GlideDate} [upper] When specified, used as the maximum time for the
		 * 		starting date of an event.
		 * @return {Array} Array of JSON objects describing the events currently
		 * 		associated with the current calendar sorted by begin date.
		 */
		"getEvents": function(published, featured, lower, upper) {
			var gathering = new GlideRecord("u_portal_calendar_event"),
				events = [];

			gathering.addQuery("u_calendar", this.calendar.getValue("sys_id"));
			if(lower) {
				gathering.addQuery("u_begin_date", ">=", lower);
			}
			if(upper) {
				gathering.addQuery("u_begin_date", "<=", upper);
			}
			if(published) {
				gathering.addQuery("u_publish_to_calendar", published);
			}
			if(featured) {
				gathering.addQuery("u_featured_event", featured);
			}
			gathering.orderBy("u_begin_date");
			gathering.orderBy("u_title");
			gathering.query();
			
			while(gathering.next()) {
				events.push(convert(gathering));
			}
			
			return events;
		},
		/**
		 * Get the key values of the current calendar for managing displaying
		 * information regarding it.
		 * @method getCalendarJSON
		 * @return {Object} Describes the minimal details of the current calendar
		 * 		to avoid disclosing unneeded private information such as the owner.
		 */
		"getCalendarJSON": function() {
			var json = {},
				x;
			
			if(this.calendar) {
				for(x=0; x<toJSONKeys.length; x++) {
					json[toJSONMap[toJSONKeys[x]]] = this.calendar.getValue(toJSONKeys[x]);
				}
			}
			
			return json;
		}
	});
})();