summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2011-12-11 16:26:00 +0100
committerThomas Tanghus <thomas@tanghus.net>2011-12-11 16:26:00 +0100
commitc7b7758816445c400867e82f2174cdd9ef8b025c (patch)
treeaffe9f1f3e5417d8b9d862412ed3e83294b46563 /apps
parent5cb10548b30544c0407294fa7e520479673f956c (diff)
downloadnextcloud-server-c7b7758816445c400867e82f2174cdd9ef8b025c.tar.gz
nextcloud-server-c7b7758816445c400867e82f2174cdd9ef8b025c.zip
Updating of contact list when activating/adding/deleting address books now works.
Diffstat (limited to 'apps')
-rw-r--r--apps/contacts/ajax/activation.php2
-rw-r--r--apps/contacts/ajax/chooseaddressbook.php1
-rw-r--r--apps/contacts/ajax/contacts.php36
-rw-r--r--apps/contacts/index.php22
-rw-r--r--apps/contacts/js/interface.js58
-rw-r--r--apps/contacts/lib/addressbook.php63
-rw-r--r--apps/contacts/lib/vcard.php22
-rw-r--r--apps/contacts/templates/part.chooseaddressbook.php3
-rw-r--r--apps/contacts/templates/part.chooseaddressbook.rowfields.php3
-rw-r--r--apps/contacts/templates/part.contacts.php2
10 files changed, 96 insertions, 116 deletions
diff --git a/apps/contacts/ajax/activation.php b/apps/contacts/ajax/activation.php
index 6e137692dac..49aaac97287 100644
--- a/apps/contacts/ajax/activation.php
+++ b/apps/contacts/ajax/activation.php
@@ -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,
));
diff --git a/apps/contacts/ajax/chooseaddressbook.php b/apps/contacts/ajax/chooseaddressbook.php
index e150f97f541..f74b580eae7 100644
--- a/apps/contacts/ajax/chooseaddressbook.php
+++ b/apps/contacts/ajax/chooseaddressbook.php
@@ -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();
diff --git a/apps/contacts/ajax/contacts.php b/apps/contacts/ajax/contacts.php
new file mode 100644
index 00000000000..3c3bb5fe955
--- /dev/null
+++ b/apps/contacts/ajax/contacts.php
@@ -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 )));
+?>
diff --git a/apps/contacts/index.php b/apps/contacts/index.php
index f4624db31aa..cbd1dee1d91 100644
--- a/apps/contacts/index.php
+++ b/apps/contacts/index.php
@@ -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'] );
}
}
diff --git a/apps/contacts/js/interface.js b/apps/contacts/js/interface.js
index 2937e8a0925..f9539e8f363 100644
--- a/apps/contacts/js/interface.js
+++ b/apps/contacts/js/interface.js
@@ -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;
});
diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php
index 882a64bab88..23c0b106462 100644
--- a/apps/contacts/lib/addressbook.php
+++ b/apps/contacts/lib/addressbook.php
@@ -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));
@@ -57,24 +56,6 @@ class OC_Contacts_Addressbook{
}
/**
- * @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
* @return array
@@ -182,6 +163,28 @@ class OC_Contacts_Addressbook{
}
/**
+ * @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
* @param integer $name
@@ -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));
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index 87f2ff5e666..7285761fd58 100644
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -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;
}
diff --git a/apps/contacts/templates/part.chooseaddressbook.php b/apps/contacts/templates/part.chooseaddressbook.php
index dc5978d2d03..9574903cf95 100644
--- a/apps/contacts/templates/part.chooseaddressbook.php
+++ b/apps/contacts/templates/part.chooseaddressbook.php
@@ -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]);
diff --git a/apps/contacts/templates/part.chooseaddressbook.rowfields.php b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
index 4639ec20b14..f612e39ecaf 100644
--- a/apps/contacts/templates/part.chooseaddressbook.rowfields.php
+++ b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
@@ -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>";
diff --git a/apps/contacts/templates/part.contacts.php b/apps/contacts/templates/part.contacts.php
index c6772489ea2..8d89e9c7ad1 100644
--- a/apps/contacts/templates/part.contacts.php
+++ b/apps/contacts/templates/part.contacts.php
@@ -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; ?>