diff options
author | Georg Ehrke <dev@georgswebsite.de> | 2012-01-15 11:40:32 +0100 |
---|---|---|
committer | Georg Ehrke <dev@georgswebsite.de> | 2012-02-05 12:13:53 +0100 |
commit | 8337eaa1ec3962b185a2fe2e03ccf24fed7020c4 (patch) | |
tree | bbe3c95fa8ced7412b487ec665d3f460201f46d7 | |
parent | 90c4666b12e0fb2da8f0e15a018d8edd736f4f4f (diff) | |
download | nextcloud-server-8337eaa1ec3962b185a2fe2e03ccf24fed7020c4.tar.gz nextcloud-server-8337eaa1ec3962b185a2fe2e03ccf24fed7020c4.zip |
add import function for contacts
-rw-r--r-- | apps/contacts/ajax/importdialog.php | 17 | ||||
-rw-r--r-- | apps/contacts/appinfo/app.php | 1 | ||||
-rw-r--r-- | apps/contacts/import.php | 120 | ||||
-rw-r--r-- | apps/contacts/import_tmp/Info | 2 | ||||
-rw-r--r-- | apps/contacts/js/loader.js | 81 | ||||
-rw-r--r-- | apps/contacts/templates/part.import.php | 27 |
6 files changed, 248 insertions, 0 deletions
diff --git a/apps/contacts/ajax/importdialog.php b/apps/contacts/ajax/importdialog.php new file mode 100644 index 00000000000..17b4cebdbd1 --- /dev/null +++ b/apps/contacts/ajax/importdialog.php @@ -0,0 +1,17 @@ +<?php +/** + * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de> + * 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/apps/contacts/appinfo/app.php b/apps/contacts/appinfo/app.php index a5b31006247..23a5a4253d0 100644 --- a/apps/contacts/appinfo/app.php +++ b/apps/contacts/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/apps/contacts/import.php b/apps/contacts/import.php new file mode 100644 index 00000000000..f145c9a8822 --- /dev/null +++ b/apps/contacts/import.php @@ -0,0 +1,120 @@ +<?php +/** + * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de> + * 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/apps/contacts/import_tmp/Info b/apps/contacts/import_tmp/Info new file mode 100644 index 00000000000..abafbce435c --- /dev/null +++ b/apps/contacts/import_tmp/Info @@ -0,0 +1,2 @@ +This folder contains static files with the percentage of the import. +Requires write permission diff --git a/apps/contacts/js/loader.js b/apps/contacts/js/loader.js new file mode 100644 index 00000000000..eb59185d728 --- /dev/null +++ b/apps/contacts/js/loader.js @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de> + * 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('<div id="contacts_import"></div>'); + $('#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/apps/contacts/templates/part.import.php b/apps/contacts/templates/part.import.php new file mode 100644 index 00000000000..570eda9b07d --- /dev/null +++ b/apps/contacts/templates/part.import.php @@ -0,0 +1,27 @@ +<div id="contacts_import_dialog" title="<?php echo $l->t("Import a contacts file"); ?>"> +<div id="form_container"> +<input type="hidden" id="filename" value="<?php echo $_['filename'];?>"> +<input type="hidden" id="path" value="<?php echo $_['path'];?>"> +<input type="hidden" id="progressfile" value="<?php echo md5(session_id()) . '.txt';?>"> +<p style="text-align:center;"><b><?php echo $l->t('Please choose the addressbook'); ?></b> +<select style="width:100%;" id="contacts" name="contacts"> +<?php +$contacts_options = OC_Contacts_Addressbook::all(OC_User::getUser()); +$contacts_options[] = array('id'=>'newaddressbook', 'displayname'=>$l->t('create a new addressbook')); +echo html_select_options($contacts_options, $contacts_options[0]['id'], array('value'=>'id', 'label'=>'displayname')); +?> +</select> +<div id="newaddressbookform" style="display: none;"> + <input type="text" style="width: 97%;" placeholder="<?php echo $l->t('Name of new addressbook'); ?>" id="newaddressbook" name="newaddressbook"> +</div> +<input type="button" value="<?php echo $l->t("Import");?>!" id="startimport"> +</div> +<div id="progressbar_container" style="display: none"> +<p style="text-align:center;"><b><?php echo $l->t('Importing contacts'); ?></b> +<div id="progressbar"></div> +<div id="import_done" style="display: none;"> +<p style="text-align:center;"><b><?php echo $l->t('Contacts imported successfully'); ?></b></p> +<input type="button" value="<?php echo $l->t('Close Dialog'); ?>" id="import_done_button"> +</div> +</div> +</div>
\ No newline at end of file |