1 /*
  2  * ***** BEGIN LICENSE BLOCK *****
  3  * Zimbra Collaboration Suite Web Client
  4  * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
  5  *
  6  * The contents of this file are subject to the Common Public Attribution License Version 1.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at: https://www.zimbra.com/license
  9  * The License is based on the Mozilla Public License Version 1.1 but Sections 14 and 15
 10  * have been added to cover use of software over a computer network and provide for limited attribution
 11  * for the Original Developer. In addition, Exhibit A has been modified to be consistent with Exhibit B.
 12  *
 13  * Software distributed under the License is distributed on an "AS IS" basis,
 14  * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing rights and limitations under the License.
 16  * The Original Code is Zimbra Open Source Web Client.
 17  * The Initial Developer of the Original Code is Zimbra, Inc.  All rights to the Original Code were
 18  * transferred by Zimbra, Inc. to Synacor, Inc. on September 14, 2015.
 19  *
 20  * All portions of the code are Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc. All Rights Reserved.
 21  * ***** END LICENSE BLOCK *****
 22  */
 23 
 24 /**
 25  * Creates a resource.
 26  * @class
 27  * This class represents a resource.
 28  * 
 29  * @param	{String}	id		the id
 30  * @param	{ZmList}	list	the list
 31  * @param	{constant}	resType		the resource type
 32  * 
 33  * @extends		ZmContact
 34  * @see		ZmCalBaseItem
 35  */
 36 ZmResource = function(id, list, resType) {
 37 	id = id ? id : Dwt.getNextId();
 38 	ZmContact.call(this, id, list, ZmItem.RESOURCE);
 39 	
 40 	this.resType = resType;
 41 };
 42 
 43 ZmResource.F_capacity			= "zimbraCalResCapacity";
 44 ZmResource.F_contactMail		= "zimbraCalResContactEmail";
 45 ZmResource.F_locationName		= "zimbraCalResLocationDisplayName";
 46 ZmResource.F_mail			= "email";
 47 ZmResource.F_name			= "fullName";
 48 ZmResource.F_type			= "zimbraCalResType";
 49 ZmResource.F_description		= "description";
 50 
 51 ZmResource.ATTR_LOCATION	= "Location";
 52 ZmResource.ATTR_EQUIPMENT	= "Equipment";
 53 
 54 ZmContact.initAttrVariants(ZmResource);
 55 
 56 /**
 57 * Creates a resource from an XML node.
 58 *
 59 * @param node		a "calresource" XML node
 60 * @param args		args to pass to the constructor
 61 */
 62 ZmResource.createFromDom =
 63 function(node, args) {
 64 	var resource = new ZmResource(node.id, args.list);
 65 	resource._loadFromDom(node);
 66 	resource.resType = (resource.getAttr(ZmResource.F_type) == ZmResource.ATTR_LOCATION) ?
 67 						ZmCalBaseItem.LOCATION : ZmCalBaseItem.EQUIPMENT;
 68 	if (!resource.list) {
 69 		var calApp = appCtxt.getApp(ZmApp.CALENDAR);
 70 		resource.list = (resource.resType == ZmCalBaseItem.LOCATION) ? calApp.getLocations() :
 71 																   calApp.getEquipment();
 72 	}
 73 	
 74 	return resource;
 75 };
 76 
 77 ZmResource.prototype = new ZmContact;
 78 ZmResource.prototype.constructor = ZmResource;
 79 
 80 ZmResource.prototype.toString =
 81 function() {
 82 	return "ZmResource";
 83 };
 84 
 85 /**
 86  * Checks if the resource is a location.
 87  * 
 88  * @return	{Boolean}	<code>true</code> if is location
 89  */
 90 ZmResource.prototype.isLocation =
 91 function() {
 92 	return (this.resType == ZmCalBaseItem.LOCATION);
 93 };
 94 
 95 /**
 96  * Gets the resource email.
 97  * 
 98  * @return	{String}	the email
 99  */
100 ZmResource.prototype.getEmail =
101 function() {
102 	var attr = this.getAttr(ZmResource.F_mail);
103 	return attr instanceof Array ? attr[0] : attr;
104 };
105 
106 /**
107  * Gets the resource full name.
108  * 
109  * @return	{String}	the full name
110  */
111 ZmResource.prototype.getFullName =
112 function() {
113 	return ( this.getAttr(ZmResource.F_name)
114             || this.getAttr(ZmResource.F_locationName) );
115 };
116 
117 /**
118  * Initializes from an email address.
119  *
120  * @param {AjxEmailAddress|String}	email	an email address object an email string
121  */
122 ZmResource.prototype.initFromEmail =
123 function(email) {
124 	if (email instanceof AjxEmailAddress) {
125 		this.setAttr(ZmResource.F_mail, email.getAddress());
126 		this.setAttr(ZmResource.F_name, email.getName());
127 	} else {
128 		this.setAttr(ZmResource.F_mail, email);
129 	}
130 };
131 
132 ZmResource.prototype.getAttendeeText =
133 function(type, shortForm) {
134 	var text = "";
135 	var name = this.getFullName();
136 	var email = this._lookupEmail || this.getEmail();
137 	if (shortForm) {
138 		text = name || email || "";
139 	} else {
140 		var e = new AjxEmailAddress(email, null, name);
141 		text = e.toString();
142 	}
143 	return text;
144 };
145