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

Updating of contact list when activating/adding/deleting address books now works.

This commit is contained in:
Thomas Tanghus 2011-12-11 16:26:00 +01:00
parent 98475d5fbd
commit 82fc2095c6
10 changed files with 96 additions and 116 deletions

View File

@ -18,6 +18,6 @@ $book = OC_Contacts_Addressbook::find($bookid);
/* is there an OC_JSON::error() ? */
OC_JSON::success(array(
'active' => $book['active'],
'active' => OC_Contacts_Addressbook::isActive($bookid),
'bookid' => $bookid,
));

View File

@ -12,7 +12,6 @@ if(!OC_USER::isLoggedIn()) {
die("<script type=\"text/javascript\">document.location = oc_webroot;</script>");
}
OC_JSON::checkAppEnabled('contacts');
OC_Log::write('contacts','chooseaddressbook.php',OC_Log::DEBUG);
$output = new OC_TEMPLATE("contacts", "part.chooseaddressbook");
$output -> printpage();

36
ajax/contacts.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* Copyright (c) 2011 Thomas Tanghus <thomas@tanghus.net>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
function contacts_namesort($a,$b){
return strcmp($a['fullname'],$b['fullname']);
}
require_once('../../../lib/base.php');
if(!OC_USER::isLoggedIn()) {
die("<script type=\"text/javascript\">document.location = oc_webroot;</script>");
}
OC_JSON::checkAppEnabled('contacts');
$addressbooks = OC_Contacts_Addressbook::activeAddressbooks(OC_User::getUser());
$contacts = array();
foreach( $addressbooks as $addressbook ){
$addressbookcontacts = OC_Contacts_VCard::all($addressbook['id']);
foreach( $addressbookcontacts as $contact ){
if(is_null($contact['fullname'])){
continue;
}
$contacts[] = $contact;
}
}
usort($contacts,'contacts_namesort');
$tmpl = new OC_TEMPLATE("contacts", "part.contacts");
$tmpl->assign('contacts', $contacts);
$page = $tmpl->fetchPage();
OC_JSON::success(array('data' => array( 'page' => $page )));
?>

View File

