<commit_before>/*##############################################################################
#    VNC-Virtual Network Consult GmbH.
#    Copyright (C) 2004-TODAY VNC-Virtual Network Consult GmbH
#    (<http://www.vnc.biz>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################*/
function com_zimbra_erp_mail_connector_HandlerObject() {
}

com_zimbra_erp_mail_connector_HandlerObject.prototype= new ZmZimletBase;

com_zimbra_erp_mail_connector_HandlerObject.constructor = com_zimbra_erp_mail_connector_HandlerObject;

com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID="send_and_push";

com_zimbra_erp_mail_connector_HandlerObject.BUTTON3_ID="push_to_erp";

com_zimbra_erp_mail_connector_HandlerObject.prototype.initializeToolbar = function(app, toolbar, controller,view) {
	var patt="COMPOSE";
	// only add this button for the following 3 views
	if(view.match(patt)=="COMPOSE"){
		if (toolbar.getOp(com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID)) {
			return;
		}
		var btnIndex = -1;
		for ( var i = 0, count = toolbar.opList.length; i < count; i++) {
			if (toolbar.opList[i] == ZmOperation.SEND) {
				btnIndex = i + 1;
				break;
			}
		}
		// create params obj with button details
		var send_and_push=this.getMessage("send_and_push");
		var send_and_push_tooltip=this.getMessage("send_and_push_tooltip");
		var buttonArgs = {
			text :send_and_push,
			tooltip :send_and_push_tooltip,
			index : btnIndex, // position of the button
			image : "NewMessage" // icon
		};
		var button = toolbar.createOp(com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID, buttonArgs);
		button.addSelectionListener(new AjxListener(this,this._handle_push_and_save, controller));	
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleCalSyncBtnClick = function(controller) {
	var calendars = appCtxt.getFolderTree().getByType(ZmOrganizer.CALENDAR);
	for(var i in calendars){
		if(calendars[i].url != undefined){
			calendars[i].sync();
		}
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._handle_push_and_save = function(controller) {
	var currController = appCtxt.getCurrentController();
	var respCallback = new AjxCallback(this, this._handleResponsefromsend);
	currController.sendMsg("","",respCallback);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleResponsefromsend = function(result) {
	var resp=result.getResponse();
	var ac = window.parentAppCtxt || window.appCtxt;
	var msgid=resp.m[0].id;
	var list = ac.getApp(ZmApp.MAIL).getMailListController().getList();
	this.msg = new ZmMailMsg(resp.m[0].id, list, true); // do not cache this temp msg
	this.msg._loadFromDom(resp.m[0]);
	this.msg._loaded = true; // bug fix #8868 - force load for rfc822 msgs since they may not return any content
	this.msg.readReceiptRequested = false; // bug #36247 - never allow read receipt for rfc/822 message
	this.msg._part = resp.m[0].part;
	var from=new AjxEmailAddress(appCtxt.getUsername(),AjxEmailAddress.FROM);
	var msgLoadCallback = new AjxCallback(this, this._msgLoaded,[result]);
	this.msg.load({forceLoad:true,callback:msgLoadCallback});
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._msgLoaded = function(result){
	var res=result.getResponse();
	this.mail_from=[];
	var rec=this.msg.getAddress(ZmMailMsg.HDR_TO);
	if(rec.toString().indexOf("<") > -1){
		rec=rec.toString().split("<")[1];
		rec=rec.toString().split(">")[0];
	}
	this.mail_from.push(rec);
	this.msgFlag=1;
	this.doDrop(this.msg);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleSOAPErrorResponseXML = function(result) {
	if (result.isException()) {
		this.alert_critical("mail_push_exception");
		var exception = result.getException();
		return;
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.getFolderDialog=function(){
	var title=this.getMessage("title_Contact_Dialog");
	var dialog = appCtxt.getChooseFolderDialog();
	dialog.registerCallback(DwtDialog.OK_BUTTON, new AjxCallback(this, this._handleChooseFolder));
	var params = {
		overviewId:     dialog.getOverviewId(ZmApp.CONTACTS),
		title:          title,
		treeIds:        [ZmOrganizer.ADDRBOOK],
		skipReadOnly:   true,
		skipRemote:     false,
		noRootSelect:   true,
		appName:        ZmApp.CONTACTS
	};
	params.omit = {};
	params.omit[ZmFolder.ID_TRASH] = true;
	params.omit[addBook_ContactId]=true;
	params.omit[addBook_EcontactId]=true;
	dialog.popup(params);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype.onParticipantActionMenuInitialized = function(controller, menu) {
	this.onActionMenuInitialized(controller, menu);
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.onActionMenuInitialized = function(controller, menu) {
	this.addMenuButton(controller, menu);
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.addMenuButton = function(controller, menu) {
	var ID = com_zimbra_erp_mail_connector_HandlerObject.BUTTON3_ID;
	var btnName=this.getMessage("push_to_btn");
	var btnTooltip=this.getMessage("push_to_btn_tooltip");
	if (!menu.getMenuItem(ID)) {
		var op = {
			id : ID,
			text : btnName,
			tooltip: btnTooltip,
			image : "NewMessage"
		};
		var opDesc = ZmOperation.defineOperation(null, op);
		menu.addOp(ID, 1000);// add the button at the bottom
		menu.addSelectionListener(ID, new AjxListener(this,this._menuButtonListener, controller));
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._menuButtonListener = function(controller) {
	this.mailtopush = "";
	var con=appCtxt.getCurrentController();
	this.mailtopush = controller.getCurrentView().getDnDSelection();
	this.mailtopush = (this.mailtopush instanceof Array) ? this.mailtopush : [this.mailtopush];
	var eml;
	for ( var i = 0; i < this.mailtopush.length; i++) {
		if (this.mailtopush[i].type === ZmId.ITEM_CONV) {
			var arry = this.mailtopush[i].participants.getArray();
			if(arry.length >0) {
				eml = arry[arry.length - 1].address;
			}
		} else if (this.mailtopush[i].type === ZmId.ITEM_MSG) {
			var obj = this.mailtopush[i].getAddress(AjxEmailAddress.FROM);
			if(obj)
			eml = obj.address;
		}
	}
	this.mail_from=[];
	this.mail_from.push(eml);
	if(this.mailtopush != null){
		this.doDrop(this.mailtopush[0]);
	}
};

var erpConnector = "";

com_zimbra_erp_mail_connector_HandlerObject.prototype.init=function(){
	//Read user credentials from database
	this.mail_from = [];
	AjxRpc.__RPC_CACHE_MAX = 200;
	docList=this.getUserProperty("doc_list");
	if(docList.length<=0){
		var doclist="{\"records\":[{\"id\":\"1\",\"title\":\"Partner\",\"docname\":\"res.partner\"},{\"id\":\"2\",\"title\":\"Address\",\"docname\":\"res.partner.address\"},{\"id\":\"3\",\"title\":\"Lead\",\"docname\":\"crm.lead\"}]}";
		this.setUserProperty("doc_list",this.trim(doclist));
		this.saveUserProperties();
	}
	erpConnector = com_zimbra_erp_mail_connector_HandlerObject;
	var jspurl="/service/zimlet/com_zimbra_erp_mail_connector/readConfig.jsp";
	var response = AjxRpc.invoke(null,jspurl, null, null, true);
	var allConfigurations = JSON.parse(response.text);
	erpConnector.urladdress = allConfigurations.urladdress;
	erpConnector.port = allConfigurations.port;
	erpConnector.getdatabase = allConfigurations.getdatabase;
	this.tagcreate();
	this.msgFlag = "";
	this.participants = "";
	AjxPackage.require("vnc.json.json2");
}<commit_msg>Storing zimlet reference in global struct erpConnector<commit_after>/*##############################################################################
#    VNC-Virtual Network Consult GmbH.
#    Copyright (C) 2004-TODAY VNC-Virtual Network Consult GmbH
#    (<http://www.vnc.biz>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################*/
function com_zimbra_erp_mail_connector_HandlerObject() {
}

com_zimbra_erp_mail_connector_HandlerObject.prototype= new ZmZimletBase;

com_zimbra_erp_mail_connector_HandlerObject.constructor = com_zimbra_erp_mail_connector_HandlerObject;

com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID="send_and_push";

com_zimbra_erp_mail_connector_HandlerObject.BUTTON3_ID="push_to_erp";

com_zimbra_erp_mail_connector_HandlerObject.prototype.initializeToolbar = function(app, toolbar, controller,view) {
	var patt="COMPOSE";
	// only add this button for the following 3 views
	if(view.match(patt)=="COMPOSE"){
		if (toolbar.getOp(com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID)) {
			return;
		}
		var btnIndex = -1;
		for ( var i = 0, count = toolbar.opList.length; i < count; i++) {
			if (toolbar.opList[i] == ZmOperation.SEND) {
				btnIndex = i + 1;
				break;
			}
		}
		// create params obj with button details
		var send_and_push=this.getMessage("send_and_push");
		var send_and_push_tooltip=this.getMessage("send_and_push_tooltip");
		var buttonArgs = {
			text :send_and_push,
			tooltip :send_and_push_tooltip,
			index : btnIndex, // position of the button
			image : "NewMessage" // icon
		};
		var button = toolbar.createOp(com_zimbra_erp_mail_connector_HandlerObject.BUTTON2_ID, buttonArgs);
		button.addSelectionListener(new AjxListener(this,this._handle_push_and_save, controller));	
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleCalSyncBtnClick = function(controller) {
	var calendars = appCtxt.getFolderTree().getByType(ZmOrganizer.CALENDAR);
	for(var i in calendars){
		if(calendars[i].url != undefined){
			calendars[i].sync();
		}
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._handle_push_and_save = function(controller) {
	var currController = appCtxt.getCurrentController();
	var respCallback = new AjxCallback(this, this._handleResponsefromsend);
	currController.sendMsg("","",respCallback);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleResponsefromsend = function(result) {
	var resp=result.getResponse();
	var ac = window.parentAppCtxt || window.appCtxt;
	var msgid=resp.m[0].id;
	var list = ac.getApp(ZmApp.MAIL).getMailListController().getList();
	this.msg = new ZmMailMsg(resp.m[0].id, list, true); // do not cache this temp msg
	this.msg._loadFromDom(resp.m[0]);
	this.msg._loaded = true; // bug fix #8868 - force load for rfc822 msgs since they may not return any content
	this.msg.readReceiptRequested = false; // bug #36247 - never allow read receipt for rfc/822 message
	this.msg._part = resp.m[0].part;
	var from=new AjxEmailAddress(appCtxt.getUsername(),AjxEmailAddress.FROM);
	var msgLoadCallback = new AjxCallback(this, this._msgLoaded,[result]);
	this.msg.load({forceLoad:true,callback:msgLoadCallback});
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._msgLoaded = function(result){
	var res=result.getResponse();
	this.mail_from=[];
	var rec=this.msg.getAddress(ZmMailMsg.HDR_TO);
	if(rec.toString().indexOf("<") > -1){
		rec=rec.toString().split("<")[1];
		rec=rec.toString().split(">")[0];
	}
	this.mail_from.push(rec);
	this.msgFlag=1;
	this.doDrop(this.msg);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype._handleSOAPErrorResponseXML = function(result) {
	if (result.isException()) {
		this.alert_critical("mail_push_exception");
		var exception = result.getException();
		return;
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.getFolderDialog=function(){
	var title=this.getMessage("title_Contact_Dialog");
	var dialog = appCtxt.getChooseFolderDialog();
	dialog.registerCallback(DwtDialog.OK_BUTTON, new AjxCallback(this, this._handleChooseFolder));
	var params = {
		overviewId:     dialog.getOverviewId(ZmApp.CONTACTS),
		title:          title,
		treeIds:        [ZmOrganizer.ADDRBOOK],
		skipReadOnly:   true,
		skipRemote:     false,
		noRootSelect:   true,
		appName:        ZmApp.CONTACTS
	};
	params.omit = {};
	params.omit[ZmFolder.ID_TRASH] = true;
	params.omit[addBook_ContactId]=true;
	params.omit[addBook_EcontactId]=true;
	dialog.popup(params);
}

com_zimbra_erp_mail_connector_HandlerObject.prototype.onParticipantActionMenuInitialized = function(controller, menu) {
	this.onActionMenuInitialized(controller, menu);
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.onActionMenuInitialized = function(controller, menu) {
	this.addMenuButton(controller, menu);
};

com_zimbra_erp_mail_connector_HandlerObject.prototype.addMenuButton = function(controller, menu) {
	var ID = com_zimbra_erp_mail_connector_HandlerObject.BUTTON3_ID;
	var btnName=this.getMessage("push_to_btn");
	var btnTooltip=this.getMessage("push_to_btn_tooltip");
	if (!menu.getMenuItem(ID)) {
		var op = {
			id : ID,
			text : btnName,
			tooltip: btnTooltip,
			image : "NewMessage"
		};
		var opDesc = ZmOperation.defineOperation(null, op);
		menu.addOp(ID, 1000);// add the button at the bottom
		menu.addSelectionListener(ID, new AjxListener(this,this._menuButtonListener, controller));
	}
};

com_zimbra_erp_mail_connector_HandlerObject.prototype._menuButtonListener = function(controller) {
	this.mailtopush = "";
	var con=appCtxt.getCurrentController();
	this.mailtopush = controller.getCurrentView().getDnDSelection();
	this.mailtopush = (this.mailtopush instanceof Array) ? this.mailtopush : [this.mailtopush];
	var eml;
	for ( var i = 0; i < this.mailtopush.length; i++) {
		if (this.mailtopush[i].type === ZmId.ITEM_CONV) {
			var arry = this.mailtopush[i].participants.getArray();
			if(arry.length >0) {
				eml = arry[arry.length - 1].address;
			}
		} else if (this.mailtopush[i].type === ZmId.ITEM_MSG) {
			var obj = this.mailtopush[i].getAddress(AjxEmailAddress.FROM);
			if(obj)
			eml = obj.address;
		}
	}
	this.mail_from=[];
	this.mail_from.push(eml);
	if(this.mailtopush != null){
		this.doDrop(this.mailtopush[0]);
	}
};

var erpConnector = "";

com_zimbra_erp_mail_connector_HandlerObject.prototype.init=function(){
	//Read user credentials from database
	this.mail_from = [];
	AjxRpc.__RPC_CACHE_MAX = 200;
	docList=this.getUserProperty("doc_list");
	if(docList.length<=0){
		var doclist="{\"records\":[{\"id\":\"1\",\"title\":\"Partner\",\"docname\":\"res.partner\"},{\"id\":\"2\",\"title\":\"Address\",\"docname\":\"res.partner.address\"},{\"id\":\"3\",\"title\":\"Lead\",\"docname\":\"crm.lead\"}]}";
		this.setUserProperty("doc_list",this.trim(doclist));
		this.saveUserProperties();
	}
	erpConnector = com_zimbra_erp_mail_connector_HandlerObject;
	var jspurl="/service/zimlet/com_zimbra_erp_mail_connector/readConfig.jsp";
	var response = AjxRpc.invoke(null,jspurl, null, null, true);
	var allConfigurations = JSON.parse(response.text);
	erpConnector.urladdress = allConfigurations.urladdress;
	erpConnector.port = allConfigurations.port;
	erpConnector.getdatabase = allConfigurations.getdatabase;
	erpConnector.zimlet = this;
	this.tagcreate();
	this.msgFlag = "";
	this.participants = "";
	AjxPackage.require("vnc.json.json2");
}
{'fraction_non_alphanumeric': 0.14620284786410193, 'fraction_numerical': 0.006620034973769673, 'mean_word_length': 5.562704918032787, 'pattern_counts': {'":': 20, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836435', 'n_tokens_mistral': 5682, 'n_tokens_neox': 5379, 'n_words': 952}