1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00

Contacts: Added import with with file picker.

This commit is contained in:
Thomas Tanghus 2012-04-26 03:20:28 +02:00
parent fefe8aa158
commit f01fc2e855
5 changed files with 76 additions and 8 deletions

View File

@ -34,17 +34,52 @@ function debug($msg) {
OC_Log::write('contacts','ajax/uploadimport.php: '.$msg, OC_Log::DEBUG); 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. // 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); $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false);
if($fn) { if($fn) {
$view = OC_App::getStorage('contacts');
$tmpfile = md5(rand());
if($view->file_put_contents('/'.$tmpfile, file_get_contents('php://input'))) { if($view->file_put_contents('/'.$tmpfile, file_get_contents('php://input'))) {
debug($fn.' uploaded'); debug($fn.' uploaded');
OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile))); OC_JSON::success(array('data' => array('path'=>'', 'file'=>$tmpfile)));
exit();
} else { } else {
bailOut(OC_Contacts_App::$l10n->t('Error uploading contacts to storage.')); 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?');
}
?> ?>

View File

@ -87,9 +87,9 @@ dl.addresscard dd > ul { margin: 0.3em; padding: 0.3em; }
#adr_zipcode {} #adr_zipcode {}
#adr_country {} #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; } input[type="checkbox"] { width: 20px; height: 20px; vertical-align: bottom; }
.big { font-weight:bold; font-size:1.2em; } .big { font-weight:bold; font-size:1.2em; }
.huge { font-weight:bold; font-size:1.5em; } .huge { font-weight:bold; font-size:1.5em; }

View File

@ -24,6 +24,11 @@ if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
} else { } else {
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); $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'){ if(isset($_POST['method']) && $_POST['method'] == 'new'){
$id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']); $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']);
OC_Contacts_Addressbook::setActive($id, 1); OC_Contacts_Addressbook::setActive($id, 1);

View File

@ -1345,8 +1345,13 @@ Contacts={
} }
}, },
loadImportHandlers:function() { 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'); this.droptarget = $('#import_drop_target');
console.log($('#import_drop_target').html());
$(this.droptarget).bind('dragover',function(event){ $(this.droptarget).bind('dragover',function(event){
$(event.target).addClass('droppable'); $(event.target).addClass('droppable');
event.stopPropagation(); event.stopPropagation();
@ -1414,6 +1419,31 @@ Contacts={
xhr.send(file); 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){ importAddressbook:function(object){
var tr = $(document.createElement('tr')) var tr = $(document.createElement('tr'))
.load(OC.filePath('contacts', 'ajax', 'importaddressbook.php')); .load(OC.filePath('contacts', 'ajax', 'importaddressbook.php'));
@ -1590,7 +1620,7 @@ $(document).ready(function(){
* Profile picture upload handling * Profile picture upload handling
*/ */
// New profile picture selected // New profile picture selected
$('#file_upload_start').live('change',function(){ $('#file_upload_start').change(function(){
Contacts.UI.Card.uploadPhoto(this.files); Contacts.UI.Card.uploadPhoto(this.files);
}); });
$('#contacts_details_photo_wrapper').bind('dragover',function(event){ $('#contacts_details_photo_wrapper').bind('dragover',function(event){

View File

@ -24,7 +24,6 @@ $id = isset($_['id']) ? $_['id'] : '';
<div id="contact_photo" class="contactsection"> <div id="contact_photo" class="contactsection">
<form class="float" id="file_upload_form" action="ajax/uploadphoto.php" method="post" enctype="multipart/form-data" target="file_upload_target"> <form class="float" id="file_upload_form" action="ajax/uploadphoto.php" method="post" enctype="multipart/form-data" target="file_upload_target">
<fieldset id="photo">
<div class="tip propertycontainer" id="contacts_details_photo_wrapper" title="<?php echo $l->t('Click or drop to upload picture'); ?> (max <?php echo $_['uploadMaxHumanFilesize']; ?>)" data-element="PHOTO"> <div class="tip propertycontainer" id="contacts_details_photo_wrapper" title="<?php echo $l->t('Click or drop to upload picture'); ?> (max <?php echo $_['uploadMaxHumanFilesize']; ?>)" data-element="PHOTO">
<!-- img style="padding: 1em;" id="contacts_details_photo" alt="Profile picture" src="photo.php?id=<?php echo $_['id']; ?>" / --> <!-- img style="padding: 1em;" id="contacts_details_photo" alt="Profile picture" src="photo.php?id=<?php echo $_['id']; ?>" / -->
<progress id="contacts_details_photo_progress" style="display:none;" value="0" max="100">0 %</progress> <progress id="contacts_details_photo_progress" style="display:none;" value="0" max="100">0 %</progress>
@ -34,7 +33,6 @@ $id = isset($_['id']) ? $_['id'] : '';
<input type="hidden" class="max_human_file_size" value="(max <?php echo $_['uploadMaxHumanFilesize']; ?>)"> <input type="hidden" class="max_human_file_size" value="(max <?php echo $_['uploadMaxHumanFilesize']; ?>)">
<input id="file_upload_start" type="file" accept="image/*" name="imagefile" /> <input id="file_upload_start" type="file" accept="image/*" name="imagefile" />
<iframe name="file_upload_target" id='file_upload_target' src=""></iframe> <iframe name="file_upload_target" id='file_upload_target' src=""></iframe>
</fieldset>
</form> </form>
</div> <!-- contact_photo --> </div> <!-- contact_photo -->