contactimporter/js/dialogs/ImportPanel.js

560 lines
15 KiB
JavaScript
Raw Normal View History

/**
* ImportPanel.js zarafa contact to vcf im/exporter
*
* Author: Christoph Haas <christoph.h@sprinternet.at>
2016-06-13 22:59:05 +02:00
* Copyright (C) 2012-2016 Christoph Haas
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
2016-06-13 22:59:05 +02:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/**
* ImportPanel
*
* The main Panel of the contactimporter plugin.
*/
2016-06-13 22:59:05 +02:00
Ext.namespace("Zarafa.plugins.contactimporter.dialogs");
/**
* @class Zarafa.plugins.contactimporter.dialogs.ImportPanel
* @extends Ext.form.FormPanel
*/
Zarafa.plugins.contactimporter.dialogs.ImportPanel = Ext.extend(Ext.Panel, {
/* path to vcf file on server... */
vcffile: null,
2016-06-13 22:59:05 +02:00
/* The store for the selection grid */
2016-06-13 22:59:05 +02:00
store : null,
2016-06-14 00:57:00 +02:00
/* selected folder */
folder : null,
/**
* @constructor
* @param {object} config
*/
2016-06-13 22:59:05 +02:00
constructor: function (config) {
config = config || {};
var self = this;
2016-06-13 22:59:05 +02:00
if (!Ext.isEmpty(config.filename)) {
this.vcffile = config.filename;
}
2016-06-13 22:59:05 +02:00
2016-06-14 00:57:00 +02:00
if (!Ext.isEmpty(config.folder)) {
this.folder = config.folder;
}
// create the data store
// we only display the firstname, lastname, homephone and primary email address in our grid
this.store = new Ext.data.ArrayStore({
fields: [
{name: 'display_name'},
{name: 'given_name'},
{name: 'surname'},
{name: 'company_name'},
{name: 'record'}
]
});
2016-06-13 22:59:05 +02:00
Ext.apply(config, {
2016-06-13 22:59:05 +02:00
xtype : 'contactimporter.importpanel',
ref : "importcontactpanel",
layout : {
type : 'form',
align: 'stretch'
},
2016-06-13 22:59:05 +02:00
anchor : '100%',
bodyStyle: 'background-color: inherit;',
defaults : {
border : true,
bodyStyle: 'background-color: inherit; padding: 3px 0px 3px 0px; border-style: none none solid none;'
},
2016-06-13 22:59:05 +02:00
items : [
this.createSelectBox(),
this.initForm(),
this.createGrid()
],
2016-06-13 22:59:05 +02:00
buttons : [
this.createSubmitAllButton(),
this.createSubmitButton(),
this.createCancelButton()
2016-06-13 22:59:05 +02:00
],
listeners: {
afterrender: function (cmp) {
2016-06-13 22:59:05 +02:00
this.loadMask = new Ext.LoadMask(this.getEl(), {msg: 'Loading...'});
if (this.vcffile != null) { // if we have got the filename from an attachment
this.parseContacts(this.vcffile);
}
},
2016-06-13 22:59:05 +02:00
scope : this
}
});
2016-06-13 22:59:05 +02:00
Zarafa.plugins.contactimporter.dialogs.ImportPanel.superclass.constructor.call(this, config);
},
/**
* Init embedded form, this is the form that is
* posted and contains the attachments
* @private
*/
2016-06-13 22:59:05 +02:00
initForm: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : 'form',
ref : 'addContactFormPanel',
layout : 'column',
fileUpload: true,
2016-06-13 22:59:05 +02:00
autoWidth : true,
autoHeight: true,
2016-06-13 22:59:05 +02:00
border : false,
bodyStyle : 'padding: 5px;',
defaults : {
anchor : '95%',
border : false,
bodyStyle: 'padding: 5px;'
},
2016-06-13 22:59:05 +02:00
items : [this.createUploadField()]
};
},
getAllContactFolders: function(asDropdownStore) {
asDropdownStore = Ext.isEmpty(asDropdownStore) ? false : asDropdownStore;
2016-06-14 01:18:00 +02:00
var allFolders = [];
var defaultContactFolder = container.getHierarchyStore().getDefaultFolder('contact');
var inbox = container.getHierarchyStore().getDefaultStore();
var pub = container.getHierarchyStore().getPublicStore();
if(!Ext.isEmpty(inbox.subStores) && inbox.subStores.folders.totalLength > 0) {
for(var i=0; i < inbox.subStores.folders.totalLength; i++) {
var folder = inbox.subStores.folders.getAt(i);
if(folder.get("container_class") == "IPF.Contact") {
if(asDropdownStore) {
allFolders.push([
folder.get("entryid"),
folder.get("display_name")
]);
} else {
allFolders.push({
display_name : folder.get("display_name"),
entryid : folder.get("entryid"),
store_entryid: folder.get("store_entryid"),
is_public : false
});
}
}
}
}
if(!Ext.isEmpty(pub.subStores) && pub.subStores.folders.totalLength > 0) {
for(var j=0; j < pub.subStores.folders.totalLength; j++) {
var folder = pub.subStores.folders.getAt(j);
if(folder.get("container_class") == "IPF.Contact") {
if(asDropdownStore) {
allFolders.push([
folder.get("entryid"),
folder.get("display_name") + " (Public)"
]);
} else {
allFolders.push({
display_name : folder.get("display_name"),
entryid : folder.get("entryid"),
store_entryid: folder.get("store_entryid"),
is_public : true
});
}
}
}
}
if(asDropdownStore) {
return allFolders.sort(this.dynamicSort(1));
} else {
return allFolders;
}
},
dynamicSort: function(property) {
var sortOrder = 1;
if(property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
return function (a,b) {
var result = (a[property].toLowerCase() < b[property].toLowerCase()) ? -1 : (a[property].toLowerCase() > b[property].toLowerCase()) ? 1 : 0;
return result * sortOrder;
}
},
getContactFolderByName: function(name) {
var folders = this.getAllContactFolders(false);
for(var i=0; i<folders.length; i++) {
if(folders[i].display_name == name) {
return folders[i];
}
}
return container.getHierarchyStore().getDefaultFolder('contact');
2016-06-14 01:18:00 +02:00
},
/**
* Reloads the data of the grid
* @private
*/
2016-06-13 22:59:05 +02:00
reloadGridStore: function (contactdata) {
var parsedData = [];
2016-06-13 22:59:05 +02:00
if (contactdata) {
parsedData = new Array(contactdata.contacts.length);
var i = 0;
2016-06-13 22:59:05 +02:00
for (i = 0; i < contactdata.contacts.length; i++) {
2016-06-13 10:18:47 +02:00
parsedData[i] = [
contactdata.contacts[i]["display_name"],
contactdata.contacts[i]["given_name"],
contactdata.contacts[i]["surname"],
contactdata.contacts[i]["company_name"],
contactdata.contacts[i]
2016-06-13 10:18:47 +02:00
];
}
} else {
return null;
}
this.store.loadData(parsedData, false);
},
2016-06-13 22:59:05 +02:00
/**
* Init embedded form, this is the form that is
* posted and contains the attachments
* @private
*/
2016-06-13 22:59:05 +02:00
createGrid: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : 'grid',
ref : 'contactGrid',
columnWidth: 1.0,
2016-06-13 22:59:05 +02:00
store : this.store,
width : '100%',
height : 300,
title : 'Select contacts to import',
frame : false,
viewConfig : {
forceFit: true
},
2016-06-13 22:59:05 +02:00
colModel : new Ext.grid.ColumnModel({
defaults: {
2016-06-13 22:59:05 +02:00
width : 300,
sortable: true
},
2016-06-13 22:59:05 +02:00
columns : [
{id: 'Displayname', header: 'Displayname', width: 350, sortable: true, dataIndex: 'display_name'},
{header: 'Firstname', width: 200, sortable: true, dataIndex: 'given_name'},
{header: 'Lastname', width: 200, sortable: true, dataIndex: 'surname'},
{header: 'Company', sortable: true, dataIndex: 'company_name'}
]
}),
2016-06-13 22:59:05 +02:00
sm : new Ext.grid.RowSelectionModel({multiSelect: true})
}
},
2016-06-13 22:59:05 +02:00
createSelectBox: function () {
var myStore = this.getAllContactFolders(true);
2016-06-13 22:59:05 +02:00
return {
2016-06-13 22:59:05 +02:00
xtype : "selectbox",
ref : 'addressbookSelector',
editable : false,
name : "choosen_addressbook",
value : Ext.isEmpty(this.folder) ? this.getContactFolderByName(container.getSettingsModel().get("zarafa/v1/plugins/contactimporter/default_addressbook")).entryid : this.folder,
2016-06-13 22:59:05 +02:00
width : 100,
2016-06-14 00:57:00 +02:00
fieldLabel : "Select folder",
2016-06-13 22:59:05 +02:00
store : myStore,
mode : 'local',
labelSeperator: ":",
2016-06-13 22:59:05 +02:00
border : false,
anchor : "100%",
scope : this,
hidden : Ext.isEmpty(this.folder) ? false : true,
2016-06-13 22:59:05 +02:00
allowBlank : false
}
},
2016-06-13 22:59:05 +02:00
createUploadField: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : "fileuploadfield",
ref : 'contactfileuploadfield',
columnWidth: 1.0,
2016-06-13 22:59:05 +02:00
id : 'form-file',
name : 'vcfdata',
emptyText : 'Select an .vcf addressbook',
border : false,
anchor : "100%",
2016-06-14 00:57:00 +02:00
height : "30",
2016-06-13 22:59:05 +02:00
scope : this,
allowBlank : false,
listeners : {
'fileselected': this.onFileSelected,
2016-06-13 22:59:05 +02:00
scope : this
}
}
},
2016-06-13 22:59:05 +02:00
createSubmitButton: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : "button",
ref : "../submitButton",
disabled : true,
width : 100,
border : false,
text : _("Import"),
anchor : "100%",
handler : this.importCheckedContacts,
scope : this,
allowBlank: false
}
},
2016-06-13 22:59:05 +02:00
createSubmitAllButton: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : "button",
ref : "../submitAllButton",
disabled : true,
width : 100,
border : false,
text : _("Import All"),
anchor : "100%",
handler : this.importAllContacts,
scope : this,
allowBlank: false
}
},
2016-06-13 22:59:05 +02:00
createCancelButton: function () {
return {
2016-06-13 22:59:05 +02:00
xtype : "button",
width : 100,
border : false,
text : _("Cancel"),
anchor : "100%",
handler : this.close,
scope : this,
allowBlank: false
}
},
2016-06-13 22:59:05 +02:00
/**
* This is called when a file has been seleceted in the file dialog
* in the {@link Ext.ux.form.FileUploadField} and the dialog is closed
* @param {Ext.ux.form.FileUploadField} uploadField being added a file to
*/
2016-06-13 22:59:05 +02:00
onFileSelected: function (uploadField) {
var form = this.addContactFormPanel.getForm();
if (form.isValid()) {
form.submit({
waitMsg: 'Uploading and parsing contacts...',
2016-06-13 22:59:05 +02:00
url : 'plugins/contactimporter/php/upload.php',
failure: function (file, action) {
this.submitButton.disable();
this.submitAllButton.disable();
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _(action.result.error),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
},
2016-06-13 22:59:05 +02:00
success: function (file, action) {
uploadField.reset();
this.vcffile = action.result.vcf_file;
2016-06-13 22:59:05 +02:00
this.parseContacts(this.vcffile);
},
2016-06-13 22:59:05 +02:00
scope : this
});
}
},
2016-06-13 22:59:05 +02:00
parseContacts: function (vcfPath) {
this.loadMask.show();
2016-06-13 22:59:05 +02:00
// call export function here!
var responseHandler = new Zarafa.plugins.contactimporter.data.ResponseHandler({
successCallback: this.handleParsingResult.createDelegate(this)
});
2016-06-13 22:59:05 +02:00
container.getRequest().singleRequest(
'contactmodule',
'load',
{
vcf_filepath: vcfPath
},
responseHandler
);
},
2016-06-13 22:59:05 +02:00
handleParsingResult: function (response) {
this.loadMask.hide();
2016-06-13 22:59:05 +02:00
if (response["status"] == true) {
this.submitButton.enable();
this.submitAllButton.enable();
2016-06-13 22:59:05 +02:00
this.reloadGridStore(response.parsed);
} else {
this.submitButton.disable();
this.submitAllButton.disable();
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Parser Error'),
msg : _(response["message"]),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
}
},
close: function () {
this.addContactFormPanel.getForm().reset();
this.dialog.close()
},
importCheckedContacts: function () {
var newRecords = this.contactGrid.selModel.getSelections();
this.importContacts(newRecords);
2016-06-13 22:59:05 +02:00
},
importAllContacts: function () {
//receive Records from grid rows
this.contactGrid.selModel.selectAll(); // select all entries
var newRecords = this.contactGrid.selModel.getSelections();
this.importContacts(newRecords);
2016-06-13 22:59:05 +02:00
},
/**
* This function stores all given events to the appointmentstore
* @param events
*/
importContacts: function (contacts) {
//receive existing contact store
var folderValue = this.addressbookSelector.getValue();
2016-06-13 22:59:05 +02:00
if (folderValue == undefined) { // no addressbook choosen
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _('You have to choose an addressbook!'),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
} else {
var addressbookexist = true;
2016-06-13 22:59:05 +02:00
if (this.contactGrid.selModel.getCount() < 1) {
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _('You have to choose at least one contact to import!'),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
} else {
var contactStore = new Zarafa.contact.ContactStore();
2016-06-13 22:59:05 +02:00
var contactFolder = container.getHierarchyStore().getDefaultFolder('contact');
var pubStore = container.getHierarchyStore().getPublicStore();
var pubFolder = pubStore.getDefaultFolder("publicfolders");
var pubSubFolders = pubFolder.getChildren();
2016-06-13 22:59:05 +02:00
if (folderValue != "contact") {
var subFolders = contactFolder.getChildren();
var i = 0;
2016-06-13 22:59:05 +02:00
for (i = 0; i < pubSubFolders.length; i++) {
if (pubSubFolders[i].isContainerClass("IPF.Contact")) {
subFolders.push(pubSubFolders[i]);
}
}
2016-06-13 22:59:05 +02:00
for (i = 0; i < subFolders.length; i++) {
// look up right folder
// TODO: improve!!
2016-06-13 22:59:05 +02:00
if (subFolders[i].getDisplayName() == folderValue) {
contactFolder = subFolders[i];
break;
}
}
2016-06-13 22:59:05 +02:00
if (contactFolder.isDefaultFolder()) {
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _('Selected addressbook does not exist!'),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
addressbookexist = false;
}
}
2016-06-13 22:59:05 +02:00
if (addressbookexist) {
this.loadMask.show();
2016-06-13 10:18:47 +02:00
var uids = [];
var store_entryid = "";
2016-06-13 22:59:05 +02:00
//receive Records from grid rows
2016-06-13 22:59:05 +02:00
Ext.each(contacts, function (newRecord) {
uids.push(newRecord.data.record.internal_fields.contact_uid);
}, this);
store_entryid = contactFolder.get('store_entryid');
2016-06-13 22:59:05 +02:00
var responseHandler = new Zarafa.plugins.contactimporter.data.ResponseHandler({
successCallback: this.importContactsDone.createDelegate(this)
});
2016-06-13 22:59:05 +02:00
container.getRequest().singleRequest(
'contactmodule',
'import',
{
2016-06-13 22:59:05 +02:00
storeid : contactFolder.get("store_entryid"),
folderid : contactFolder.get("entryid"),
uids : uids,
vcf_filepath: this.vcffile
},
responseHandler
);
2016-06-13 22:59:05 +02:00
}
}
}
},
2016-06-13 22:59:05 +02:00
importContactsDone: function (response) {
this.loadMask.hide();
this.dialog.close();
2016-06-13 22:59:05 +02:00
if (response.status == true) {
container.getNotifier().notify('info', 'Imported', 'Imported ' + response.count + ' contacts. Please reload your addressbook!');
} else {
Zarafa.common.dialogs.MessageBox.show({
2016-06-13 22:59:05 +02:00
title : _('Error'),
msg : _('Import failed: ') + response.message,
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
}
}
});
Ext.reg('contactimporter.importcontactpanel', Zarafa.plugins.contactimporter.dialogs.ImportPanel);