contactimporter/js/dialogs/ImportPanel.js

595 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
2016-06-19 00:16:25 +02:00
* @extends Ext.Panel
*/
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()]
};
},
2016-06-19 00:16:25 +02:00
/**
* Get all contact folders.
* @param {boolean} asDropdownStore If true, a simple array store will be returned.
* @returns {*}
*/
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();
2016-06-19 00:16:25 +02:00
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);
2016-06-19 00:16:25 +02:00
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
});
}
}
}
}
2016-06-19 00:16:25 +02:00
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);
2016-06-19 00:16:25 +02:00
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
});
}
}
}
}
2016-06-19 00:16:25 +02:00
if (asDropdownStore) {
return allFolders.sort(this.dynamicSort(1));
} else {
return allFolders;
}
},
2016-06-19 00:16:25 +02:00
/**
* Dynamic sort function, sorts by property name.
* @param {string|int} property
* @returns {Function}
*/
dynamicSort: function (property) {
var sortOrder = 1;
2016-06-19 00:16:25 +02:00
if (property[0] === "-") {
sortOrder = -1;
property = property.substr(1);
}
2016-06-19 00:16:25 +02:00
return function (a, b) {
var result = (a[property].toLowerCase() < b[property].toLowerCase()) ? -1 : (a[property].toLowerCase() > b[property].toLowerCase()) ? 1 : 0;
return result * sortOrder;
}
},
2016-06-19 00:16:25 +02:00
/**
* Return a contact folder element by name.
* @param {string} name
* @returns {*}
*/
getContactFolderByName: function (name) {
var folders = this.getAllContactFolders(false);
2016-06-19 00:16:25 +02:00
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
},
2016-06-19 00:22:30 +02:00
/**
* Return a contact folder element by entryid.
* @param {string} entryid
* @returns {*}
*/
getContactFolderByEntryid: function (entryid) {
var folders = this.getAllContactFolders(false);
for (var i = 0; i < folders.length; i++) {
if (folders[i].entryid == entryid) {
return folders[i];
}
}
return container.getHierarchyStore().getDefaultFolder('contact');
},
/**
* 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
2016-06-19 00:16:25 +02:00
/**
* Generate the UI calendar select box.
* @returns {*}
*/
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
2016-06-19 00:16:25 +02:00
/**
* Generate the UI upload field.
* @returns {*}
*/
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
2016-06-19 00:16:25 +02:00
/**
* Generate the UI submit button.
* @returns {*}
*/
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
2016-06-19 00:16:25 +02:00
/**
* Generate the UI submit all button.
* @returns {*}
*/
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
2016-06-19 00:16:25 +02:00
/**
* Generate the UI cancel button.
* @returns {*}
*/
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
2016-06-19 00:16:25 +02:00
/**
* Start request to server to parse the given vCard file.
* @param {string} vcfPath
*/
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
2016-06-19 00:16:25 +02:00
/**
* Callback for the parsing request.
* @param {Object} response
*/
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
});
}
},
2016-06-19 00:16:25 +02:00
/**
* Close the UI dialog.
*/
close: function () {
this.addContactFormPanel.getForm().reset();
this.dialog.close()
},
2016-06-19 00:16:25 +02:00
/**
* Create a request to import all selected contacts.
*/
importCheckedContacts: function () {
var newRecords = this.contactGrid.selModel.getSelections();
this.importContacts(newRecords);
2016-06-13 22:59:05 +02:00
},
2016-06-19 00:16:25 +02:00
/**
* Check all contacts and import them.
*/
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
},
/**
2016-06-19 00:16:25 +02:00
* This function stores all given events to the contact store
* @param {array} contacts
*/
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 {
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 {
2016-06-19 00:22:30 +02:00
var contactFolder = this.getContactFolderByEntryid(folderValue);
2016-06-13 22:59:05 +02:00
2016-06-19 00:22:30 +02:00
this.loadMask.show();
var uids = [];
2016-06-19 00:22:30 +02:00
//receive Records from grid rows
Ext.each(contacts, function (newRecord) {
uids.push(newRecord.data.record.internal_fields.contact_uid);
}, this);
2016-06-13 22:59:05 +02:00
2016-06-19 00:22:30 +02:00
var responseHandler = new Zarafa.plugins.contactimporter.data.ResponseHandler({
successCallback: this.importContactsDone.createDelegate(this)
});
2016-06-13 22:59:05 +02:00
2016-06-19 00:22:30 +02:00
container.getRequest().singleRequest(
'contactmodule',
'import',
{
storeid : contactFolder.store_entryid,
folderid : contactFolder.entryid,
uids : uids,
vcf_filepath: this.vcffile
},
responseHandler
);
}
}
},
2016-06-13 22:59:05 +02:00
2016-06-19 00:16:25 +02:00
/**
* Callback for the import request.
* @param {Object} response
*/
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);