1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2025-01-30 19:52:17 +01:00

add import function for contacts

This commit is contained in:
Georg Ehrke 2012-01-15 11:40:32 +01:00
parent 87a69f28d1
commit 0e1b92aafd
6 changed files with 248 additions and 0 deletions

17
ajax/importdialog.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* 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.
*/
require_once('../../../lib/base.php');
OC_JSON::checkLoggedIn();
OC_Util::checkAppEnabled('contacts');
$l10n = new OC_L10N('contacts');
$tmpl = new OC_Template('contacts', 'part.import');
$tmpl->assign('path', $_POST['path']);
$tmpl->assign('filename', $_POST['filename']);
$tmpl->printpage();
?>

View File

@ -22,4 +22,5 @@ OC_App::addNavigationEntry( array(
OC_APP::registerPersonal('contacts','settings'); OC_APP::registerPersonal('contacts','settings');
OC_UTIL::addScript('contacts', 'loader');
require_once('apps/contacts/lib/search.php'); require_once('apps/contacts/lib/search.php');

120
import.php Normal file
View File

@ -0,0 +1,120 @@
<?php
/**
* 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.
*/
//check for calendar rights or create new one
ob_start();
require_once ('../../lib/base.php');
OC_JSON::checkLoggedIn();
OC_Util::checkAppEnabled('calendar');
$nl = "\n";
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '10');
fclose($progressfopen);
}
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
if($_POST['method'] == 'new'){
$id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']);
OC_Contacts_Addressbook::setActive($id, 1);
}else{
$contacts = OC_Contacts_Addressbook::find($_POST['id']);
if($contacts['userid'] != OC_USER::getUser()){
OC_JSON::error();
exit();
}
$id = $_POST['id'];
}
//analyse the calendar file
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '20');
fclose($progressfopen);
}
$searchfor = array('VCARD');
$parts = $searchfor;
$filearr = explode($nl, $file);
$inelement = false;
$parts = array();
$i = 0;
foreach($filearr as $line){
foreach($searchfor as $search){
if(substr_count($line, $search) == 1){
list($attr, $val) = explode(':', $line);
if($attr == 'BEGIN'){
$parts[]['begin'] = $i;
$inelement = true;
}
if($attr == 'END'){
$parts[count($parts) - 1]['end'] = $i;
$inelement = false;
}
}
}
$i++;
}
//import the calendar
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '40');
fclose($progressfopen);
}
$start = '';
for ($i = 0; $i < $parts[0]['begin']; $i++) {
if($i == 0){
$start = $filearr[0];
}else{
$start .= $nl . $filearr[$i];
}
}
$end = '';
for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){
if($i == $parts[count($parts) - 1]['end'] + 1){
$end = $filearr[$parts[count($parts) - 1]['end'] + 1];
}else{
$end .= $nl . $filearr[$i];
}
}
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '50');
fclose($progressfopen);
}
$importready = array();
foreach($parts as $part){
for($i = $part['begin']; $i <= $part['end'];$i++){
if($i == $part['begin']){
$content = $filearr[$i];
}else{
$content .= $nl . $filearr[$i];
}
}
$importready[] = $start . $nl . $content . $nl . $end;
}
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '70');
fclose($progressfopen);
}
if(count($parts) == 1){
OC_Contacts_VCard::add($id, $file);
}else{
foreach($importready as $import){
OC_Contacts_VCard::add($id, $import);
}
}
//done the import
if(is_writable('import_tmp/')){
$progressfopen = fopen($progressfile, 'w');
fwrite($progressfopen, '100');
fclose($progressfopen);
}
sleep(3);
if(is_writable('import_tmp/')){
unlink($progressfile);
}
OC_JSON::success();

2
import_tmp/Info Normal file
View File

@ -0,0 +1,2 @@
This folder contains static files with the percentage of the import.
Requires write permission

81
js/loader.js Normal file
View File

@ -0,0 +1,81 @@
/**
* 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{
$('#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');
};
});

27
templates/part.import.php Normal file
View File

@ -0,0 +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(OC_User::getUser());
$contacts_options[] = array('id'=>'newaddressbook', 'displayname'=>$l->t('create a new addressbook'));
echo 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>
</div>