]> source.dussan.org Git - nextcloud-server.git/commitdiff
Contacts: Add error checking and reporting to import from files app.
authorThomas Tanghus <thomas@tanghus.net>
Sun, 13 May 2012 21:24:04 +0000 (23:24 +0200)
committerThomas Tanghus <thomas@tanghus.net>
Sun, 13 May 2012 21:27:14 +0000 (23:27 +0200)
apps/contacts/import.php
apps/contacts/js/loader.js
apps/contacts/lib/addressbook.php
apps/contacts/templates/part.import.php

index 2386a1cff980d6f7bf579dfe7c5a15cce1a19dd5..7c1c98d9f5d0cc41b691152e4c4e7713e4427651 100755 (executable)
@@ -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);
index 95fd7dc94eadab603fcb3281ee11fc1241196056..8c79ea8a1d676b9ea155d0a9c2c583b926db8379 100644 (file)
@@ -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{
+/**\r
+ * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>\r
+ * This file is licensed under the Affero General Public License version 3 or\r
+ * later.\r
+ * See the COPYING-README file.\r
+ */\r
+Contacts_Import={\r
+       importdialog: function(filename){\r
+               var path = $('#dir').val();\r
+               $('body').append('<div id="contacts_import"></div>');\r
+               $('#contacts_import').load(OC.filePath('contacts', 'ajax', 'importdialog.php'), {filename:filename, path:path}, function(){Contacts_Import.initdialog(filename);});\r
+       },\r
+       initdialog: function(filename){\r
+               $('#contacts_import_dialog').dialog({\r
+                       width : 500,\r
+                       close : function() {\r
+                               $(this).dialog('destroy').remove();\r
+                               $('#contacts_import').remove();\r
+                       }\r
+               });\r
+               $('#import_done_button').click(function(){\r
+                       $('#contacts_import_dialog').dialog('destroy').remove();\r
+                       $('#contacts_import').remove();\r
+               });\r
+               $('#progressbar').progressbar({value: 0});\r
+               $('#startimport').click(function(){\r
+                       var filename = $('#filename').val();\r
+                       var path = $('#path').val();\r
+                       var method = 'old';\r
+                       var addressbookid = $('#contacts option:selected').val();\r
+                       if($('#contacts option:selected').val() == 'newaddressbook'){\r
+                               var method = 'new';\r
+                               var addressbookname = $('#newaddressbook').val();\r
+                               var addressbookname = $.trim(addressbookname);\r
+                               if(addressbookname == ''){\r
+                                       $('#newaddressbook').css('background-color', '#FF2626');\r
+                                       $('#newaddressbook').focus(function(){\r
+                                               $('#newaddressbook').css('background-color', '#F8F8F8');\r
+                                       });\r
+                                       return false;\r
+                               }\r
+                       }\r
+                       $('#newaddressbook').attr('readonly', 'readonly');\r
+                       $('#contacts').attr('disabled', 'disabled');\r
+                       var progressfile = $('#progressfile').val();\r
+                       $.post(OC.filePath('contacts', '', 'import.php'), {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){\r
+                               if(jsondata.status == 'success'){\r
+                                       $('#progressbar').progressbar('option', 'value', 100);\r
+                                       $('#import_done').find('p').html(t('contacts', 'Result: ') + jsondata.data.imported + t('contacts', ' imported, ') + jsondata.data.failed + t('contacts', ' failed.'));\r
+                               } else {\r
+                                       $('#import_done').find('p').html(jsondata.data.message);\r
+                               }\r
+                               $('#import_done').show().find('p').addClass('bold');\r
+                               $('#progressbar').fadeOut('slow');\r
+                       });\r
+                       $('#form_container').css('display', 'none');\r
+                       $('#progressbar_container').css('display', 'block');\r
+                       window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);\r
+               });\r
+               $('#contacts').change(function(){\r
+                       if($('#contacts option:selected').val() == 'newaddressbook'){\r
+                               $('#newaddressbookform').slideDown('slow');\r
+                       }else{\r
+                               $('#newaddressbookform').slideUp('slow');\r
+                       }\r
+               });\r
+       },\r
+       getimportstatus: function(progressfile){\r
+               $.get(OC.filePath('contacts', 'import_tmp', progressfile), function(percent){\r
+                       $('#progressbar').progressbar('option', 'value', parseInt(percent));\r
+                       if(percent < 100){\r
+                               window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);\r
+                       }else{\r
                                $('#import_done').css('display', 'block');\r
-                       }
-               });
-       }
-}
-$(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');
-       };
+                       }\r
+               });\r
+       }\r
+}\r
+$(document).ready(function(){\r
+       if(typeof FileActions !== 'undefined'){\r
+               FileActions.register('text/vcard','importaddressbook', '', Contacts_Import.importdialog); \r
+               FileActions.setDefault('text/vcard','importaddressbook');\r
+               FileActions.register('text/x-vcard','importaddressbook', '', Contacts_Import.importdialog); \r
+               FileActions.setDefault('text/x-vcard','importaddressbook');\r
+       };\r
 });
\ No newline at end of file
index 78e94762f2e90dd8b54e849683c8b9494b25b30d..79445ceeee1dabcc6f59d77002181e3d70ee8041 100755 (executable)
@@ -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){
index 2dac7dd0ca14fdd171c50a48f86fc80801996227..b8793042997ce94c646d34c96cb74397360c4185 100755 (executable)
@@ -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