calendarimporter/js/dialogs/ImportPanel.js

676 lines
18 KiB
JavaScript
Raw Normal View History

2013-03-30 14:55:18 +01:00
/**
* ImportPanel.js zarafa calender to ics im/exporter
*
* Author: Christoph Haas <christoph.h@sprinternet.at>
2016-06-20 15:04:53 +02:00
* Copyright (C) 2012-2016 Christoph Haas
2013-03-30 14:55:18 +01:00
*
* 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.
*
* 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 calendarimporter plugin.
*/
Ext.namespace("Zarafa.plugins.calendarimporter.dialogs");
/**
* @class Zarafa.plugins.calendarimporter.dialogs.ImportPanel
2016-06-20 15:04:53 +02:00
* @extends Ext.Panel
*/
Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.Panel, {
/* store the imported timezone here... */
timezone: null,
2013-03-30 14:55:18 +01:00
/* ignore daylight saving time... */
ignoredst: null,
/* path to ics file on server... */
icsfile: null,
/* loadmask for timezone/dst changes... */
loadMask: null,
2013-03-30 14:55:18 +01:00
/* export event buffer */
2016-06-20 15:04:53 +02:00
exportResponse: [],
2013-03-30 14:55:18 +01:00
/* how many requests are still running? */
runningRequests: null,
/* The store for the selection grid */
store: null,
2016-06-20 15:04:53 +02:00
/* selected folder */
folder : null,
/**
* @constructor
* @param {object} config
*/
2013-03-30 14:55:18 +01:00
constructor : function (config) {
config = config || {};
var self = this;
2013-03-30 14:55:18 +01:00
this.timezone = container.getSettingsModel().get("zarafa/v1/plugins/calendarimporter/default_timezone");
2016-06-20 15:04:53 +02:00
if (!Ext.isEmpty(config.filename)) {
2013-03-30 14:55:18 +01:00
this.icsfile = config.filename;
}
2016-06-20 15:04:53 +02:00
if (!Ext.isEmpty(config.folder)) {
this.folder = config.folder;
}
2013-03-30 14:55:18 +01:00
// create the data store
this.store = new Ext.data.ArrayStore({
fields: [
2016-06-20 19:42:07 +02:00
{name: 'subject'},
{name: 'startdate'},
{name: 'enddate'},
2013-03-30 14:55:18 +01:00
{name: 'location'},
2016-06-20 19:42:07 +02:00
{name: 'body'},
2013-03-30 14:55:18 +01:00
{name: 'priority'},
{name: 'label'},
{name: 'busy'},
2016-06-20 19:42:07 +02:00
{name: 'class'},
2013-03-30 14:55:18 +01:00
{name: 'organizer'},
2016-06-20 19:42:07 +02:00
{name: 'alarms'},
{name: 'timezone'},
{name: 'record'}
2013-03-30 14:55:18 +01:00
]
});
Ext.apply(config, {
xtype : 'calendarimporter.importpanel',
2013-03-30 14:55:18 +01:00
ref : "importpanel",
id : "importpanel",
layout : {
type : 'form',
align : 'stretch'
},
2013-03-30 14:55:18 +01: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;'
},
items : [
this.createSelectBox(),
this.createTimezoneBox(),
this.createDaylightSavingCheckBox(),
2013-03-30 14:55:18 +01:00
this.initForm(),
this.createGrid()
],
buttons: [
this.createSubmitAllButton(),
this.createSubmitButton(),
this.createCancelButton()
2013-03-30 14:55:18 +01:00
],
listeners: {
afterrender: function (cmp) {
2016-06-20 15:04:53 +02:00
this.loadMask = new Ext.LoadMask(this.getEl(), {msg:'Loading...'});
2013-03-30 14:55:18 +01:00
if(this.icsfile != null) { // if we have got the filename from an attachment
this.parseCalendar(this.icsfile, this.timezone, this.ignoredst);
}
},
2016-06-20 15:04:53 +02:00
scope: this
2013-03-30 14:55:18 +01:00
}
});
2013-03-30 14:55:18 +01:00
Zarafa.plugins.calendarimporter.dialogs.ImportPanel.superclass.constructor.call(this, config);
},
/**
* Init embedded form, this is the form that is
* posted and contains the attachments
* @private
*/
2013-03-30 14:55:18 +01:00
initForm : function () {
return {
xtype: 'form',
ref: 'addFormPanel',
layout : 'column',
fileUpload: true,
autoWidth: true,
autoHeight: true,
border: false,
bodyStyle: 'padding: 5px;',
defaults: {
anchor: '95%',
border: false,
bodyStyle: 'padding: 5px;'
},
items: [this.createUploadField()]
};
},
2016-06-20 15:04:53 +02:00
/**
* Get all calendar folders.
* @param {boolean} asDropdownStore If true, a simple array store will be returned.
* @returns {*}
*/
getAllCalendarFolders: function (asDropdownStore) {
asDropdownStore = Ext.isEmpty(asDropdownStore) ? false : asDropdownStore;
var allFolders = [];
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.Appointment") {
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.Appointment") {
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;
}
},
/**
* Dynamic sort function, sorts by property name.
* @param {string|int} property
* @returns {Function}
*/
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;
}
},
/**
* Return a calendar folder element by name.
* @param {string} name
* @returns {*}
*/
getCalendarFolderByName: function (name) {
var folders = this.getAllCalendarFolders(false);
for (var i = 0; i < folders.length; i++) {
if (folders[i].display_name == name) {
return folders[i];
}
}
return container.getHierarchyStore().getDefaultFolder('calendar');
},
/**
* Return a calendar folder element by entryid.
* @param {string} entryid
* @returns {*}
*/
getCalendarFolderByEntryid: function (entryid) {
var folders = this.getAllCalendarFolders(false);
for (var i = 0; i < folders.length; i++) {
if (folders[i].entryid == entryid) {
return folders[i];
}
}
return container.getHierarchyStore().getDefaultFolder('calendar');
},
/**
2013-03-30 14:55:18 +01:00
* Reloads the data of the grid
* @private
*/
2013-03-30 14:55:18 +01:00
reloadGridStore: function(eventdata) {
var parsedData = [];
if(eventdata !== null) {
parsedData = new Array(eventdata.events.length);
var i = 0;
for(i = 0; i < eventdata.events.length; i++) {
2016-06-20 19:42:07 +02:00
parsedData[i] = [
eventdata.events[i]["subject"],
2016-06-20 20:00:44 +02:00
new Date(parseInt(eventdata.events[i]["startdate"]) * 1000),
2016-06-25 15:17:19 +02:00
new Date(parseInt(eventdata.events[i]["duedate"]) * 1000),
2016-06-20 19:42:07 +02:00
eventdata.events[i]["location"],
eventdata.events[i]["body"],
eventdata.events[i]["priority"],
eventdata.events[i]["label"],
2016-06-25 15:17:19 +02:00
eventdata.events[i]["busystatus"],
eventdata.events[i]["private"],
2016-06-20 19:42:07 +02:00
eventdata.events[i]["organizer"],
eventdata.events[i]["alarms"],
eventdata.events[i]["timezone"],
eventdata.events[i]
];
}
} else {
return null;
}
2013-03-30 14:55:18 +01:00
this.store.loadData(parsedData, false);
},
/**
* Init embedded form, this is the form that is
* posted and contains the attachments
* @private
*/
createGrid : function() {
return {
xtype: 'grid',
ref: 'eventgrid',
id: 'eventgrid',
columnWidth: 1.0,
2013-03-30 14:55:18 +01:00
store: this.store,
width: '100%',
height: 300,
title: 'Select events to import',
2013-03-30 14:55:18 +01:00
frame: false,
viewConfig:{
forceFit:true
},
colModel: new Ext.grid.ColumnModel({
defaults: {
width: 300,
sortable: true
},
columns: [
2016-06-20 19:42:07 +02:00
{id: 'Summary', header: 'Title', width: 200, sortable: true, dataIndex: 'subject'},
{header: 'Start', width: 200, sortable: true, dataIndex: 'startdate', renderer : Zarafa.common.ui.grid.Renderers.datetime},
{header: 'End', width: 200, sortable: true, dataIndex: 'enddate', renderer : Zarafa.common.ui.grid.Renderers.datetime},
{header: 'Location', width: 150, sortable: true, dataIndex: 'location'},
2016-06-20 19:42:07 +02:00
{header: 'Description', sortable: true, dataIndex: 'body'},
{header: "Priority", dataIndex: 'priority', hidden: true},
{header: "Label", dataIndex: 'label', hidden: true},
{header: "Busystatus", dataIndex: 'busy', hidden: true},
2016-06-20 19:42:07 +02:00
{header: "Privacystatus", dataIndex: 'class', hidden: true},
{header: "Organizer", dataIndex: 'organizer', hidden: true},
2016-06-20 19:42:07 +02:00
{header: "Alarm", dataIndex: 'alarms', hidden: true, renderer : Zarafa.common.ui.grid.Renderers.datetime},
{header: "Timezone", dataIndex: 'timezone', hidden: true}
]
}),
sm: new Ext.grid.RowSelectionModel({multiSelect:true})
}
},
createSelectBox: function() {
2016-06-20 15:04:53 +02:00
var myStore = this.getAllCalendarFolders(true);
return {
xtype: "selectbox",
ref: 'calendarselector',
editable: false,
name: "choosen_calendar",
2016-06-20 15:04:53 +02:00
value: Ext.isEmpty(this.folder) ? this.getCalendarFolderByName(container.getSettingsModel().get("zarafa/v1/plugins/calendarimporter/default_calendar")).entryid : this.folder,
width: 100,
2016-06-20 15:04:53 +02:00
fieldLabel: "Select folder",
store: myStore,
mode: 'local',
labelSeperator: ":",
border: false,
anchor: "100%",
scope: this,
2016-06-20 15:04:53 +02:00
hidden : Ext.isEmpty(this.folder) ? false : true,
allowBlank: false
}
},
createTimezoneBox: function() {
return {
xtype: "selectbox",
ref: 'timezoneselector',
editable: false,
name: "choosen_timezone",
2013-03-30 14:55:18 +01:00
value: Zarafa.plugins.calendarimporter.data.Timezones.unMap(container.getSettingsModel().get("zarafa/v1/plugins/calendarimporter/default_timezone")),
width: 100,
2016-06-20 15:48:24 +02:00
fieldLabel: "Timezone",
store: Zarafa.plugins.calendarimporter.data.Timezones.store,
labelSeperator: ":",
mode: 'local',
border: false,
anchor: "100%",
scope: this,
allowBlank: true,
listeners: {
2016-06-20 15:04:53 +02:00
select: this.onTimezoneSelected,
scope: this
}
}
},
createDaylightSavingCheckBox: function() {
return {
xtype: "checkbox",
ref: 'dstcheck',
name: "dst_check",
width: 100,
2016-06-20 15:48:24 +02:00
fieldLabel: "Ignore DST",
2013-03-30 14:55:18 +01:00
boxLabel: 'This will ignore "Daylight saving time" offsets.',
labelSeperator: ":",
border: false,
anchor: "100%",
scope: this,
allowBlank: true,
listeners: {
2016-06-20 15:04:53 +02:00
check: this.onDstChecked,
scope: this
}
}
},
createUploadField: function() {
return {
xtype: "fileuploadfield",
ref: 'fileuploadfield',
columnWidth: 1.0,
id: 'form-file',
name: 'icsdata',
emptyText: 'Select an .ics calendar',
border: false,
anchor: "100%",
2016-06-20 15:04:53 +02:00
height : "30",
scope: this,
allowBlank: false,
listeners: {
2016-06-20 15:04:53 +02:00
fileselected: this.onFileSelected,
scope: this
}
}
},
createSubmitButton: function() {
return {
xtype: "button",
2016-06-20 19:42:07 +02:00
ref: "../submitButton",
disabled: true,
width: 100,
border: false,
text: _("Import"),
anchor: "100%",
handler: this.importCheckedEvents,
2016-06-20 15:04:53 +02:00
scope: this
}
},
createSubmitAllButton: function() {
return {
xtype: "button",
2016-06-20 19:42:07 +02:00
ref: "../submitAllButton",
disabled: true,
width: 100,
border: false,
text: _("Import All"),
anchor: "100%",
handler: this.importAllEvents,
2016-06-20 15:04:53 +02:00
scope: this
}
},
createCancelButton: function() {
return {
xtype: "button",
width: 100,
border: false,
text: _("Cancel"),
anchor: "100%",
handler: this.close,
2016-06-20 15:04:53 +02:00
scope: this
}
},
/**
* This is called when a timezone has been seleceted in the timezone dialog
* @param {Ext.form.ComboBox} combo
* @param {Ext.data.Record} record
* @param {Number} index
*/
onTimezoneSelected : function(combo, record, index) {
this.timezone = record.data.field1;
2013-03-30 14:55:18 +01:00
if(this.icsfile != null) {
this.parseCalendar(this.icsfile, this.timezone, this.ignoredst);
}
},
/**
* This is called when the dst checkbox has been selected
2016-06-20 15:04:53 +02:00
* @param {Ext.form.CheckBox} checkbox
2013-03-30 14:55:18 +01:00
* @param {boolean} checked
*/
onDstChecked : function(checkbox, checked) {
2013-03-30 14:55:18 +01:00
this.ignoredst = checked;
if(this.icsfile != null) {
this.parseCalendar(this.icsfile, this.timezone, this.ignoredst);
}
},
/**
* 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
*/
2013-03-30 14:55:18 +01:00
onFileSelected : function(uploadField) {
var form = this.addFormPanel.getForm();
if (form.isValid()) {
form.submit({
waitMsg: 'Uploading and parsing calendar...',
url: 'plugins/calendarimporter/php/upload.php',
failure: function(file, action) {
2016-06-20 18:02:57 +02:00
this.submitButton.disable();
this.submitAllButton.disable();
Zarafa.common.dialogs.MessageBox.show({
title : _('Error'),
2013-03-30 14:55:18 +01:00
msg : _(action.result.error),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons : Zarafa.common.dialogs.MessageBox.OK
});
},
success: function(file, action){
uploadField.reset();
2013-03-30 14:55:18 +01:00
this.icsfile = action.result.ics_file;
2013-03-30 14:55:18 +01:00
this.parseCalendar(this.icsfile, this.timezone, this.ignoredst);
},
scope : this
});
}
},
2013-03-30 14:55:18 +01:00
parseCalendar: function (icsPath, timezone, ignoredst) {
this.loadMask.show();
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
var responseHandler = new Zarafa.plugins.calendarimporter.data.ResponseHandler({
2016-06-20 15:48:24 +02:00
successCallback: this.handleParsingResult,
scope: this
2013-03-30 14:55:18 +01:00
});
container.getRequest().singleRequest(
'calendarmodule',
2016-06-20 15:48:24 +02:00
'load',
2013-03-30 14:55:18 +01:00
{
ics_filepath: icsPath,
timezone: timezone,
ignore_dst: ignoredst
},
responseHandler
);
},
handleParsingResult: function(response) {
2016-06-20 19:42:07 +02:00
var self = this.scope;
self.loadMask.hide();
2013-03-30 14:55:18 +01:00
if(response["status"] == true) {
2016-06-20 19:42:07 +02:00
self.submitButton.enable();
self.submitAllButton.enable();
2016-06-20 15:48:24 +02:00
if(typeof response.parsed.calendar["X-WR-TIMEZONE"] !== "undefined") {
2016-06-20 19:42:07 +02:00
self.timezone = response.parsed.calendar["X-WR-TIMEZONE"];
self.timezoneselector.setValue(Zarafa.plugins.calendarimporter.data.Timezones.unMap(this.timezone));
}
2016-06-20 19:42:07 +02:00
self.reloadGridStore(response.parsed);
2013-03-30 14:55:18 +01:00
} else {
2016-06-20 19:42:07 +02:00
self.submitButton.disable();
self.submitAllButton.disable();
2013-03-30 14:55:18 +01:00
Zarafa.common.dialogs.MessageBox.show({
title : _('Parser Error'),
msg : _(response["message"]),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons : Zarafa.common.dialogs.MessageBox.OK
});
}
},
close: function () {
this.addFormPanel.getForm().reset();
this.dialog.close()
},
importCheckedEvents: function () {
var newRecords = this.eventgrid.selModel.getSelections();
this.importEvents(newRecords);
},
importAllEvents: function () {
//receive Records from grid rows
this.eventgrid.selModel.selectAll(); // select all entries
var newRecords = this.eventgrid.selModel.getSelections();
this.importEvents(newRecords);
},
2016-06-20 15:48:24 +02:00
/**
* This function stores all given events to the appointmentstore
* @param events
*/
importEvents: function (events) {
//receive existing calendar store
2016-06-20 15:48:24 +02:00
var calValue = this.calendarselector.getValue();
if(Ext.isEmpty(calValue)) { // no calendar choosen
Zarafa.common.dialogs.MessageBox.show({
title : _('Error'),
msg : _('You have to choose a calendar!'),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons : Zarafa.common.dialogs.MessageBox.OK
});
} else {
2016-06-20 15:48:24 +02:00
if(this.eventgrid.selModel.getCount() < 1) {
Zarafa.common.dialogs.MessageBox.show({
title : _('Error'),
2016-06-20 15:48:24 +02:00
msg : _('You have to choose at least one event to import!'),
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons : Zarafa.common.dialogs.MessageBox.OK
});
2016-06-20 15:48:24 +02:00
} else {
var calendarFolder = this.getCalendarFolderByEntryid(calValue);
2013-03-30 14:55:18 +01:00
2016-06-20 15:48:24 +02:00
this.loadMask.show();
var uids = [];
//receive Records from grid rows
Ext.each(events, function(newRecord) {
uids.push(newRecord.data.record.internal_fields.event_uid);
}, this);
var responseHandler = new Zarafa.plugins.calendarimporter.data.ResponseHandler({
2016-06-20 15:48:24 +02:00
successCallback: this.importEventsDone,
scope: this
2013-03-30 14:55:18 +01:00
});
2016-06-20 15:48:24 +02:00
2013-03-30 14:55:18 +01:00
container.getRequest().singleRequest(
'calendarmodule',
2016-06-20 15:48:24 +02:00
'import',
2013-03-30 14:55:18 +01:00
{
storeid : calendarFolder.store_entryid,
folderid : calendarFolder.entryid,
2016-06-20 15:48:24 +02:00
uids : uids,
ics_filepath: this.icsfile
2013-03-30 14:55:18 +01:00
},
responseHandler
);
}
2016-06-20 15:48:24 +02:00
}
2013-03-30 14:55:18 +01:00
},
2016-06-20 15:48:24 +02:00
/**
2016-06-20 15:48:24 +02:00
* Callback for the import request.
* @param {Object} response
*/
2016-06-20 15:48:24 +02:00
importEventsDone: function (response) {
var self = this.scope;
self.loadMask.hide();
self.dialog.close();
2016-06-20 15:48:24 +02:00
if (response.status == true) {
container.getNotifier().notify('info', 'Imported', 'Imported ' + response.count + ' events. Please reload your calendar!');
} else {
Zarafa.common.dialogs.MessageBox.show({
2016-06-20 15:48:24 +02:00
title : _('Error'),
msg : _('Import failed: ') + response.message,
icon : Zarafa.common.dialogs.MessageBox.ERROR,
buttons: Zarafa.common.dialogs.MessageBox.OK
});
}
2016-06-20 15:48:24 +02:00
}
});
Ext.reg('calendarimporter.importpanel', Zarafa.plugins.calendarimporter.dialogs.ImportPanel);