From: Thomas Tanghus Date: Thu, 26 Apr 2012 01:20:28 +0000 (+0200) Subject: Contacts: Added import with with file picker. X-Git-Tag: v4.0.0beta~182 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5dbf362623ccf9532c2ca6422dedb41ec713cbf5;p=nextcloud-server.git Contacts: Added import with with file picker. --- diff --git a/apps/contacts/ajax/uploadimport.php b/apps/contacts/ajax/uploadimport.php index ab680c8823f..f44335a961e 100644 --- a/apps/contacts/ajax/uploadimport.php +++ b/apps/contacts/ajax/uploadimport.php @@ -34,17 +34,52 @@ function debug($msg) { OC_Log::write('contacts','ajax/uploadimport.php: '.$msg, OC_Log::DEBUG); } +$view = OC_App::getStorage('contacts'); +$tmpfile = md5(rand()); + // If it is a Drag'n'Drop transfer it's handled here. $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false); if($fn) { - $view = OC_App::getStorage('contacts'); - $tmpfile = md5(rand()); if($view->file_put_contents('/'.$tmpfile, file_get_contents('php://input'))) { debug($fn.' uploaded'); OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile))); + exit(); } else { bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); } } +// File input transfers are handled here +if (!isset($_FILES['importfile'])) { + OC_Log::write('contacts','ajax/uploadphoto.php: No file was uploaded. Unknown error.', OC_Log::DEBUG); + OC_JSON::error(array('data' => array( 'message' => 'No file was uploaded. Unknown error' ))); + exit(); +} +$error = $_FILES['importfile']['error']; +if($error !== UPLOAD_ERR_OK) { + $errors = array( + 0=>OC_Contacts_App::$l10n->t("There is no error, the file uploaded with success"), + 1=>OC_Contacts_App::$l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini").ini_get('upload_max_filesize'), + 2=>OC_Contacts_App::$l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"), + 3=>OC_Contacts_App::$l10n->t("The uploaded file was only partially uploaded"), + 4=>OC_Contacts_App::$l10n->t("No file was uploaded"), + 6=>OC_Contacts_App::$l10n->t("Missing a temporary folder") + ); + bailOut($errors[$error]); +} +$file=$_FILES['importfile']; + +$tmpfname = tempnam("/tmp", "occOrig"); +if(file_exists($file['tmp_name'])) { + if($view->file_put_contents('/'.$tmpfile, file_get_contents($file['tmp_name']))) { + debug($fn.' uploaded'); + OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile))); + } else { + bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); + } +} else { + bailOut('Temporary file: \''.$file['tmp_name'].'\' has gone AWOL?'); +} + + ?> diff --git a/apps/contacts/css/contacts.css b/apps/contacts/css/contacts.css index 7338eed4a7f..ae96c5e4e9f 100644 --- a/apps/contacts/css/contacts.css +++ b/apps/contacts/css/contacts.css @@ -87,9 +87,9 @@ dl.addresscard dd > ul { margin: 0.3em; padding: 0.3em; } #adr_zipcode {} #adr_country {} -#file_upload_target, #crop_target { display:none; } +#file_upload_target, #import_upload_target, #crop_target { display:none; } -#file_upload_start { opacity:0; filter:alpha(opacity=0); z-index:1; /*position:absolute; left:0; top:0;*/ width:0; height:0;} +#file_upload_start, #import_upload_start { opacity:0; filter:alpha(opacity=0); z-index:1; /*position:absolute; left:0; top:0;*/ width:0; height:0;} input[type="checkbox"] { width: 20px; height: 20px; vertical-align: bottom; } .big { font-weight:bold; font-size:1.2em; } .huge { font-weight:bold; font-size:1.5em; } diff --git a/apps/contacts/import.php b/apps/contacts/import.php index 93af7e62814..45f1b0c53dc 100644 --- a/apps/contacts/import.php +++ b/apps/contacts/import.php @@ -24,6 +24,11 @@ if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { } else { $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); } +if(!$file) { + OC_JSON::error(array('message' => 'Import file was empty.')); + exit(); +} +error_log('File: '.$file); if(isset($_POST['method']) && $_POST['method'] == 'new'){ $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']); OC_Contacts_Addressbook::setActive($id, 1); diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js index 8f2e7a0d23c..56be90975a0 100644 --- a/apps/contacts/js/contacts.js +++ b/apps/contacts/js/contacts.js @@ -1345,8 +1345,13 @@ Contacts={ } }, loadImportHandlers:function() { + $('#import_upload_start').change(function(){ + Contacts.UI.Addressbooks.uploadImport(this.files); + }); + $('#importaddressbook_dialog').find('.upload').click(function() { + $('#import_upload_start').trigger('click'); + }); this.droptarget = $('#import_drop_target'); - console.log($('#import_drop_target').html()); $(this.droptarget).bind('dragover',function(event){ $(event.target).addClass('droppable'); event.stopPropagation(); @@ -1414,6 +1419,31 @@ Contacts={ xhr.send(file); } }, + uploadImport:function(filelist) { + if(!filelist) { + OC.dialogs.alert(t('contacts','No files selected for upload.'), t('contacts', 'Error')); + return; + } + //var file = filelist.item(0); + var file = filelist[0]; + var target = $('#import_upload_target'); + var form = $('#import_upload_form'); + var totalSize=0; + if(file.size > $('#max_upload').val()){ + OC.dialogs.alert(t('contacts','The file you are trying to upload exceed the maximum size for file uploads on this server.'), t('contacts', 'Error')); + return; + } else { + target.load(function(){ + var response=jQuery.parseJSON(target.contents().text()); + if(response != undefined && response.status == 'success'){ + Contacts.UI.Addressbooks.doImport(response.data.path, response.data.file); + }else{ + OC.dialogs.alert(response.data.message, t('contacts', 'Error')); + } + }); + form.submit(); + } + }, importAddressbook:function(object){ var tr = $(document.createElement('tr')) .load(OC.filePath('contacts', 'ajax', 'importaddressbook.php')); @@ -1590,7 +1620,7 @@ $(document).ready(function(){ * Profile picture upload handling */ // New profile picture selected - $('#file_upload_start').live('change',function(){ + $('#file_upload_start').change(function(){ Contacts.UI.Card.uploadPhoto(this.files); }); $('#contacts_details_photo_wrapper').bind('dragover',function(event){ diff --git a/apps/contacts/templates/part.contact.php b/apps/contacts/templates/part.contact.php index dec081a9b89..64a024c0926 100644 --- a/apps/contacts/templates/part.contact.php +++ b/apps/contacts/templates/part.contact.php @@ -24,7 +24,6 @@ $id = isset($_['id']) ? $_['id'] : '';
-
@@ -34,7 +33,6 @@ $id = isset($_['id']) ? $_['id'] : ''; -