Contacts: UI updates and ajax methods for categories.

This commit is contained in:
Thomas Tanghus 2012-03-07 16:39:56 +01:00
parent faf6055baa
commit 75323b86d1
14 changed files with 593 additions and 101 deletions

View File

@ -27,14 +27,16 @@ require_once('../../../lib/base.php');
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$id = $_POST['id']; $id = isset($_POST['id'])?$_POST['id']:null;
$vcard = OC_Contacts_App::getContactVCard( $id ); $name = isset($_POST['name'])?$_POST['name']:null;
$value = isset($_POST['value'])?$_POST['value']:null;
$parameters = isset($_POST['parameters'])?$_POST['parameters']:array();
$vcard = OC_Contacts_App::getContactVCard($id);
$name = $_POST['name'];
$value = $_POST['value'];
if(!is_array($value)){ if(!is_array($value)){
$value = trim($value); $value = trim($value);
if(!$value && in_array($name, array('TEL', 'EMAIL', 'ORG', 'BDAY', 'NICKNAME'))) { if(!$value && in_array($name, array('TEL', 'EMAIL', 'ORG', 'BDAY', 'NICKNAME', 'NOTE'))) {
OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Cannot add empty property.')))); OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Cannot add empty property.'))));
exit(); exit();
} }
@ -51,7 +53,6 @@ if(!is_array($value)){
exit(); exit();
} }
} }
$parameters = isset($_POST['parameters']) ? $_POST['parameters'] : array();
// Prevent setting a duplicate entry // Prevent setting a duplicate entry
$current = $vcard->select($name); $current = $vcard->select($name);
@ -82,7 +83,9 @@ switch($name) {
} }
case 'N': case 'N':
case 'ORG': case 'ORG':
case 'NOTE':
case 'NICKNAME': case 'NICKNAME':
// TODO: Escape commas and semicolons.
break; break;
case 'EMAIL': case 'EMAIL':
$value = strtolower($value); $value = strtolower($value);

View File

