diff options
author | Thomas Tanghus <thomas@tanghus.net> | 2012-05-13 23:24:04 +0200 |
---|---|---|
committer | Thomas Tanghus <thomas@tanghus.net> | 2012-05-13 23:27:14 +0200 |
commit | 9295f49553a04f17b58391523317881ad8edba19 (patch) | |
tree | 88b337b7eeeeda4c0ed9df8e80432593b7610eb4 /apps | |
parent | 1629c77eb5737ade319801c7ed8fba7b75d0a2bf (diff) | |
download | nextcloud-server-9295f49553a04f17b58391523317881ad8edba19.tar.gz nextcloud-server-9295f49553a04f17b58391523317881ad8edba19.zip |
Contacts: Add error checking and reporting to import from files app.
Diffstat (limited to 'apps')
-rwxr-xr-x | apps/contacts/import.php | 55 | ||||
-rw-r--r-- | apps/contacts/js/loader.js | 165 | ||||
-rwxr-xr-x | apps/contacts/lib/addressbook.php | 2 | ||||
-rwxr-xr-x | apps/contacts/templates/part.import.php | 48 |
4 files changed, 134 insertions, 136 deletions
diff --git a/apps/contacts/import.php b/apps/contacts/import.php index 2386a1cff98..7c1c98d9f5d 100755 --- a/apps/contacts/import.php +++ b/apps/contacts/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/apps/contacts/js/loader.js b/apps/contacts/js/loader.js index 95fd7dc94ea..8c79ea8a1d6 100644 --- a/apps/contacts/js/loader.js +++ b/apps/contacts/js/loader.js @@ -1,83 +1,86 @@ -/** - * 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{ +/**
+ * 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 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/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php index 78e94762f2e..79445ceeee1 100755 --- a/apps/contacts/lib/addressbook.php +++ b/apps/contacts/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/apps/contacts/templates/part.import.php b/apps/contacts/templates/part.import.php index 2dac7dd0ca1..b8793042997 100755 --- a/apps/contacts/templates/part.import.php +++ b/apps/contacts/templates/part.import.php @@ -1,27 +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(OCP\USER::getUser()); -$contacts_options[] = array('id'=>'newaddressbook', 'displayname'=>$l->t('create a new addressbook')); -echo OCP\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="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 class="bold" style="text-align:center;"><?php echo $l->t('Please choose the addressbook'); ?></p> + <select style="width:100%;" id="contacts" name="contacts"> + <?php + $contacts_options = OC_Contacts_Addressbook::all(OCP\USER::getUser()); + $contacts_options[] = array('id'=>'newaddressbook', 'displayname'=>$l->t('create a new addressbook')); + echo OCP\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> + <p style="text-align:center;"><?php echo $l->t('Importing contacts'); ?></p> + <div id="progressbar"></div> + <div id="import_done" style="display: none;"> + <p style="text-align:center;"></p> + <input type="button" value="<?php echo $l->t('Close'); ?>" id="import_done_button"> + </div> + </div> </div>
\ No newline at end of file |