From 0e1b92aafd63d169a339cb44a1483d011918bab5 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 15 Jan 2012 11:40:32 +0100 Subject: [PATCH] add import function for contacts --- ajax/importdialog.php | 17 ++++++ appinfo/app.php | 1 + import.php | 120 ++++++++++++++++++++++++++++++++++++++ import_tmp/Info | 2 + js/loader.js | 81 +++++++++++++++++++++++++ templates/part.import.php | 27 +++++++++ 6 files changed, 248 insertions(+) create mode 100644 ajax/importdialog.php create mode 100644 import.php create mode 100644 import_tmp/Info create mode 100644 js/loader.js create mode 100644 templates/part.import.php diff --git a/ajax/importdialog.php b/ajax/importdialog.php new file mode 100644 index 00000000..17b4cebd --- /dev/null +++ b/ajax/importdialog.php @@ -0,0 +1,17 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +require_once('../../../lib/base.php'); +OC_JSON::checkLoggedIn(); +OC_Util::checkAppEnabled('contacts'); +$l10n = new OC_L10N('contacts'); +$tmpl = new OC_Template('contacts', 'part.import'); +$tmpl->assign('path', $_POST['path']); +$tmpl->assign('filename', $_POST['filename']); +$tmpl->printpage(); +?> diff --git a/appinfo/app.php b/appinfo/app.php index a5b31006..23a5a425 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -22,4 +22,5 @@ OC_App::addNavigationEntry( array( OC_APP::registerPersonal('contacts','settings'); +OC_UTIL::addScript('contacts', 'loader'); require_once('apps/contacts/lib/search.php'); \ No newline at end of file diff --git a/import.php b/import.php new file mode 100644 index 00000000..f145c9a8 --- /dev/null +++ b/import.php @@ -0,0 +1,120 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +//check for calendar rights or create new one +ob_start(); +require_once ('../../lib/base.php'); +OC_JSON::checkLoggedIn(); +OC_Util::checkAppEnabled('calendar'); +$nl = "\n"; +$progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '10'); + fclose($progressfopen); +} +$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); +if($_POST['method'] == 'new'){ + $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']); + OC_Contacts_Addressbook::setActive($id, 1); +}else{ + $contacts = OC_Contacts_Addressbook::find($_POST['id']); + if($contacts['userid'] != OC_USER::getUser()){ + OC_JSON::error(); + exit(); + } + $id = $_POST['id']; +} +//analyse the calendar file +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '20'); + fclose($progressfopen); +} +$searchfor = array('VCARD'); +$parts = $searchfor; +$filearr = explode($nl, $file); +$inelement = false; +$parts = array(); +$i = 0; +foreach($filearr as $line){ + foreach($searchfor as $search){ + if(substr_count($line, $search) == 1){ + list($attr, $val) = explode(':', $line); + if($attr == 'BEGIN'){ + $parts[]['begin'] = $i; + $inelement = true; + } + if($attr == 'END'){ + $parts[count($parts) - 1]['end'] = $i; + $inelement = false; + } + } + } + $i++; +} +//import the calendar +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '40'); + fclose($progressfopen); +} +$start = ''; +for ($i = 0; $i < $parts[0]['begin']; $i++) { + if($i == 0){ + $start = $filearr[0]; + }else{ + $start .= $nl . $filearr[$i]; + } +} +$end = ''; +for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){ + if($i == $parts[count($parts) - 1]['end'] + 1){ + $end = $filearr[$parts[count($parts) - 1]['end'] + 1]; + }else{ + $end .= $nl . $filearr[$i]; + } +} +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '50'); + fclose($progressfopen); +} +$importready = array(); +foreach($parts as $part){ + for($i = $part['begin']; $i <= $part['end'];$i++){ + if($i == $part['begin']){ + $content = $filearr[$i]; + }else{ + $content .= $nl . $filearr[$i]; + } + } + $importready[] = $start . $nl . $content . $nl . $end; +} +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '70'); + fclose($progressfopen); +} +if(count($parts) == 1){ + OC_Contacts_VCard::add($id, $file); +}else{ + foreach($importready as $import){ + OC_Contacts_VCard::add($id, $import); + } +} +//done the import +if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, '100'); + fclose($progressfopen); +} +sleep(3); +if(is_writable('import_tmp/')){ + unlink($progressfile); +} +OC_JSON::success(); \ No newline at end of file diff --git a/import_tmp/Info b/import_tmp/Info new file mode 100644 index 00000000..abafbce4 --- /dev/null +++ b/import_tmp/Info @@ -0,0 +1,2 @@ +This folder contains static files with the percentage of the import. +Requires write permission diff --git a/js/loader.js b/js/loader.js new file mode 100644 index 00000000..eb59185d --- /dev/null +++ b/js/loader.js @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2012 Georg Ehrke + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +Contacts_Import={ + importdialog: function(filename){ + var path = $('#dir').val(); + $('body').append('
'); + $('#contacts_import').load(OC.filePath('contacts', 'ajax', 'importdialog.php'), {filename:filename, path:path}, function(){Contacts_Import.initdialog(filename);}); + }, + initdialog: function(filename){ + $('#contacts_import_dialog').dialog({ + width : 500, + close : function() { + $(this).dialog('destroy').remove(); + $('#contacts_import').remove(); + } + }); + $('#import_done_button').click(function(){ + $('#contacts_import_dialog').dialog('destroy').remove(); + $('#contacts_import').remove(); + }); + $('#progressbar').progressbar({value: 0}); + $('#startimport').click(function(){ + var filename = $('#filename').val(); + var path = $('#path').val(); + var addressbookid = $('#contacts option:selected').val(); + if($('#contacts option:selected').val() == 'newaddressbook'){ + var method = 'new'; + var addressbookname = $('#newaddressbook').val(); + var addressbookname = $.trim(addressbookname); + if(newaddressbook == ''){ + $('#newaddressbook').css('background-color', '#FF2626'); + $('#newaddressbook').focus(function(){ + $('#newaddressbook').css('background-color', '#F8F8F8'); + }); + return false; + } + }else{ + var method = 'old'; + } + $('#newaddressbook').attr('readonly', 'readonly'); + $('#contacts').attr('disabled', 'disabled'); + var progressfile = $('#progressfile').val(); + $.post(OC.filePath('contacts', '', 'import.php'), {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(data){ + if(data.status == 'success'){ + $('#progressbar').progressbar('option', 'value', 100); + $('#import_done').css('display', 'block'); + } + }); + $('#form_container').css('display', 'none'); + $('#progressbar_container').css('display', 'block'); + window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500); + }); + $('#contacts').change(function(){ + if($('#contacts option:selected').val() == 'newaddressbook'){ + $('#newaddressbookform').slideDown('slow'); + }else{ + $('#newaddressbookform').slideUp('slow'); + } + }); + }, + getimportstatus: function(progressfile){ + $.get(OC.filePath('contacts', 'import_tmp', progressfile), function(percent){ + $('#progressbar').progressbar('option', 'value', parseInt(percent)); + if(percent < 100){ + window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500); + }else{ + $('#import_done').css('display', 'block'); + } + }); + } +} +$(document).ready(function(){ + if(typeof FileActions !== 'undefined'){ + FileActions.register('text/vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.setDefault('text/vcard','importaddressbook'); + }; +}); \ No newline at end of file diff --git a/templates/part.import.php b/templates/part.import.php new file mode 100644 index 00000000..570eda9b --- /dev/null +++ b/templates/part.import.php @@ -0,0 +1,27 @@ +
"> +
+ + + +

t('Please choose the addressbook'); ?> + +

+!" id="startimport"> +
+ +
\ No newline at end of file