@ -0,0 +1,39 @@
<?php
/**
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* 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_JSON::checkAppEnabled('contacts');
function bailOut($msg) {
OC_JSON::error(array('data' => array('message' => $msg)));
OC_Log::write('contacts','ajax/categories/add.php: '.$msg, OC_Log::DEBUG);
exit();
}
function debug($msg) {
OC_Log::write('contacts','ajax/categories/add.php: '.$msg, OC_Log::DEBUG);
}
$category = isset($_GET['category'])?strip_tags($_GET['category']):null;
if(is_null($category)) {
bailOut(OC_Contacts_App::$l10n->t('No category to add?'));
}
debug(print_r($category, true));
$categories = new OC_VCategories('contacts');
if($categories->hasCategory($category)) {
bailOut(OC_Contacts_App::$l10n->t('This category already exists: '.$category));
} else {
$categories->add($category, true);
}
OC_JSON::success(array('data' => array('categories'=>$categories->categories())));
?>

View File

@ -0,0 +1,28 @@
<?php
/**
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* 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_JSON::checkAppEnabled('contacts');
$id = isset($_GET['id'])?$_GET['id']:null;
if(is_null($id)) {
OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('No ID provided'))));
exit();
}
$vcard = OC_Contacts_App::getContactVCard( $id );
foreach($vcard->children as $property){
//OC_Log::write('contacts','ajax/categories/checksumfor.php: '.$property->name, OC_Log::DEBUG);
if($property->name == 'CATEGORIES') {
$checksum = md5($property->serialize());
OC_JSON::success(array('data' => array('checksum'=>$checksum)));
exit();
}
}
OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error setting checksum.'))));
?>

View File

@ -0,0 +1,60 @@
<?php
/**
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* 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_JSON::checkAppEnabled('contacts');
foreach ($_POST as $key=>$element) {
debug('_POST: '.$key.'=>'.print_r($element, true));
}
function bailOut($msg) {
OC_JSON::error(array('data' => array('message' => $msg)));
OC_Log::write('contacts','ajax/categories/delete.php: '.$msg, OC_Log::DEBUG);
exit();
}
function debug($msg) {
OC_Log::write('contacts','ajax/categories/delete.php: '.$msg, OC_Log::DEBUG);
}
$categories = isset($_POST['categories'])?$_POST['categories']:null;
if(is_null($categories)) {
bailOut(OC_Contacts_App::$l10n->t('No categories selected for deletion.'));
}
debug(print_r($categories, true));
$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
if(count($addressbooks) == 0) {
bailOut(OC_Contacts_App::$l10n->t('No address books found.'));
}
$addressbookids = array();
foreach($addressbooks as $addressbook) {
$addressbookids[] = $addressbook['id'];
}
$contacts = OC_Contacts_VCard::all($addressbookids);
if(count($contacts) == 0) {
bailOut(OC_Contacts_App::$l10n->t('No contacts found.'));
}
$cards = array();
foreach($contacts as $contact) {
$cards[] = array($contact['id'], $contact['carddata']);
}
debug('Before delete: '.print_r($categories, true));
$catman = new OC_VCategories('contacts');
$catman->delete($categories, $cards);
debug('After delete: '.print_r($catman->categories(), true));
OC_Contacts_VCard::updateDataByID($cards);
OC_JSON::success(array('data' => array('categories'=>$catman->categories())));
?>

View File

@ -0,0 +1,28 @@
<?php
/**
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* 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_JSON::checkAppEnabled('contacts');
function bailOut($msg) {
OC_JSON::error(array('data' => array('message' => $msg)));
OC_Log::write('contacts','ajax/categories/edit.php: '.$msg, OC_Log::DEBUG);
exit();
}
function debug($msg) {
OC_Log::write('contacts','ajax/categories/edit.php: '.$msg, OC_Log::DEBUG);
}
$tmpl = new OC_TEMPLATE("contacts", "part.edit_categories_dialog");
$categories = OC_Contacts_App::$categories->categories();
debug(print_r($categories, true));
$tmpl->assign('categories',$categories);
$tmpl->printpage();
?>

View File

@ -0,0 +1,17 @@
<?php
/**
* Copyright (c) 2012 Thomas Tanghus <thomas@tanghus.net>
* 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_JSON::checkAppEnabled('contacts');
$categories = OC_Contacts_App::$categories->categories();
OC_JSON::success(array('data' => array('categories'=>$categories)));
?>

View File

@ -71,5 +71,5 @@ if(isset($details['PHOTO'])) {
$details['PHOTO'] = false; $details['PHOTO'] = false;
} }
$details['id'] = $id; $details['id'] = $id;
OC_Contacts_App::setLastModifiedHeader($vcard);
OC_JSON::success(array('data' => $details)); OC_JSON::success(array('data' => $details));

View File

@ -36,7 +36,7 @@ function debug($msg) {
OC_Log::write('contacts','ajax/saveproperty.php: '.$msg, OC_Log::DEBUG); OC_Log::write('contacts','ajax/saveproperty.php: '.$msg, OC_Log::DEBUG);
} }
foreach ($_POST as $key=>$element) { foreach ($_POST as $key=>$element) {
debug('_POST: '.$key.'=>'.$element); debug('_POST: '.$key.'=>'.print_r($element, true));
} }
$id = isset($_POST['id'])?$_POST['id']:null; $id = isset($_POST['id'])?$_POST['id']:null;
@ -51,12 +51,8 @@ $checksum = isset($_POST['checksum'])?$_POST['checksum']:null;
// } // }
// } // }
if(is_array($value)){ if(!$name) {
$value = array_map('strip_tags', $value); bailOut(OC_Contacts_App::$l10n->t('element name is not set.'));
ksort($value); // NOTE: Important, otherwise the compound value will be set in the order the fields appear in the form!
$value = OC_VObject::escapeSemicolons($value);
} else {
$value = trim(strip_tags($value));
} }
if(!$id) { if(!$id) {
bailOut(OC_Contacts_App::$l10n->t('id is not set.')); bailOut(OC_Contacts_App::$l10n->t('id is not set.'));
@ -64,14 +60,22 @@ if(!$id) {
if(!$checksum) { if(!$checksum) {
bailOut(OC_Contacts_App::$l10n->t('checksum is not set.')); bailOut(OC_Contacts_App::$l10n->t('checksum is not set.'));
} }
if(!$name) { if(is_array($value)){
bailOut(OC_Contacts_App::$l10n->t('element name is not set.')); $value = array_map('strip_tags', $value);
ksort($value); // NOTE: Important, otherwise the compound value will be set in the order the fields appear in the form!
if($name == 'CATEGORIES') {
$value = OC_Contacts_VCard::escapeDelimiters($value, ',');
} else {
$value = OC_Contacts_VCard::escapeDelimiters($value, ';');
}
} else {
$value = trim(strip_tags($value));
} }
$vcard = OC_Contacts_App::getContactVCard( $id ); $vcard = OC_Contacts_App::getContactVCard( $id );
$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum); $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
if(is_null($line)) { if(is_null($line)) {
bailOut(OC_Contacts_App::$l10n->t('Information about vCard is incorrect. Please reload the page.'.$checksum.' "'.$line.'"')); bailOut(OC_Contacts_App::$l10n->t('Information about vCard is incorrect. Please reload the page: ').$checksum);
} }
$element = $vcard->children[$line]->name; $element = $vcard->children[$line]->name;
@ -91,7 +95,9 @@ switch($element) {
} }
case 'N': case 'N':
case 'ORG': case 'ORG':
case 'NOTE':
case 'NICKNAME': case 'NICKNAME':
case 'CATEGORIES':
debug('Setting string:'.$name.' '.$value); debug('Setting string:'.$name.' '.$value);
$vcard->setString($name, $value); $vcard->setString($name, $value);
break; break;
@ -123,12 +129,8 @@ $checksum = md5($vcard->children[$line]->serialize());
debug('New checksum: '.$checksum); debug('New checksum: '.$checksum);
if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) { if(!OC_Contacts_VCard::edit($id,$vcard->serialize())) {
OC_JSON::error(array('data' => array('message' => OC_Contacts_App::$l10n->t('Error updating contact property.')))); bailOut(OC_Contacts_App::$l10n->t('Error updating contact property.'));
OC_Log::write('contacts','ajax/setproperty.php: Error updating contact property: '.$value, OC_Log::ERROR);
exit(); exit();
} }
//$adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
//$phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
OC_JSON::success(array('data' => array( 'line' => $line, 'checksum' => $checksum, 'oldchecksum' => $_POST['checksum'] ))); OC_JSON::success(array('data' => array( 'line' => $line, 'checksum' => $checksum, 'oldchecksum' => $_POST['checksum'] )));

View File

@ -20,7 +20,8 @@
#firstrun { /*border: thin solid lightgray;*/ width: 80%; margin: 5em auto auto auto; text-align: center; font-weight:bold; font-size:1.5em; color:#777;} #firstrun { /*border: thin solid lightgray;*/ width: 80%; margin: 5em auto auto auto; text-align: center; font-weight:bold; font-size:1.5em; color:#777;}
#firstrun #selections { /*border: thin solid lightgray;*/ font-size:0.8em; width: 100%; margin: 2em auto auto auto; clear: both; } #firstrun #selections { /*border: thin solid lightgray;*/ font-size:0.8em; width: 100%; margin: 2em auto auto auto; clear: both; }
#card input[type="text"].contacts_property,input[type="email"].contacts_property { width: 16em; } #card input[type="text"].contacts_property,input[type="email"].contacts_property { width: 14em; }
.categories { float: left; width: 16em; }
#card input[type="text"],input[type="email"],input[type="tel"],input[type="date"], select { background-color: #f8f8f8; border: 0 !important; -webkit-appearance:none !important; -moz-appearance:none !important; -webkit-box-sizing:none !important; -moz-box-sizing:none !important; box-sizing:none !important; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; -moz-border-radius: 0px; -webkit-border-radius: 0px; border-radius: 0px; float: left; } #card input[type="text"],input[type="email"],input[type="tel"],input[type="date"], select { background-color: #f8f8f8; border: 0 !important; -webkit-appearance:none !important; -moz-appearance:none !important; -webkit-box-sizing:none !important; -moz-box-sizing:none !important; box-sizing:none !important; -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none; -moz-border-radius: 0px; -webkit-border-radius: 0px; border-radius: 0px; float: left; }
#card input[type="text"]:hover, input[type="text"]:focus, input[type="text"]:active,input[type="email"]:hover,input[type="tel"]:hover,input[type="date"]:hover,input[type="date"],input[type="date"]:hover,input[type="date"]:active,input[type="date"]:active,input[type="date"]:active,input[type="email"]:active,input[type="tel"]:active, select:hover, select:focus, select:active { border: 0 !important; -webkit-appearance:textfield; -moz-appearance:textfield; -webkit-box-sizing:content-box; -moz-box-sizing:content-box; box-sizing:content-box; background:#fff; color:#333; border:1px solid #ddd; -moz-box-shadow:0 1px 1px #fff, 0 2px 0 #bbb inset; -webkit-box-shadow:0 1px 1px #fff, 0 1px 0 #bbb inset; box-shadow:0 1px 1px #fff, 0 1px 0 #bbb inset; -moz-border-radius:.5em; -webkit-border-radius:.5em; border-radius:.5em; outline:none; float: left; } #card input[type="text"]:hover, input[type="text"]:focus, input[type="text"]:active,input[type="email"]:hover,input[type="tel"]:hover,input[type="date"]:hover,input[type="date"],input[type="date"]:hover,input[type="date"]:active,input[type="date"]:active,input[type="date"]:active,input[type="email"]:active,input[type="tel"]:active, select:hover, select:focus, select:active { border: 0 !important; -webkit-appearance:textfield; -moz-appearance:textfield; -webkit-box-sizing:content-box; -moz-box-sizing:content-box; box-sizing:content-box; background:#fff; color:#333; border:1px solid #ddd; -moz-box-shadow:0 1px 1px #fff, 0 2px 0 #bbb inset; -webkit-box-shadow:0 1px 1px #fff, 0 1px 0 #bbb inset; box-shadow:0 1px 1px #fff, 0 1px 0 #bbb inset; -moz-border-radius:.5em; -webkit-border-radius:.5em; border-radius:.5em; outline:none; float: left; }
input[type="text"]:invalid,input[type="email"]:invalid,input[type="tel"]:invalid,input[type="date"]:invalid { background-color: #ffc0c0 !important; } input[type="text"]:invalid,input[type="email"]:invalid,input[type="tel"]:invalid,input[type="date"]:invalid { background-color: #ffc0c0 !important; }
@ -68,7 +69,7 @@ dl.form
/*background-color: yellow;*/ /*background-color: yellow;*/
} }
.loading { background: url('../../../core/img/loading.gif') no-repeat center !important;} .loading { background: url('../../../core/img/loading.gif') no-repeat center !important; /*cursor: progress; */ cursor: wait; }
/*.add { cursor: pointer; width: 25px; height: 25px; margin: 0px; float: right; position:relative; content: "\+"; font-weight: bold; color: #666; font-size: large; bottom: 0px; right: 0px; clear: both; text-align: center; vertical-align: bottom; display: none; }*/ /*.add { cursor: pointer; width: 25px; height: 25px; margin: 0px; float: right; position:relative; content: "\+"; font-weight: bold; color: #666; font-size: large; bottom: 0px; right: 0px; clear: both; text-align: center; vertical-align: bottom; display: none; }*/
@ -185,4 +186,12 @@ input[type="checkbox"] { width: 20px; height: 20px; vertical-align: bottom; }
.propertylist li > input[type="checkbox"],input[type="radio"] { float: left; clear: left; width: 20px; height: 20px; vertical-align: middle; } .propertylist li > input[type="checkbox"],input[type="radio"] { float: left; clear: left; width: 20px; height: 20px; vertical-align: middle; }
.propertylist li > select { float: left; max-width: 8em; } .propertylist li > select { float: left; max-width: 8em; }
.typelist { float: left; max-width: 10em; } /* for multiselect */ .typelist { float: left; max-width: 10em; } /* for multiselect */
.addresslist { clear: both; } .addresslist { clear: both; }
#categoryform .scrollarea { position: absolute; left: 10px; top: 10px; right: 10px; bottom: 50px; overflow: auto; border:1px solid #ddd; background: #f8f8f8; }
#categoryform .bottombuttons { position: absolute; bottom: 10px;}
#categoryform .bottombuttons * { float: left;}
/*#categorylist { border:1px solid #ddd;}*/
#categorylist li { background:#f8f8f8; padding:.3em .8em; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; -webkit-transition:background-color 500ms; -moz-transition:background-color 500ms; -o-transition:background-color 500ms; transition:background-color 500ms; }
#categorylist li:hover, li:active { background:#eee; }
#category_addinput { width: 10em; }

