From 8f9248aaffeb53bc3876cd5f5bb44423f2a7b2da Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Thu, 6 Dec 2012 23:10:23 +0000 Subject: [PATCH] calendarexporter: TODO: write new list module, ics creation improvements --- js/data/ResponseHandler.js | 9 ++++ js/dialogs/ImportPanel.js | 49 +++++++++++++++++--- js/plugin.calendarimporter.js | 2 +- php/download.php | 23 ++++++++++ php/module.calendarexporter.php | 81 +++++++++++++++++++++++++++++++-- 5 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 php/download.php diff --git a/js/data/ResponseHandler.js b/js/data/ResponseHandler.js index ed8e8be..9796b7d 100644 --- a/js/data/ResponseHandler.js +++ b/js/data/ResponseHandler.js @@ -22,6 +22,15 @@ Zarafa.plugins.calendarimporter.data.ResponseHandler = Ext.extend(Zarafa.core.da this.successCallback(response); }, + /** + * Call the successCallback callback function. + * @param {Object} response Object contained the response data. + */ + doList : function(response) + { + this.successCallback(response); + }, + /** * In case exception happened on server, server will return * exception response with the code of exception. diff --git a/js/dialogs/ImportPanel.js b/js/dialogs/ImportPanel.js index bb55107..0d6318a 100644 --- a/js/dialogs/ImportPanel.js +++ b/js/dialogs/ImportPanel.js @@ -395,11 +395,18 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa }); container.getRequest().singleRequest( - 'calendarexportermodule', - 'export', + 'appointmentlistmodule', + 'list', { - calFolder : calValue, - calIndex : selIndex, + groupDir: "ASC", + restriction: { + startdate: 0, + duedate: 2145826800 // 2037... highest unix timestamp + }, + sort: [{ + "field": "startdate", + "direction": "DESC" + }], store_entryid : calendarFolder.data.store_entryid, entryid : calendarFolder.data.entryid }, @@ -415,13 +422,41 @@ Zarafa.plugins.calendarimporter.dialogs.ImportPanel = Ext.extend(Ext.form.FormPa */ exportDone : function(response) { - if(response.status === true) { - container.getNotifier().notify('info', 'Exported', 'Exported ' + response.entries + ' entries'); + if(response.item.length > 0) { + // call export function here! + var responseHandler = new Zarafa.plugins.calendarimporter.data.ResponseHandler({ + successCallback: this.downLoadICS.createDelegate(this) + }); + + container.getRequest().singleRequest( + 'calendarexportermodule', + 'export', + { + data: response, + calendar: this.calendarselector.value + }, + responseHandler + ); + container.getNotifier().notify('info', 'Exported', 'Exported ' + response.item.length + ' entries'); } else { - container.getNotifier().notify('error', 'Export Failed', 'Failed to export your calendar'); + container.getNotifier().notify('info', 'Export Failed', 'There were no items to export!'); } this.dialog.close(); }, + + /** + * download ics file =) + * @param {Object} response + * @private + */ + downLoadICS : function(response) + { + if(response.status === true) { + document.location.href = 'plugins/calendarimporter/php/download.php?fileid='+response.fileid+'&basedir='+response.basedir+'&secid='+response.secid+'&realname='+response.realname; + } else { + container.getNotifier().notify('error', 'Export Failed', 'ICal File creation failed!'); + } + }, importCheckedEvents: function () { //receive existing calendar store diff --git a/js/plugin.calendarimporter.js b/js/plugin.calendarimporter.js index a7b8199..1611d1e 100644 --- a/js/plugin.calendarimporter.js +++ b/js/plugin.calendarimporter.js @@ -1,6 +1,6 @@ Ext.namespace("Zarafa.plugins.calendarimporter"); // Assign the right namespace -Zarafa.plugins.calendarimporter.ImportPlugin = Ext.extend(Zarafa.core.Plugin, { // create new import plugin +Zarafa.plugins.calendarimporter.ImportPlugin = Ext.extend(Zarafa.core.Plugin, { // create new import plugin /** * @constructor diff --git a/php/download.php b/php/download.php new file mode 100644 index 0000000..d10ba3c --- /dev/null +++ b/php/download.php @@ -0,0 +1,23 @@ + download! +if(file_exists($secfile)) { + @header("Last-Modified: " . @gmdate("D, d M Y H:i:s",time()) . " GMT"); + @header("Content-type: text/calendar"); + header("Content-Length: " . filesize($icsfile)); + header("Content-Disposition: attachment; filename=" . $realname . ".ics"); + + //write ics + readfile($icsfile); + unlink($secfile); + unlink($icsfile); +} + +?> \ No newline at end of file diff --git a/php/module.calendarexporter.php b/php/module.calendarexporter.php index 04af4a7..55c7a3e 100644 --- a/php/module.calendarexporter.php +++ b/php/module.calendarexporter.php @@ -76,11 +76,86 @@ class CalendarexporterModule extends Module { return $result; } - private function exportCalendar($actionType, $actionData) { - /* look up functionality in class.appointmentlistmodule.php (webapp/server/modules)*/ + private function randomstring($length = 6) { + // $chars - all allowed charakters + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; + + srand((double)microtime()*1000000); + $i = 0; + while ($i < $length) { + $num = rand() % strlen($chars); + $tmp = substr($chars, $num, 1); + $pass = $pass . $tmp; + $i++; + } + return $pass; + } + private function createSecIDFile($secid) { + $lockFile = TMP_PATH . "/secid." . $secid; + $fh = fopen($lockFile, 'w') or die("can't open secid file"); + $stringData = date(DATE_RFC822); + fwrite($fh, $stringData); + fclose($fh); + } + + private function writeICSHead($fh, $calname) { + $icshead = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Zarafa Webapp//Zarafa Calendar Exporter//DE\nMETHOD:PUBLISH\nX-WR-CALNAME:" . $calname. "\n"; + fwrite($fh, $icshead); + } + + private function writeICSEnd($fh) { + $icsend = "END:VCALENDAR"; + fwrite($fh, $icsend); + } + + private function writeEvent($fh, $event) { + $head = "BEGIN:VEVENT\n"; + $end = "END:VEVENT\n"; + + $fields = array( + "DTSTART" => $event["startdate"], + "DTEND" => $event["duedate"], + "DTSTAMP" => $event["creation_time"], + "DESCRIPTION" => "nothing...", + "LOCATION" => $event["location"], + "SUMMARY" => $event["subject"] + ); + + fwrite($fh, $head); + + // event fields: + foreach ($fields as $key => $value) { + fwrite($fh, $key . ": " . $value . "\n"); + } + + unset($fields); + + fwrite($fh, $end); + } + + private function exportCalendar($actionType, $actionData) { + $secid = $this->randomstring(); + $this->createSecIDFile($secid); + $tmpname = stripslashes($actionData["calendar"] . ".ics." . $this->randomstring(8)); + $filename = TMP_PATH . "/" . $tmpname . "." . $secid; + + // create ics file.... + $fh = fopen($filename, 'w') or die("can't open ics file"); + $this->writeICSHead($fh, $actionData["calendar"]); + + foreach($actionData["data"]["item"] as $event) { + $this->writeEvent($fh, $event["props"]); + } + + $this->writeICSEnd($fh); + fclose($fh); + $response['status'] = true; - $response['entries'] = "1002"; // number of entries that will be exported + $response['fileid'] = $tmpname; // number of entries that will be exported + $response['basedir'] = TMP_PATH; + $response['secid'] = $secid; + $response['realname'] = $actionData["calendar"]; $this->addActionData($actionType, $response); $GLOBALS["bus"]->addData($this->getResponseData());