]> source.dussan.org Git - nextcloud-server.git/commitdiff
Contacts: Added import with with file picker.
authorThomas Tanghus <thomas@tanghus.net>
Thu, 26 Apr 2012 01:20:28 +0000 (03:20 +0200)
committerThomas Tanghus <thomas@tanghus.net>
Thu, 26 Apr 2012 01:21:52 +0000 (03:21 +0200)
apps/contacts/ajax/uploadimport.php
apps/contacts/css/contacts.css
apps/contacts/import.php
apps/contacts/js/contacts.js
apps/contacts/templates/part.contact.php

index ab680c8823f7744d1a3b5aa5af983bfd687f044b..f44335a961e237256a239388fad838b3ca370ae3 100644 (file)
@@ -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?');
+}
+
+
 ?>
index 7338eed4a7fc966c90e90c717c13957fef0cd5e8..ae96c5e4e9fecb4f44e43b4c537fcbf70fbd6f19 100644 (file)
@@ -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; }
index 93af7e628140dca7d2d7eae260442e4f80b0e716..45f1b0c53dca2025820c67a26e1903bc2f7c00cf 100644 (file)
@@ -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);
index 8f2e7a0d23cd8fbf0581db1adbd09b4f818072b2..56be90975a090a0ee241684108bab85f272f51ef 100644 (file)
@@ -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){
index dec081a9b8930b5d6966fdc920c4562873a6e230..64a024c0926d56cdfdf1dcc348761c84f3d7ae8d 100644 (file)
@@ -24,7 +24,6 @@ $id = isset($_['id']) ? $_['id'] : '';
        <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">
-       <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">
                <!-- 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>
@@ -34,7 +33,6 @@ $id = isset($_['id']) ? $_['id'] : '';
                <input type="hidden" class="max_human_file_size" value="(max <?php echo $_['uploadMaxHumanFilesize']; ?>)">
                <input id="file_upload_start" type="file" accept="image/*" name="imagefile" />
                <iframe name="file_upload_target" id='file_upload_target' src=""></iframe>
-       </fieldset>
        </form>
        </div> <!-- contact_photo -->