From: Thomas Tanghus Date: Sun, 8 Jul 2012 13:41:38 +0000 (+0200) Subject: Improve checking for active addressbooks and creating default addressbook. X-Git-Tag: v4.0.5~32 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=25b95a9ec7da492e43c26ba5d6f38c773cecd39f;p=nextcloud-server.git Improve checking for active addressbooks and creating default addressbook. --- diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php index 79445ceeee1..73b30e942fa 100644 --- a/apps/contacts/lib/addressbook.php +++ b/apps/contacts/lib/addressbook.php @@ -169,16 +169,26 @@ class OC_Contacts_Addressbook{ $uid = OCP\USER::getUser(); } $prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null); + $prefbooks = explode(';',$prefbooks); + for ($i = 0; $i < count($prefbooks); $i++) { + if(!self::find($prefbooks[$i])) { + unset($prefbooks[$i]); + } + } if(!$prefbooks){ + OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No active addressbooks',OCP\Util::DEBUG); $addressbooks = OC_Contacts_Addressbook::all($uid); if(count($addressbooks) == 0){ - OC_Contacts_Addressbook::add($uid,'default','Default Address Book'); + OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No addressbooks',OCP\Util::DEBUG); + $id = self::add($uid,'default','Default Address Book'); + OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, Created addressbook: '.$id,OCP\Util::DEBUG); + self::setActive($id, true); $addressbooks = OC_Contacts_Addressbook::all($uid); } - $prefbooks = $addressbooks[0]['id']; - OCP\Config::setUserValue($uid,'contacts','openaddressbooks',$prefbooks); + $prefbooks[] = $addressbooks[0]['id']; + OCP\Config::setUserValue($uid,'contacts','openaddressbooks',implode(';',$prefbooks)); } - return explode(';',$prefbooks); + return $prefbooks; } /** @@ -189,6 +199,9 @@ class OC_Contacts_Addressbook{ public static function active($uid){ $active = self::activeIds($uid); $addressbooks = array(); + if(!$active) { + return $addressbooks; + } $ids_sql = join(',', array_fill(0, count($active), '?')); $prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname'; try { @@ -213,7 +226,7 @@ class OC_Contacts_Addressbook{ * @param integer $name * @return boolean */ - public static function setActive($id,$active){ + public static function setActive($id,$active=true){ // Need these ones for checking uri //$addressbook = self::find($id); diff --git a/apps/contacts/lib/vcard.php b/apps/contacts/lib/vcard.php index 71a874d783b..2868643387d 100644 --- a/apps/contacts/lib/vcard.php +++ b/apps/contacts/lib/vcard.php @@ -49,25 +49,31 @@ class OC_Contacts_VCard{ */ public static function all($id){ $result = null; - if(is_array($id)) { + if(is_array($id) && count($id) > 1) { $id_sql = join(',', array_fill(0, count($id), '?')); $prep = 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.$id_sql.') ORDER BY fullname'; try { $stmt = OCP\DB::prepare( $prep ); $result = $stmt->execute($id); } catch(Exception $e) { - OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::DEBUG); - OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '.join(',', $id),OCP\Util::DEBUG); + OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::ERROR); + OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '.count($id).' '.join(',', $id),OCP\Util::DEBUG); OCP\Util::writeLog('contacts','SQL:'.$prep,OCP\Util::DEBUG); } } elseif($id) { + if(is_array($id)) { + $id = $id[0]; + } try { $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' ); $result = $stmt->execute(array($id)); } catch(Exception $e) { - OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::DEBUG); - OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '. $id,OCP\Util::DEBUG); + OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::ERROR); + OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, id: '. $id,OCP\Util::DEBUG); } + } else { + OCP\Util::writeLog('contacts','OC_Contacts_VCard:all: No ID given.',OCP\Util::ERROR); + return array(); } $cards = array(); if(!is_null($result)) {