summaryrefslogtreecommitdiffstats
path: root/apps/contacts
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2011-12-09 15:10:51 +0100
committerThomas Tanghus <thomas@tanghus.net>2011-12-09 15:10:51 +0100
commitb3661adf028570f6158dfd5092ecb1398d5be0df (patch)
treeeb421923ccdea71a34203a83fbec16732760b196 /apps/contacts
parent830efdccac71ebbd4ee5f9414abd0878f2ff9349 (diff)
downloadnextcloud-server-b3661adf028570f6158dfd5092ecb1398d5be0df.tar.gz
nextcloud-server-b3661adf028570f6158dfd5092ecb1398d5be0df.zip
Various fixes to multiple address books handling
Diffstat (limited to 'apps/contacts')
-rw-r--r--apps/contacts/ajax/chooseaddressbook.php2
-rw-r--r--apps/contacts/ajax/createaddressbook.php6
-rw-r--r--apps/contacts/ajax/showaddcard.php2
-rw-r--r--apps/contacts/ajax/updateaddressbook.php2
-rw-r--r--apps/contacts/index.php37
-rw-r--r--apps/contacts/js/interface.js8
-rw-r--r--apps/contacts/lib/addressbook.php82
-rw-r--r--apps/contacts/lib/hooks.php2
-rw-r--r--apps/contacts/lib/vcard.php26
-rw-r--r--apps/contacts/templates/index.php2
-rw-r--r--apps/contacts/templates/part.chooseaddressbook.php6
-rw-r--r--apps/contacts/templates/part.chooseaddressbook.rowfields.php6
-rw-r--r--apps/contacts/templates/part.contacts.php2
13 files changed, 146 insertions, 37 deletions
diff --git a/apps/contacts/ajax/chooseaddressbook.php b/apps/contacts/ajax/chooseaddressbook.php
index d55fdf3e93b..e150f97f541 100644
--- a/apps/contacts/ajax/chooseaddressbook.php
+++ b/apps/contacts/ajax/chooseaddressbook.php
@@ -12,7 +12,7 @@ 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); */
+OC_Log::write('contacts','chooseaddressbook.php',OC_Log::DEBUG);
$output = new OC_TEMPLATE("contacts", "part.chooseaddressbook");
$output -> printpage();
diff --git a/apps/contacts/ajax/createaddressbook.php b/apps/contacts/ajax/createaddressbook.php
index 3dd38703c17..8fec07ebcae 100644
--- a/apps/contacts/ajax/createaddressbook.php
+++ b/apps/contacts/ajax/createaddressbook.php
@@ -17,10 +17,10 @@ OC_JSON::checkAppEnabled('contacts');
$userid = OC_User::getUser();
$bookid = OC_Contacts_Addressbook::add($userid, $_POST['name'], null);
OC_Contacts_Addressbook::setActive($bookid, 1);
-$book = OC_Contacts_Addressbook::find($bookid);
+$addressbook = OC_Contacts_Addressbook::find($bookid);
$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
-$tmpl->assign('addressbook', $book);
+$tmpl->assign('addressbook', $addressbook);
OC_JSON::success(array(
'page' => $tmpl->fetchPage(),
- 'bookid' => $bookid,
+ 'addressbook' => $addressbook,
));
diff --git a/apps/contacts/ajax/showaddcard.php b/apps/contacts/ajax/showaddcard.php
index 98367758fd4..5842046f00c 100644
--- a/apps/contacts/ajax/showaddcard.php
+++ b/apps/contacts/ajax/showaddcard.php
@@ -32,7 +32,7 @@ OC_JSON::checkAppEnabled('contacts');
$adr_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'ADR');
$phone_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'TEL');
-$addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser());
+$addressbooks = OC_Contacts_Addressbook::allAddressbooks(OC_USER::getUser());
$tmpl = new OC_Template('contacts','part.addcardform');
$tmpl->assign('addressbooks',$addressbooks);
$tmpl->assign('adr_types',$adr_types);
diff --git a/apps/contacts/ajax/updateaddressbook.php b/apps/contacts/ajax/updateaddressbook.php
index ccf1cef03f7..011dfc35d1a 100644
--- a/apps/contacts/ajax/updateaddressbook.php
+++ b/apps/contacts/ajax/updateaddressbook.php
@@ -19,7 +19,7 @@ OC_Contacts_Addressbook::edit($bookid, $_POST['name'], null);
OC_Contacts_Addressbook::setActive($bookid, $_POST['active']);
$addressbook = OC_Contacts_Addressbook::find($bookid);
$tmpl = new OC_Template('contacts', 'part.chooseaddressbook.rowfields');
-$tmpl->assign('book', $book);
+$tmpl->assign('addressbook', $addressbook);
OC_JSON::success(array(
'page' => $tmpl->fetchPage(),
'addressbook' => $addressbook,
diff --git a/apps/contacts/index.php b/apps/contacts/index.php
index 57c28a5761c..917335fb68b 100644
--- a/apps/contacts/index.php
+++ b/apps/contacts/index.php
@@ -21,7 +21,7 @@
*/
function contacts_namesort($a,$b){
- return strcmp($a['name'],$b['name']);
+ return strcmp($a['fullname'],$b['fullname']);
}
// Init owncloud
@@ -32,16 +32,17 @@ OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('contacts');
// Check if the user has an addressbook
-$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
-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);
-}
+$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);
+// }
// Load the files we need
OC_App::setActiveNavigationEntry( 'contacts_index' );
@@ -52,16 +53,18 @@ $id = isset( $_GET['id'] ) ? $_GET['id'] : null;
// sort addressbooks (use contactsort)
usort($addressbooks,'contacts_namesort');
// Addressbooks to load
-$openaddressbooks = explode(';',$prefbooks);
+//$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( $addressbookcontacts as $contact ){
if(is_null($contact['fullname'])){
continue;
}
- $contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
+ $contacts[] = $contact;
+ //$contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
}
}
@@ -75,10 +78,6 @@ if( !is_null($id) || count($contacts)){
$details = OC_Contacts_VCard::structureContact($vcard);
}
-$l10n = new OC_L10N('contacts');
-$adr_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'ADR');
-$phone_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'TEL');
-
// Include Style and Script
OC_Util::addScript('contacts','interface');
OC_Util::addStyle('contacts','styles');
@@ -86,6 +85,10 @@ OC_Util::addStyle('contacts','formtastic');
OC_Util::addScript('', 'jquery.multiselect');
OC_Util::addStyle('', 'jquery.multiselect');
+$l10n = new OC_L10N('contacts');
+$adr_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'ADR');
+$phone_types = OC_Contacts_VCard::getTypesOfProperty($l10n, 'TEL');
+
// Process the template
$tmpl = new OC_Template( 'contacts', 'index', 'user' );
$tmpl->assign('adr_types',$adr_types);
diff --git a/apps/contacts/js/interface.js b/apps/contacts/js/interface.js
index f34cde89576..8e699f52c93 100644
--- a/apps/contacts/js/interface.js
+++ b/apps/contacts/js/interface.js
@@ -6,10 +6,15 @@ 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){
- /*alert('Address books.moveToTop');*/
$('#chooseaddressbook_dialog').dialog('moveToTop');
}else{
$('#dialog_holder').load(OC.filePath('contacts', 'ajax', 'chooseaddressbook.php'), function(){
@@ -71,6 +76,7 @@ Contacts={
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();
} else {
alert('Error: ' + data.message);
diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php
index 93f069cff2f..882a64bab88 100644
--- a/apps/contacts/lib/addressbook.php
+++ b/apps/contacts/lib/addressbook.php
@@ -43,8 +43,9 @@ class OC_Contacts_Addressbook{
* @param string $uid
* @return array
*/
- public static function all($uid){
- $stmt = OC_DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ?' );
+ 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));
$addressbooks = array();
@@ -56,13 +57,31 @@ 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
*/
public static function allWherePrincipalURIIs($principaluri){
$uid = self::extractUserID($principaluri);
- return self::all($uid);
+ return self::allAddressbooks($uid);
}
/**
@@ -85,7 +104,7 @@ class OC_Contacts_Addressbook{
* @return insertid
*/
public static function add($userid,$name,$description){
- $all = self::all($userid);
+ $all = self::allAddressbooks($userid);
$uris = array();
foreach($all as $i){
$uris[] = $i['uri'];
@@ -141,6 +160,28 @@ class OC_Contacts_Addressbook{
}
/**
+ * @brief Get active addressbooks for a user.
+ * @param integer $uid User id. If null current user will be used.
+ * @return array
+ */
+ public static function activeAddressbookIds($uid){
+ if(is_null($uid)){
+ $uid = OC_User::getUser();
+ }
+ $prefbooks = OC_Preferences::getValue($uid,'contacts','openaddressbooks',null);
+ if(is_null($prefbooks)){
+ $addressbooks = OC_Contacts_Addressbook::allAddressbooks($uid);
+ if(count($addressbooks) == 0){
+ OC_Contacts_Addressbook::add($uid,'default','Default Address Book');
+ $addressbooks = OC_Contacts_Addressbook::allAddressbooks($uid);
+ }
+ $prefbooks = $addressbooks[0]['id'];
+ OC_Preferences::setValue($uid,'contacts','openaddressbooks',$prefbooks);
+ }
+ return explode(';',$prefbooks);
+ }
+
+ /**
* @brief Activates an addressbook
* @param integer $id
* @param integer $name
@@ -149,18 +190,51 @@ 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);
+ }
+ } else {
+ if(in_array($id, $openaddressbooks)) {
+ array_pop($openaddressbooks, $id);
+ $openaddressbooks = array_diff( $openaddressbooks, array($id) );
+ }
+ }
+ sort($openaddressbooks, SORT_NUMERIC);
+ OC_Log::write('contacts','setActive('.$id.'):all '.implode(';', $openaddressbooks),OC_Log::DEBUG);
+ OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',implode(';', $openaddressbooks));
return true;
}
/**
+ * @brief Checks if an addressbook is active.
+ * @param integer $id ID of the address book.
+ * @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());
+ }
+
+ /**
* @brief removes an address book
* @param integer $id
* @return boolean
diff --git a/apps/contacts/lib/hooks.php b/apps/contacts/lib/hooks.php
index 155cf40f914..356ae26df97 100644
--- a/apps/contacts/lib/hooks.php
+++ b/apps/contacts/lib/hooks.php
@@ -30,7 +30,7 @@ class OC_Contacts_Hooks{
* @return array
*/
public function deleteUser($parameters) {
- $addressbooks = OC_Contacts_Addressbook::all($parameters['uid']);
+ $addressbooks = OC_Contacts_Addressbook::allAddressbooks($parameters['uid']);
foreach($addressbooks as $addressbook) {
OC_Contacts_Addressbook::delete($addressbook['id']);
diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php
index 4865fae7642..b03e6ede998 100644
--- a/apps/contacts/lib/vcard.php
+++ b/apps/contacts/lib/vcard.php
@@ -47,15 +47,35 @@ 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 = ?' );
$result = $stmt->execute(array($id));
+ OC_Log::write('contacts','OC_Contacts_VCard::all: result->numRows(): '.$result->numRows(),OC_Log::DEBUG);
- $addressbooks = array();
+ $cards = array();
while( $row = $result->fetchRow()){
- $addressbooks[] = $row;
+ $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 $addressbooks;
+ return $cards;
}
/**
diff --git a/apps/contacts/templates/index.php b/apps/contacts/templates/index.php
index 20a1e34eb78..2ecadb4d60f 100644
--- a/apps/contacts/templates/index.php
+++ b/apps/contacts/templates/index.php
@@ -8,7 +8,7 @@
</form>
</div>
<div id="leftcontent" class="leftcontent">
- <ul>
+ <ul id="contacts">
<?php echo $this->inc("part.contacts"); ?>
</ul>
</div>
diff --git a/apps/contacts/templates/part.chooseaddressbook.php b/apps/contacts/templates/part.chooseaddressbook.php
index a698a334174..dc5978d2d03 100644
--- a/apps/contacts/templates/part.chooseaddressbook.php
+++ b/apps/contacts/templates/part.chooseaddressbook.php
@@ -1,11 +1,15 @@
<div id="chooseaddressbook_dialog" title="<?php echo $l->t("Choose active Address Books"); ?>">
<table width="100%" style="border: 0;">
<?php
-$option_addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
+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]);
+ $tmpl->assign('active', OC_Contacts_Addressbook::isActive($option_addressbooks[$i]['id']));
$tmpl->printpage();
echo "</tr>";
}
diff --git a/apps/contacts/templates/part.chooseaddressbook.rowfields.php b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
index 7f7b5a9154e..4639ec20b14 100644
--- a/apps/contacts/templates/part.chooseaddressbook.rowfields.php
+++ b/apps/contacts/templates/part.chooseaddressbook.rowfields.php
@@ -1,4 +1,6 @@
<?php
- echo "<td width=\"20px\"><input id=\"active_" . $_['addressbook']["id"] . "\" type=\"checkbox\" onClick=\"Contacts.UI.Addressbooks.activation(this, " . $_['addressbook']["id"] . ")\"" . ($_['addressbook']["active"] ? ' checked="checked"' : '') . "></td>";
- echo "<td><label for=\"active_" . $_['addressbook']["id"] . "\">" . $_['addressbook']["displayname"] . "</label></td>";
+ 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 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 6664a3671ac..c6772489ea2 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']; ?>"><a href="index.php?id=<?php echo $contact['id']; ?>"><?php echo $contact['name']; ?></a> </li>
+ <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>
<?php endforeach; ?>