calendar choosing, timezone fix, import all

this is version 1.0 =)
This commit is contained in:
Christoph Haas 2012-11-17 11:42:48 +00:00
parent af3fcfb457
commit e64d8415bc
4 changed files with 466 additions and 442 deletions

View File

@ -34,6 +34,7 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
this.initForm() this.initForm()
], ],
buttons: [ buttons: [
this.createSubmitAllButton(),
this.createSubmitButton(), this.createSubmitButton(),
this.createCancelButton() this.createCancelButton()
] ]
@ -81,7 +82,7 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
var parsedData = new Array(eventdata.events.length); var parsedData = new Array(eventdata.events.length);
for(var i=0; i < eventdata.events.length; i++) { for(var i=0; i < eventdata.events.length; i++) {
parsedData[i] = new Array(eventdata.events[i]["SUMMARY"], parseInt(eventdata.events[i]["DTSTART"]), parseInt(eventdata.events[i]["DTEND"]), eventdata.events[i]["LOCATION"], eventdata.events[i]["DESCRIPTION"]); parsedData[i] = new Array(eventdata.events[i]["SUMMARY"], new Date(parseInt(eventdata.events[i]["DTSTART"])), new Date(parseInt(eventdata.events[i]["DTEND"])), eventdata.events[i]["LOCATION"], eventdata.events[i]["DESCRIPTION"]);
} }
} }
@ -127,7 +128,7 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
createSelectBox: function() { createSelectBox: function() {
ctx = container.getContextByName('calendar'); ctx = container.getContextByName('calendar');
model = ctx.getModel(); model = ctx.getModel();
defaultFolder = model.getDefaultFolder(); defaultFolder = model.getDefaultFolder(); // @type: Zarafa.hierarchy.data.MAPIFolderRecord
subFolders = defaultFolder.getChildren(); subFolders = defaultFolder.getChildren();
var myStore = new Ext.data.ArrayStore({ var myStore = new Ext.data.ArrayStore({
@ -162,6 +163,8 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
return { return {
xtype: "selectbox", xtype: "selectbox",
ref: 'calendarselector',
id: 'calendarselector',
editable: false, editable: false,
name: "choosen_calendar", name: "choosen_calendar",
width: 100, width: 100,
@ -206,6 +209,22 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
border: false, border: false,
text: _("Import"), text: _("Import"),
anchor: "100%", anchor: "100%",
handler: this.importCheckedEvents,
scope: this,
allowBlank: false
}
},
createSubmitAllButton: function() {
return {
xtype: "button",
ref: "submitAllButton",
id: "submitAllButton",
disabled: true,
width: 100,
border: false,
text: _("Import All"),
anchor: "100%",
handler: this.importAllEvents, handler: this.importAllEvents,
scope: this, scope: this,
allowBlank: false allowBlank: false
@ -238,7 +257,8 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
waitMsg: 'Uploading and parsing calendar...', waitMsg: 'Uploading and parsing calendar...',
url: 'plugins/calendarimporter/php/upload.php', url: 'plugins/calendarimporter/php/upload.php',
failure: function(file, action) { failure: function(file, action) {
Ext.getCmp('submitButton').disable(); // momstly called... Ext.getCmp('submitButton').disable();
Ext.getCmp('submitAllButton').disable();
Ext.MessageBox.show({ Ext.MessageBox.show({
title : _('Error'), title : _('Error'),
msg : _(action.result.errors[action.result.errors.type]), msg : _(action.result.errors[action.result.errors.type]),
@ -249,6 +269,7 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
success: function(file, action){ success: function(file, action){
uploadField.reset(); uploadField.reset();
Ext.getCmp('submitButton').enable(); Ext.getCmp('submitButton').enable();
Ext.getCmp('submitAllButton').enable();
this.timezone = action.result.response.calendar["X-WR-TIMEZONE"]; this.timezone = action.result.response.calendar["X-WR-TIMEZONE"];
this.insert(this.items.length,this.createGrid(action.result.response)); this.insert(this.items.length,this.createGrid(action.result.response));
this.doLayout(); this.doLayout();
@ -286,20 +307,92 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa
importAllEvents: function () { importAllEvents: function () {
//receive existing calendar store //receive existing calendar store
var selIndex = this.calendarselector.selectedIndex;
var calValue = this.calendarselector.value;
if(selIndex == -1) { // no calendar choosen
Ext.MessageBox.show({
title : _('Error'),
msg : _('You have to choose a calendar!'),
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.OK
});
} else {
var calendarStore = new Zarafa.calendar.AppointmentStore(); var calendarStore = new Zarafa.calendar.AppointmentStore();
var calendarFolder = container.getHierarchyStore().getDefaultFolder('calendar'); var calendarFolder = container.getHierarchyStore().getDefaultFolder('calendar');
if(calValue != "calendar") {
var subFolders = calendarFolder.getChildren();
for(i=0;i<subFolders.length;i++) {
// loo up right folder
// TODO: improve!!
if(subFolders[i].getDisplayName() == calValue) {
calendarFolder = subFolders[i];
break;
}
}
}
//receive Records from grid rows //receive Records from grid rows
this.eventgrid.selModel.selectAll(); // select all entries
var newRecords = this.eventgrid.selModel.getSelections(); var newRecords = this.eventgrid.selModel.getSelections();
Ext.each(newRecords, function(newRecord) { Ext.each(newRecords, function(newRecord) {
var record = this.convertToAppointmentRecord(calendarFolder,newRecord.data); var record = this.convertToAppointmentRecord(calendarFolder,newRecord.data);
console.log(record);
calendarStore.add(record); calendarStore.add(record);
}, this); }, this);
calendarStore.save(); calendarStore.save();
this.dialog.close(); this.dialog.close();
} }
},
importCheckedEvents: function () {
//receive existing calendar store
var selIndex = this.calendarselector.selectedIndex;
var calValue = this.calendarselector.value;
if(selIndex == -1) { // no calendar choosen
Ext.MessageBox.show({
title : _('Error'),
msg : _('You have to choose a calendar!'),
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.OK
});
} else {
if(this.eventgrid.selModel.getCount() < 1) {
Ext.MessageBox.show({
title : _('Error'),
msg : _('You have to choose at least one event to import!'),
icon : Ext.MessageBox.ERROR,
buttons : Ext.MessageBox.OK
});
} else {
var calendarStore = new Zarafa.calendar.AppointmentStore();
var calendarFolder = container.getHierarchyStore().getDefaultFolder('calendar');
if(calValue != "calendar") {
var subFolders = calendarFolder.getChildren();
for(i=0;i<subFolders.length;i++) {
// loo up right folder
// TODO: improve!!
if(subFolders[i].getDisplayName() == calValue) {
calendarFolder = subFolders[i];
break;
}
}
}
//receive Records from grid rows
var newRecords = this.eventgrid.selModel.getSelections();
Ext.each(newRecords, function(newRecord) {
var record = this.convertToAppointmentRecord(calendarFolder,newRecord.data);
calendarStore.add(record);
}, this);
calendarStore.save();
this.dialog.close();
}
}
}
}); });
Ext.reg('calendarimporter.importpanel', Zarafa.plugins.calendarimporter.dialogs.ImportPanel); Ext.reg('calendarimporter.importpanel', Zarafa.plugins.calendarimporter.dialogs.ImportPanel);

View File

@ -2,7 +2,7 @@
<!DOCTYPE plugin SYSTEM "manifest.dtd"> <!DOCTYPE plugin SYSTEM "manifest.dtd">
<plugin version="2"> <plugin version="2">
<info> <info>
<version>0.1</version> <version>1.0</version>
<name>calendarimporter</name> <name>calendarimporter</name>
<title>ICS Calendar Importer</title> <title>ICS Calendar Importer</title>
<author>Christoph Haas</author> <author>Christoph Haas</author>

View File

@ -5,10 +5,12 @@
* PHP Version 5 * PHP Version 5
* *
* @category Parser * @category Parser
* @author Martin Thoma
* @author Christoph Haas <mail@h44z.net> * @author Christoph Haas <mail@h44z.net>
* @modified 17.11.2012 by Christoph Haas (original at http://code.google.com/p/ics-parser/)
* @license http://www.opensource.org/licenses/mit-license.php MIT License * @license http://www.opensource.org/licenses/mit-license.php MIT License
* @version SVN: 13 * @version SVN: 16
* @example $ical = new ical('MyCal.ics'); * @example $ical = new ical('calendar.ics');
* print_r( $ical->events() ); * print_r( $ical->events() );
*/ */
@ -18,8 +20,7 @@
* @param {string} filename The name of the file which should be parsed * @param {string} filename The name of the file which should be parsed
* @constructor * @constructor
*/ */
class ICal class ICal {
{
/* How many ToDos are in this ical? */ /* How many ToDos are in this ical? */
public /** @type {int} */ $todo_count = 0; public /** @type {int} */ $todo_count = 0;
@ -35,6 +36,9 @@ class ICal
/* Which keyword has been added to cal at last? */ /* Which keyword has been added to cal at last? */
private /** @type {string} */ $_lastKeyWord; private /** @type {string} */ $_lastKeyWord;
/* The default timezone, used to convert UTC Time */
private /** @type {string} */ $default_timezone = "Europe/Vienna";
/** /**
* Creates the iCal-Object * Creates the iCal-Object
* *
@ -42,8 +46,7 @@ class ICal
* *
* @return Object The iCal-Object * @return Object The iCal-Object
*/ */
public function __construct($filename) public function __construct($filename) {
{
if (!$filename) { if (!$filename) {
$this->errors = "No filename specified"; $this->errors = "No filename specified";
return false; return false;
@ -54,7 +57,7 @@ class ICal
$this->errors = "Not a valid ical file"; $this->errors = "Not a valid ical file";
return false; return false;
} else { } else {
// TODO: Fix multiline-description problem (see http://tools.ietf.org/html/rfc2445#section-4.8.1.5)
foreach ($lines as $line) { foreach ($lines as $line) {
$line = trim($line); $line = trim($line);
$add = $this->keyValueFromString($line); $add = $this->keyValueFromString($line);
@ -96,80 +99,7 @@ class ICal
$type = "VCALENDAR"; $type = "VCALENDAR";
break; break;
default: default:
$this->addCalendarComponentWithKeyAndValue($type, $this->addCalendarComponentWithKeyAndValue($type, $keyword, $value);
$keyword,
$value);
break;
}
}
return $this->cal;
}
}
/**
* Creates the iCal-Object
*
* @param {string} $filecontent The content of the iCal-file
*
* @return Object The iCal-Object
*/
public function setContent($filecontent)
{
if (!$filecontent) {
$this->errors = "No filecontent";
return false;
}
$lines = explode("\n", $filecontent);
if (stristr($lines[0], 'BEGIN:VCALENDAR') === false) {
$this->errors = "Not a valid ical file";
return false;
} else {
// TODO: Fix multiline-description problem (see http://tools.ietf.org/html/rfc2445#section-4.8.1.5)
foreach ($lines as $line) {
$line = trim($line);
$add = $this->keyValueFromString($line);
if ($add === false) {
$this->addCalendarComponentWithKeyAndValue($type, false, $line);
continue;
}
list($keyword, $value) = $add;
switch ($line) {
// http://www.kanzaki.com/docs/ical/vtodo.html
case "BEGIN:VTODO":
$this->todo_count++;
$type = "VTODO";
break;
// http://www.kanzaki.com/docs/ical/vevent.html
case "BEGIN:VEVENT":
//echo "vevent gematcht";
$this->event_count++;
$type = "VEVENT";
break;
//all other special strings
case "BEGIN:VCALENDAR":
case "BEGIN:DAYLIGHT":
// http://www.kanzaki.com/docs/ical/vtimezone.html
case "BEGIN:VTIMEZONE":
case "BEGIN:STANDARD":
$type = $value;
break;
case "END:VTODO": // end special text - goto VCALENDAR key
case "END:VEVENT":
case "END:VCALENDAR":
case "END:DAYLIGHT":
case "END:VTIMEZONE":
case "END:STANDARD":
$type = "VCALENDAR";
break;
default:
$this->addCalendarComponentWithKeyAndValue($type,
$keyword,
$value);
break; break;
} }
} }
@ -186,10 +116,7 @@ class ICal
* *
* @return {None} * @return {None}
*/ */
public function addCalendarComponentWithKeyAndValue($component, public function addCalendarComponentWithKeyAndValue($component, $keyword, $value) {
$keyword,
$value)
{
if ($keyword == false) { if ($keyword == false) {
$keyword = $this->last_keyword; $keyword = $this->last_keyword;
@ -215,6 +142,10 @@ class ICal
$keyword = $keyword[0]; // remove additional content like VALUE=DATE $keyword = $keyword[0]; // remove additional content like VALUE=DATE
} }
if (stristr($keyword, "TIMEZONE")) {
$this->default_timezone = $value; // store the calendertimezone
}
switch ($component) { switch ($component) {
case "VTODO": case "VTODO":
$this->cal[$component][$this->todo_count - 1][$keyword] = $value; $this->cal[$component][$this->todo_count - 1][$keyword] = $value;
@ -242,10 +173,8 @@ class ICal
* *
* @return {array} array("VCALENDAR", "Begin") * @return {array} array("VCALENDAR", "Begin")
*/ */
public function keyValueFromString($text) public function keyValueFromString($text) {
{
preg_match("/(^[^a-z:]+)[:]([\w\W]*)/", $text, $matches); preg_match("/(^[^a-z:]+)[:]([\w\W]*)/", $text, $matches);
error_log("Matching: " . count($matches) . " " . $text);
if (count($matches) == 0) { if (count($matches) == 0) {
return false; return false;
} }
@ -261,8 +190,7 @@ class ICal
* *
* @return {int} * @return {int}
*/ */
public function iCalDateToUnixTimestamp($icalDate) public function iCalDateToUnixTimestamp($icalDate) {
{
$icalDate = str_replace('T', '', $icalDate); $icalDate = str_replace('T', '', $icalDate);
$icalDate = str_replace('Z', '', $icalDate); $icalDate = str_replace('Z', '', $icalDate);
@ -286,7 +214,15 @@ class ICal
(int)$date[2], (int)$date[2],
(int)$date[3], (int)$date[3],
(int)$date[1]); (int)$date[1]);
return $timestamp;
$utcdate = new DateTime();
$utcdate->setTimestamp($timestamp);
$utcdate->setTimezone(new DateTimeZone($this->default_timezone));
$utcoffset = $utcdate->getOffset();
return ($timestamp + $utcoffset);
} }
/** /**
@ -295,8 +231,7 @@ class ICal
* *
* @return {array} * @return {array}
*/ */
public function events() public function events() {
{
$array = $this->cal; $array = $this->cal;
return $array['VEVENT']; return $array['VEVENT'];
} }
@ -306,8 +241,7 @@ class ICal
* *
* @return {array} * @return {array}
*/ */
public function calendar() public function calendar() {
{
$array = $this->cal; $array = $this->cal;
return $array['VCALENDAR']; return $array['VCALENDAR'];
} }
@ -317,8 +251,7 @@ class ICal
* *
* @return {boolean} * @return {boolean}
*/ */
public function hasEvents() public function hasEvents() {
{
return ( count($this->events()) > 0 ? true : false ); return ( count($this->events()) > 0 ? true : false );
} }
@ -335,8 +268,7 @@ class ICal
* *
* @return {mixed} * @return {mixed}
*/ */
public function eventsFromRange($rangeStart = false, $rangeEnd = false) public function eventsFromRange($rangeStart = false, $rangeEnd = false) {
{
$events = $this->sortEventsWithOrder($this->events(), SORT_ASC); $events = $this->sortEventsWithOrder($this->events(), SORT_ASC);
if (!$events) { if (!$events) {
@ -380,8 +312,7 @@ class ICal
* *
* @return {array} * @return {array}
*/ */
public function sortEventsWithOrder($events, $sortOrder = SORT_ASC) public function sortEventsWithOrder($events, $sortOrder = SORT_ASC) {
{
$extendedEvents = array(); $extendedEvents = array();
// loop through all events by adding two new elements // loop through all events by adding two new elements

View File

@ -47,7 +47,7 @@ class Plugincalendarimporter extends Plugin {
'plugins' => Array( 'plugins' => Array(
'calendarimporter' => Array( 'calendarimporter' => Array(
'enable' => PLUGIN_CALENDARIMPORTER_USER_DEFAULT_ENABLE, 'enable' => PLUGIN_CALENDARIMPORTER_USER_DEFAULT_ENABLE,
'default_calendar' => PLUGIN_CALENDARIMPORTER_DEFAULT 'default_calendar' => PLUGIN_CALENDARIMPORTER_DEFAULT // currently not used, maybe in next release
) )
) )
) )