From f5a9b8d2e7a9d7b8ddefebd844905889a24a0361 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Sun, 13 May 2012 23:24:04 +0200 Subject: [PATCH] Contacts: Add error checking and reporting to import from files app. --- import.php | 55 ++++++------- js/loader.js | 165 +++++++++++++++++++------------------- lib/addressbook.php | 2 +- templates/part.import.php | 48 +++++------ 4 files changed, 134 insertions(+), 136 deletions(-) diff --git a/import.php b/import.php index 2386a1cf..7c1c98d9 100755 --- a/import.php +++ b/import.php @@ -12,11 +12,15 @@ OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('contacts'); $nl = "\n"; $progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '10'); - fclose($progressfopen); + +function writeProgress($pct) { + if(is_writable('import_tmp/')){ + $progressfopen = fopen($progressfile, 'w'); + fwrite($progressfopen, $pct); + fclose($progressfopen); + } } +writeProgress('10'); $view = $file = null; if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { $view = OCP\App::getStorage('contacts'); @@ -28,20 +32,23 @@ if(!$file) { OCP\JSON::error(array('message' => 'Import file was empty.')); exit(); } - if(isset($_POST['method']) && $_POST['method'] == 'new'){ $id = OC_Contacts_Addressbook::add(OCP\USER::getUser(), $_POST['addressbookname']); + if(!$id) { + OCP\JSON::error(array('message' => 'Error creating address book.')); + exit(); + } OC_Contacts_Addressbook::setActive($id, 1); }else{ $id = $_POST['id']; + if(!$id) { + OCP\JSON::error(array('message' => 'Error getting the ID of the address book.')); + exit(); + } OC_Contacts_App::getAddressbook($id); // is owner access check } //analyse the contacts file -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '20'); - fclose($progressfopen); -} +writeProgress('20'); $searchfor = array('VCARD'); $parts = $searchfor; $filearr = explode($nl, $file); @@ -65,11 +72,7 @@ foreach($filearr as $line){ $i++; } //import the contacts -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '40'); - fclose($progressfopen); -} +writeProgress('40'); $start = ''; for ($i = 0; $i < $parts[0]['begin']; $i++) { if($i == 0){ @@ -86,11 +89,7 @@ for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){ $end .= $nl . $filearr[$i]; } } -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '50'); - fclose($progressfopen); -} +writeProgress('50'); $importready = array(); foreach($parts as $part){ for($i = $part['begin']; $i <= $part['end'];$i++){ @@ -102,16 +101,16 @@ foreach($parts as $part){ } $importready[] = $start . $nl . $content . $nl . $end; } -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '70'); - fclose($progressfopen); -} +writeProgress('70'); if(count($parts) == 1){ $importready = array($file); } $imported = 0; $failed = 0; +if(!count($importready) > 0) { + OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.')); + exit(); +} foreach($importready as $import){ $card = OC_VObject::parse($import); if (!$card) { @@ -123,11 +122,7 @@ foreach($importready as $import){ OC_Contacts_VCard::add($id, $card); } //done the import -if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, '100'); - fclose($progressfopen); -} +writeProgress('100'); sleep(3); if(is_writable('import_tmp/')){ unlink($progressfile); diff --git a/js/loader.js b/js/loader.js index 95fd7dc9..8c79ea8a 100644 --- a/js/loader.js +++ b/js/loader.js @@ -1,83 +1,86 @@ -/** - * 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{ +/** + * 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 method = 'old'; + var addressbookid = $('#contacts option:selected').val(); + if($('#contacts option:selected').val() == 'newaddressbook'){ + var method = 'new'; + var addressbookname = $('#newaddressbook').val(); + var addressbookname = $.trim(addressbookname); + if(addressbookname == ''){ + $('#newaddressbook').css('background-color', '#FF2626'); + $('#newaddressbook').focus(function(){ + $('#newaddressbook').css('background-color', '#F8F8F8'); + }); + return false; + } + } + $('#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(jsondata){ + if(jsondata.status == 'success'){ + $('#progressbar').progressbar('option', 'value', 100); + $('#import_done').find('p').html(t('contacts', 'Result: ') + jsondata.data.imported + t('contacts', ' imported, ') + jsondata.data.failed + t('contacts', ' failed.')); + } else { + $('#import_done').find('p').html(jsondata.data.message); + } + $('#import_done').show().find('p').addClass('bold'); + $('#progressbar').fadeOut('slow'); + }); + $('#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'); - FileActions.register('text/x-vcard','importaddressbook', '', Contacts_Import.importdialog); - FileActions.setDefault('text/x-vcard','importaddressbook'); - }; + } + }); + } +} +$(document).ready(function(){ + if(typeof FileActions !== 'undefined'){ + FileActions.register('text/vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.setDefault('text/vcard','importaddressbook'); + FileActions.register('text/x-vcard','importaddressbook', '', Contacts_Import.importdialog); + FileActions.setDefault('text/x-vcard','importaddressbook'); + }; }); \ No newline at end of file diff --git a/lib/addressbook.php b/lib/addressbook.php index 78e94762..79445cee 100755 --- a/lib/addressbook.php +++ b/lib/addressbook.php @@ -84,7 +84,7 @@ class OC_Contacts_Addressbook{ * @param string $description * @return insertid */ - public static function add($userid,$name,$description){ + public static function add($userid,$name,$description=''){ $all = self::all($userid); $uris = array(); foreach($all as $i){ diff --git a/templates/part.import.php b/templates/part.import.php index 2dac7dd0..b8793042 100755 --- a/templates/part.import.php +++ b/templates/part.import.php @@ -1,27 +1,27 @@
"> -
- - - -

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

-!" id="startimport"> -
+
+ + + +

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

+ + + !" id="startimport"> +
+

t('Importing contacts'); ?>

+
+ +
\ No newline at end of file