@ -31,18 +31,8 @@ require_once('../../lib/base.php');
OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('contacts');
// Check if the user has an addressbook
$openaddressbooks = OC_Contacts_Addressbook::activeAddressbookIds(OC_User::getUser());
OC_Log::write('contacts','Got IDs'.implode(',', $openaddressbooks),OC_Log::DEBUG);
// if( count($addressbooks) == 0){
// OC_Contacts_Addressbook::add(OC_User::getUser(),'default','Default Address Book');
// $addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
// }
// $prefbooks = OC_Preferences::getValue(OC_User::getUser(),'contacts','openaddressbooks',null);
// if(is_null($prefbooks)){
// $prefbooks = $addressbooks[0]['id'];
// OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',$prefbooks);
// }
// Get active address books. This creates a default one if none exists.
$addressbooks = OC_Contacts_Addressbook::activeAddressbooks(OC_User::getUser());
// Load the files we need
OC_App::setActiveNavigationEntry( 'contacts_index' );
@ -52,19 +42,15 @@ $id = isset( $_GET['id'] ) ? $_GET['id'] : null;
// sort addressbooks (use contactsort)
usort($addressbooks,'contacts_namesort');
// Addressbooks to load
//$openaddressbooks = explode(';',$prefbooks);
$contacts = array();
foreach( $openaddressbooks as $addressbook ){
$addressbookcontacts = OC_Contacts_VCard::all($addressbook);
OC_Log::write('contacts','index.php. Getting contacts for: '.$addressbook,OC_Log::DEBUG);
foreach( $addressbooks as $addressbook ){
$addressbookcontacts = OC_Contacts_VCard::all($addressbook['id']);
foreach( $addressbookcontacts as $contact ){
if(is_null($contact['fullname'])){
continue;
}
$contacts[] = $contact;
//$contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
}
}

View File

@ -6,19 +6,12 @@ Contacts={
$('#carddav_url').show();
$('#carddav_url_close').show();
},
Contacts:{
update:function(){
alert('Contacts.update()');
}
},
Addressbooks:{
overview:function(){
/* alert('overview');*/
if($('#chooseaddressbook_dialog').dialog('isOpen') == true){
$('#chooseaddressbook_dialog').dialog('moveToTop');
}else{
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'chooseaddressbook.php'), function(){
/*alert('Address books.load');*/
$('#chooseaddressbook_dialog').dialog({
width : 600,
close : function(event, ui) {
@ -30,25 +23,17 @@ Contacts={
},
activation:function(checkbox, bookid)
{
/* TODO:
* Add integer field 'active' to table 'contacts_addressbooks'. See apps/contacts/README.tanghus */
$.post(OC.filePath('contacts', 'ajax', 'activation.php'), { bookid: bookid, active: checkbox.checked?1:0 },
function(data) {
/*
* Arguments:
* data.status
* data.bookid
* data.active
*/
if (data.status == 'success'){
checkbox.checked = data.active == 1;
alert('TODO: Update Contacts list.');
/* TODO: Update Contacts list.
if (data.active == 1){
$('#calendar_holder').fullCalendar('addEventSource', data.eventSource);
}else{
$('#calendar_holder').fullCalendar('removeEventSource', data.eventSource.url);
}
*/
Contacts.UI.Contacts.update();
}
});
},
@ -71,10 +56,6 @@ Contacts={
$.post(OC.filePath('contacts', 'ajax', 'deletebook.php'), { id: bookid},
function(data) {
if (data.status == 'success'){
/* alert('TODO: Update Contacts list.'); */
/* TODO: Update Contacts list.
var url = 'ajax/deletebook.php?id='+bookid;
$('#calendar_holder').fullCalendar('removeEventSource', url);*/
$('#chooseaddressbook_dialog').dialog('destroy').remove();
Contacts.UI.Contacts.update();
Contacts.UI.Addressbooks.overview();
@ -105,6 +86,29 @@ Contacts={
cancel:function(button, bookid){
$(button).closest('tr').prev().show().next().remove();
}
},
Contacts:{
update:function(){
$.getJSON('ajax/contacts.php',{},function(jsondata){
if(jsondata.status == 'success'){
$('#contacts').html(jsondata.data.page);
}
else{
alert(jsondata.data.message);
}
});
/*
var contactlist = $('#contacts');
var contacts = contactlist.children('li').get();
//alert(contacts);
contacts.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(contacts, function(idx, itm) { contactlist.append(itm); });
*/
}
}
}
}
@ -192,18 +196,6 @@ $(document).ready(function(){
$('#chooseaddressbook').click(function(){
Contacts.UI.Addressbooks.overview();
/*
$.getJSON('ajax/showaddcard.php',{},function(jsondata){
if(jsondata.status == 'success'){
$('#rightcontent').data('id','');
$('#rightcontent').html(jsondata.data.page)
.find('select').chosen();
}
else{
alert(jsondata.data.message);
}
});
*/
return false;
});

View File

@ -44,7 +44,6 @@ class OC_Contacts_Addressbook{
* @return array
*/
public static function allAddressbooks($uid){
OC_Log::write('contacts','allAddressbooks',OC_Log::DEBUG);
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ORDER BY displayname' );
$result = $stmt->execute(array($uid));
@ -56,24 +55,6 @@ class OC_Contacts_Addressbook{
return $addressbooks;
}
/**
* @brief Returns the list of active addressbooks for a specific user.
* @param string $uid
* @return array
*/
public static function activeAddressbooks($uid){
$active = implode(',', self::activeAddressbookIds());
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN (?) AND userid = ? ORDER BY displayname' );
$result = $stmt->execute(array($active, $uid));
$addressbooks = array();
while( $row = $result->fetchRow()){
$addressbooks[] = $row;
}
return $addressbooks;
}
/**
* @brief Returns the list of addressbooks for a principal (DAV term of user)
* @param string $principaluri
@ -181,6 +162,28 @@ class OC_Contacts_Addressbook{
return explode(';',$prefbooks);
}
/**
* @brief Returns the list of active addressbooks for a specific user.
* @param string $uid
* @return array
*/
public static function activeAddressbooks($uid){
$active = self::activeAddressbookIds($uid);
$addressbooks = array();
/** FIXME: Is there a way to prepare a statement 'WHERE id IN ([range])'?
*/
foreach( $active as $aid ){
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id = ? ORDER BY displayname' );
$result = $stmt->execute(array($aid,));
while( $row = $result->fetchRow()){
$addressbooks[] = $row;
}
}
return $addressbooks;
}
/**
* @brief Activates an addressbook
* @param integer $id
@ -190,34 +193,23 @@ class OC_Contacts_Addressbook{
public static function setActive($id,$active){
// Need these ones for checking uri
//$addressbook = self::find($id);
OC_Log::write('contacts','setActive('.$id.'): '.$active,OC_Log::DEBUG);
if(is_null($id)){
$id = 0;
}
/**
* For now I have active state redundant both in preferences and in the address book
* table as I can't get the OC_Contacts_Addressbook::isActive() call to work when
* iterating over several results.
*/
$stmt = OC_DB::prepare( 'UPDATE *PREFIX*contacts_addressbooks SET active=?, ctag=ctag+1 WHERE id=?' );
$result = $stmt->execute(array($active,$id));
$openaddressbooks = self::activeAddressbookIds();
if($active) {
if(!in_array($id, $openaddressbooks)) {
// TODO: Test this instead
//$openaddressbooks[] = $id;
array_push($openaddressbooks, $id);
$openaddressbooks[] = $id;
}
} else {
} else {
if(in_array($id, $openaddressbooks)) {
array_pop($openaddressbooks, $id);
$openaddressbooks = array_diff( $openaddressbooks, array($id) );
unset($openaddressbooks[array_search($id, $openaddressbooks)]);
}
}
sort($openaddressbooks, SORT_NUMERIC);
OC_Log::write('contacts','setActive('.$id.'):all '.implode(';', $openaddressbooks),OC_Log::DEBUG);
// FIXME: I alway end up with a ';' prepending when imploding the array..?
OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',implode(';', $openaddressbooks));
return true;
@ -229,8 +221,6 @@ class OC_Contacts_Addressbook{
* @return boolean
*/
public static function isActive($id){
OC_Log::write('contacts','isActive('.$id.')',OC_Log::DEBUG);
OC_Log::write('contacts','isActive('.$id.'): '.in_array($id, self::activeAddressbookIds()),OC_Log::DEBUG);
return in_array($id, self::activeAddressbookIds());
}
@ -240,6 +230,7 @@ class OC_Contacts_Addressbook{
* @return boolean
*/
public static function delete($id){
self::setActive($id, false);
$stmt = OC_DB::prepare( 'DELETE FROM *PREFIX*contacts_addressbooks WHERE id = ?' );
$stmt->execute(array($id));

View File

@ -47,33 +47,13 @@ class OC_Contacts_VCard{
* ['carddata']
*/
public static function all($id){
OC_Log::write('contacts','OC_Contacts_VCard::all ids: '.$id,OC_Log::DEBUG);
// if(is_array($id)) {
// OC_Log::write('contacts','OC_Contacts_VCard::all Array?: '.$id,OC_Log::DEBUG);
// OC_Log::write('contacts','count: '.implode(',', $id),OC_Log::DEBUG);
// $ids = implode(',', $id);
// $prep = '?'.str_repeat ( ',?' , count($id)-1 );
// //$repeat = str_repeat ( ',?' , count($id)-1 );
// //OC_Log::write('contacts','OC_Contacts_VCard::all: repeat: '.$repeat,OC_Log::DEBUG);
// OC_Log::write('contacts','OC_Contacts_VCard::all: from: '.$ids,OC_Log::DEBUG);
// OC_Log::write('contacts','OC_Contacts_VCard::all: PREP: SELECT * FROM contacts_cards WHERE addressbookid IN ('.$prep.')',OC_Log::DEBUG);
// OC_Log::write('contacts','OC_Contacts_VCard::all: SQL: SELECT * FROM contacts_cards WHERE addressbookid IN ('.$prep.')',OC_Log::DEBUG);
// $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.'?,?,?'.') ORDER BY fullname' );
// } else {
// $ids = $id;
// $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
// }
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ?' );
$stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
$result = $stmt->execute(array($id));
OC_Log::write('contacts','OC_Contacts_VCard::all: result->numRows(): '.$result->numRows(),OC_Log::DEBUG);
$cards = array();
while( $row = $result->fetchRow()){
$cards[] = $row;
//OC_Log::write('contacts','OC_Contacts_VCard::all: fullname: '.$row['fullname'],OC_Log::DEBUG);
}
OC_Log::write('contacts','OC_Contacts_VCard::all: count($cards): '.count($cards),OC_Log::DEBUG);
return $cards;
}

View File

@ -1,11 +1,8 @@
<div id="chooseaddressbook_dialog" title="<?php echo $l->t("Choose active Address Books"); ?>">
<table width="100%" style="border: 0;">
<?php
OC_Log::write('contacts','part.chooseaddressbook.php',OC_Log::DEBUG);
$option_addressbooks = OC_Contacts_Addressbook::allAddressbooks(OC_User::getUser());
for($i = 0; $i < count($option_addressbooks); $i++){
OC_Log::write('contacts','part.chooseaddressbook.php: '.$option_addressbooks[$i]['id'],OC_Log::DEBUG);
echo "<tr>";
$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
$tmpl->assign('addressbook', $option_addressbooks[$i]);

View File

@ -1,6 +1,5 @@
<?php
OC_Log::write('contacts','part.chooseaddressbook.rowfields.php',OC_Log::DEBUG);
echo "<td width=\"20px\"><input id=\"active_" . $_['addressbook']["id"] . "\" type=\"checkbox\" onClick=\"Contacts.UI.Addressbooks.activation(this, " . $_['addressbook']["id"] . ")\"" . (OC_Contacts_Addressbook::isActive($_['addressbook']["id"]) ? ' checked="checked"' : '') . "></td>";
echo "<td>Active: ".$_['active'].",ID: " . $_['addressbook']["id"] . " - <label for=\"active_" . $_['addressbook']["id"] . "\">" . $_['addressbook']["displayname"] . "</label></td>";
echo "<td><label for=\"active_" . $_['addressbook']["id"] . "\">" . $_['addressbook']["displayname"] . "</label></td>";
echo "<td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.showCardDAVUrl('" . OC_User::getUser() . "', '" . $_['addressbook']["uri"] . "');\" title=\"" . $l->t("CardDav Link") . "\" class=\"action\"><img class=\"svg action\" src=\"../../core/img/actions/public.svg\"></a></td><td width=\"20px\"><a href=\"export.php?bookid=" . $_['addressbook']["id"] . "\" title=\"" . $l->t("Download") . "\" class=\"action\"><img class=\"svg action\" src=\"../../core/img/actions/download.svg\"></a></td><td width=\"20px\"><a href=\"#\" title=\"" . $l->t("Edit") . "\" class=\"action\" onclick=\"Contacts.UI.Addressbooks.editAddressbook(this, " . $_['addressbook']["id"] . ");\"><img class=\"svg action\" src=\"../../core/img/actions/rename.svg\"></a></td><td width=\"20px\"><a href=\"#\" onclick=\"Contacts.UI.Addressbooks.deleteAddressbook('" . $_['addressbook']["id"] . "');\" title=\"" . $l->t("Delete") . "\" class=\"action\"><img class=\"svg action\" src=\"../../core/img/actions/delete.svg\"></a></td>";

View File

@ -1,3 +1,3 @@
<?php foreach( $_['contacts'] as $contact ): ?>
<li data-id="<?php echo $contact['id']; ?>"><?php echo $contact['addressbookid']; ?> - <a href="index.php?id=<?php echo $contact['id']; ?>"><?php echo $contact['fullname']; ?></a> </li>
<li book-id="<?php echo $contact['addressbookid']; ?>" data-id="<?php echo $contact['id']; ?>"><a href="index.php?id=<?php echo $contact['id']; ?>"><?php echo $contact['fullname']; ?></a> </li>
<?php endforeach; ?>