View File

@ -34,6 +34,7 @@ if(!is_null($id)) {
} }
$property_types = OC_Contacts_App::getAddPropertyOptions(); $property_types = OC_Contacts_App::getAddPropertyOptions();
$phone_types = OC_Contacts_App::getTypesOfProperty('TEL'); $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
$categories = OC_Contacts_App::$categories->categories();
$upload_max_filesize = OC_Helper::computerFileSize(ini_get('upload_max_filesize')); $upload_max_filesize = OC_Helper::computerFileSize(ini_get('upload_max_filesize'));
$post_max_size = OC_Helper::computerFileSize(ini_get('post_max_size')); $post_max_size = OC_Helper::computerFileSize(ini_get('post_max_size'));
@ -59,6 +60,7 @@ $tmpl->assign('uploadMaxFilesize', $maxUploadFilesize);
$tmpl->assign('uploadMaxHumanFilesize', OC_Helper::humanFileSize($maxUploadFilesize)); $tmpl->assign('uploadMaxHumanFilesize', OC_Helper::humanFileSize($maxUploadFilesize));
$tmpl->assign('property_types',$property_types); $tmpl->assign('property_types',$property_types);
$tmpl->assign('phone_types',$phone_types); $tmpl->assign('phone_types',$phone_types);
$tmpl->assign('categories',$categories);
$tmpl->assign('addressbooks', $addressbooks); $tmpl->assign('addressbooks', $addressbooks);
$tmpl->assign('contacts', $contacts); $tmpl->assign('contacts', $contacts);
$tmpl->assign('details', $details ); $tmpl->assign('details', $details );

View File

@ -4,13 +4,105 @@ function ucwords (str) {
}); });
} }
String.prototype.strip_tags = function(){ Categories={
tags = this; edit:function(){
stripped = tags.replace(/[\<\>]/gi, ""); console.log('Categories.edit');
return stripped; $('body').append('<div id="category_dialog"></div>');
$('#category_dialog').load(OC.filePath('contacts', 'ajax', 'categories/edit.php'), function(response, status, xhr){
try {
var response = jQuery.parseJSON(response);
console.log('status: ' + status + ', response: ' + response + ', response.status:' + response.status);
if(response.status == 'error'){
alert(response.data.message);
} else {
alert(response);
}
} catch(e) {
$('#edit_categories_dialog').dialog({
modal: true,
height: 350, minHeight:200, width: 250, minWidth: 200,
buttons: {
'Delete':function() {
Categories.delete();
},
'Rescan':function() {
Categories.rescan();
}
},
close : function(event, ui) {
//alert('close');
$(this).dialog('destroy').remove();
$('#category_dialog').remove();
},
open : function(event, ui) {
$('#category_addinput').live('input',function(){
if($(this).val().length > 0) {
$('#category_addbutton').removeAttr('disabled');
}
});
$('#categoryform').submit(function() {
Categories.add($('#category_addinput').val());
$('#category_addinput').val('');
$('#category_addbutton').attr('disabled', 'disabled');
return false;
});
$('#category_addbutton').live('click',function(e){
e.preventDefault();
if($('#category_addinput').val().length > 0) {
Categories.add($('#category_addinput').val());
$('#category_addinput').val('');
}
});
}
});
}
});
},
delete:function(){
var categories = $('#categorylist').find('input[type="checkbox"]').serialize();
console.log('Categories.delete: ' + categories);
$.post(OC.filePath('contacts', 'ajax', 'categories/delete.php'),categories,function(jsondata){
if(jsondata.status == 'success'){
Categories._update(jsondata.data.categories);
} else {
alert(jsondata.data.message);
}
});
},
add:function(category){
console.log('Categories.add ' + category);
$.getJSON(OC.filePath('contacts', 'ajax', 'categories/add.php'),{'category':category},function(jsondata){
if(jsondata.status == 'success'){
Categories._update(jsondata.data.categories);
} else {
alert(jsondata.data.message);
}
});
return false;
},
rescan:function(){
console.log('Categories.rescan');
$.getJSON(OC.filePath('contacts', 'ajax', 'categories/rescan.php'),{},function(jsondata){
if(jsondata.status == 'success'){
Categories._update(jsondata.data.categories);
} else {
alert(jsondata.data.message);
}
});
},
_update:function(categories){
var categorylist = $('#categorylist');
categorylist.find('li').remove();
for(var category in categories) {
var item = '<li><input type="checkbox" name="categories" value="' + categories[category] + '" />' + categories[category] + '</li>';
$(item).appendTo(categorylist);
}
if(Categories.changed != undefined) {
Categories.changed(categories);
}
}
} }
Contacts={ Contacts={
UI:{ UI:{
notImplemented:function() { notImplemented:function() {
@ -125,22 +217,26 @@ Contacts={
// NOTE: Do we ever get here? // NOTE: Do we ever get here?
$('#messagebox').dialog('moveToTop'); $('#messagebox').dialog('moveToTop');
}else{ }else{
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'messagebox.php'), function(){ $('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'messagebox.php'), function(jsondata){
$('#messagebox').dialog( if(jsondata.status != 'error'){
{ $('#messagebox').dialog(
autoOpen: true, {
title: title, autoOpen: true,
buttons: [{ title: title,
text: "Ok", buttons: [{
click: function() { $(this).dialog("close"); } text: "Ok",
}], click: function() { $(this).dialog("close"); }
close: function(event, ui) { }],
$(this).dialog('destroy').remove(); close: function(event, ui) {
}, $(this).dialog('destroy').remove();
open: function(event, ui) { },
$('#messagebox_msg').html(msg); open: function(event, ui) {
} $('#messagebox_msg').html(msg);
}); }
});
} else {
alert(jsondata.data.message);
}
}); });
} }
}, },
@ -182,6 +278,12 @@ Contacts={
$('#bday').datepicker({ $('#bday').datepicker({
dateFormat : 'dd-mm-yy' dateFormat : 'dd-mm-yy'
}); });
$('#categories_value').find('select').multiselect({
noneSelectedText: t('contacts', 'Select categories'),
header: false,
selectedList: 6,
classes: 'categories'
});
// Style phone types // Style phone types
$('#phonelist').find('select[class*="contacts_property"]').multiselect({ $('#phonelist').find('select[class*="contacts_property"]').multiselect({
noneSelectedText: t('contacts', 'Select type'), noneSelectedText: t('contacts', 'Select type'),
@ -361,7 +463,7 @@ Contacts={
this.data = jsondata; this.data = jsondata;
this.id = this.data.id; this.id = this.data.id;
$('#rightcontent').data('id',this.id); $('#rightcontent').data('id',this.id);
//console.log('loaded: ' + this.data.FN[0]['value']); console.log('loaded: ' + this.data.FN[0]['value']);
this.populateNameFields(); this.populateNameFields();
this.loadCategories(); this.loadCategories();
this.loadPhoto(); this.loadPhoto();
@ -369,6 +471,16 @@ Contacts={
this.loadPhones(); this.loadPhones();
this.loadAddresses(); this.loadAddresses();
this.loadSingleProperties(); this.loadSingleProperties();
// TODO: load NOTE ;-)
if(this.data.NOTE) {
$('#note').data('checksum', this.data.NOTE[0]['checksum']);
$('#note').find('textarea').val(this.data.NOTE[0]['value']);
$('#note').show();
} else {
$('#note').data('checksum', '');
$('#note').find('textarea').val('');
$('#note').hide();
}
}, },
loadSingleProperties:function() { loadSingleProperties:function() {
var props = ['BDAY', 'NICKNAME', 'ORG']; var props = ['BDAY', 'NICKNAME', 'ORG'];
@ -457,21 +569,64 @@ Contacts={
$('#contact_identity').find('*[data-element="FN"]').data('checksum', this.data.FN[0]['checksum']); $('#contact_identity').find('*[data-element="FN"]').data('checksum', this.data.FN[0]['checksum']);
$('#contact_identity').show(); $('#contact_identity').show();
}, },
loadCategories:function(){ hasCategory:function(category) {
if(this.data.CATEGORIES) { if(this.data.CATEGORIES) {
// for(var c in this.data.CATEGORIES[0]['value']) {
var cat = this.data.CATEGORIES[0]['value'][c];
//console.log('hasCategory: ' + cat + ' === ' + category + '?');
if(typeof cat === 'string' && (cat.toUpperCase() === category.toUpperCase())) {
//console.log('Yes');
return true;
}
}
} }
return false;
},
categoriesChanged:function(categories) { // Categories added/deleted.
console.log('categoriesChanged for ' + Contacts.UI.Card.id + ' : ' + categories);
var categorylist = $('#categories_value').find('select');
categorylist.find('option').remove();
for(var category in categories) {
console.log('categoriesChanged: ' + categories[category]);
var selected = Contacts.UI.Card.hasCategory(categories[category]) ? ' selected="selected"' : '';
var item = '<option value="' + categories[category] + '"' + selected + '>' + categories[category] + '</option>';
$(item).appendTo(categorylist);
}
$('#categories_value').find('select').multiselect('refresh');
$.getJSON(OC.filePath('contacts', 'ajax', 'categories/checksumfor.php'),{'id':Contacts.UI.Card.id},function(jsondata){
if(jsondata.status == 'success'){
console.log('Setting checksum: ' + jsondata.data.checksum);
$('#categories_value').data('checksum', jsondata.data.checksum);
} else {
Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
}
});
},
loadCategories:function(){ // On loading contact.
var categories = $('#categories_value').find('select');
if(this.data.CATEGORIES) {
$('#categories_value').data('checksum', this.data.CATEGORIES[0]['checksum']);
} else {
$('#categories_value').data('checksum', '');
}
categories.find('option').each(function(){
if(Contacts.UI.Card.hasCategory($(this).val())) {
$(this).attr('selected', 'selected');
} else {
$(this).removeAttr('selected');
}
});
categories.multiselect('refresh');
}, },
editNew:function(){ // add a new contact editNew:function(){ // add a new contact
this.id = ''; this.fn = ''; this.fullname = ''; this.givname = ''; this.famname = ''; this.addname = ''; this.honpre = ''; this.honsuf = ''; this.id = ''; this.fn = ''; this.fullname = ''; this.givname = ''; this.famname = ''; this.addname = ''; this.honpre = ''; this.honsuf = '';
$.getJSON('ajax/newcontact.php',{},function(jsondata){ $.getJSON(OC.filePath('contacts', 'ajax', 'newcontact.php'),{},function(jsondata){
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
id = ''; id = '';
$('#rightcontent').data('id',''); $('#rightcontent').data('id','');
$('#rightcontent').html(jsondata.data.page); $('#rightcontent').html(jsondata.data.page);
Contacts.UI.Card.editName(); Contacts.UI.Card.editName();
} } else {
else{
Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message); Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
//alert(jsondata.data.message); //alert(jsondata.data.message);
} }
@ -479,14 +634,19 @@ Contacts={
}, },
savePropertyInternal:function(name, fields, oldchecksum, checksum){ savePropertyInternal:function(name, fields, oldchecksum, checksum){
// TODO: Add functionality for new fields. // TODO: Add functionality for new fields.
//console.log('savePropertyInternal: ' + name + ', checksum: ' + checksum); console.log('savePropertyInternal: ' + name + ', fields: ' + fields + 'checksum: ' + checksum);
//console.log('savePropertyInternal: ' + this.data[name]); console.log('savePropertyInternal: ' + this.data[name]);
var multivalue = ['CATEGORIES'];
var params = {}; var params = {};
var value = undefined; var value = multivalue.indexOf(name) != -1 ? new Array() : undefined;
jQuery.each(fields, function(i, field){ jQuery.each(fields, function(i, field){
//.substring(11,'parameters[TYPE][]'.indexOf(']')) //.substring(11,'parameters[TYPE][]'.indexOf(']'))
if(field.name.substring(0, 5) === 'value') { if(field.name.substring(0, 5) === 'value') {
value = field.value; if(multivalue.indexOf(name) != -1) {
value.push(field.value);
} else {
value = field.value;
}
} else if(field.name.substring(0, 10) === 'parameters') { } else if(field.name.substring(0, 10) === 'parameters') {
var p = field.name.substring(11,'parameters[TYPE][]'.indexOf(']')); var p = field.name.substring(11,'parameters[TYPE][]'.indexOf(']'));
if(!(p in params)) { if(!(p in params)) {
@ -506,7 +666,7 @@ Contacts={
saveProperty:function(obj){ saveProperty:function(obj){
// I couldn't get the selector to filter on 'contacts_property' so I filter by hand here :-/ // I couldn't get the selector to filter on 'contacts_property' so I filter by hand here :-/
if(!$(obj).hasClass('contacts_property')) { if(!$(obj).hasClass('contacts_property')) {
//console.log('Filtering out object.' + obj); console.log('Filtering out object.' + obj);
return false; return false;
} }
if($(obj).hasClass('nonempty') && $(obj).val().trim() == '') { if($(obj).hasClass('nonempty') && $(obj).val().trim() == '') {
@ -529,32 +689,38 @@ Contacts={
if(checksum != undefined && checksum != '') { // save if(checksum != undefined && checksum != '') { // save
q = q + '&checksum=' + checksum; q = q + '&checksum=' + checksum;
console.log('Saving: ' + q); console.log('Saving: ' + q);
$(obj).attr('disabled', 'disabled');
$.post('ajax/saveproperty.php',q,function(jsondata){ $.post('ajax/saveproperty.php',q,function(jsondata){
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
container.data('checksum', jsondata.data.checksum); container.data('checksum', jsondata.data.checksum);
Contacts.UI.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum); Contacts.UI.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum);
Contacts.UI.loading(container, false); Contacts.UI.loading(container, false);
$(obj).removeAttr('disabled');
return true; return true;
} }
else{ else{
Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message); Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
Contacts.UI.loading(container, false); Contacts.UI.loading(container, false);
$(obj).removeAttr('disabled');
return false; return false;
} }
},'json'); },'json');
} else { // add } else { // add
console.log('Adding: ' + q); console.log('Adding: ' + q);
$(obj).attr('disabled', 'disabled');
$.post('ajax/addproperty.php',q,function(jsondata){ $.post('ajax/addproperty.php',q,function(jsondata){
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
container.data('checksum', jsondata.data.checksum); container.data('checksum', jsondata.data.checksum);
// TODO: savePropertyInternal doesn't know about new fields // TODO: savePropertyInternal doesn't know about new fields
//Contacts.UI.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum); //Contacts.UI.Card.savePropertyInternal(name, fields, checksum, jsondata.data.checksum);
Contacts.UI.loading(container, false); Contacts.UI.loading(container, false);
$(obj).removeAttr('disabled');
return true; return true;
} }
else{ else{
Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message); Contacts.UI.messageBox(t('contacts', 'Error'), jsondata.data.message);
Contacts.UI.loading(container, false); Contacts.UI.loading(container, false);
$(obj).removeAttr('disabled');
return false; return false;
} }
},'json'); },'json');
@ -565,10 +731,14 @@ Contacts={
console.log('addProperty:' + type); console.log('addProperty:' + type);
switch (type) { switch (type) {
case 'PHOTO': case 'PHOTO':
this.loadPhoto(); this.loadPhoto(true);
$('#file_upload_form').show(); $('#file_upload_form').show();
$('#contacts_propertymenu a[data-type="'+type+'"]').parent().hide(); $('#contacts_propertymenu a[data-type="'+type+'"]').parent().hide();
break; break;
case 'NOTE':
$('#note').show();
$('#contacts_propertymenu a[data-type="'+type+'"]').parent().hide();
break;
case 'EMAIL': case 'EMAIL':
if($('#emaillist>li').length == 1) { if($('#emaillist>li').length == 1) {
$('#emails').show(); $('#emails').show();
@ -647,8 +817,9 @@ Contacts={
if($('#edit_name_dialog').dialog('isOpen') == true){ if($('#edit_name_dialog').dialog('isOpen') == true){
$('#edit_name_dialog').dialog('moveToTop'); $('#edit_name_dialog').dialog('moveToTop');
}else{ // TODO: If id=='' call addcontact.php (or whatever name) instead and reload view with id. }else{ // TODO: If id=='' call addcontact.php (or whatever name) instead and reload view with id.
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'editname.php')+'?id='+this.id, function(){ $('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'editname.php')+'?id='+this.id, function(jsondata){
$('#edit_name_dialog' ).dialog({ if(jsondata.status != 'error'){
$('#edit_name_dialog' ).dialog({
modal: (isnew && true || false), modal: (isnew && true || false),
closeOnEscape: (isnew == '' && false || true), closeOnEscape: (isnew == '' && false || true),
title: (isnew && t('contacts', 'Add contact') || t('contacts', 'Edit name')), title: (isnew && t('contacts', 'Add contact') || t('contacts', 'Edit name')),
@ -667,7 +838,10 @@ Contacts={
open : function(event, ui) { open : function(event, ui) {
// load 'N' property - maybe :-P // load 'N' property - maybe :-P
}*/ }*/
}); });
} else {
alert(jsondata.data.message);
}
}); });
} }
}, },
@ -692,7 +866,14 @@ Contacts={
$('#fn_select option').remove(); $('#fn_select option').remove();
//$('#fn_select').combobox('value', this.fn); //$('#fn_select').combobox('value', this.fn);
var names = [this.fullname, this.givname + ' ' + this.famname, this.famname + ' ' + this.givname, this.famname + ', ' + this.givname]; var tmp = [this.fullname, this.givname + ' ' + this.famname, this.famname + ' ' + this.givname, this.famname + ', ' + this.givname];
var names = new Array();
for(var name in tmp) {
console.log('idx: ' + names.indexOf(tmp[name]));
if(names.indexOf(tmp[name]) == -1) {
names.push(tmp[name]);
}
}
$.each(names, function(key, value) { $.each(names, function(key, value) {
$('#fn_select') $('#fn_select')
.append($('<option></option>') .append($('<option></option>')
@ -771,8 +952,9 @@ Contacts={
if($('#edit_address_dialog').dialog('isOpen') == true){ if($('#edit_address_dialog').dialog('isOpen') == true){
$('#edit_address_dialog').dialog('moveToTop'); $('#edit_address_dialog').dialog('moveToTop');
}else{ }else{
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'editaddress.php')+q, function(){ $('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'editaddress.php')+q, function(jsondata){
$('#edit_address_dialog' ).dialog({ if(jsondata.status != 'error'){
$('#edit_address_dialog' ).dialog({
/*modal: true,*/ /*modal: true,*/
height: 'auto', width: 'auto', height: 'auto', width: 'auto',
buttons: { buttons: {
@ -803,7 +985,10 @@ Contacts={
open : function(event, ui) { open : function(event, ui) {
// load 'ADR' property - maybe :-P // load 'ADR' property - maybe :-P
}*/ }*/
}); });
} else {
alert(jsondata.data.message);
}
}); });
} }
}, },
@ -867,8 +1052,8 @@ Contacts={
form.submit(); form.submit();
} }
}, },
loadPhoto:function(){ loadPhoto:function(force){
if(this.data.PHOTO) { if(this.data.PHOTO||force==true) {
$.getJSON('ajax/loadphoto.php',{'id':this.id},function(jsondata){ $.getJSON('ajax/loadphoto.php',{'id':this.id},function(jsondata){
if(jsondata.status == 'success'){ if(jsondata.status == 'success'){
//alert(jsondata.data.page); //alert(jsondata.data.page);
@ -1006,13 +1191,17 @@ Contacts={
if($('#chooseaddressbook_dialog').dialog('isOpen') == true){ if($('#chooseaddressbook_dialog').dialog('isOpen') == true){
$('#chooseaddressbook_dialog').dialog('moveToTop'); $('#chooseaddressbook_dialog').dialog('moveToTop');
}else{ }else{
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'chooseaddressbook.php'), function(){ $('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'chooseaddressbook.php'), function(jsondata){
$('#chooseaddressbook_dialog').dialog({ if(jsondata.status != 'error'){
width : 600, $('#chooseaddressbook_dialog').dialog({
close : function(event, ui) { width : 600,
$(this).dialog('destroy').remove(); close : function(event, ui) {
} $(this).dialog('destroy').remove();
}); }
});
} else {
alert(jsondata.data.message);
}
}); });
} }
}, },
@ -1121,6 +1310,7 @@ Contacts={
$(document).ready(function(){ $(document).ready(function(){
Contacts.UI.loadHandlers(); Contacts.UI.loadHandlers();
Categories.changed = Contacts.UI.Card.categoriesChanged;
/** /**
* Show the Addressbook chooser * Show the Addressbook chooser
@ -1189,7 +1379,8 @@ $(document).ready(function(){
// NOTE: For some reason the selector doesn't work when I select by '.contacts_property' too... // NOTE: For some reason the selector doesn't work when I select by '.contacts_property' too...
// I do the filtering in the event handler instead. // I do the filtering in the event handler instead.
$('input[type="text"],input[type="checkbox"],input[type="email"],input[type="tel"],input[type="date"], select').live('change', function(){ //$('input[type="text"],input[type="checkbox"],input[type="email"],input[type="tel"],input[type="date"], select').live('change', function(){
$('.contacts_property').live('change', function(){
Contacts.UI.Card.saveProperty(this); Contacts.UI.Card.saveProperty(this);
}); });
@ -1298,11 +1489,29 @@ $(document).ready(function(){
xhr.send(file); xhr.send(file);
} }
$('body').live('click',function(e){
if(!$(e.target).is('#contacts_propertymenu_button')) {
$('#contacts_propertymenu').hide();
}
});
$('#contacts_propertymenu_button').live('click',function(){ $('#contacts_propertymenu_button').live('click',function(){
$('#contacts_propertymenu').is(':hidden') && $('#contacts_propertymenu').slideDown() || $('#contacts_propertymenu').slideUp(); var menu = $('#contacts_propertymenu');
if(menu.is(':hidden')) {
menu.show();
menu.find('ul').focus();
} else {
menu.hide();
}
}); });
$('#contacts_propertymenu a').live('click',function(){ $('#contacts_propertymenu a').live('click',function(){
Contacts.UI.Card.addProperty(this); Contacts.UI.Card.addProperty(this);
$('#contacts_propertymenu').hide(); $('#contacts_propertymenu').hide();
}); });
}); });
String.prototype.strip_tags = function(){
tags = this;
stripped = tags.replace(/[\<\>]/gi, "");
return stripped;
}

View File

@ -4,6 +4,7 @@
* *
* @author Jakob Sack * @author Jakob Sack
* @copyright 2011 Jakob Sack mail@jakobsack.de * @copyright 2011 Jakob Sack mail@jakobsack.de
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
@ -275,6 +276,29 @@ class OC_Contacts_VCard{
return $newid; return $newid;
} }
/**
* @brief Mass updates an array of cards
* @param array $objects An array of [id, carddata].
*/
public static function updateDataByID($objects){
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_cards SET carddata = ?, lastmodified = ? WHERE id = ?' );
$now = new DateTime;
foreach($objects as $object) {
$vcard = OC_VObject::parse($object[1]);
if(!is_null($vcard)){
$vcard->setString('REV', $now->format(DateTime::W3C));
$data = $vcard->serialize();
try {
$result = $stmt->execute(array($data,time(),$object[0]));
//OC_Log::write('contacts','OC_Contacts_VCard::updateDataByID, id: '.$object[0].': '.$object[1],OC_Log::DEBUG);
} catch(Exception $e) {
OC_Log::write('contacts','OC_Contacts_VCard::updateDataByID:, exception: '.$e->getMessage(),OC_Log::DEBUG);
OC_Log::write('contacts','OC_Contacts_VCard::updateDataByID, id: '.$object[0],OC_Log::DEBUG);
}
}
}
}
/** /**
* @brief edits a card * @brief edits a card
* @param integer $id id of card * @param integer $id id of card
@ -378,6 +402,43 @@ class OC_Contacts_VCard{
return true; return true;
} }
/**
* @brief Escapes delimiters from an array and returns a string.
* @param array $value
* @param char $delimiter
* @return string
*/
public static function escapeDelimiters($value, $delimiter=';') {
foreach($value as &$i ) {
$i = implode("\\$delimiter", explode($delimiter, $i));
}
return implode($delimiter, $value);
}
/**
* @brief Creates an array out of a multivalue property
* @param string $value
* @param char $delimiter
* @return array
*/
public static function unescapeDelimiters($value, $delimiter=';') {
$array = explode($delimiter,$value);
for($i=0;$i<count($array);$i++) {
if(substr($array[$i],-1,1)=="\\") {
if(isset($array[$i+1])) {
$array[$i] = substr($array[$i],0,count($array[$i])-2).$delimiter.$array[$i+1];
unset($array[$i+1]);
} else {
$array[$i] = substr($array[$i],0,count($array[$i])-2).$delimiter;
}
$i = $i - 1;
}
}
$array = array_map('trim', $array);
return $array;
}
/** /**
* @brief Data structure of vCard * @brief Data structure of vCard
* @param object $property * @param object $property
@ -416,7 +477,9 @@ class OC_Contacts_VCard{
$value = $property->value; $value = $property->value;
//$value = htmlspecialchars($value); //$value = htmlspecialchars($value);
if($property->name == 'ADR' || $property->name == 'N'){ if($property->name == 'ADR' || $property->name == 'N'){
$value = OC_VObject::unescapeSemicolons($value); $value = self::unescapeDelimiters($value);
} elseif($property->name == 'CATEGORIES') {
$value = self::unescapeDelimiters($value, ',');
} }
$temp = array( $temp = array(
'name' => $property->name, 'name' => $property->name,

View File

@ -13,6 +13,7 @@ $id = isset($_['id']) ? $_['id'] : '';
<li><a data-type="TEL"><?php echo $l->t('Phone'); ?></a></li> <li><a data-type="TEL"><?php echo $l->t('Phone'); ?></a></li>
<li><a data-type="EMAIL"><?php echo $l->t('Email'); ?></a></li> <li><a data-type="EMAIL"><?php echo $l->t('Email'); ?></a></li>
<li><a data-type="ADR"><?php echo $l->t('Address'); ?></a></li> <li><a data-type="ADR"><?php echo $l->t('Address'); ?></a></li>
<li><a data-type="NOTE"><?php echo $l->t('Note'); ?></a></li>
</ul> </ul>
</div> </div>
<img onclick="Contacts.UI.Card.export();" class="svg action" id="contacts_downloadcard" src="<?php echo image_path('', 'actions/download.svg'); ?>" title="<?php echo $l->t('Download contact');?>" /> <img onclick="Contacts.UI.Card.export();" class="svg action" id="contacts_downloadcard" src="<?php echo image_path('', 'actions/download.svg'); ?>" title="<?php echo $l->t('Download contact');?>" />
@ -45,7 +46,7 @@ $id = isset($_['id']) ? $_['id'] : '';
<dt><label for="fn"><?php echo $l->t('Display name'); ?></label></dt> <dt><label for="fn"><?php echo $l->t('Display name'); ?></label></dt>
<dd class="propertycontainer" data-element="FN"> <dd class="propertycontainer" data-element="FN">
<select id="fn_select" title="<?php echo $l->t('Format custom, Short name, Full name, Reverse or Reverse with comma'); ?>" style="width:16em;"> <select id="fn_select" title="<?php echo $l->t('Format custom, Short name, Full name, Reverse or Reverse with comma'); ?>" style="width:16em;">
</select><a id="edit_name" class="edit" title="<?php echo $l->t('Edit name details'); ?>"></a> </select><a id="edit_name" class="action edit" title="<?php echo $l->t('Edit name details'); ?>"></a>
</dd> </dd>
<dt style="display:none;" id="org_label" data-element="ORG"><label for="org"><?php echo $l->t('Organization'); ?></label></dt> <dt style="display:none;" id="org_label" data-element="ORG"><label for="org"><?php echo $l->t('Organization'); ?></label></dt>
<dd style="display:none;" class="propertycontainer" id="org_value" data-element="ORG"><input id="org" required="required" name="value[ORG]" type="text" class="contacts_property" style="width:16em;" name="value" value="" placeholder="<?php echo $l->t('Organization'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd> <dd style="display:none;" class="propertycontainer" id="org_value" data-element="ORG"><input id="org" required="required" name="value[ORG]" type="text" class="contacts_property" style="width:16em;" name="value" value="" placeholder="<?php echo $l->t('Organization'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd>
@ -53,8 +54,19 @@ $id = isset($_['id']) ? $_['id'] : '';
<dd style="display:none;" class="propertycontainer" id="nickname_value" data-element="NICKNAME"><input id="nickname" required="required" name="value[NICKNAME]" type="text" class="contacts_property" style="width:16em;" name="value" value="" placeholder="<?php echo $l->t('Enter nickname'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd> <dd style="display:none;" class="propertycontainer" id="nickname_value" data-element="NICKNAME"><input id="nickname" required="required" name="value[NICKNAME]" type="text" class="contacts_property" style="width:16em;" name="value" value="" placeholder="<?php echo $l->t('Enter nickname'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd>
<dt style="display:none;" id="bday_label" data-element="BDAY"><label for="bday"><?php echo $l->t('Birthday'); ?></label></dt> <dt style="display:none;" id="bday_label" data-element="BDAY"><label for="bday"><?php echo $l->t('Birthday'); ?></label></dt>
<dd style="display:none;" class="propertycontainer" id="bday_value" data-element="BDAY"><input id="bday" required="required" name="value" type="text" class="contacts_property" value="" placeholder="<?php echo $l->t('dd-mm-yyyy'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd> <dd style="display:none;" class="propertycontainer" id="bday_value" data-element="BDAY"><input id="bday" required="required" name="value" type="text" class="contacts_property" value="" placeholder="<?php echo $l->t('dd-mm-yyyy'); ?>" /><a class="delete" onclick="$(this).tipsy('hide');Contacts.UI.Card.deleteProperty(this, 'single');" title="<?php echo $l->t('Delete'); ?>"></a></dd>
<dt id="categories_label" data-element="CATEGORIES"><label for="categories"><?php echo $l->t('Categories'); ?></label></dt>
<dd class="propertycontainer" id="categories_value" data-element="CATEGORIES">
<select class="contacts_property" multiple="multiple" id="categories" name="value[]">
<?php echo html_select_options($_['categories'], array(), array('combine'=>true)) ?>
</select>
<a class="action edit" onclick="$(this).tipsy('hide');Categories.edit();" title="<?php echo $l->t('Edit categories'); ?>"></a>
</dd>
</dl> </dl>
</fieldset> </fieldset>
<fieldset id="note" class="formfloat propertycontainer" style="display:none;" data-element="NOTE">
<legend><?php echo $l->t('Note'); ?></legend>
<textarea class="contacts_property note" name="value"></textarea>
</fieldset>
</form> </form>
</div> </div>

View File

@ -49,12 +49,14 @@ class OC_VCategories {
* @param $user The user whos data the object will operate on. This * @param $user The user whos data the object will operate on. This
* parameter should normally be omitted but to make an app able to * parameter should normally be omitted but to make an app able to
* update categories for all users it is made possible to provide it. * update categories for all users it is made possible to provide it.
* @param $defcategories An array of default categories to be used if none is stored.
* NOTE: Not implemented.
*/ */
public function __construct($app, $user=null) { public function __construct($app, $user=null, $defcategories=null) {
$this->app = $app; $this->app = $app;
$this->user = is_null($user) ? OC_User::getUser() : $user; $this->user = is_null($user) ? OC_User::getUser() : $user;
$categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, '')); $categories = trim(OC_Preferences::getValue($this->user, $app, self::PREF_CATEGORIES_LABEL, ''));
$this->categories = $categories != '' ? OC_VObject::unescapeSemicolons($categories) : array(); $this->categories = $categories != '' ? unserialize($categories) : array();
} }
/** /**
@ -62,6 +64,7 @@ class OC_VCategories {
* @returns array containing the categories as strings. * @returns array containing the categories as strings.
*/ */
public function categories() { public function categories() {
OC_Log::write('core','OC_VCategories::categories: '.print_r($this->categories, true), OC_Log::DEBUG);
return $this->categories; return $this->categories;
} }
@ -81,7 +84,7 @@ class OC_VCategories {
* @param $sync bool When true, save the categories * @param $sync bool When true, save the categories
* @returns bool Returns false on error. * @returns bool Returns false on error.
*/ */
public function add($names, $sync=true) { public function add($names, $sync=false) {
if(!is_array($names)) { if(!is_array($names)) {
$names = array($names); $names = array($names);
} }
@ -95,7 +98,7 @@ class OC_VCategories {
if(count($newones) > 0) { if(count($newones) > 0) {
$this->categories = array_merge($this->categories, $newones); $this->categories = array_merge($this->categories, $newones);
natcasesort($this->categories); // Dunno if this is necessary natcasesort($this->categories); // Dunno if this is necessary
if($sync) { if($sync === true) {
$this->save(); $this->save();
} }
} }
@ -106,7 +109,7 @@ class OC_VCategories {
* @brief Extracts categories from a vobject and add the ones not already present. * @brief Extracts categories from a vobject and add the ones not already present.
* @param $vobject The instance of OC_VObject to load the categories from. * @param $vobject The instance of OC_VObject to load the categories from.
*/ */
public function loadFromVObject($vobject, $sync=true) { public function loadFromVObject($vobject, $sync=false) {
$this->add($vobject->getAsArray('CATEGORIES'), $sync); $this->add($vobject->getAsArray('CATEGORIES'), $sync);
} }
@ -126,14 +129,15 @@ class OC_VCategories {
* } * }
* $categories->rescan($objects); * $categories->rescan($objects);
*/ */
public function rescan($objects) { public function rescan($objects, $sync=true) {
$this->categories = array(); $this->categories = array();
foreach($objects as $object) { foreach($objects as $object) {
//OC_Log::write('core','OC_VCategories::rescan: '.substr($object, 0, 100).'(...)', OC_Log::DEBUG);
$vobject = OC_VObject::parse($object); $vobject = OC_VObject::parse($object);
if(!is_null($vobject)) { if(!is_null($vobject)) {
$this->loadFromVObject($vobject, false); $this->loadFromVObject($vobject, $sync);
} else { } else {
OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG); OC_Log::write('core','OC_VCategories::rescan, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
} }
} }
$this->save(); $this->save();
@ -142,35 +146,51 @@ class OC_VCategories {
/** /**
* @brief Save the list with categories * @brief Save the list with categories
*/ */
public function save() { private function save() {
$escaped_categories = OC_VObject::escapeSemicolons($this->categories); $escaped_categories = serialize($this->categories);
OC_Log::write('core','OC_VCategories::save: '.print_r($this->categories, true), OC_Log::DEBUG);
OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories); OC_Preferences::setValue($this->user, $this->app, self::PREF_CATEGORIES_LABEL, $escaped_categories);
} }
/** /**
* @brief Delete a category from the db and from all the vobject supplied * @brief Delete categories from the db and from all the vobject supplied
* @param $name * @param $names An array of categories to delete
* @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table. * @param $objects An array of arrays with [id,vobject] (as text) pairs suitable for updating the apps object table.
*/ */
public function delete($name, array &$objects) { public function delete($names, array &$objects) {
if(!$this->hasCategory($name)) { if(!is_array($names)) {
return; $names = array($names);
}
OC_Log::write('core','OC_VCategories::delete, before: '.print_r($this->categories, true), OC_Log::DEBUG);
foreach($names as $name) {
OC_Log::write('core','OC_VCategories::delete: '.$name, OC_Log::DEBUG);
if($this->hasCategory($name)) {
OC_Log::write('core','OC_VCategories::delete: '.$name.' got it', OC_Log::DEBUG);
unset($this->categories[$this->array_searchi($name, $this->categories)]);
}
} }
unset($this->categories[$this->array_searchi($name, $this->categories)]);
$this->save(); $this->save();
OC_Log::write('core','OC_VCategories::delete, after: '.print_r($this->categories, true), OC_Log::DEBUG);
foreach($objects as $key=>&$value) { foreach($objects as $key=>&$value) {
$vobject = OC_VObject::parse($value[1]); $vobject = OC_VObject::parse($value[1]);
if(!is_null($vobject)){ if(!is_null($vobject)){
$categories = $vobject->getAsArray('CATEGORIES'); $categories = $vobject->getAsArray('CATEGORIES');
$idx = $this->array_searchi($name, $categories); //OC_Log::write('core','OC_VCategories::delete, before: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
if($idx) { foreach($names as $name) {
unset($categories[$this->array_searchi($name, $categories)]); $idx = $this->array_searchi($name, $categories);
$vobject->setString('CATEGORIES', implode(',', $categories)); OC_Log::write('core','OC_VCategories::delete, loop: '.$name.', '.print_r($idx, true), OC_Log::DEBUG);
$value[1] = $vobject->serialize(); if($idx !== false) {
$objects[$key] = $value; OC_Log::write('core','OC_VCategories::delete, unsetting: '.$categories[$this->array_searchi($name, $categories)], OC_Log::DEBUG);
unset($categories[$this->array_searchi($name, $categories)]);
//unset($categories[$idx]);
}
} }
OC_Log::write('core','OC_VCategories::delete, after: '.$key.': '.print_r($categories, true), OC_Log::DEBUG);
$vobject->setString('CATEGORIES', implode(',', $categories));
$value[1] = $vobject->serialize();
$objects[$key] = $value;
} else { } else {
OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 20).'(...)', OC_Log::DEBUG); OC_Log::write('core','OC_VCategories::delete, unable to parse. ID: '.$value[0].', '.substr($value[1], 0, 50).'(...)', OC_Log::DEBUG);
} }
} }
} }